-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
105 lines (87 loc) · 2.65 KB
/
main.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
#include "graph.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <string>
using namespace std;
// Funkcija za citanje dvaju integera iz stringa
void extractIntegerWords(string str, int* a, int* b)
{
stringstream ss;
ss << str;
string temp;
ss >> temp;
if(stringstream(temp) >> *a) {}
temp = "";
ss >> temp;
if(stringstream(temp) >> *b) {}
}
// Funkcija za citanje jednog integera iz stringa
void extractIntegerWord(string str, int* V)
{
stringstream ss;
ss << str;
string temp;
ss >> temp;
if(stringstream(temp) >> *V) {}
temp = "";
}
int main( int argc, char** argv )
{
if( argc != 3 ){
cout << "Upotreba: " << argv[0] << " vrh1 vrh2 (brid je reprezentiran dvoclanim skupom {vrh1, vrh2})" << endl;
exit(0);
}
/* Ovaj zakomentirani graf ucitat cemo iz datoteke
Graph g( 8 );
g.addEdge( 0, 2 );
g.addEdge( 0, 1 );
g.addEdge( 1, 3 );
g.addEdge( 3, 4 );
g.addEdge( 4, 6 );
g.addEdge( 6, 7 );
g.addEdge( 7, 5 );
g.addEdge( 5, 4 );
g.addEdge( 5, 6 );
g.addEdge( 5, 3 );
*/
// Ucitavanje grafa iz datoteke
ifstream inFile;
inFile.open( "data.txt" );
if( !inFile ) {
cout << "Neuspjesno otvaranje datoteke data.txt" << endl;
exit(3);
}
string s;
getline( inFile, s ); // U prvoj liniji zapisan je broj vrhova
int V;
extractIntegerWord( s, &V );
Graph g( V ); // Poziv konstruktora strukture Graph
int a, b;
while( !inFile.eof() ) {
getline( inFile, s ); // Datoteku citamo liniju po liniju
extractIntegerWords( s, &a, &b ); // Iz svake linije procitamo dva integera
if(a < 0 || a >= V || b < 0 || b >= V )
{
cout << "Neispravan unos u tekstualnoj datoteci!" << endl;
exit(4);
}
g.addEdge( a, b ); // Dodajemo odgovarajuci brid u graf
}
inFile.close();
int v = atoi( argv[1] );
if( v < 0 || v >= g.V ){ // Provjera postoji li vrh v u grafu (je li input korektan)
cout << "Vrh " << v << " ne postoji u grafu!" << endl;
exit(1);
}
int w = atoi( argv[2] );
if( w < 0 || w >= g.V ){ // Provjera postoji li vrh w u grafu (je li input korektan)
cout << "Vrh " << w << " ne postoji u grafu!" << endl;
exit(2);
}
g.belongsToCyle( v, w )? // Provjera pripada li dani brid ciklusu ili ne
cout << "Dani brid pripada ciklusu." << endl:
cout << "Dani brid ne pripada ciklusu." << endl;
return 0;
}