大一上学期C语言学生奖学金管理系统

大一上学期C语言课程设计

总结:程序中还存在很多的bug,也有很多功能没完善,但这是人生中第一个课设,故发表出来纪念一下。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct student) 

struct student
{
   char num[15];
   char name[10];
   int clas;
   int Math;
   int English;
   int Physic;
   int sum;
   int rank;
   struct student *next;	
} ;

void create(void)  //录入信息 
{
	struct student *head,*p1,*p2;
	FILE *fp;
	int n=0; 
	head=NULL;
	p1=p2=(struct student *)malloc(LEN);
	printf("please enter the schoolnumber(such as 5116),name,class(such as 11),and the score of Math,English and Physic:\n");
	scanf("%s",p1->num);
	getchar();
	while(p1->num[0]!='@')  //录入信息以@结束 
	{
		scanf("%s%d%d%d%d",p1->name,&p1->clas,&p1->Math,&p1->English,&p1->Physic);  //出错,因没加&!!! 
		p1->rank=0;
		p1->sum=0;   
		n++;
		if(n==1) head=p1;
		else p2->next=p1;
		p2=p1;
		p1=(struct student *)malloc(LEN);
		scanf("%s",p1->num);
		getchar();
	}
	p2->next=NULL;
	free(p1);
	printf("record is OK!\n");
	if((fp=fopen("student.txt","w"))==NULL)
	{
		printf("cannot open file!\n");
		//fclose(fp);
		exit(0);
	}
	p1=head;
	while(p1!=NULL)  //数据存放再文件中 
	{ 
		fprintf(fp,"%s %s %d %d %d %d %d %d\n",p1->num,p1->name,p1->clas,p1->Math,p1->English,p1->Physic,p1->sum,p1->rank);
		p1=p1->next; 
	} 
	fclose(fp);	  	
}

/***********
void record(struct student *p1)//讲链表中的数据放在文件student.txt中 
{
  	FILE *fp;
  	struct student *p;
  	//char filename[10];
  	p=p1;
  	//printf("...........\n");
  	//scanf("%s",filename);
	if((fp=fopen("student.txt","w"))==NULL)
	{
		printf("cannot open file!\n");
		//fclose(fp);
		exit(0);
	}
	while(p!=NULL)  //数据存放再文件中 
	{ 
		fprintf(fp,"%s %s %d %d %d %d %d %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
		p=p->next; 
	} 
	free(p);  //释放内存                                                               //     出错!     因把p1内存释放导致出错 
	fclose(fp);	  	
	
}
*********/
void print(struct student *head)  //输出全部数据函数 
{
	struct student *p;
	p=head; 
	printf("---------------------------STUDENT--------------------------------------\n");
	printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    printf("------------------------------------------------------------------------\n");	
	while(p!=NULL)
	{
		printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
		p=p->next;
	}
}

void searchbyscore(struct student *head)  //按成绩段查询信息 
{
	struct student *p;
	char s[10];
	int a,b,k;
	p=head;
	printf("please enter the range of score,such as 60--70:\n");
	scanf("%d--%d",&a,&b);
	if(a>b||a==b)
	printf("Your enter is wrong !\n");
	else
	{
		printf("please choose the course ,Math,English or Physic:\n");
		scanf("%s",s);  //存放要查询的成绩
		if(strcmp(s,"Math")==0)
		{
			k=0;
			while(p!=NULL)
			{
				if(p->Math>=a&&p->Math<=b)
				{
					k++;
					if(k==1)
					{
						printf("---------------------------STUDENT--------------------------------------\n");
						printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    					printf("------------------------------------------------------------------------\n");
    				}
					printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
				}
				p=p->next;
			}
			if(k==0)
			printf("not found the statistic !\n");
		} 
		else if(strcmp(s,"English")==0)
		{
			
			k=0;
			while(p!=NULL)
			{
				if(p->English>=a&&p->English<=b)
				{
					k++;
					if(k==1)
					{
						printf("---------------------------STUDENT--------------------------------------\n");
						printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    					printf("------------------------------------------------------------------------\n");
    				}
					printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
				 }
				p=p->next;
			}
			if(k==0)
			printf("not found the statistic !\n");
		} 		
		else if(strcmp(s,"Physic")==0)
		{
			
			k=0;
			while(p!=NULL)
			{
				if(p->Physic>=a&&p->Physic<=b)
				{
					k++;
					if(k==1)
					{
						printf("---------------------------STUDENT--------------------------------------\n");
						printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    					printf("------------------------------------------------------------------------\n");
    				}
					printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
				}
				p=p->next;
			}
			if(k==0)
			printf("not found the statistic !\n");
		}  
		else
		printf("not found this course !\n");
	}
}  
void searchbyrank(struct student *head)  //按名次查询 
{
	struct student *p;
	int n,k;
	k=0;
	p=head;
	if(p->rank==0)
	printf("the statistic has not been sorted !You can choose 7 to rank.\n"); //输出还未排序 
	else
	{
		printf("please enter the rank:\n");  //输入排名 
		scanf("%d",&n);
		while(p!=NULL)
		{
			if(p->rank==n)
			{
				printf("---------------------------STUDENT--------------------------------------\n");
				printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    			printf("------------------------------------------------------------------------\n");
    			printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
				k=1;
				break;
			}
			p=p->next;
		}
		if(k==0)
		printf("not this rank !\n");  //没有此排名 
	}
}

void searchbyclass(struct student *head)  //按班级查找前3名 
{
	struct student *p1,*p2;
	int k,n,i;
	k=0;
	p1=p2=head;
	printf("please enter the class :\n");  //输入班级 
	scanf("%d",&n);
	while(p1!=NULL)
	{
		if(p1->clas==n)
		{
			k++;
			i=0;
			p2=head;
			if(k==1)
			{
				printf("the top 3 in this is :\n");
				printf("---------------------------STUDENT--------------------------------------\n");
				printf("|num           |name   |clas  |Math  |English  |Physic     |sum  |rank  |\n");
    			printf("------------------------------------------------------------------------\n");
			}
			while(p2!=NULL)
			{
				if(p2->clas==n)
				{
					if(p1->sum<p2->sum)  //统计比p1总分多的人数量 
					i++;
				}
				p2=p2->next;
			}
		if(i<3)
		printf(" %-15s%-10s%-7d%-7d%-10d%-10d%-7d%-5d\n",p1->num,p1->name,p1->clas,p1->Math,p1->English,p1->Physic,p1->sum,p1->rank);
		} 
		p1=p1->next;
	}
	if(k==0)
	printf("the class is not found !\n"); //没找到 
} 
 
void rank(struct student *head)   //计算排名 
{
	struct student *p1,*p2,*p;
	FILE *fp; 
	p1=p=head;
	int i;
	while(p1!=NULL)
	{
		i=0;
		p2=head;
		while(p2!=NULL)
		{
			if(p1->sum<p2->sum)
			i++;
			p2=p2->next;
		}
		p1->rank=i+1;
		p1=p1->next;
	}
	printf("rank record OK !\n"); 
	if((fp=fopen("student.txt","w"))==NULL)  //将数据存放在文件student.txt 
	{
		printf("cannot open file!\n");
		exit(0);
	}
	while(p!=NULL)  //数据存放再文件中 
	{ 
		fprintf(fp,"%s %s %d %d %d %d %d %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
		p=p->next; 
	}                                                                 
	fclose(fp);	  	
	
}
void sortmenu(void)
{
	printf("**********************************************\n");
	printf("1---------------------Sort by rank\n");
	printf("2---------------------Sort by classnumber\n");
	printf("3---------------------Exit\n");
	printf("**********************************************\n");		
}
void sortbyrank(struct student *head)  //按排名排序 
{
	struct student *p,*t;
	char str1[10],str2[15];
	int t1,t2,t3,t4,t5,t6;
	p=head;
	if(p->sum==0)
	printf("no sum yet,please choose 4 first!\n");
	else
	{
		while(p!=NULL)
		{
			t=p->next;
			while(t!=NULL)
			{
				if(p->rank>t->rank)  //交换数据 
				{
					strcpy(str1,p->name);
					strcpy(str2,p->num);
					strcpy(p->name,t->name);
					strcpy(p->num,t->num);
					strcpy(t->name,str1);
					strcpy(t->num,str2);
					t1=p->Math,t2=p->English,t3=p->Physic,t4=p->clas,t5=p->sum,t6=p->rank;
					p->Math=t->Math,p->English=t->English,p->Physic=t->Physic,p->clas=t->clas,p->sum=t->sum,p->rank=t->rank;
					t->Math=t1,t->English=t2,t->Physic=t3,t->clas=t4,t->sum=t5,t->rank=t6;
					
				}
				t=t->next;
			}
			p=p->next;
		}
		printf("sort by rank is OK !\n");
	}
			
}
void sortbynum(struct student *head)  //以学号从小到大排序 
{
	struct student *p,*t;
	char str1[10],str2[15];
	int t1,t2,t3,t4,t5,t6;
	p=head;
	while(p!=NULL)
	{
		t=p->next;
		while(t!=NULL)   
		{
			if(strcmp(p->num,t->num)>0)
			{
				strcpy(str1,p->name);
				strcpy(str2,p->num);
				strcpy(p->name,t->name);
				strcpy(p->num,t->num);
				strcpy(t->name,str1);
				strcpy(t->num,str2);
				t1=p->Math,t2=p->English,t3=p->Physic,t4=p->clas,t5=p->sum,t6=p->rank;
				p->Math=t->Math,p->English=t->English,p->Physic=t->Physic,p->clas=t->clas,p->sum=t->sum,p->rank=t->rank;
				t->Math=t1,t->English=t2,t->Physic=t3,t->clas=t4,t->sum=t5,t->rank=t6;	
			}
			t=t->next;
		}
		p=p->next;
	}
	printf("sort by classnumber is OK !\n");	
}
void sort(struct student *head)
{
	int n;
	sortmenu();
	do
	{
		printf("please choose the option:\n");
		scanf("%d",&n);
		switch(n)
		{
			case 1:sortbyrank(head);break;
			case 2:sortbynum(head);break;
			case 3:break;
			default:printf("input error !\n");
		}
	} while (n!=3);
} 

void score(struct student *head)  //计算总分,并追加到student.txt中 
{
	FILE *fp;
	struct student *p;
	p=head;
	while(p!=NULL)
	{
		p->sum=p->English+p->Math+p->Physic; 
		p=p->next;
	}
	printf("statistic students sum OK !\n");   //提示信息 
	
}  

void add(struct student *head)  //增加数据,并保存在student2.txt中 
{
	struct student *p,*p1,*p2;
	FILE *fp;
	int n;
	p=head;
	while(p->next!=NULL)  //链表尾部 
	p=p->next;
	printf("please enter the schoolnumber(such as 5116),name,class(such as 11),and the score of Math,English and Physic:\n");
	p1=p2=(struct student *)malloc(LEN);
	scanf("%s",p1->num);
	getchar();
	n=0;
	while(p1->num[0]!='@')
	{
		scanf("%s%d%d%d%d",p1->name,&p1->clas,&p1->Math,&p1->English,&p1->Physic);
		getchar();
		p1->sum=0,p1->rank=0;
		n++;
		if(n==1)  p->next=p1;
		else p2->next=p1;
		p2=p1;
		p1=(struct student *)malloc(LEN);
		scanf("%s",p1->num);
		getchar();
	} 
	p2->next=NULL;
	printf("record OK !\n");
	p=head;
	if((fp=fopen("student2.txt","w"))==NULL)
	{
		printf("cannot open file!\n");
		exit(0);
	}
	while(p!=NULL)  //数据存放再文件中 
	{ 
		fprintf(fp,"%s %s %d %d %d %d %d %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
		p=p->next; 
	} 
	fclose(fp);	  	
} 

void delet(struct student *head)  //删除数据函数 
{
	struct student *p,*t;
	FILE *fp;
	int k=0;
	char namee[10];  //存放删除的名字 
	p=head;
	printf("please input the name you want to delete :\n");   //输出要删除的学生姓名
	scanf("%s",namee);
	if(strcmp(p->name,namee)==0)
	{
		head=p->next;
		k=1;
	}
	else
	{
		while(p!=NULL)
		{
			t=p;
			p=p->next;
			if(strcmp(p->name,namee)==0)
			{
				k=1;
				t->next=p->next;
				break;
			}
		}
	}
	if(k==0)
	printf("cannot find the name !\n");
	else
	{
		p=head;
		if((fp=fopen("student2.txt","w"))==NULL)
		{
			printf("cannot open file!\n");
			exit(0);
	 	}
		while(p!=NULL)  //数据存放再文件中 
		{	 
			fprintf(fp,"%s %s %d %d %d %d %d %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
			p=p->next; 
		} 
		fclose(fp);	  	
	}	
}
void mainmenu()  //主菜单 
{
	printf("**********************************************\n");
	printf("1---------------------Record\n");
	printf("2---------------------Show all record\n");
	printf("3---------------------Search\n");
	printf("4---------------------Statistic\n");
	printf("5---------------------Delete\n");
	printf("6---------------------Add\n");
	printf("7---------------------Sort\n");
	printf("8---------------------Save the file\n");
	printf("9---------------------Copy the file\n");
	printf("10--------------------Exit\n");
	printf("**********************************************\n");
	
			
}
void searchmenu(void)
{
	printf("**********************************************\n");
	printf("1--------------------Search by rank\n");
	printf("2--------------------Search the top 3 in the class\n");
	printf("3--------------------Search by score\n");
	printf("4--------------------Exit\n");
	printf("**********************************************\n");	
}
void search(struct student *head)
{
	int n;
	do
	{
		searchmenu();
		printf("please choose the option :\n");
		scanf("%d",&n);
		switch(n)
		{
			case 1:searchbyrank(head);break;
			case 2:searchbyclass(head);break;
			case 3:searchbyscore(head);break;
			case 4:break;
			default:printf("input eaaor !\n");
		}
	} while(n!=4);
}

void statisticmenu(void) //统计用的菜单
{
	printf("**********************************************\n");
	printf("1--------------------Statistic student sum\n");
	printf("2--------------------Statistic class sum\n");
	printf("3--------------------Exit\n");
	printf("**********************************************\n");	
}


void statisticclass(struct student *head)  //按班级计算总分 
{
	struct student *p;
	int clas[10]={0};
	long s[10]={0};
	int i,j,t;
	i=0;
	p=head;
	while(p!=NULL)  //统计总共有几个班 
	{
		t=1;
		for(j=0;j<i;j++)
		{
			if(clas[j]==p->clas)
			{
				t=0;
				break;
			}	
		}		
		if(t==1)
		clas[i++]=p->clas;	
		p=p->next; 
	}
	p=head;
	while(p!=NULL)
	{
		for(j=0;j<i;j++)
		if(clas[j]==p->clas)
		s[j]+=p->sum;
		p=p->next;
	} 
	printf(" class     sum\n");
	for(j=0;j<i;j++)
	printf(" %-10d%d\n",clas[j],s[j]);
	
} 
void statistic(struct student *head)
{
	int n;
	struct student *p;
	p=head;
	do
	{
		statisticmenu();	
		printf("please choose the option :\n");
		scanf("%d",&n);
		switch(n)
		{
			case 1:score(head);rank(head);break;
			case 2:statisticclass(head);break;
			case 3:break;
			default:printf("input error !\n");	
		}
	} while(n!=3);		
}




 
void copyfile(void)  //复制文件函数 
{
	FILE *in,*out;
	char ch,infile[10],outfile[10];
	struct student *p,stu;
	p=&stu;
	printf("....\n");
	scanf("%s",infile);
	getchar();
	printf("..\n");
	scanf("%s",outfile);
	getchar();
	if((in=fopen(infile,"r"))==NULL)
	{
		printf("cannot open file !\n");
		exit(0);
	}	
	if((out=fopen(outfile,"w"))==NULL)
	{
		printf("cannot open file !\n");
		exit(0);
	}
	while(!feof(in))
	{
		fscanf(in,"%s%s%d%d%d%d%d%d",p->num,p->name,&p->clas,&p->Math,&p->English,&p->Physic,&p->sum,&p->rank);
		fprintf(out,"%s  %s  %d  %d  %d  %d  %d  %d\n",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);  //?????为什么这样会把最后一行数据重复输出 
		//fread(&stu,LEN,1,in);
		//fwrite(&stu,LEN,1,out);
	}
	fclose(in);
	fclose(out);
}
void savefile(struct student *head)
{
	FILE *fp;
	struct student *p;
	char filename[10];
	p=head;
	printf("please input the file name you want to copy:\n");
	scanf("%s",filename);
	if((fp=fopen("filename","w"))==NULL)
	{
		printf("cannot open file !\n");
		exit(0);
	}
	while(!feof(fp))
	{
		fprintf(fp,"%s%s%d%d%d%d%d%d",p->num,p->name,p->clas,p->Math,p->English,p->Physic,p->sum,p->rank);
	}
	fclose(fp);
}

struct student *filenode()
{
	FILE *fp;
	int n=0;
	struct student *head,*p1,*p2;
	if((fp=fopen("student.txt","r"))==NULL)
	{
		printf("cannot open file!\n");
		exit(0);
	}
	head=NULL;
	p1=p2=(struct student*)malloc(LEN);
	fscanf(fp,"%s%s%d%d%d%d%d%d",p1->num,p1->name,&p1->clas,&p1->Math,&p1->English,&p1->Physic,&p1->sum,&p1->rank);
	while(!feof(fp))
	{
		n++;
		if(n==1) head=p1;
		else p2->next=p1;
		p2=p1;
		p1=(struct student *)malloc(LEN);
		fscanf(fp,"%s%s%d%d%d%d%d%d",p1->num,p1->name,&p1->clas,&p1->Math,&p1->English,&p1->Physic,&p1->sum,&p1->rank); 
	}
	p2->next=NULL;
	fclose(fp);
	return (head);
}


void studentmenu(void)
{
	printf("**********************************************\n");
	printf("1--------------------Search\n");
	printf("2--------------------Show all record\n");
	printf("3--------------------Exit\n");
	printf("**********************************************\n");	
} 




int main()
{
	struct student *p;
	char code[10];
	int n,a,b;
	p=filenode();
//	p=create();
	//record(p);
	//score(p); 
	//rank(p); 
	//searchbyscore(p); 
	//searchbyrank(p);
	//searchbyclass(p);
	//add(p);
	//delet(p);
	//sort(p); 
	//sortbyrank(p);
//	statistic(p);
	//search(p);
	//copyfile();
   // print(p);
    
    
    
    
    
    printf("please choose 1 or 2 ,1 for student and 2 for teacher :\n");
    scanf("%d",&n);
    if(n==1)
    {
    	do
    	{
    		studentmenu();
    		printf("please choose the option :\n");
    		scanf("%d",&a);
			switch(a)
			{
			 	case 1:search(p);break;
			 	case 2:print(p);break;
			 	case 3:break;
			 	default:printf("input error!\n");
			}
		}while(a!=3);
    	
	}
	else if(n==2)
	{
		printf("please enter the password:\n ");
		scanf("%s",code);
		if(strcmp(code,"123456")==0)  //判断密码是否正确! 
		{
			do
			{
				mainmenu();
				printf("please choose the option:\n");
    			scanf("%d",&b);
				switch(b)
				{
					case 1:create();p=filenode();break; 
					case 2:print(p);break;
					case 3:search(p);break;
					case 4:statistic(p);break;
					case 5:delet(p);break;
					case 6:add(p);break;
					case 7:sort(p);break;
					case 8:savefile(p);break;
					case 9:copyfile();break;
					case 10:break;
					default:printf("input error!\n"); 
				}
			} while(b!=10);
		}
		else
		printf("Your password is wrong !\n");
	}
	else
	printf("date error!\n");
	return 0;
}

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值