用链表实现学生简单的学生成绩管理系统,有添加、输出、查找、排序、删除、修改、文件保存的功能。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int N=0; //记录学生人数
struct student
{
	char num[10];
	char name[20];
	float score;
};
struct node
{
	struct student data;
	struct node *next;
};
struct node *head;


//创建链表
void create()
{
	head=(struct node *)malloc(sizeof(struct node));
	head->next=NULL;
}


//菜单
void menu()
{
	printf("\t学生成绩管理系统\n");
	printf("_________________________________");
	printf("\n\n");
	printf("######## 1、添加学生成绩 ########\n");
    printf("######## 2、显示成绩信息 ########\n");
    printf("######## 3、按照成绩排序 ########\n");
    printf("######## 4、删除学生成绩 ########\n");
	printf("######## 5、查找学生成绩 ########\n");
    printf("######## 6、修改学生信息 ########\n");
    printf("_________________________________");
	printf("\n\n");
	printf("请输入您的选择(1~6):");
}

//添加学生
void tianjia()
{
	struct node *p;
	p=(struct node *)malloc(sizeof(struct node));
	p->next=NULL;
	printf("请输入学号、姓名、成绩,用空格隔开:\n");
	scanf("%s%s%f",p->data.num,p->data.name,&p->data.score);
	p->next=head->next;
	head->next=p;
	printf("添加成功!");
	N++;
}

//输出所有信息
void xianshi()
{
	struct node *p;
	p=head->next;
	if(p==NULL)
		printf("成绩单为空!\n");
	else
	{
		printf("学号     姓名     成绩\n");
		while(p!=NULL)
		{
			printf("-----------------------\n");
			printf("%s     %-10s%.1f",p->data.num,p->data.name,p->data.score);
			printf("\n");
			p=p->next;
		}
	}
}


//按成绩排序
void paixu()
{
	struct student s;
	struct node *p1,*p2;
	p1=head->next;
	if(p1==NULL)
		printf("成绩单为空!\n");
	else
	{
		while(p1->next!=NULL)
		{
			p2=p1->next;
			while(p2!=NULL)
			{
				if(p1->data.score<p2->data.score)
				{
					s=p1->data;
					p1->data=p2->data;
					p2->data=s;
				}
				p2=p2->next;
			}
			p1=p1->next;
		}
		printf("排序成功!\n");
	}
}


//删除
void shanchu()
{
	int f=0;
	char str[10];
	struct node *p,*q;
	q=head;
	p=head->next;
	printf("请输入要删除学生的学号:");
	scanf("%s",str);
	while(p!=NULL)
	{
		if(!strcmp(p->data.num,str))
		{
			q->next=p->next;
			free(p);
			p=q->next;
			N--;
			f=1;
		}
		else
		{
			q=p;
			p=p->next;
		}
	}
	if(f==1)
		printf("删除成功!\n");
	else
		printf("不存在该学生,删除失败!\n");
}


//查找
void chazhao()
{
	int k1,k2,flag=0;
	char str[20];
	struct node *p;
	p=head->next;
	printf("请输入要查找的学号或姓名:");
	scanf("%s",str);
	printf("\n查找结果:\n");
	while(p!=NULL)
	{
		k1=strcmp(p->data.num,str);
		k2=strcmp(p->data.name,str);
		if(k1==0||k2==0)
		{
			printf("%3s      %-10s%.1f\n",p->data.num,p->data.name,p->data.score);
			flag=1;
		}
			p=p->next;
	}
	if(flag==0)
		printf("不存在该学生!\n");
}


//修改
void xiugai()
{
	struct node *p;
	char str[10];
	int k,flag=0;
	printf("请输入要修改的学生的学号:");
	scanf("%s",str);
	p=head->next;
	while(p!=NULL)
	{
		if(!strcmp(p->data.num,str))
			break;
		else
			p=p->next;
	}
	if(p!=NULL)
	{
		printf("1、学号\n");
		printf("2、姓名\n");
		printf("3、成绩\n");
		printf("\n");
		printf("请选择要修改的内容:");
		scanf("%d",&k);
		switch(k)
		{
		case 1:
			printf("请输入修改后的学号:");
			scanf("%s",p->data.num);
			printf("修改成功!\n");
			break;
		case 2:
			printf("请输入修改后的姓名:");
			scanf("%s",p->data.name);
			printf("修改成功!\n");
			break;
		case 3:
			printf("请输入修改后的成绩:");
			scanf("%f",&p->data.score);
			printf("修改成功!\n");
			break;
		default:printf("输入错误!请重新输入\n");
		}
	}
	else printf("不存在该学生!\n");
}


//读取文件
void read()
{
	int i;
	struct node *p,*r;   //r是尾节点
	FILE *fp;
	//if((fp=fopen("student2.txt","r"))==NULL) //如果文件不存在则建立文件
	//{
	//	fp=fopen("student2.txt","w");
	//}
	if((fp=fopen("student2.txt","r"))==NULL)
	{
		printf("文件无法打开!\n");
		exit(1);
	}
	r=head;
	fscanf(fp,"%d",&N);
	for(i=0;i<N;i++)
	{
		p=(struct node *)malloc(sizeof(struct node));
		fscanf(fp,"%s%s%f",p->data.num,p->data.name,&p->data.score);//将数据读取到节点的数据域中
		r->next=p;  //每读取到一个数据就将节点插入表尾
		r=p;
	}
	r->next=NULL;
	fclose(fp);
}

//写入文件
void write()
{
	struct node *p;
	FILE *fp=fopen("student2.txt","w");
	p=head->next;
	fprintf(fp,"%d",N);
	fprintf(fp,"\n");
	while(p!=NULL)
	{
		fprintf(fp,"%s     %-10s%.1f",p->data.num,p->data.name,p->data.score);
		fprintf(fp,"\n");
		p=p->next;
	}
	fclose(fp);
}

void main()
{
	int k;
	create();
	read();
	while(1)
	{
		menu();
		scanf("%d",&k);
		switch(k)
		{
		case 1:
			system("cls");
			tianjia();
			write();
			break;
		case 2:
			system("cls");
			xianshi();
			break;
		case 3:
			system("cls");
			paixu();
			write();
			break;
		case 4:
			system("cls");
			shanchu();
			write();
			break;
		case 5:
			system("cls");
			chazhao();
			break;
		case 6:
			system("cls");
			xiugai();
			write();
			break;
		default:
			system("cls");
			printf("输入错误!请重新输入。\n");
			//getchar();
		}
			getchar();
			printf("\n按回车键返回主菜单..\n");
			getchar();
		system("cls");
	}
}

文件保存如下

2
1002     李四      90.0
1001     张三      89.0

运行结果 

 运行结果

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐