-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc.cpp
205 lines (194 loc) · 5.37 KB
/
Calc.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "Calc.h"
using std::string;
using std::map;
//return true if the grhap exists in map
bool Calc::isGraphExist(const std::string& name)
{
auto itr = elements.find(name);
return itr == elements.end();
}
//get a name of graph to delete if not exists throws an error
void Calc::deleteGraph(const std::string& name)
{
auto itr = elements.find(name);
if (itr != elements.end())
this->elements.erase(name);
else
{
throw UndefinedVariable(name);
}
}
//adds a graph to the map key is the name and value is the graph itself
void Calc::addGraph(const std::string& name, const std::string& graphRepresentation)
{
try
{
elements[name] = Graph(graphRepresentation);
}
catch (const std::runtime_error & e)
{
std::cerr << e.what();
}
}
//takes an existing grahp and translate it to a string that the defualt con' of graph can change back
std::string Calc::graphToString(const std::string& name)
{
auto itr = elements.find(name);
if (itr != elements.end())
{
std::string graphWithoutName = itr->second.graphToString();
return graphWithoutName;
}
else
{
throw UndefinedVariable(name);
}
}//return a graph by a given name if the graph does not exsits throw exception
//return a grhap by a given name, if the graph does not exists throw exceptione
Graph Calc::getGraphByName(const std::string& name)
{
auto itr = this->elements.find(name);
if (itr == this->elements.end())
throw UndefinedVariable(name);
return itr->second;
}
//adds a graph to the map key is the name and value is the graph itself
void Calc::addGraph(const std::string& name, const Graph& g)
{
try
{
auto it = elements.find(name);
if (it != elements.end()) //the graph already exsits in the map
{
Graph g_old = it->second;
elements.erase(it);
elements.emplace(name, g);
}
else
{
elements.emplace(name, g);
}
}
catch (const std::exception & e)
{
std::cerr << e.what();
}
}
//prints a given graph by its name
void Calc::print(const std::string& name)
{
auto itr = elements.find(name);
if (itr != elements.end())
itr->second.print();
else
{
throw UndefinedVariable(name);
}
}
//prints the names of all the variables saved in the calc
void Calc::who() //-----------need to pass os stream
{
for (auto const& g : this->elements)
{
std::cout << g.first << std::endl;
}
}
//reset the calc, erasing the map
void Calc::reset()
{
this->elements.clear();
}
//save a grhap as a binary file throw exception if there is a problem with the file
void Calc::saveGraph(const char* file_name, const Graph& g)
{
std::ofstream outfile(file_name, std::ios_base::binary);//outfile.write((const char*)&num, sizeof(int));
if (!outfile.is_open())
{
throw InvalidFileForSave(file_name);
}
unsigned int number_of_vertices = (unsigned int)g.getVertices().size();
unsigned int number_of_edges = (unsigned int)g.getEdges().size();
outfile.write((const char*)&number_of_vertices, sizeof(unsigned int));
outfile.write((const char*)&number_of_edges, sizeof(unsigned int));
for (const auto& ver : g.getVertices())
{
unsigned int size_of_ver_name = (unsigned int)ver.size();
outfile.write((const char*)&size_of_ver_name, sizeof(unsigned int));//size of vertex name
for (auto const& c : ver)
{
outfile.write((const char*)&c, sizeof(char));//wtite the ver name char by char
}
}
for (const auto& edg : g.getEdges())
{
unsigned int size_of_src_edg_name = (unsigned int)edg.first.size();
outfile.write((const char*)&size_of_src_edg_name, sizeof(unsigned int));//size of edge src name
for (auto const& c : edg.first)
{
outfile.write((const char*)&c, sizeof(char));//wtite the src edge name char by char
}
unsigned int size_of_dest_edg_name = (int)edg.second.size();
outfile.write((const char*)&size_of_dest_edg_name, sizeof(unsigned int));//size of edge dest name
for (auto const& c : edg.second)
{
outfile.write((const char*)&c, sizeof(unsigned char));//wtite the src edge name char by char
}
}
}
//return a string from a file throw exception if there is a problem with the file
string Calc::loadGraph(const char* file_name)
{
string graph = "{";
std::ifstream infile(file_name, std::ios_base::binary);//outfile.write((const char*)&num, sizeof(int));
if (!infile.is_open())
{
throw InvalidFileForRead(file_name);
}
unsigned number_of_ver, number_of_edg, number_of_chars_name;
char c;
infile.read((char*)&number_of_ver, sizeof(unsigned int));
infile.read((char*)&number_of_edg, sizeof(unsigned int));
for (unsigned i = 0; i < number_of_ver; i++)
{
string ver;
infile.read((char*)&number_of_chars_name, sizeof(unsigned int));
for (unsigned i = 0; i < number_of_chars_name; i++)
{
infile.read((char*)&c, sizeof(char));
ver += c;
}
graph += ver;
graph += ",";
}
if (number_of_ver > 1)//removes the last comma
{
graph.pop_back();
}
graph += "|";
for (unsigned int i = 0; i < number_of_edg; i++)
{
string edg;
edg += "<";
infile.read((char*)&number_of_chars_name, sizeof(unsigned int));
for (unsigned int i = 0; i < number_of_chars_name; i++)//read src ver
{
infile.read((char*)&c, sizeof(char));
edg += c;
}
edg += ",";
infile.read((char*)&number_of_chars_name, sizeof(unsigned int));
for (unsigned int i = 0; i < number_of_chars_name; i++)//read dest ver
{
infile.read((char*)&c, sizeof(char));
edg += c;
}
edg += ">";
graph += edg;
graph += ",";
}
if (number_of_ver > 1)//removes the last comma
{
graph.pop_back();
}
return graph += "}";
}