写一个歌唱比赛系统,要求用链表和文件读写
时间: 2024-05-14 08:18:40 浏览: 77
好的,以下是一个简单的歌唱比赛系统的实现,包括参赛者的信息的录入、显示、删除和文件读写等功能。
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Singer {
string name;
string song;
int score;
Singer* next;
};
Singer* head = nullptr;
void insertSinger() {
Singer* newSinger = new Singer;
cout << "Enter singer's name: ";
cin >> newSinger->name;
cout << "Enter singer's song: ";
cin >> newSinger->song;
newSinger->score = 0;
newSinger->next = nullptr;
if (head == nullptr) {
head = newSinger;
}
else {
Singer* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newSinger;
}
}
void displaySingers() {
if (head == nullptr) {
cout << "No singers found." << endl;
return;
}
Singer* current = head;
while (current != nullptr) {
cout << "Name: " << current->name << endl;
cout << "Song: " << current->song << endl;
cout << "Score: " << current->score << endl << endl;
current = current->next;
}
}
void deleteSinger() {
if (head == nullptr) {
cout << "No singers found." << endl;
return;
}
string name;
cout << "Enter singer's name to delete: ";
cin >> name;
if (head->name == name) {
Singer* temp = head;
head = head->next;
delete temp;
cout << "Singer deleted." << endl;
return;
}
Singer* current = head;
while (current->next != nullptr) {
if (current->next->name == name) {
Singer* temp = current->next;
current->next = temp->next;
delete temp;
cout << "Singer deleted." << endl;
return;
}
current = current->next;
}
cout << "Singer not found." << endl;
}
void saveToFile() {
ofstream outFile("singers.txt");
if (!outFile) {
cerr << "Failed to open file." << endl;
return;
}
Singer* current = head;
while (current != nullptr) {
outFile << current->name << " " << current->song << " " << current->score << endl;
current = current->next;
}
outFile.close();
cout << "Singers saved to file." << endl;
}
void loadFromFile() {
ifstream inFile("singers.txt");
if (!inFile) {
cerr << "Failed to open file." << endl;
return;
}
string name, song;
int score;
while (inFile >> name >> song >> score) {
Singer* newSinger = new Singer;
newSinger->name = name;
newSinger->song = song;
newSinger->score = score;
newSinger->next = nullptr;
if (head == nullptr) {
head = newSinger;
}
else {
Singer* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newSinger;
}
}
inFile.close();
cout << "Singers loaded from file." << endl;
}
int main() {
int choice;
do {
cout << "1. Insert singer" << endl;
cout << "2. Display singers" << endl;
cout << "3. Delete singer" << endl;
cout << "4. Save to file" << endl;
cout << "5. Load from file" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
insertSinger();
break;
case 2:
displaySingers();
break;
case 3:
deleteSinger();
break;
case 4:
saveToFile();
break;
case 5:
loadFromFile();
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}
cout << endl;
} while (choice != 6);
return 0;
}
```
这个程序使用了一个 `Singer` 结构体来存储参赛者的信息,然后用链表来存储多个参赛者的信息。主要的功能包括录入、显示、删除、文件读写等。在录入参赛者信息时,程序会将其添加到链表的末尾;在显示参赛者信息时,程序会遍历整个链表并输出每个参赛者的信息;在删除参赛者时,程序会根据参赛者的姓名在链表中查找并删除;在保存到文件时,程序会将链表中的每个参赛者的信息写入到文件中;在从文件中读取时,程序会从文件中读取每个参赛者的信息并添加到链表中。
希望这个简单的例子能够帮助你了解如何使用链表和文件读写来实现一个较为完整的程序。
阅读全文