自己做的一个小型异质链表加文件的读写(BY期末大作业)

#include<string>
#include<iostream>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<fstream>
using namespace std;
int ju = 0;
class List;
class Person {
	friend class List;
	friend ostream& operator<<(ostream& os, Person* k) {
		os << "----------------------------" << endl;
		os << "the name: " << k->name << endl;
		os << "the age: " << k->age << endl;
		os << "the address: " << k->address << endl;
		os << "the telephone: " << k->telephone << endl;
		os << "-----------------------------" << endl;
		return os;
	}
protected:
	string name;
	int age;
	string address;
	string telephone;
	Person* next;
	Person* ptr;
public:
	Person(string n, int a, string aa, string t) {
		name = n;
		age = a;
		address = aa;
		telephone = t;
	}
	~Person() {}
	virtual void print() {
		cout << "----------------------------" << endl;
		cout << "the name: " << this->name << endl;
		cout << "the age: " << this->age << endl;
		cout << "the address: " << this->address << endl;
		cout << "the telephone: " << this->telephone << endl;
		cout << "-----------------------------" << endl;
	}
	virtual void insert() {
		ptr = new Person(name, age, address, telephone);
	}
	virtual int judge() {
		return 0;
	}
};
class Student :public Person {
	friend class List;
	friend ostream& operator<<(ostream& os, Student* p) {
		os << "----------------------------" << endl;
		os << "the name: " << p->name << endl;
		os << "the age: " << p->age << endl;
		os << "the address: " << p->address << endl;
		os << "the telephone: " << p->telephone << endl;
		os << "the level: " << p->level << endl;
		os << "the grade_point_average: " << p->grade_point_average << endl;
		os << "-----------------------------" << endl;
		return os;
	}
private:
	int level;
	float grade_point_average;
public:
	Student(string s1, int a, string s2, string s3, int l, float g) :Person(s1, a, s2, s3) {
		level = l;
		grade_point_average = g;
	}
	~Student() {}
	void print();
	void insert();
	int judge() {
		return 1;
	}
};
void Student::insert()
{
	ptr = new Student(name, age, address, telephone, level, grade_point_average);
}
void Student::print() {
	cout << "----------------------------" << endl;
	cout << "the name: " << this->name << endl;
	cout << "the age: " << this->age << endl;
	cout << "the address: " << this->address << endl;
	cout << "the telephone: " << this->telephone << endl;
	cout << "the level: " << this->level;
	cout << "the grade_point_average: " << this->grade_point_average << endl;
	cout << "-----------------------------" << endl;
}
class Teacher :public Person {
	friend class List;
	friend ostream& operator<<(ostream& os, Teacher* p) {
		os << "----------------------------" << endl;
		os << "the name: " << p->name << endl;
		os << "the age: " << p->age << endl;
		os << "the address: " << p->address << endl;
		os << "the telephone: " << p->telephone << endl;
		os << "the salary: " << p->salary << endl;
		os << "-----------------------------" << endl;
		return os;
	}
private:
	float salary;
public:
	Teacher(string s1, int a, string s2, string s3, float s) :Person(s1, a, s2, s3) {
		salary = s;
	}
	~Teacher() {}
	void print();
	void insert();
	int judge() {
		return 2;
	}
};
void Teacher::insert() {
	ptr = new Teacher(name, age, address, telephone, salary);
}
void Teacher::print() {
	cout << "----------------------------" << endl;
	cout << "the name: " << this->name << endl;
	cout << "the age: " << this->age << endl;
	cout << "the address: " << this->address << endl;
	cout << "the telephone: " << this->telephone << endl;
	cout << "the salary: " << this->salary << endl;
	cout << "-----------------------------" << endl;
}
class Staff :public Person {
	friend class List;
	friend ostream& operator<<(ostream& os, Staff* p) {
		os << "----------------------------" << endl;
		os << "the name: " <<p->name << endl;
		os << "the age: " << p->age << endl;
		os << "the address: " << p->address << endl;
		os << "the telephone: " << p->telephone << endl;
		os << "the hourly_wages: " << p->hourly_wages << endl;
		os << "-----------------------------" << endl;
		return os;
	}
private:
	float hourly_wages;
public:
	Staff(string s1, int a, string s2, string s3, float h) :Person(s1, a, s2, s3) {
		hourly_wages = h;
	}
	~Staff() {}
	void print();
	void insert();
	int judge() {
		return 3;
	}
};
void Staff::insert() {
	ptr = new Staff(name, age, address, telephone, hourly_wages);
}
void Staff::print() {
	cout << "----------------------------" << endl;
	cout << "the name: " << this->name << endl;
	cout << "the age: " << this->age << endl;
	cout << "the address: " << this->address << endl;
	cout << "the telephone: " << this->telephone << endl;
	cout << "the hourly_wages: " << this->hourly_wages << endl;
	cout << "-----------------------------" << endl;
}
class List {
private:
	Person* root;
public:
	List() {
		root = NULL;
	}
	~List() {}
	void insert_list(Person* node);
	void print_list()const;
	void readfiles();
	void writefiles();
};
void List::insert_list(Person* node) {
	string s = node->name;
	Person* middle = root;
	Person* previous = NULL;
	while (middle && s < middle->name) {
		previous = middle;
		middle = middle->next;
	}
	node->insert();
	node->ptr->next = middle;
	if (previous == NULL) {
		root = node->ptr;
	}
	else
		previous->next = node->ptr;
}
void List::print_list()const {
	Person* r = root;
	while (r) {
		cout << r;
		r = r->next;
	}
}
void List::readfiles()
{
	cout << "请输入读取的文件名\n";
	string ss;
	cin >> ss;
	ifstream InFiles;
	InFiles.open(ss.c_str());
	if (!InFiles.is_open()) {
		cout << "文件不存在\n";
		exit(EXIT_FAILURE);
	}
	else {
		while (!InFiles.eof()) {
			char ch;
			InFiles >> ch;
			cout << ch << endl;
			if (ch == 'S') {
				string a;int b;string c, d;int f; float e=0.0;
				InFiles >> a >> c >> d >> b >> e >> f;
				if (InFiles.fail() == true) {
					cout << "发现输入流挂起,这一整行数据被忽略\n";
					InFiles.clear();InFiles.ignore(1024, '\n');
				}
				else {
					Student* middle = new Student(a, b, c, d, f, e);
					this->insert_list(middle);
					InFiles.get();
				}
			}
			else if (ch == 'E') {
				string a; int b; string c, d; float e;
				InFiles >> a >> c >> d >> b >> e;
				if (InFiles.fail() == true) {
					cout << "发现输入流挂起,这一整行数据被忽略\n";
					InFiles.clear();InFiles.ignore(1024, '\n');
				}
				else {
					Staff* middle = new Staff(a, b, c, d, e);
					this->insert_list(middle);
					InFiles.get();
				}
			}
			else if (ch == 'T') {
				string a; int b; string c, d; float e;
				InFiles >> a >> c >> d >> b >> e;
				if (InFiles.fail() == true) {
					cout << "发现输入流挂起,这一整行数据被忽略\n";
					InFiles.clear();InFiles.ignore(1024, '\n');
				}
				else {
					Teacher* middle = new Teacher(a, b, c, d, e);
					this->insert_list(middle);
					InFiles.get();
				}
			}
			else if (ch == 'P') {
				string a; int b; string c, d;
				InFiles >> a >> c >> d >> b;
				if (InFiles.fail() == true) {
					cout << "发现输入流挂起,这一整行数据被忽略\n";
					InFiles.clear();InFiles.ignore(1024, '\n');
				}
				else {
					Person* middle = new Person(a, b, c, d);
					this->insert_list(middle);
					InFiles.get();
				}
			}
			else {
				cout << "发现不匹配类型,这一整行数据被忽略\n";
				InFiles.ignore(1024,'\n');
			}
		}
	}
	InFiles.close();
}
void List::writefiles()
{
	
	cout << "请输入要写入的文件名\n";
	string s; cin >> s;
	ofstream OutFiles(s);
	if (!OutFiles.is_open()) {
		cout << "文件无法打开\n";
		exit(EXIT_FAILURE);
	}
	else {
		Person* h = root;
		while (h) {
			if (h->judge() == 1) {
				Student* pr = (Student*)h;
				OutFiles << pr;
			}
			else if (h->judge() == 2) {
				Teacher* pr = (Teacher*)h;
				OutFiles << pr;
			}
			else if (h->judge() == 3) {
				Staff* pr = (Staff*)h;
				OutFiles << pr;
			}
			else if (h->judge() == 0) {
				OutFiles << h;
			}
			h = h->next;
		}	
	}
	OutFiles.close();
}
int main()
{
	List people;
	people.readfiles();
	people.readfiles();
	people.writefiles();
	system("pause");
}
```cpp
好的,我可以给你一个简单的歌唱比赛系统的实现,使用链表文件读写来存储参赛者信息和比赛结果。 首先,我们需要定义一个参赛者的结构体,包括姓名、年龄、性别和得分等信息。 ```c typedef struct contestant { char name[50]; int age; char gender; float score; struct contestant* next; } Contestant; ``` 然后,我们需要实现一个函数来从文件读取参赛者信息并创建一个链表。我们可以将每个参赛者存储为一个结构体,并将它们连接成一个链表。 ```c Contestant* read_contestants(char* filename) { FILE* fp = fopen(filename, "r"); Contestant* head = NULL; Contestant* tail = NULL; char line[100]; while (fgets(line, 100, fp) != NULL) { Contestant* c = (Contestant*)malloc(sizeof(Contestant)); sscanf(line, "%s %d %c", c->name, &c->age, &c->gender); c->score = 0.0; c->next = NULL; if (head == NULL) { head = c; } else { tail->next = c; } tail = c; } fclose(fp); return head; } ``` 接下来,我们可以实现一个函数来让评委们给每个参赛者打分。 ```c void rate_contestants(Contestant* head) { Contestant* current = head; while (current != NULL) { printf("Please rate %s's performance (0-10): ", current->name); float score; scanf("%f", &score); current->score += score; current = current->next; } } ``` 最后,我们可以实现一个函数来将比赛结果写入文件中。 ```c void write_results(Contestant* head, char* filename) { FILE* fp = fopen(filename, "w"); Contestant* current = head; while (current != NULL) { fprintf(fp, "%s %d %c %.2f\n", current->name, current->age, current->gender, current->score); current = current->next; } fclose(fp); } ``` 这些函数可以组合在一起,形成一个完整的歌唱比赛系统。例如: ```c int main() { Contestant* contestants = read_contestants("contestants.txt"); rate_contestants(contestants); write_results(contestants, "results.txt"); return 0; } ``` 在这个示例中,我们从名为“contestants.txt”的文件读取参赛者信息,然后让评委们给每个参赛者打分,最后将比赛结果写入名为“results.txt”的文件中。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值