c语言文件管理系统

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *fp;
struct xsonde
{
	int xh;
	char xm[15];
	int gs;
	int yy;
	int wl;
	double pj;
	struct xsonde *next;
};
void *bc(struct xsonde *head)//保存在文件夹中的函数
{
	struct xsonde *p;
	p=head;
	if((fp=fopen("xxxxxxxxxxxx王大锤.txt","w"))==NULL)
	{
		printf("打不开此文件\n");
		exit(0);
	}
	while(p!=NULL){
	fprintf(fp,"学号:%d姓名:%s高数成绩:%d英语成绩:%d物理成绩:%d\n",p->xh,p->xm,p->gs,p->yy,p->wl);
	p=p->next;
	}
	printf("数据已写入文件夹\n\n\n");
	fclose(fp);
	return 0;
}
void *ck(struct xsonde *head)//查看保存在文件夹中的函数
{
	int i=0;
	char a;
	struct xsonde *p;
	p=head;
	if((fp=fopen("xxxxxxxxxxxx王大锤","r"))==NULL)
	{
		printf("打不开此文件\n");
		exit(0);
	}
	printf("\n所有人成绩为:\n");
	a=getc(fp);
	while(!feof(fp))
	{ 
	putchar(a);
	a=getc(fp);
	} 

	fclose(fp);
	return 0;
}
struct xsonde *lr()//录入成绩函数
{
	int i=0,n=0;
	struct xsonde *p1,*p2,*head;
	p1=p2=(struct xsonde *)malloc(sizeof(struct xsonde));
	printf("第%d名学生学号:",i+1);
	scanf("%d",&p1->xh);
	printf("姓名:");
	scanf("%s",p1->xm);
	printf("高数成绩:");
	scanf("%d",&p1->gs);
	printf("英语成绩:");
	scanf("%d",&p1->yy);
	printf("物理成绩:");
	scanf("%d",&p1->wl);
	head=NULL;
	while(n>=0)
	{
		n++;
		i++;
		if(i==1)head=p1;
		else p2->next=p1;
		p2=p1;
		p1=(struct xsonde *)malloc(sizeof(struct xsonde));
		printf("第%d名学生学号:",i+1);
		scanf("%d",&p1->xh);
		if(p1->xh==0)
		{
			printf("录入成绩完成\n");
			break;
		}
		printf("姓名:");
		scanf("%s",p1->xm);
		printf("高数成绩:");
		scanf("%d",&p1->gs);
		printf("英语成绩:");
		scanf("%d",&p1->yy);
		printf("物理成绩:");
		scanf("%d",&p1->wl);
	}
	p2->next=NULL;
	return (head);
}
struct xsonde *xgxm(struct xsonde *head)//修改学号和姓名函数
{
	int i,j,a;
	char b[15];
	struct xsonde *p;
	p=head;
	printf("请选择:\n1、修改学号\n2、修改姓名\n");
	scanf("%d",&j);
	printf("输入需要修改学生的原学号:");
	scanf("%d",&i);
	while(p->xh!=i)
	{
		p=p->next;
		if(p==NULL)
		{
			printf("无此学号!\n");
			return(head);
		}
	}
	switch(j){
				case(1):{
							printf("输入修改后的新学号:");
							scanf("%d",&a);
							p->xh=a;break;
						}
				case(2):{
							printf("输入修改后的姓名:");
							scanf("%s",b);
							strcpy(p->xm,b);break;
						}
				default:printf("请选择正确的选项!");
		}	
	printf("修改完成\n");
	bc(head);
	return (head);
}
struct xsonde *xgcj(struct xsonde *head)//修改成绩函数
{
	int i,j,a;
	struct xsonde *p;
	p=head;
	printf("输入需要修改成绩学生的学号:");
	scanf("%d",&i);
	while(p->xh!=i)
		{
			p=p->next;
			if(p==NULL)
			{
				printf("无此学号!\n");
				return(head);
			}
		}
	printf("输入需要修改成绩的科目:\n1、高数\n2、英语\n3、物理\n");
	scanf("%d",&j);
	printf("输入修改后的新成绩:");
	scanf("%d",&a);
	if(j==1) p->gs =a;
	if(j==2) p->yy =a;
	if(j==3) p->wl =a;
	printf("修改完成\n");
	bc(head);
	return (head);			
}
struct xsonde *scsj(struct xsonde *head)//删除数据函数
{
	int i;	
	struct xsonde *p,*q;
	p=head;
	printf("输入要删除学生的学号:");
	scanf("%d",&i);
	while(p->xh!=i)
	{
		p=p->next;
		if(p==NULL)
			{
				printf("无此学号!\n");
				return(head);
			}
	}
	p=head;
	if(head->xh==i)
	{
		head=head->next;
		bc(head);
		return(head);
	}
	else
	{
		p=head;
		q=head;
		while((p->xh!=i)&&(p->next!=NULL))
			{
				q=p;
				p=p->next;
			}
		q->next=p->next;
	}
	free(p);
	printf("删除成功!\n");
	bc(head);
	return(head);
}
struct xsonde *jspj(struct xsonde *head)//查询平均成绩函数
{
	int sum=0,i;
	struct xsonde *p;
	p=head;
	printf("输入查询平均成绩学生的学号:");
	scanf("%d",&i);
	while(p->xh!=i)
	{
		p=p->next;
		if(p==NULL)
			{
				printf("无此学号!\n");
				return(head);
			}
	}
	sum=(p->gs+p->yy+p->wl );
	p->pj =sum/3.0;
	printf("该生平均成绩为:%4.2f",p->pj );
	printf("\n");
	return (head);
}
struct xsonde *cxcj(struct xsonde *head)//查询成绩函数
{
	int i,sum;
	struct xsonde *p;
	p=head;
	printf("输入查询成绩学生的学号:");
	scanf("%d",&i);
	while(p->xh!=i)
	{
		p=p->next;
		if(p==NULL)
			{
				printf("无此学号!\n");
				return(head);
			}
	}
	sum=(p->gs+p->yy+p->wl );
	p->pj =sum/3.0;
	printf("高数:%d\t英语%d\t物理%d\t平均成绩%4.2f",p->gs,p->yy,p->wl,p->pj );
	printf("\n");
	return (head);
}
struct xsonde *xz(struct xsonde *head)//添加同学
{
	struct xsonde *p,*q;
	p=head;
	q=(struct xsonde *)malloc(sizeof(struct xsonde));
	while(p->next!=NULL)
	{
		p=p->next;
	}
	printf("输入要新增学生的学号:");
	scanf("%d",&q->xh);
	printf("姓名:");
	scanf("%s",q->xm);
	printf("高数成绩:");
	scanf("%d",&q->gs);
	printf("英语成绩:");
	scanf("%d",&q->yy);
	printf("物理成绩:");
	scanf("%d",&q->wl);
	p->next=q;
	q->next=NULL;
	printf("添加成功!\n");
	bc(head);
	return(head);
}

int main ()//主函数
{
	struct xsonde *head;
	int i=0,a;
	printf("下面录入各学生成绩:(学号输入 0 时停止录入)\n");
	head=lr();
	bc(head);
	while(i==0)
	{
		printf("选择下方功能:\n");
		printf("1.修改或学号及姓名\n2.修改成绩\n3.删除学生数据\n4.计算平均成绩\n5.按学号查询成绩\n6.查看所有同学成绩\n7.新增同学\n8.退出\n");
		printf("选择对应序号:");
		scanf("%d",&a);	
		switch(a)
		{
			case(1):xgxm(head);break;
			case(2):xgcj(head);break;
			case(3):scsj(head);break;
			case(4):jspj(head);break;
			case(5):cxcj(head);break;
			case(6):ck(head);break;
			case(7):xz(head);break;
			case(8):i++;break;
			default:printf("错误!\n");
		}
		if(i==0)printf("\n若退出,输入 1 ,若不退出,输入 0 :");
		else break;
		scanf("%d",&i);
		printf("\n\n");
	}
	printf("\n");
	return 0;
}
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值