C语言课程设计,学籍管理系统整理

只供参考, 带文件操作

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>

struct student
{
	char id[9];
	char name[20];
	char sex[3];
	int math;
	int langue_c;
	int English;
	double aver;
	char phone[12];
	struct student *next; 
};

int m_count=0;
char m_filename[40]={'k'};

struct student * input_list(struct student *head);
int output_list(struct student *head);
int free_all(struct student *head);
struct student * search_node_by_id(struct student *head);
int display_node(struct student *p);
int display_list(struct student *head);
int search_node_by_name(struct student *head);
int search_node(struct student *head);
struct student * delete_node(struct student *head);
int modify_node(struct student *head);
int get_average(struct student *head);
struct student * sort_list_select(struct student *head);
int output_list1(struct student *head);
int output_list(struct student *head);
struct student * output_list2();
void write_inf_file(struct student *head,char ch,char *str);

int main()
{
	int select=-1,t;
	struct student *head=NULL,*p=NULL;
	while(select!=0)
	{
		printf("\t\t\t\t   ************************************\n");
		printf("\t\t\t\t   **        ================        **\n");
		printf("\t\t\t\t   **        学生信息管理系统        **\n");
		printf("\t\t\t\t   **        1.  录入信息            **\n");
		printf("\t\t\t\t   **        2.  修改信息            **\n");
		printf("\t\t\t\t   **        3.  删除信息            **\n");
		printf("\t\t\t\t   **        4.  查询信息            **\n");
		printf("\t\t\t\t   **        5.  打印报表            **\n");
		printf("\t\t\t\t   **        0.  退    出            **\n");
		printf("\t\t\t\t   ************************************\n");
		printf("请选择:");
		scanf("%d",&select);
		switch(select)
		{
			case 1:
				head=input_list(head);
				get_average(head);
				break;
			case 2:
				t=modify_node(head);		
				break;
			case 3:
				head=delete_node(head);	
				break;
			case 4:
				t=search_node(head);
				break;
			case 5:
				output_list(head);
				break;
			case 0:
				system("cls");
				t=free_all(head);
				printf("\t\t\t\t   (=~ω~=) 感谢使用本程序 (=~ω~=)\n");
				getch();
				break;
			default:			
				printf("输入错误!请重新输入!\n");	
				break;	
		}
	}
	free_all(head);
	return 0;
}

struct student * input_list(struct student *head)
{
	char c,ch;
	char mid[9];
	char mname[20];
	char msex[3];
	int mmath;
	int mlangue_c;
	int mEnglish;
	char mphone[12];
	FILE *fp;

	struct student *p=NULL,*q=NULL;

	printf("请输入第%d个学生的信息:\n",m_count+1);
	printf("学号:");
	scanf("%s",mid);
	while(1)
	{
		p=head;
		while(p)
		{
			if(strcmp(p->id,mid)==0) break;
			p=p->next;
		}
		if(p!=NULL)
		{
			printf("%s学号已存在!请重新输入!\n",mid);
			printf("学号:");
			scanf("%s",mid);
		}
		if(p==NULL) break;
	}
	printf("姓名:");
	scanf("%s",mname);
	printf("性别:");
	scanf("%s",msex);
	printf("数学成绩:");
	scanf("%d",&mmath);
	printf("英语成绩:");
	scanf("%d",&mEnglish);
	printf("C语言成绩:");
	scanf("%d",&mlangue_c);
	printf("电话号码:");
	scanf("%s",mphone);

	printf("是否保存:		是:Y  否:N\n");
	c=getch();
	if(m_count==0)
	{
		while(1)
		{
			if(c=='y'||c=='Y')
			{
				p=(struct student *)malloc(sizeof(struct student));
				if(p==NULL)
				{
					printf("申请内存失败!按任意键继续!");
					getch();
					return head;
				}
				head=p;
				strcpy(p->id,mid);
				strcpy(p->name,mname);
				strcpy(p->sex,msex);
				p->English=mEnglish;
				p->math=mmath;
				p->langue_c=mlangue_c;
				strcpy(p->phone,mphone);
				p->next=NULL;
				m_count++;
				printf("保存完毕!按任意键继续!\n");
				getch();
				printf("是否保存到磁盘文件中:		是:Y  否:N\n");
				ch=getch();
				while(1)
				{
					if(ch=='y'||ch=='Y')
					{
						printf("请输入文件名称:");
						scanf("%s",m_filename);
						fp=fopen(m_filename,"w");
						if(fp==NULL)
						{
							printf("文件打开失败!按任意键继续!\n");
							getch();
							return head;
						}
						fprintf(fp,"%s  %s  %s  %d  %d  %d  %.2lf  %s\n",p->id,p->name,p->sex,p->math,p->English,p->langue_c,(p->math+p->English+p->langue_c)/3.0,p->phone);
						fclose(fp);
						printf("已保存到%s中!按任意键继续!\n",m_filename);
						getch();
						return head;
					}

					else if(ch=='n'||ch=='N')
					{
						printf("没有保存到磁盘文件中!按任意键继续!\n");
						getch();
						return head;
					}
					else
					{
						printf("输入出错!请重新输入!\n");
						ch=getch();
					}
				}						
			}
			else if(c=='N'||c=='n') 
			{
				printf("没有保存!按任意键继续!\n");
				getch();
				return head;
			}
			else
			{
				printf("输入出错!请重新输入!\n");
				c=getch();
			}
		}
	}

	while(1)
	{
		if(c=='y'||c=='Y')
		{

			p=(struct student *)malloc(sizeof(struct student));
			if(p==NULL)
			{
				printf("申请内存失败!按任意键继续!");
				getch();
				return head;
			}		
			strcpy(p->id,mid);
			strcpy(p->name,mname);
			strcpy(p->sex,msex);
			p->English=mEnglish;
			p->math=mmath;
			p->langue_c=mlangue_c;
			strcpy(p->phone,mphone);
			p->next=NULL;
			m_count++;
			q=head;
			while(1)
			{		
				if(q->next==NULL) break;
				q=q->next;
			}
			q->next=p;
			printf("保存完毕!按任意键继续!\n");
			getch();
			printf("是否保存到磁盘文件中:		是:Y  否:N\n");
			ch=getch();
			while(1)
			{
				if(ch=='y'||ch=='Y')
				{
					if(m_filename[0]=='k')
					{
						printf("请输入文件名称:");
						scanf("%s",m_filename);
					}
					fp=fopen(m_filename,"a");
					if(fp==NULL)
					{
						printf("文件打开失败!按任意键继续!\n");
						getch();
						return head;
					}
					fprintf(fp,"%s  %s  %s  %d  %d  %d  %.2lf  %s\n",p->id,p->name,p->sex,p->math,p->English,p->langue_c,(p->math+p->English+p->langue_c)/3.0,p->phone);
					fclose(fp);
					printf("已保存到%s中!按任意键继续!\n",m_filename);
					getch();
					return head;
				}

				else if(ch=='n'||ch=='N')
				{
					printf("没有保存到磁盘文件中!按任意键继续!\n");
					getch();
					return head;
				}
				else
				{
					printf("输入出错!请重新输入!\n");
					ch=getch();
				}
			}									
		}
		else if(c=='N'||c=='n') 
		{
			printf("没有保存!按任意键继续!\n");
			getch();
			return head;
		}
		else
		{
			printf("输入出错!请重新输入!\n");
			c=getch();
		}
	}
}

int output_list_order(struct student *head)
{
	struct student *p;
	p=head;
	system("cls");
	if(p==NULL) 
	{
		printf("您当前并未录入任何信息!按任意键继续!\n");
		getch();
		return 0;
	}
	printf("学号\t    姓名\t性别\t数学\t英语\tC语言\t平均分\t电话号码\n");
	while(1)
	{
		printf("%s    ",p->id);
		printf("%s\t",p->name);
		printf("%s\t",p->sex);
		printf("%d\t",p->math);
		printf("%d\t",p->English);
		printf("%d\t",p->langue_c);
		printf("%.2lf\t",p->aver);
		printf("%s\n",p->phone);
		p=p->next;
		if(p==NULL)  break;
	}
	printf("显示完毕!按任意键继续!\n");
	getch();
	return 0;
}

int free_all(struct student *head)
{
	struct student *p,*q;
	p=head;
	if(p==NULL) return 0;
	q=p->next;

	while(1)
	{
		free(p);
		p=q;
		if(p==NULL) break;
		q=q->next;	
	}
	return 0;
}

struct student * search_node_by_id(struct student *head)
{
	char mid[9];
	struct student *p;
	p=head;

	printf("请输入您要查找的学号:");
	scanf("%s",mid);

	while(1)
	{
		if(strcmp(p->id,mid)==0) break;
		p=p->next;
		if(p==NULL) break;
	}

	if(p==NULL) 
	{
		printf("查询失败,该学号不存在!按任意键继续!\n");
		getch();
		return NULL;
	}
	printf("查询完毕!按任意键继续!\n\n");
	getch();
	display_node(p);
	printf("显示完毕!按任意键继续!\n");
	getch();
	return p;
}

int display_node(struct student *p)
{
	printf("学号:%s\n",p->id);
	printf("姓名:%s\n",p->name);
	printf("性别:%s\n",p->sex);
	printf("数学:%d\n",p->math);
	printf("英语:%d\n",p->English);
	printf("C语言:%d\n",p->langue_c);
	printf("平均分:%.2lf\n",p->aver);
	printf("电话号码:%s\n\n",p->phone);
	return 0;
}

int search_node_by_name(struct student *head)
{
	int count=0;
	char mname[20];
	struct student *mhead=NULL,*p,*q,*k,*y;
	p=head;
	printf("请输入您要查找的姓名:");
	scanf("%s",mname);

	while(1)
	{
		if(strcmp(p->name,mname)==0)
		{
			if(count==0)
			{
				q=(struct student *)malloc(sizeof(struct student));
				if(p==NULL)
				{
					printf("申请内存失败!按任意键继续!");
					getch();
					return 0;
				}
				strcpy(q->id,p->id);
				strcpy(q->name,p->name);
				strcpy(q->sex,p->sex);
				q->English=p->English;
				q->math=p->math;
				q->langue_c=p->langue_c;
				q->aver=p->aver;
				strcpy(q->phone,p->phone);
				q->next=NULL;
				mhead=q;
				count++;
			}
			else 
			{
				k=(struct student *)malloc(sizeof(struct student));
				if(k==NULL)
				{
					printf("申请内存失败!按任意键继续!");
					getch();
					return 0;
				}		
				strcpy(k->id,p->id);
				strcpy(k->name,p->name);
				strcpy(k->sex,p->sex);
				k->English=p->English;
				k->math=p->math;
				k->langue_c=p->langue_c;
				k->aver=p->aver;
				strcpy(k->phone,p->phone);
				k->next=NULL;
				count++;
				y=mhead;
				while(1)
				{		
					if(y->next==NULL) break;
					y=y->next;
				}
				y->next=k;
			}
		}
		p=p->next;
		if(p==NULL) break;
	}
	display_list(mhead);
	free_all(mhead);
	return 0;
}

int display_list(struct student *head)
{
	struct student *p;
	p=head;

	if(p==NULL) 
	{
		printf("查询失败,该姓名不存在!按任意键继续!\n");
		getch();
		return 0;
	}
	printf("查询完毕!按任意键继续!\n\n");
	getch();
	while(1)
	{
		display_node(p);
		p=p->next;
		if(p==NULL) break;
	}
	printf("显示完毕!按任意键继续!\n");
	getch();
	return 0;
}

int search_node(struct student *head)
{
	char c;
	printf("\n");
	printf("**********************\n");
	printf("**==================**\n");
	printf("**     查询信息     **\n");
	printf("**     A:按学号     **\n");
	printf("**     B:按姓名     **\n");
	printf("**==================**\n");
	printf("**********************\n");
	printf("请选择:");
	scanf("\n%c",&c);

	while(1)
	{
		if(c=='A'||c=='a') 
		{
			search_node_by_id(head);
			break;
		}
		else if(c=='B'||c=='b') 
		{
			search_node_by_name(head);
			break;
		}
		else
		{
			printf("输入错误!请重新输入:");
			scanf("\n%c",&c);
		}
	}
	return 0;
}

struct student * delete_node(struct student *head)
{
	char c,ch;
	char mid[9];
	struct student *p,*q;
	p=head;
	if(p==NULL)
	{
		printf("您当前未存入任何数据,无法删除!按任意键继续!\n");
		getch();
		return head;
	}
	q=p->next;

	printf("请输入您要删除学生的学号:");
	scanf("%s",mid);

	if(strcmp(p->id,mid)==0) 
	{	
		if(q==NULL)
		{
			display_node(p);
			printf("确认是否删除:		是:Y  否:N\n");
			c=getch();
			while(1)
			{
				if(c=='y'||c=='Y')
				{
					free(p);
					m_count--;
					printf("删除完毕!按任意键继续!\n");
					head=NULL;
					printf("是否删除文件中的信息:		是:Y  否:N\n");
					ch=getch();
					while(1)
					{
						if(ch=='y'||ch=='Y') 
						{
							FILE *fp;
							fp=fopen(m_filename,"w");
							if(fp==NULL)
							{
								printf("文件打开失败!");
								return head;
							}
							fclose(fp);
							printf("文件信息删除完毕!按任意键继续!\n");
							getch();
							return head;
						}
						else if(c=='n'||c=='N')
						{
							printf("删除失败!按任意键继续!\n");
							getch();
							return head;
						}
						else
						{
							printf("输入错误!请重新输入!\n");
							c=getch();
						}
					}
				}
				else if(c=='n'||c=='N')
				{
					printf("删除失败!按任意键继续!\n");
					getch();
					return head;
				}
				else 
				{
					printf("输入错误!请重新输入!");
					c=getch();
				}
			}
		}

		display_node(p);
		printf("确认是否删除:		是:Y  否:N\n");
		c=getch();
		while(1)
		{
			if(c=='y'||c=='Y')
			{
				head=p->next;
				free(p);
				m_count--;
				printf("删除完毕!按任意键继续!\n");
				getch();
				printf("是否删除文件中的信息:		是:Y  否:N\n");
				ch=getch();
				write_inf_file(head,ch,"删除");
				return head;
			}
		
			else if(c=='n'||c=='N')
			{
				printf("删除失败!按任意键继续!\n");
				getch();
				return head;
			}
			else
			{
				printf("输入错误!请重新输入!\n");
				c=getch();
			}
		}
	}
	
	while(1)
	{
		if(strcmp(q->id,mid)==0) break;
		q=q->next;
		p=p->next;
		if(q==NULL) break;
	}
	if(q==NULL) 
	{
		printf("您并未存入该项信息!按任意键继续!\n");
		getch();
		return head;
	}
	display_node(q);
	printf("确认是否删除:		是:Y  否:N\n");
	c=getch();
	while(1)
	{
		if(c=='y'||c=='Y')
		{
			p->next=q->next;
			free(q);
			m_count--;
			printf("删除完毕!按任意键继续!\n");
			getch();
			printf("是否删除文件中的信息:		是:Y  否:N\n");
			ch=getch();
			while(1)
			{
				if(ch=='y'||ch=='Y') 
				{
					write_inf_file(head,ch,"删除");
					return head;
				}
				else if(c=='n'||c=='N')
				{
					printf("删除失败!按任意键继续!\n");
					getch();
					return head;
				}
				else
				{
					printf("输入错误!请重新输入!\n");
					c=getch();
				}
			}
		}
		else if(c=='n'||c=='N')
		{
			printf("删除失败!按任意键继续!");
			getch();
			return head;
		}
		else
		{
			printf("输入错误!请重新输入!");
			c=getch();
		}
	}
}

int modify_node(struct student *head)
{
	char mid[9],ch;
	struct student *p,*q;
	p=head;
	if(p==NULL)
	{
		printf("您当前并未录入任何信息,无法修改!按任意键继续!\n");
		getch();
		return 0;
	}

	printf("请输入您要修改学生的学号:");
	scanf("%s",mid);

	while(1)
	{
		if(strcmp(p->id,mid)==0) break;
		p=p->next;
		if(p==NULL) break;
	}

	if(p==NULL)
	{
		printf("您并未存入该项信息!按任意键继续!\n");
		getch();
		return 0;
	}

	printf("原信息如下:\n");
	display_node(p);
	printf("新学号:");
	scanf("%s",p->id);
	while(1)
	{
		q=head;
		while(q)
		{
			if(strcmp(q->id,p->id)==0&&q!=p) break;
			q=q->next;
		}
		if(q!=NULL)
		{
			printf("%s学号已存在!请重新输入!\n",p->id);
			printf("新学号:");
			scanf("%s",p->id);
		}
		if(q==NULL) break;
	}

	printf("新姓名:");
	scanf("%s",p->name);
	printf("新性别:");
	scanf("%s",p->sex);
	printf("新数学成绩:");
	scanf("%d",&p->math);
	printf("新英语成绩:");
	scanf("%d",&p->English);
	printf("新C语言成绩:");
	scanf("%d",&p->langue_c);
	p->aver=(p->English+p->langue_c+p->math)/3.0;
	printf("新电话号码:");
	scanf("%s",p->phone);
	printf("修改完毕!按任意键继续!\n");
	getch();
	printf("是否保存到文件中:		是:Y  否:N\n");
	ch=getch();
	while(1)
	{
		if(ch=='y'||ch=='Y')
		{
			write_inf_file(head,ch,"修改");
			return 0;
		}
		else if(ch=='n'||ch=='N')
		{
			printf("保存到文件中失败!按任意键继续!\n");
			getch();
			return 0;
		}
		else
		{
			printf("输入出错!请重新输入!\n");
			ch=getch();
		}
	}
}

int get_average(struct student *head)
{
	struct student *p;
	p=head;

	if(p==NULL) return 0;

	while(1)
	{
		p->aver=(p->English+p->math+p->langue_c)/3.0;
		p=p->next;
		if(p==NULL) break;
	}
	return 0;
}

struct student * sort_list_select(struct student *head)
{
	struct student *headt=NULL,*t;
	struct student *p,*q;

	if(head==NULL||head->next==NULL) return head;

	p=head;
	t=headt=q=(struct student *)malloc(sizeof(struct student));
	strcpy(q->id,p->id);
	strcpy(q->name,p->name);
	strcpy(q->sex,p->sex);
	q->English=p->English;
	q->math=p->math;
	q->langue_c=p->langue_c;
	q->aver=p->aver;
	strcpy(q->phone,p->phone);
	q->next=NULL;
	p=p->next;
	while(1)
	{
		q=(struct student *)malloc(sizeof(struct student));
		strcpy(q->id,p->id);
		strcpy(q->name,p->name);
		strcpy(q->sex,p->sex);
		q->English=p->English;
		q->math=p->math;
		q->langue_c=p->langue_c;
		q->aver=p->aver;
		strcpy(q->phone,p->phone);
		q->next=NULL;
		t->next=q;
		p=p->next;
		t=t->next;
		if(p==NULL) break;
	}

	t=(struct student *)malloc(sizeof(struct student));

	for(p=headt;p!=NULL;p=p->next)
	{
		for(q=p->next;q!=NULL;q=q->next)
		{
			if(p->aver<q->aver)
			{
				strcpy(t->id,p->id);
				strcpy(t->name,p->name);
				strcpy(t->sex,p->sex);
				t->English=p->English;
				t->math=p->math;
				t->langue_c=p->langue_c;
				t->aver=p->aver;
				strcpy(t->phone,p->phone);
				strcpy(p->id,q->id);
				strcpy(p->name,q->name);
				strcpy(p->sex,q->sex);
				p->English=q->English;
				p->math=q->math;
				p->langue_c=q->langue_c;
				p->aver=q->aver;
				strcpy(p->phone,q->phone);
				strcpy(q->id,t->id);
				strcpy(q->name,t->name);
				strcpy(q->sex,t->sex);
				q->English=t->English;
				q->math=t->math;
				q->langue_c=t->langue_c;
				q->aver=t->aver;
				strcpy(q->phone,t->phone);
			}
		}
	}
	free(t);
	return headt;
}

int output_list1(struct student *head)
{
	int t;
	char c;
	struct student *headt=NULL;

	printf("\n");
	printf("******************************\n");
	printf("**==========================**\n");
	printf("**         打印报表         **\n");
	printf("**         A:按输入顺序     **\n");
	printf("**         B:按均分高低     **\n");
	printf("**==========================**\n");
	printf("******************************\n");
	printf("请选择:");
	scanf("\n%c",&c);

	while(1)
	{
		if(c=='A'||c=='a') 
		{
			output_list_order(head);
			break;
		}
		else if(c=='B'||c=='b') 
		{
			headt=sort_list_select(head);
			t=output_list_order(headt);
			free_all(headt);
			break;
		}
		else
		{
			printf("输入错误!请重新输入:\n");
			scanf("\n%c",&c);
		}
	}
	return 0;
}

int output_list(struct student *head)
{
	struct student *head1=NULL;
	char c;
	printf("\n");
	printf("**************************************\n");
	printf("**==================================**\n");
	printf("**             打印报表             **\n");
	printf("**        A:打印本次录入信息        **\n");
	printf("**        B:打印指定文件信息        **\n");
	printf("**==================================**\n");
	printf("**************************************\n");
	printf("请选择:");
	scanf("\n%c",&c);
	while(1)
	{
		if(c=='a'||c=='A')
		{
			output_list1(head);
			return 0;
		}
		else if(c=='b'||c=='B')
		{
			head1=output_list2();
			if(head1==NULL) 
			{
				printf("当前文件无任何信息!按任意键继续!\n");
				getch();
				return 0;
			}
			output_list1(head1);
			free_all(head1);
			return 0;
		}
		else
		{
			printf("输入错误!请重新输入!\n");
			scanf("\n%c",&c);
		}
	}
}

struct student * output_list2()
{
	int count=0;
	FILE *fp;
	char mfilename[40],c;
	struct student *head=NULL,*p,*q;
	printf("请输入文件名(包含文件路径):");
	scanf("%s",mfilename);
	fp=fopen(mfilename,"r");
	if(fp==NULL)
	{
		printf("文件打开失败!按任意键继续!\n");
		getch();
		return NULL;
	}

	while(1)
	{
		if(count==0)
		{
			c=fgetc(fp);
			if(c==EOF) return NULL; 
			head=(struct student *)malloc(sizeof(struct student));
			fscanf(fp,"%s  %s  %s  %d  %d  %d  %lf  %s\n",head->id,head->name,head->sex,&head->math,&head->English,&head->langue_c,&head->aver,head->phone);
			head->next=NULL;
			count++;
			c=fgetc(fp);
			if(c==EOF) break; 			
		}
		p=(struct student *)malloc(sizeof(struct student));
		fscanf(fp,"%s  %s  %s  %d  %d  %d  %lf  %s\n",p->id,p->name,p->sex,&p->math,&p->English,&p->langue_c,&p->aver,p->phone);
		p->next=NULL;
		q=head;
		while(1)
		{
			if(q->next==NULL) break;
			q=q->next;
		}
		q->next=p;
		count++;
		if(feof(fp)) 
		{
			fclose(fp);
			break;
		}
	}
		return head;
}

void write_inf_file(struct student *head,char ch,char *str)
{
	struct student *p;
	FILE *fp;
	while(1)
	{
		if(ch=='y'||ch=='Y')
		{
			fp=fopen(m_filename,"w");
			if(fp==NULL)
			{
				printf("文件打开失败!按任意键继续!\n");
				getch();
				return ;
			}
			p=head;
			while(1)
			{
				fprintf(fp,"%s  %s  %s  %d  %d  %d  %.2lf  %s\n",p->id,p->name,p->sex,p->math,p->English,p->langue_c,(p->math+p->English+p->langue_c)/3.0,p->phone);
				p=p->next;
				if(p==NULL) break;
			}
			fclose(fp);
			printf("文件信息%s完毕!按任意键继续!\n",str);
			getch();
			return ;
		}
		else if(ch=='n'||ch=='N')
		{
			printf("文件信息%s失败!按任意键继续!\n",str);
			getch();
			return ;
		}
		else
		{
			printf("输入错误!请重新输入!\n");
			ch=getch();
		}
	}
}



发布了30 篇原创文章 · 获赞 47 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44040023/article/details/103247972
今日推荐