-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_db.cpp
64 lines (58 loc) · 1.25 KB
/
problem_db.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
#include "problem_db.h"
#include <iostream>
#include <map>
#include <fstream>
using namespace std;
bool problem_db::add(int id, string filename){
auto it = mymap->find(id);
if(it != mymap->end())
return false;
ifstream fin;
fin.open(filename, ios::in);
if(!fin)
return false;
else{
int num;
fin >> num;
total+=num;
fin.close();
}
mymap->insert(pair<int, string>(id, filename));
return true;
}
void problem_db::remove(int key){
auto it = mymap->find(key);
if(it == mymap->end())
return;
ifstream fin;
fin.open(it->second, ios::in);
int num;
fin >> num;
total-=num;
fin.close();
mymap->erase(key);
}
void problem_db::readin(){
ifstream fin("index.txt", ios::in);
fin >> total;
int id;
string filename;
while(fin >> id){
fin >> filename;
add(id, filename);
}
fin.close();
}
void problem_db::writeout(){
ofstream fout("index", ios::out | ios::ate);
fout << total << endl;
for(auto it : *mymap)
fout << it.first << " " << it.second << "\n";
fout.close();
}
string problem_db::find(int key){
auto it = mymap->find(key);
if(it == mymap->end())
return "";
return it->second;
}