#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; }

C++ 排行榜程序:添加玩家、显示排名、写入文件

原文地址: https://www.cveoy.top/t/topic/bUqM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录