-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvparser.cpp
More file actions
179 lines (175 loc) · 4.97 KB
/
csvparser.cpp
File metadata and controls
179 lines (175 loc) · 4.97 KB
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
#include "csvparser.hpp"
CSVParser::CSVParser(std::ifstream &file)
{
std::string line_str;
std::getline(file, line_str);
std::istringstream line_istr(line_str);
line_istr.get(); // extracting first comma
while(std::getline(line_istr, line_str, ',')) // parsing first line
{
column_name_list.push_back(line_str); // completing list of horizontal headers
}
while(std::getline(file, line_str) && (line_str != "")) // parsing rest of the file
{
int row = 0;
line_istr = std::istringstream(line_str);
line_istr >> row;
std::list<std::string>::iterator it = column_name_list.begin();
row_name_list.push_back(row); // completing list of vertical headers
line_istr.get(); // extracting extra comma from the line
while(std::getline(line_istr, line_str, ',')) // parsing rest of the line
{
std::string cell_addr = getCellAddr(*it, row);
if(line_str[0] != '=') // checking if the cell is formula
{
table.insert(std::pair<std::string, int>(cell_addr, atoi(line_str.c_str()))); // inserts cell in the table
}
else
{
table.insert(std::pair<std::string, int>(cell_addr, -112358)); // inserts "blank" cell in the table
raw_cells_map.insert(std::pair<std::string, std::string>(cell_addr, line_str)); // insrts cell to the list of not solved cells
}
it++;
}
}
}
void CSVParser::printInput()
{
std::cout << "______Input______" << std::endl;
std::cout << std::endl;
for(auto c : column_name_list)
{
std::cout <<"," + c;
}
std::cout << std::endl;
for(auto v : row_name_list)
{
std::cout << v;
for(auto h : column_name_list)
{
if(raw_cells_map.count(getCellAddr(h, v)) == 0)
{
std::cout << "," << table.at(getCellAddr(h, v));
}
else
{
std::cout << "," << raw_cells_map.at(getCellAddr(h, v));
}
}
std::cout << std::endl;
}
}
void CSVParser::printResault()
{
std::cout << "______Output______" << std::endl;
std::cout << std::endl;
for(auto c : column_name_list)
{
std::cout <<"," + c;
}
std::cout << std::endl;
for(auto v : row_name_list)
{
std::cout << v;
for(auto h : column_name_list)
{
std::cout << "," << table.at(getCellAddr(h, v));
}
std::cout << std::endl;
}
}
std::string CSVParser::getCellAddr(std::string &column, int &row)
{
return column + std::to_string(row);
}
void CSVParser::completeTable()
{
while(raw_cells_map.size() != 0)
{
for(auto raw_cell : raw_cells_map)
{
std::string cell1 = "", cell2 = "", raw_cell_addr = raw_cell.first, operators = "+-*/";
char c, op;
int arg1, arg2;
std::istringstream istr(raw_cell.second);
istr.get(); // extracting '='
istr.get(c);
while(operators.find(c) == std::string::npos) // while c is not an operator
{
cell1.push_back(c);
istr.get(c);
}
op = c;
istr >> cell2;
if(raw_cells_map.count(cell1) != 0 || raw_cells_map.count(cell2) != 0) // solve another cell if cell in the formula is not solved yet
{
continue;
}
else if(table.count(cell1) == 0)
{
throw std::invalid_argument("Unsolvable cell " + cell1);
}
else if(table.count(cell2) == 0)
{
throw std::invalid_argument("Unsolvable cell " + cell2);
}
else
{
switch(op)
{
case '+':
arg1 = table.find(cell1)->second;
arg2 = table.find(cell2)->second;
table.find(raw_cell_addr)->second = arg1 + arg2;
break;
case '-':
arg1 = table.find(cell1)->second;
arg2 = table.find(cell2)->second;
table.find(raw_cell_addr)->second = arg1 - arg2;
break;
case '*':
arg1 = table.find(cell1)->second;
arg2 = table.find(cell2)->second;
table.find(raw_cell_addr)->second = arg1 * arg2;
break;
case '/':
arg1 = table.find(cell1)->second;
arg2 = table.find(cell2)->second;
table.find(raw_cell_addr)->second = arg1 / arg2;
break;
default:
break;
}
raw_cells_map.erase(raw_cell_addr);
break; // start for again to prevent segmentation fault
}
}
}
}
// void CSVParser::printAll()
// {
// std::cout << "Map values: \n";
// for(auto t : table)
// {
// std::cout << t.first << " is " << t.second << std::endl;
// }
// std::cout << std::endl;
// std::cout << "Vertical headers: \n";
// for(auto v : row_name_list)
// {
// std::cout << v << " ";
// }
// std::cout << std::endl;
// std::cout << "Horizontal headers: \n";
// for(auto h : column_name_list)
// {
// std::cout << h << " ";
// }
// std::cout << std::endl;
// std::cout << "Unsolved cells \n";
// for(auto c : raw_cells_map)
// {
// std::cout << c.first << c.second << std::endl;
// }
// std::cout << std::endl;
// }