-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.cpp
74 lines (67 loc) · 1.04 KB
/
history.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
#include "history.hpp"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
history::history()
{
fileName="save.txt";
}
history::history(string s)
{
ifstream fs;
fileName=s;
fs.open(s,ifstream::in);
if(!fs.good())
{
fileName="save.txt";
fs.close();
}
else
{
string temp;
while(true)
{
fs >> temp;
if(fs.eof()) break;
for(int i=0;i<s.length();i++)
{
if(s[i]=='&') s[i]=' ';
}
his.push_back(temp);
}
fs.close();
}
}
void history::write(string s)
{
for(int i=0;i<s.length();i++)
{
if(s[i]==' ') s[i]='&';
}
his.push_back(s);
}
string history::getLast(int n)
{
if(his.size()-n-1<0 || his.size()-n-1>=his.size()) return "";
else
{
string s=his[his.size()-n-1];
for(int i=0;i<s.length();i++)
{
if(s[i]=='&') s[i]=' ';
}
return s;
}
}
void history::save()
{
ofstream fs;
fs.open(fileName,ofstream::out | ios::trunc);
if(!fs.is_open()) throw runtime_error("error opening file: "+fileName);
for(int i=0;i<his.size();i++)
{
fs << his[i] << endl;
}
fs.close();
}