数据结构课设小作业 | 成绩管理系统

#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define LIST_INIT_SIZE 1000
#define LISTINCREMENT 100
struct student
{
	char id[100];
	char name[100];
	char gender[100];
	int age;
	int score;
	bool operator< (const student& b)
	{
		return strcmp(id, b.id) < 0;
	}
	//student(char a[], char b[], char c[], int d, int e) : id(a), name(b), gender(c), age(d), score(e) {}
};
typedef struct
{
	student* base;
	int length;
	int listsize;
}SqList;

void IniList(SqList* l)
{
	l->base = (student*)malloc(LIST_INIT_SIZE * sizeof(student));
	l->length = 0, l->listsize = LIST_INIT_SIZE;
}

void showinf(student s)
{
	cout << s.id << " " << s.name << " " << s.gender << " " << s.age << " " << s.score << endl;
}

void showallinf(SqList* l)
{
	cout << "准考证号" << " " << "姓名" << " " << "性别" << " " << "年龄" << " " << "成绩" << endl;
	for (int i = 0; i < l->length; i++)
		showinf(l->base[i]);
}

SqList* loadinf()
{
	SqList* l = (SqList*)malloc(sizeof(SqList));
	IniList(l);
	FILE* p = fopen("score.txt", "r");
	if (!p) exit(0);
	char buf[1000];
	while (fgets(buf, 100, p))
	{
		char a[100], b[100], c[100];
		int d;
		int e;
		sscanf(buf, "%s %s %s %d %d", a, b, c, &d, &e);
		student stu;
		strcpy(stu.id, a), strcpy(stu.name, b), strcpy(stu.gender, c), stu.age = d, stu.score = e;
		l->base[l->length++] = stu;
		if (l->length == l->listsize) l->base = (student*)realloc(l->base, (l->listsize + LISTINCREMENT) * sizeof(student));
	}
	fclose(p);
	return l;
}
void writeinf(SqList* l)
{
	FILE* p = fopen("score.txt", "w");
	char buf[1000];

	for (int i = 0; i < l->length; i++)
	{
		sprintf(buf, "%s %s %s %d %d\n", l->base[i].id, l->base[i].name, l->base[i].gender, l->base[i].age, l->base[i].score);
		fputs(buf, p);
	}
	fclose(p);
}

int exist(SqList* l, char id[])//检查文件中是否已经存在考号为id的学生
{
	for (int i = 0; i < l->length; i++)
		if (strcmp(l->base[i].id, id) == 0)
			return i;
	return -1;
}

void Add()//添加学生
{
	SqList* l = loadinf();
	cout << "请输入学生信息" << endl;
	char a[100], b[100], c[100];
	int d;
	int e;
	cout << "准考证号:"; cin >> a;
	cout << "姓名:"; cin >> b;
	cout << "性别:"; cin >> c;
	cout << "年龄:"; cin >> d;
	cout << "成绩:"; cin >> e;
	student stu;
	strcpy(stu.id, a), strcpy(stu.name, b), strcpy(stu.gender, c), stu.age = d, stu.score = e;

	if (exist(l, stu.id) != -1) { cout << "该学生已存在!" << endl; return; }
	if (l->length == l->listsize) l->base = (student*)realloc(l, (LIST_INIT_SIZE + LISTINCREMENT) * sizeof(student));
	l->base[l->length++] = stu;
	sort(l->base, l->base + l->length);
	cout << "添加成功" << endl;
	writeinf(l);
}

void edit()//编辑学生
{
	SqList* l = loadinf();
	char id[100];
	cout << "请输入编辑学生的id" << endl; cin >> id;
	if (exist(l, id) == -1) { cout << "该学生不存在,无法修改" << endl; }
	else
	{
		for (int i = 0; i < l->length; i++)
			if (strcmp(l->base[i].id, id) == 0)
			{
				cout << "请输入修改后的学生信息:" << endl;
				char name[100], gender[100];
				int age;
				int score;
				cout << "姓名:"; cin >> name;
				cout << "性别:"; cin >> gender;
				cout << "年龄:"; cin >> age;
				cout << "成绩:"; cin >> score;
				student stu;
				strcpy(stu.id, id), strcpy(stu.name, name), strcpy(stu.gender, gender), stu.age = age, stu.score = score;
				l->base[i] = stu;
				cout << "添加成功:" << endl;
				cout << "该学生的信息如下:" << endl;
				showinf(l->base[i]);
				break;
			}
	}
	writeinf(l);
}
void Delete()//删除学生
{
	SqList* l = loadinf();
	char id[100];
	cout << "学生信息如下:" << endl;
	showallinf(l);
	cout << "请输入删除学生的id" << endl; cin >> id;
	int pos;
	if ((pos = exist(l, id)) == -1) { cout << "该学生不存在,无法删除!" << endl; }
	else
	{
		for (int i = pos + 1; i < l->length; i++)
			l->base[i - 1] = l->base[i];
		cout << "删除成功" << endl;
		l->length--;
		showallinf(l);
	}
	writeinf(l);
}

void save(SqList* l)
{
	FILE* p = fopen("result.txt", "w");
	char buf[1000];

	for (int i = 0; i < l->length; i++)
	{
		sprintf(buf, "%s %s %s %d %d\n", l->base[i].id, l->base[i].name, l->base[i].gender, l->base[i].age, l->base[i].score);
		fputs(buf, p);
	}
	fclose(p);
	cout << "查询结果导出成功" << endl;
}

void search(int ope) //0 - 模糊  1 - 精确
{
	SqList l;
	IniList(&l);
	FILE* p = fopen("score.txt", "r");
	if (!p) exit(0);
	char buf[1000];
	char tar[100];

	cout << "请输入姓名关键词" << endl;
	cin >> tar;
	while (fgets(buf, 1000, p))
	{
		char id[100], name[100], gender[100];
		int age;
		int score;
		sscanf(buf, "%s %s %s %d %d", id, name, gender, &age, &score);

		if (!ope && strstr(name, tar) != NULL || ope && strcmp(name, tar) == 0)
		{
			student stu;
			strcpy(stu.id, id), strcpy(stu.name, name), strcpy(stu.gender, gender), stu.age = age, stu.score = score;
			l.base[l.length++] = stu;
			if (l.length == l.listsize) l.base = (student*)realloc(l.base, (l.listsize + LISTINCREMENT) * sizeof(student));
		}
	}
	if (!l.length) { cout << "查询结果为空" << endl; }
	else
	{
		cout << "查询结果如下:" << endl;
		showallinf(&l);
	}
	cout << "是否保存为excel文件?(是/否)";
	string s; cin >> s;
	if (s == "是") save(&l);

}

bool menu()
{
	cout << "***学生成绩管理系统***" << endl;
	cout << "0、退出系统" << endl;
	cout << "1、添加学生信息" << endl;
	cout << "2、编辑学生信息" << endl;
	cout << "3、删除学生信息" << endl;
	cout << "4、精确查询学生信息" << endl;
	cout << "5、模糊查询学生信息" << endl;
	cout << "6、显示全部学生" << endl;
	cout << "请输入操作序号" << endl;
	int ope; cin >> ope;
	switch (ope)
	{
	case 0:
		return false;
	case 1:
		Add();
		break;
	case 2:
		edit();
		break;
	case 3:
		Delete();
		break;
	case 4:
		search(1);
		break;
	case 5:
		search(0);
		break;
	case 6:
		SqList * l = loadinf();
		showallinf(l);
		break;
	}
	system("pause");
	system("cls");
	return true;
}

int main()
{
	while (menu());
}