#include
#include
#include
using namespace std;
struct Player {
string m_id;
int m_score;
};
void addPlayer(vector& players) {
Player p;
string id;
int score;
cout << '请输入昵称:';
cin >> id;
cout << '请输入积分:';
cin >> score;
p.m_id = id;
p.m_score = score;
players.push_back(p);
}
bool cmp(Player p1, Player p2) {
return p1.m_score > p2.m_score;
}
void showRank(const vector& players) {
cout << '名次 昵称 积分' << endl;
int i = 1;
for (const Player& p : players) {
cout << i << ' ' << p.m_id << ' ' << p.m_score << endl;
i++;
}
}
void textShowRank(const vector& players) {
ofstream file('text02.txt');
if (file.is_open()) {
int i = 1;
file << '名次 昵称 积分' << endl;
for (const Player& p : players) {
file << i << ' ' << p.m_id << ' ' << p.m_score << endl;
i++;
}
file.close();
} else {
cout << '无法打开文件' << endl;
}
ifstream readFile('text02.txt');
if (readFile.is_open()) {
string line;
while (getline(readFile, line)) {
cout << line << endl;
}
readFile.close();
} else {
cout << '无法打开文件' << endl;
}
}
int main() {
vector players;
addPlayer(players);
showRank(players);
textShowRank(players);
return 0;
}