-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.~cpp
174 lines (168 loc) · 5.56 KB
/
Unit1.~cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <fstream.h>
#include <iostream.h>
#pragma hdrstop
struct Graph {int size; int **spisok; int *sm; int *connection;} *graph1, *graph;
void Delete_graphs(void);
void Check(int);
void Answer(void);
void Number_of_vertexes(FILE*, const char*);
//---------------------------------------------------------------------------
#pragma argsused
Graph *load_graph(const char *path)
{
FILE *in = fopen(path, "r");
if (!in) {
printf("Error: Cannot open file '%s'.\n", path);
exit(1);
}
graph = new Graph;
// Ñ÷èòûâàåì êîëè÷åñòâî âåðøèí.
//
fscanf(in, "%d", &graph->size);
fgetc(in);
// Ñ÷èòûâàåì èìåíà âåðøèí.
//
// Ñîçäàåì ñïèñîê ñìåæíîñòè è ìàññèâ êîë-â ñìåæíûõ âåðøèí.
//
graph->spisok = new int*[graph->size+1];
graph->sm = new int[graph->size+1];
// Ñ÷èòûâàåì ñïèñîê ñìåæíîñòè.
//
Number_of_vertexes(in, path);
for(int i=1;i<graph->size+1;i++){
if(graph->sm[i]>0){ //åñëè ýòî êîë-âî áîëüøå 0
graph->spisok[i]=new int[graph->sm[i]]; //è äîáàâëÿåì íîâóþ ñ íåîáõîäèìûì êîë-âîì âåðøèí
for(int j=0;j<graph->sm[i];j++){
fscanf(in, "%i", &graph->spisok[i][j]);
cout<<graph->spisok[i][j];
}
fseek(in, sizeof(int), 1); //ïðîïóñêàåì íîìåð âåðøèíû, ÿâëÿþùèìñÿ íîìåðîì òåêóùåé âåðøèíû
}
else{
graph->spisok[i] = new int[1];// âñå ìàññèâû, ó êîòîðûõ êîë-âî ñìåæíûõ âåðøèí ðàâíî íóëþ, ïîëó÷àþò ïåðâûé ýëåìåíò, ðàâíûé -1 (âåðøèíû ñ òàêèì íîìåðîì íå ñóùåñòâóåò)
graph->spisok[i][0] = -1;
fseek(in, sizeof(int), 1); //ïðîïóñêàåì íîìåð âåðøèíû, ÿâëÿþùèìñÿ íîìåðîì òåêóùåé âåðøèíû
continue;
}
}
fclose(in);
return graph;
}
void show_graph(Graph *graph){ //âûâîäèì ãðàô, êàê ñïèñîê ñìåæíîñòè
int a, b;
cout<<"\ngraph:\n"<<endl;
for(int i=1;i<graph->size+1;i++){ //äëÿ êàæäîé âåðøèíû
cout<<i<<"->";
if(graph->sm[i]==0){
cout<<endl;
continue;
}
for(int j=0;j<graph->sm[i]-1;j++){ //âûâîäèì íîìåðà ñìåæíûõ åé âåðøèí (äî ïðåäïîñëåäíåé âêëþ÷èòåëüíî)
b=graph->spisok[i][j];
cout<<b<<" , ";
}
a=graph->spisok[i][graph->sm[i]-1];
cout<<a<<endl; //âûâîäèì íîìåð ïîñëåäíåé ñìåæíîé äàííîé âåðøèíå âåðøèíû
}
}
void run_testcase(int number, const char *path){
int c;
Graph *graph1;
graph1=load_graph(path);
show_graph(graph1);
Answer();
Delete_graphs();
}
int main(int argc, char* argv[])
{
run_testcase(1, "graph1.txt");
run_testcase(2, "graph2.txt");
run_testcase(3, "graph3.txt");
run_testcase(4, "graph4.txt");
run_testcase(5, "graph5.txt");
getch();
return 0;
}
void Number_of_vertexes(FILE *in, const char *path)//ñ÷èòûâàåì êîë-âî ñìåæíûõ äàííûì âåðøèíàì âåðøèí
{
int numberofel, size;
char a;
for(int i=1;i<graph->size+1;i++){
numberofel=0;
for(int i=0; i<2; i++) fgetc(in); //ïðîïóñêàåì 2 ñèìâîëà
M:
a=fgetc(in);
if(a==' ') goto M;
if(a!='\n'){
numberofel++;
goto M;
}
graph->sm[i]=numberofel;
}
fclose(in);
in=fopen(path, "r");
fseek(in, sizeof(int), 0);
}
void Delete_graphs()
{
delete graph1;
for(int i=1;i<graph->size+1;i++) delete graph->spisok[i];
delete graph->spisok;
delete graph->sm;
delete graph;
}
void Answer()
{
int answer;
if(graph->size==0){
cout<<"Nevernie dannie!"<<endl;
return;
}
for(int i=1;i<graph->size+1;i++){
if(graph->spisok[i][0]==-1 && graph->size > 1){
cout<<"\nOtvet:"<<endl<<"Ne svyzniy graf!"<<endl;
return;
}
if(graph->spisok[i][0]==-1 && graph->size == 1){
cout<<"\nOtvet:"<<endl<<"Svyzniy graf!"<<endl;
return;
}
}
graph->connection=new int[graph->size+1];
graph->connection[1]=1;
graph->connection[0]=1;
Check(1);
if(graph->connection[0]==graph->size){
cout<<"\nOtvet:"<<endl<<"Svyzniy graf!"<<endl;
return;
}
else{
cout<<"\nOtvet:"<<endl<<"Ne svyzniy graf!"<<endl;
return;
}
}
void Check(int i)
{
int n;
for(int j=0; j<graph->sm[i]; j++){
n=0;
for(int k=1; k<=graph->connection[0]; k++){
if(graph->spisok[i][j]!=graph->connection[k]){
n++;
continue;
}
break;
}
if(n==graph->connection[0]){
graph->connection[0]++;
graph->connection[graph->connection[0]]=graph->spisok[i][j];
Check(graph->spisok[i][j]);
}
}
}
//---------------------------------------------------------------------------