-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandleHistory.cpp
90 lines (82 loc) · 2.16 KB
/
HandleHistory.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
//
// Created by Shanwer on 2023/11/1.
//
#include <iostream>
#include <fstream>
#include <chrono>
#include "HandleHistory.h"
using namespace std;
typedef struct{
int id;
int score;
unsigned int questNum;
string time;
}score;
bool saveScore(int correctTimes, unsigned int questNum){
ifstream ifs;
ifs.open("score.txt", ios::in);//I believe myself so no further checks will be made.
int lineCount = 1;
string aline;
while(getline(ifs,aline)){
lineCount++;
}
ifs.close();
// 获取当前时间点
auto now = std::chrono::system_clock::now();
// 转换为time_t以便能以年月日的形式输出
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
score presentScore{
.id = lineCount,
.score = correctTimes,
.questNum = questNum,
.time = std::ctime(&now_c),
};
fstream fs;
fs.open("score.txt", ios::app);
fs << presentScore.id << " " << presentScore.score << " " << presentScore.questNum << " "<< presentScore.time;
fs.close();
return true;
}
int findHighest(){
ifstream ifs;
ifs.open("score.txt", ios::in);
string aline;
int highest = 0;
if(ifs.peek() == EOF){
//成绩文件存在但为空则返回-1,防止逻辑判断错误
return -1;
}
while(getline(ifs,aline)){
string intString;
int startIndex, endIndex;
bool sign = false;
for(int i = 0; ; i++){
if(aline[i] == ' ' && sign){
endIndex = i+1;
break;
}else if(aline[i] == ' '){
startIndex = i+1;
sign = true;
}
}
highest = max(highest,stoi(aline.substr(startIndex,endIndex-startIndex)));
}
return highest;
}
void traverseScoreFile(){
ifstream ifs;
ifs.open("score.txt", ios::in);
string aline;
while(getline(ifs,aline)){
cout << "ID\t答对题目 总题目\t时间" << endl;
int sign = 0;
for(int i = 0 ; i<aline.length(); i++){
if(aline[i]==' ' && sign < 3){
aline.insert(i,1,'\t');
i++;
sign++;
}
}
cout << aline << endl;
}
}