-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprj2.cpp
More file actions
95 lines (93 loc) · 2.7 KB
/
Copy pathprj2.cpp
File metadata and controls
95 lines (93 loc) · 2.7 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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <map>
#include <cctype>
using namespace std;
int main(int argc, char *argv[]){
ifstream file(argv[1]);
ifstream inFS;
if(argc != 2){
cout << "Invalid command line arguments" << endl;
return 1;
}
string fileName = argv[1];
inFS.open(fileName);
if(!(inFS.is_open())){
cout << "Could not open the file." << endl;
return 1;
}
string line;
bool startString = false;
int startIndex;
bool endString = false;
int endIndex;
bool startNum = false;
int startNumIndex;
int endNumIndex = 0;
bool firstNum = false;
map<string, int> data;
while(getline(inFS, line)){
bool startString = false;
int startIndex;
bool endString = false;
int endIndex;
bool startNum = false;
int startNumIndex;
int endNumIndex = 0;
bool firstNum = false;
for(unsigned int i = 0; i < line.size(); i++){
if(line.at(i) == ' ' || line.at(i) == '\t'){
continue;
}
if(!startString && line.at(i) == '"'){
startString = true;
startIndex = i;
i++;
}
//string part
while(startString && !endString){
if(line.at(i) == '\\'){
if(line.at(i+1) == '\\' || line.at(i+1) == '"') i+=2;
else cout << "error" << endl; //error
}
if(line.at(i) == '"'){
endString = true;
endIndex = i+1;
}
i++;
}
while(endString && line.at(i) == '0' && !startNum) continue;
if(endString && isdigit(line.at(i))){
startNum = true; //allow zeros in the number
if(!firstNum){
startNumIndex = i;
firstNum = true;
}
}
if(startNum){
if(line.at(i) == ' ' || line.at(i) == '\t'){
endNumIndex = i-1;
break;
}
}
}
if(endNumIndex == 0) endNumIndex = line.size();
string key = line.substr(startIndex, endIndex-startIndex);
int value = stoi(line.substr(startNumIndex, endNumIndex-1));
auto it = data.find(key);
if(it != data.end()) {
if (value > it->second){
it->second = value;
}
}
else{
data[key] = value;
}
}
inFS.close();
for (const auto& pair : data) {
cout << pair.first << " " << pair.second << endl;
}
}