学生期末考试数据管理系统

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define len sizeof(struct Student)
typedef struct Student
{
	char a[20];
	int  score_eng;
	int score_pc;
	int score_math;
	int score;
	struct Student * next;
}Fun;
//1.包含链表的创建、插入、删除、查询、更新、排序、合并、销毁等常规操作,要求2.包含文件的读写操作,
//能够在关闭程序之前自动保存当前链表中的数据到文件,并在下次启动程序之后自动从文件中读取数据来创建链表。
Fun *firstopen();
Fun *open();
Fun *creat();
Fun *insert(Fun *head);
Fun *del(Fun *head);
Fun *seek(Fun *head);
Fun *sort(Fun *head);
void boom(Fun *head);
void print(Fun *head);
int  n = 0;	FILE *fp;
char name[100];
int main()
{
	int input;
	Fun *head;
	head = firstopen();
	while (1)
	{
		printf("***************************************************************************************************************\n");
		printf("			NO.1open a data打开\n			NO.2--creat a file创建\n			NO.3--insert some data插入\n			NO.4--delete some data删除\n");
		printf("			NO.5seek some data查询\n			NO.6 updata some data更新\n			No.7sort the data排序\n			No.8combine the data合并\n");
		printf("			NO.8 destroy the data销毁\n			NO.9 print the data打印数据库\n			NO.0 close the exe关闭程序\n");
		printf("						Copyright   2019-2020   Melrose.  All Rights Reserved.\n");
		printf("***************************************************************************************************************\n");
		printf("Please input a number.\n");
		scanf("%d", &input);
		switch (input)
		{
		case(1) : {head = open(); break; }
		case(2) : {head = creat(); break; }
		case(3) : {head = insert(head); break; }
		case(4) : {head = del(head); break; }
		case(5) : {head = seek(head); break; }
		case(6) : {fclose(fp); printf("关闭数据库成功!\n"); break; }
		case(7) : {head = sort(head); break; }
		case(8) : {boom(head); break; }
		case(9) : {print(head); break; }
		case(0) : {exit(0); }
		}
	}
}

Fun *firstopen()
{
	Fun *head = NULL;
	Fun *p1, *p2;
	fp = fopen("a.data", "r+");
	strcpy(name, "a.data");
	if (fp == NULL)
	{
		printf("该数据库不存在!您可重新选择输入编号");
		return NULL;
	}
	p1 = p2 = (Fun*)malloc(len);
	while (fscanf(fp, "%s %d %d %d %d\n", &p1->a, &p1->score_eng, &p1->score_math, &p1->score_pc, &p1->score) != EOF)
	{
		n++;
		if (head == NULL) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (Fun*)malloc(len);
	}
	p2->next = NULL;
	printf("数据库%s打开成功,您可继续其他操作\n", name);
	return head;
}

Fun *open()
{
	Fun *head = NULL;
	Fun *p1, *p2;
	printf("请输入要打开的数据库名称");
	scanf("%s", name);
	fp = fopen(name, "r+");
	if (fp == NULL)
	{
		printf("该数据库不存在!您可重新选择输入编号");
		return NULL;
	}
	p1 = p2 = (Fun*)malloc(len);
	while (fscanf(fp,"%s %d %d %d %d\n", &p1->a, &p1->score_eng, &p1->score_math, &p1->score_pc, &p1->score)!=EOF)
	{
		n++;
		if (head == NULL) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (Fun*)malloc(len);
	}
	p2->next = NULL;
	printf("数据库%s打开成功,您可继续其他操作\n",name);
	return head;
}

Fun *creat()
{
	char name[100];
	Fun *head = NULL;
	Fun *p1, *p2, *p;
	p1 = p2 = (Fun*)malloc(len);
	printf("下面开始建立新的数据库,请输入新的数据库名称\n");
	scanf("%s", name);
	fp = fopen(name, "w");
	if (fp == NULL)
	{
		printf("数据库建立失败!\n");
		return NULL;
	}
	printf("数据库建立成功!下面请输入学生姓名 以#结束\n");
	p1 = (Fun*)malloc(len);
	scanf("%s", &p1->a);
	while (strcmp(p1->a, "#") != 0)
	{
		n++;
		printf("请依次输入学生的英语成绩 数学成绩 信息成绩\n");
		scanf("%d%d%d", &p1->score_eng, &p1->score_math, &p1->score_pc);
		p1->score = p1->score_eng + p1->score_math + p1->score_pc;
		if (head == NULL) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (Fun*)malloc(len);
		printf("请输入学生姓名\n");
		scanf("%s", p1->a);
	}
	p2->next = NULL;
	p = head;
	while (p)
	{
		fprintf(fp, "%s %d %d %d %d\n", p->a, p->score_eng, p->score_math, p->score_pc, p->score);
		p = p->next;
	}
	return head;
}

Fun *insert(Fun *head)
{
	Fun *p0, *p1=NULL;
	p1 = head; int m;
	if (fp == NULL)
	{
		printf("尚未打开一个数据库!请重新打开\n");
		return NULL;
	}
	printf("请输入要插入学生的姓名 英语成绩 数学成绩 信息成绩");	
	p0 = (Fun*)malloc(len);
	scanf("%s %d %d %d", &p0->a, &p0->score_eng, &p0->score_math, &p0->score_pc);
	p0->score = p0->score_eng + p0->score_math + p0->score_pc;
	printf("请选择从表头或表尾插入\n输入编号1表示表头;0表示表尾"); scanf("%d", &m);
	if (head == NULL)
	{
		head = p0; p0->next = NULL;
	}
	else if (m)
	{
		{
			p1 = head;
			head = p0;
			p0->next = p1;
		}
		n++;
	}
	else
	{
		p1 = head;
		while (p1->next)
			p1 = p1->next;
		p1->next = p0;
		p1 = p0; p1->next = NULL; n++;
	}
	if (m){
		rewind(fp);
		fprintf(fp, "%s %d %d %d %d\n", p0->a, p0->score_eng, p0->score_math, p0->score_pc, p0->score);
	}
	else{
		fseek(fp, 0L, 2);
		fprintf(fp, "%s %d %d %d %d\n", p0->a, p0->score_eng, p0->score_math, p0->score_pc, p0->score);
	}
	fclose(fp);
	fp = fopen(name, "r+");
	printf("插入数据成功!\n\n");
	return head;
}

Fun *del(Fun *head)
{
	Fun *p1=head, *p2 = NULL,*num,*p=head;
	printf("请输入要删除数据的名字");
	num = (Fun*)malloc(len);
	scanf("%s", &num->a);
	while (strcmp(p1->a, num->a) != 0 && p1->next != NULL)
	{
		p2 = p1; p1 = p1->next;
	}
	if (strcmp(p1->a, num->a) == 0)
	{
		if (p1 == head) { head = p1->next; free(p1); }
		else { p2->next = p1->next; free(p1); }
	}
	fclose(fp);
	fopen(name, "w+");
	while (p)
	{
		fprintf(fp, "%s %d %d %d %d\n", p->a, p->score_eng, p->score_math, p->score_pc, p->score);
		p = p->next;
	}
	printf("删除数据%s成功!\n\n", num);
	fclose(fp);
	fp = fopen(name, "r+");
	return head;
}

Fun *seek(Fun *head)
{
	Fun *p1, *p2 = NULL, *num;
	p1 = head;
	printf("请输入要查询数据的名字");
	num = (Fun*)malloc(len);
	scanf("%s", &num->a);
	while (strcmp(p1->a, num->a) != 0 && p1->next != NULL)
	{
		p2 = p1; p1 = p1->next;
	}
	if (strcmp(p1->a, num->a) == 0)
	{
		printf("学生姓名 英语成绩 数学成绩 信息成绩      总分\n");
		printf("%8s %8d %8d %8d %8d\n", p1->a, p1->score_eng, p1->score_math, p1->score_pc, p1->score);
	}
	else if (!p1->next) printf("查无此人!\n");
	return head;
}

Fun *sort(Fun *head)
{
	Fun *p = NULL, *q=head; int m; char t[20]; int t1, t2, t3, t4;
	printf("请输入要排序的项目\n姓名--1 英语成绩--2 数学成绩--3 信息成绩--4 总分--5\n");
	scanf("%d", &m);
	switch (m)
	{
	case(1) : {while (p != q){
				  while (q->next != p) {
					  if (strcmp(q->a,q->next->a)<0){
						  strcpy(t, q->a); strcpy(q->a, q->next->a); strcpy(q->next->a, t);
						  t1 = q->score; q->score = q->next->score; q->next->score = t1;
						  t2 = q->score_eng; q->score_eng = q->next->score_eng; q->next->score_eng = t2;
						  t3 = q->score_math; q->score_math = q->next->score_math; q->next->score_math = t3;
						  t4 = q->score_pc; q->score_pc = q->next->score_pc; q->next->score_pc = t4; 
					  }
					  q = q->next;
				  }p = q; q = head;
	}break;
	}
	case(2) : {while (p != q){
				  while (q->next != p) {
					  if (q->score_eng < q->next->score_eng){
						  strcpy(t, q->a); strcpy(q->a, q->next->a); strcpy(q->next->a, t);
						  t1 = q->score; q->score = q->next->score; q->next->score = t1;
						  t2 = q->score_eng; q->score_eng = q->next->score_eng; q->next->score_eng = t2;
						  t3 = q->score_math; q->score_math = q->next->score_math; q->next->score_math = t3;
						  t4 = q->score_pc; q->score_pc = q->next->score_pc; q->next->score_pc = t4; 
					  }
					  q = q->next;
				  }p = q; q = head;
		}break;
	}
	case(3) : {while (p != q){
				  while (q->next != p) {
					  if (q->score_math < q->next->score_math){
						  strcpy(t, q->a); strcpy(q->a, q->next->a); strcpy(q->next->a, t);
						  t1 = q->score; q->score = q->next->score; q->next->score = t1;
						  t2 = q->score_eng; q->score_eng = q->next->score_eng; q->next->score_eng = t2;
						  t3 = q->score_math; q->score_math = q->next->score_math; q->next->score_math = t3;
						  t4 = q->score_pc; q->score_pc = q->next->score_pc; q->next->score_pc = t4; 
					  }
					  q = q->next;
				  }p = q; q = head;
		}break;
	}
	case(4) : {while (p != q){
				  while (q->next != p) {
					  if (q->score_pc < q->next->score_pc){
						  strcpy(t, q->a); strcpy(q->a, q->next->a); strcpy(q->next->a, t);
						  t1 = q->score; q->score = q->next->score; q->next->score = t1;
						  t2 = q->score_eng; q->score_eng = q->next->score_eng; q->next->score_eng = t2;
						  t3 = q->score_math; q->score_math = q->next->score_math; q->next->score_math = t3;
						  t4 = q->score_pc; q->score_pc = q->next->score_pc; q->next->score_pc = t4;
					  }
					  q = q->next;
				  }p = q; q = head;
		} break;
	}
	case(5) : {while (p != q){
				  while (q->next != p) {
					  if (q->score < q->next->score){
						  strcpy(t, q->a); strcpy(q->a, q->next->a); strcpy(q->next->a, t);
						  t1 = q->score;q->score = q->next->score; q->next->score = t1;
						  t2 = q->score_eng; q->score_eng = q->next->score_eng; q->next->score_eng = t2;
						  t3 = q->score_math; q->score_math = q->next->score_math; q->next->score_math = t3;
						  t4 = q->score_pc; q->score_pc = q->next->score_pc; q->next->score_pc = t4; 
					  }
					  q = q->next;
				  }
				  p = q;
				  q = head;
			}break;
		}
	}
	fclose(fp);
	fopen(name, "w+");
	p = head;
	while (p)
	{
		fprintf(fp, "%s %d %d %d %d\n", p->a, p->score_eng, p->score_math, p->score_pc, p->score);
		p = p->next;
	}
	printf("排序完成!\n");
	return head;
}

void boom(Fun *head)
{
	Fun *p,*q;
	fclose(fp);
	if (!remove(name)) printf("文件销毁成功!\n");
	else  printf("文件销毁失败!\n");
	p = head;//销毁链表
	while (p)
	{
		q = p->next;
		free(p);
		p = q;
	}
	head->next = NULL;
}

void print(Fun *head)
{
	Fun *p;
	printf("  学生姓名  英语成绩  数学成绩   信息成绩    总分\n");
	for (p = head; p != NULL; p = p->next)
		printf("%8s %8d %9d %9d %10d\n", p->a, p->score_eng, p->score_math, p->score_pc,p->score);
}

猜你喜欢

转载自blog.csdn.net/youtiankeng/article/details/87704862