|
| 1 | +#include <iostream> |
| 2 | +#include <string.h> |
| 3 | +#include <sstream> |
| 4 | +#include <cstdlib> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +#define INFINITY 10000000 |
| 9 | + |
| 10 | +struct Graph{ |
| 11 | + int numberOfV; |
| 12 | + int numberOfE; |
| 13 | + double **data; |
| 14 | +}; |
| 15 | + |
| 16 | + |
| 17 | +//code from stackoverflow website |
| 18 | + double string_to_double( const std::string& s ) |
| 19 | + { |
| 20 | + std::istringstream i(s); |
| 21 | + double x; |
| 22 | + if (!(i >> x)) |
| 23 | + return 0; |
| 24 | + return x; |
| 25 | + } |
| 26 | +// |
| 27 | + |
| 28 | +void insertEdge(Graph &g, int u, int v, double weight){ |
| 29 | + if (u >= g.numberOfV || v >= g.numberOfV) return; |
| 30 | + g.data[u][v] = weight; |
| 31 | + g.numberOfE++; |
| 32 | +} |
| 33 | + |
| 34 | +void loadGraph(Graph &g, int n, int m){ |
| 35 | + string line; |
| 36 | + int v1, v2; |
| 37 | + double w; |
| 38 | + //initialize the matrix |
| 39 | + g.data = new double*[n]; |
| 40 | + for (int i = 0; i < n; i++) { |
| 41 | + g.data[i] = new double[n]; |
| 42 | + } |
| 43 | + g.numberOfV = n; |
| 44 | + g.numberOfE = 0; |
| 45 | + for (int i = 0; i < n; i++){ |
| 46 | + for (int j = 0; j < n; j++){ |
| 47 | + if (i == j) g.data[i][j] = 0; |
| 48 | + else g.data[i][j] = INFINITY; |
| 49 | + } |
| 50 | + } |
| 51 | + //populate the matrix with m vertices |
| 52 | + for (int i = 0; i < m; i++){ |
| 53 | + getline(cin, line); |
| 54 | + v1 = atoi(line.substr(0, 1).c_str()); |
| 55 | + v2 = atoi(line.substr(2, 1).c_str()); |
| 56 | + w = string_to_double(line.substr(4)); |
| 57 | + insertEdge(g, v1, v2, w); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +bool findEdge(Graph &g, int u, int v, double &weight){ |
| 62 | + if (u != v && g.data[u][v] != INFINITY){ |
| 63 | + weight = g.data[u][v]; |
| 64 | + return true; |
| 65 | + } |
| 66 | + return false; |
| 67 | +} |
| 68 | + |
| 69 | +void showAsMatrix(Graph &g){ |
| 70 | + for(int i = 0; i < g.numberOfV; i++){ |
| 71 | + for (int j = 0; j < g.numberOfV; j++){ |
| 72 | + if (g.data[i][j] != INFINITY) cout << g.data[i][j] << ","; |
| 73 | + else cout << "-,"; |
| 74 | + } |
| 75 | + cout << endl; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void showAsArrayOfLists(Graph &g){ |
| 80 | + for(int i = 0; i < g.numberOfV; i++){ |
| 81 | + cout << i << ":"; |
| 82 | + for (int j = 0; j < g.numberOfV; j++){ |
| 83 | + if (i != j && g.data[i][j] != INFINITY) cout << j << "(" << g.data[i][j] << ")" << ","; |
| 84 | + } |
| 85 | + cout << endl; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +bool isCommand(const string command,const char *mnemonic){ |
| 90 | + return command==mnemonic; |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +int main(){ |
| 95 | + string line; |
| 96 | + string command; |
| 97 | + Graph *graph; |
| 98 | + int currentT=0; |
| 99 | + int value; |
| 100 | + cout << "START" << endl; |
| 101 | + while(true){ |
| 102 | + getline(cin,line); |
| 103 | + std::stringstream stream(line); |
| 104 | + stream >> command; |
| 105 | + if(line=="" || command[0]=='#') |
| 106 | + { |
| 107 | + // ignore empty line and comment |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + // copy line on output with exclamation mark |
| 112 | + cout << "!" << line << endl;; |
| 113 | + |
| 114 | + // change to uppercase |
| 115 | + command[0]=toupper(command[0]); |
| 116 | + command[1]=toupper(command[1]); |
| 117 | + |
| 118 | + // zero-argument command |
| 119 | + if(isCommand(command,"HA")){ |
| 120 | + cout << "END OF EXECUTION" << endl; |
| 121 | + break; |
| 122 | + } |
| 123 | + // zero-argument command |
| 124 | + if(isCommand(command,"SM")) |
| 125 | + { |
| 126 | + showAsMatrix(graph[currentT]); |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + if(isCommand(command,"SA")) |
| 131 | + { |
| 132 | + showAsArrayOfLists(graph[currentT]); |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + // read next argument, one int value |
| 138 | + stream >> value; |
| 139 | + |
| 140 | + if(isCommand(command,"LG")) |
| 141 | + { |
| 142 | + int m; |
| 143 | + stream >> m; |
| 144 | + loadGraph(graph[currentT],value,m); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + if(isCommand(command,"IE")) |
| 149 | + { |
| 150 | + int v; |
| 151 | + double w; |
| 152 | + stream >> v >> w; |
| 153 | + insertEdge(graph[currentT],value,v,w); |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + if(isCommand(command,"FE")) |
| 158 | + { |
| 159 | + int v; |
| 160 | + stream >> v; |
| 161 | + double w; |
| 162 | + bool ret=findEdge(graph[currentT],value,v,w); |
| 163 | + |
| 164 | + if(ret) |
| 165 | + cout << w << endl; |
| 166 | + else |
| 167 | + cout << "false" << endl; |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + if(isCommand(command,"CH")) |
| 173 | + { |
| 174 | + currentT=value; |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + if(isCommand(command,"GO")) |
| 179 | + { |
| 180 | + graph=new Graph[value]; |
| 181 | + continue; |
| 182 | + } |
| 183 | + |
| 184 | + cout << "wrong argument in test: " << command << endl; |
| 185 | + return 1; |
| 186 | + } |
| 187 | +} |
0 commit comments