首页 > 学院 > 开发设计 > 正文

C语言实现——学生管理系统

2019-11-10 19:53:53
字体:
来源:转载
供稿:网友

程序员的必经之路吧,学生管理系统。在制作过程中体会到了编写一个具体可用的软件而要做的工作,无论是编写前的准备,还是编写过程中每个细节的处理,都让我理解了这份工作是个什么样子。程序并不是抬手就来,一个软件要完整,同时还要将每个功能部分区分开,所以在编写之前一定要先规划清楚,这是一项工程。逻辑要严密,编程完全就是对逻辑思维的考验,大大小小的函数、语句都是靠逻辑连接起来的。基础知识要扎实,在我现在这个阶段,经常会遇到为干掉bug而苦思半天,在逻辑上寻觅没有出口,其实只是对用法不了解,上课时认为那种不太常考的东西不用太费心,只要把主要的知识记住就行,但事实上每个知识点都是有用的。

#include<stdio.h>#include<windows.h>//#include<conio.h>//#include<string.h>struct STUDENT {	char name[10];	int num;	int sex;	double score;	struct STUDENT* next;}*ListHead;void menu();void Import();void Show();void Add();void Search();void Rank();void Delete();void Export();void Exit();struct STUDENT* CreatListHead();void ListInsert(char *name,int num,int sex,double score);void IfListBlank();void ExchangeNope(struct STUDENT *p,struct STUDENT *q);void DeleteNode(struct STUDENT *d);void WriteList(char *path);void IfHeadEmpty();int main(){//	Load();//	clrscr();//	PRintf("en");//	system("cls");/*	if(ListHead==NULL)		printf("!!!");	else		printf("???");*/	menu();		return 0;}void menu() {	int i;	system("cls");		printf("-----------------------------------------------/n");	printf("                 学生管理系统/n");	printf("-----------------------------------------------/n");	printf("       1.导入 | 2.显示 | 3.增添 | 4.查找/n");	printf("       5.排序 | 6.删除 | 7.导出 | 8.退出/n");	printf("-----------------------------------------------/n");	printf("请选择一个选项:");	scanf("%d", &i);	switch(i) {		case 1:Import();break;		case 2:Show();break;		case 3:Add();break;		case 4:Search();break;		case 5:Rank();break;		case 6:Delete();break;		case 7:Export();break;		case 8:Exit();break;		case 9:IfListBlank();break;		default:menu();	}}//导入文件---------------------------------------------------------------------------------void Import(){	ListHead = CreatListHead();	FILE *fp;	char buf[30];	char *iname;	int inum=0;	int isex=0;	double iscore=0;	int i = 0;	char *line;	fp=fopen("students.txt","r");	if(fp==NULL)	{		printf("import failed!&& creat a new file,any key return/n");		ListHead = CreatListHead();		fflush(stdin);		getchar();		menu();	}else{		while(!feof(fp))		{			fgets(buf,30,fp);			line = strtok(buf," /t/r/n");			while(line != NULL)			{				switch(i){				case 0: iname=line; break;				case 1: inum=atoi(line); break;				case 2: isex=atoi(line); break;				case 3: iscore=atof(line); break;				default: break;				}				i++;				//	printf("%s  %d  %d  %f/n",iname,inum,isex,iscore);				//	ListInsert(iname,inum,isex,iscore);				line = strtok(NULL," /t/r/n");				}			i=0;			//	printf("%-10s%-5d %d %4.1f/n",iname,inum,isex,iscore);			ListInsert(iname,inum,isex,iscore);		}		fclose(fp); 	}		printf("import successful! anykey to return/n");	fflush(stdin);	getchar();	menu();}//显示功能---------------------------------------------------------------------------------void Show(){	IfHeadEmpty();	struct STUDENT *prep,*p;	prep = ListHead;	p = ListHead->next;	while(p)	{		prep = p;		p =p->next;		if(prep->sex)		{			printf("%-10s%-5d 男 %4.1f/n",prep->name,prep->num,prep->score);		}else{			printf("%-10s%-5d 女 %4.1f/n",prep->name,prep->num,prep->score);		}	}	printf("show successful! anykey to return/n");	fflush(stdin);	getchar();	menu();}//添加数据---------------------------------------------------------------------------------void Add(){	IfHeadEmpty();	char aname[10];	int anum=0;	int asex=0;	double ascore=0;	printf("enter data:");	fflush(stdin);	scanf("%s%d%d%lf",aname,&anum,&asex,&ascore);//	printf("%s%-5d %d %4.1f/n",aname,anum,asex,ascore);	ListInsert(aname,anum,asex,ascore);		printf("add successful! anykey to return/n");	fflush(stdin);	getchar();	menu();}//查询功能---------------------------------------------------------------------------------void Search(){	IfHeadEmpty();	struct STUDENT *prep,*p;	prep = ListHead;	p=ListHead->next;	int i;	char sname[10];	int snumsex;	double sscore;	int isfind=0;	//printf("%-10s%-5d %d %4.1f/n",name,num,sex,score);	printf("which methed you choose:1.name 2.num 3.sex 4.score 0.exit/n");	scanf("%d",&i);	switch(i){	case 0:menu();	case 1: 		printf("enter the name you are looking for:");		scanf("%s",sname);		while(p)		{			prep = p;			p = p->next;			if(strcmp(prep->name,sname)==0)			{				printf("%-10s%-5d %d %4.1f/n",prep->name,prep->num,prep->sex,prep->score);				isfind = 1;			}		}		break;	case 2:			printf("enter the num you are looking for:");		scanf("%d",&snumsex);		while(p)		{			prep = p;			p =p->next;			if(prep->num==snumsex)			{				printf("%-10s%-5d %d %4.1f/n",prep->name,prep->num,prep->sex,prep->score);				isfind = 2;			}		}		break;	case 3:		printf("enter the sex you are looking for:");		scanf("%d",&snumsex);		while(p)		{			prep = p;			p =p->next;			if(prep->sex==snumsex)			{				printf("%-10s%-5d %d %4.1f/n",prep->name,prep->num,prep->sex,prep->score);				isfind = 3;			}		}		break;	case 4:		printf("enter the score you are looking for:");		scanf("%lf",&sscore);		while(p)		{			prep = p;			p =p->next;			if(prep->score==sscore)			{				printf("%-10s%-5d %d %4.1f/n",prep->name,prep->num,prep->sex,prep->score);				isfind = 4;			}		}		break;	default:		printf("error number,re-enter:/n");		Search();	}	if(isfind!=0)		printf("Search successful! press any ket to return/n");	else		printf("Search failed! press any ket to return/n");	fflush(stdin);	getchar();//	setbuf(stdin,"4/r");//	ungetc('4',stdin);//	sscanf("4","%d",stdin);//	menu();	Search();}//排序功能---------------------------------------------------------------------------------void Rank(){	IfHeadEmpty();	struct STUDENT *prep,*p,*key;	int i=0,j=1;//	double isequal;//	printf("%-10s%-5d %d %4.1f/n",name,num,sex,score);	printf("sort by: 1.name 2.num 3.sex 4.score 0.exit/n");	scanf("%d",&i);	if(i<1 || i>4)	{		printf("wrong choose,re-enter:/n");		Rank();	}	printf("order: 1.positive 0.reverse/n");	scanf("%d",&j);	if(j<0 || j>1)	{		printf("wrong choose,re-enter:/n");		Rank();	}	switch(i){	case 0:menu();              //选择排序法:将第一个数的备份与后排比较,小于它的就将他赋值给备份交换(指针代表原数),一行循环接收进行,再将第二个数进行如此循环	case 1:			prep = ListHead;		p=ListHead->next;		while(prep->next)		{			prep = prep->next;			p = prep->next;			key = prep;			while(p)			{				if(j)				{					if(strcmp(key->name,p->name) > 0)						key = p;				}else{					if(strcmp(key->name,p->name) < 0)						key = p;				}				p = p->next;			}			ExchangeNope(prep, key);		}		break; 	case 2:		prep = ListHead;		p=ListHead->next;		while(prep->next)		{			prep = prep->next;			p = prep->next;			key = prep;			while(p)			{				if(j)				{					if((key->num-p->num) > 0)						key = p;				}else{					if((key->num-p->num) < 0)						key = p;				}				p = p->next;			}			ExchangeNope(prep, key);		}		break;	case 3:		prep = ListHead;		p=ListHead->next;		while(prep->next)		{			prep = prep->next;			p = prep->next;			key = prep;			while(p)			{				if(j)				{					if((key->sex-p->sex) > 0)						key = p;				}else{					if((key->sex-p->sex) < 0)						key = p;				}				p = p->next;			}			ExchangeNope(prep, key);		}		break;	case 4:		prep = ListHead;		p=ListHead->next;		while(prep->next)		{			prep = prep->next;			p = prep->next;			key = prep;			while(p)			{				if(j)				{					if((key->score-p->score) > 0)						key = p;				}else{					if((key->score-p->score) < 0)						key = p;				}				p = p->next;			}			ExchangeNope(prep, key);		}		break;	default:printf("wrong choose,re-enter:");		Rank();	}	Show();	printf("Rank successful!any key to return;/n");	fflush(stdin);	getchar();	menu();	}//删除---------------------------------------------------------------------------------void Delete(){	IfHeadEmpty();	struct STUDENT *prep,*p,*del;	prep = ListHead;	p=ListHead->next;	int i;	int isfind=0;	char *dname;	int dnumsex;	double dscore;	printf("select a method: 1.name 2.num 3.sex 4.score 0.exit/n");	scanf("%d",&i);	switch(i){	case 0:menu();	case 1:		printf("enter the name:/n");		scanf("%s",dname);		while(p)		{						if(!strcmp(p->name,dname))			{				prep->next = p->next;				del = p;				printf("%-10s%-5d %d %4.1f(已删除)/n",del->name,del->num,del->sex,del->score);				free(p);				p = prep->next;				isfind = 1;			}else{				prep = p;				p =p->next;			//	isfind = 0;			}		}		break;	case 2:		printf("enter the num:/n");		scanf("%d",&dnumsex);		while(p)		{						if(!(p->num-dnumsex))			{				prep->next = p->next;				del = p;				printf("%-10s%-5d %d %4.1f(已删除)/n",del->name,del->num,del->sex,del->score);				free(p);				p = prep->next;				isfind = 1;			}else{				prep = p;				p =p->next;			//	isfind = 0;			}		}		break;	case 3:		printf("enter the sex:/n");		scanf("%d",&dnumsex);		while(p)		{						if(!(p->sex-dnumsex))			{				prep->next = p->next;				del = p;				printf("%-10s%-5d %d %4.1f(已删除)/n",del->name,del->num,del->sex,del->score);				free(p);				p = prep->next;				isfind = 1;			}else{				prep = p;				p =p->next;			//	isfind = 0;			}		}		break;	case 4:		printf("enter the score:/n");		scanf("%lf",&dscore);		while(p)		{						if(!(p->score-dscore))			{				prep->next = p->next;				del = p;				printf("%-10s%-5d %d %4.1f(已删除)/n",del->name,del->num,del->sex,del->score);				free(p);				p = prep->next;				isfind = 1;			}else{				prep = p;				p =p->next;			//	isfind = 0;			}		}		break;	default:		printf("wrong choose,re-enter:");		Delete();	}	if(isfind)		printf("Delete successful!/n");	else		printf("Delete failed!/n");	printf("there is the current list:/n");	Show();	fflush(stdin);	printf("press any key to continue");	getchar();	menu();}//导出档案---------------------------------------------------------------------------------void Export(){	IfHeadEmpty();	FILE *fp;	char save[15];	int i;	printf("enter the save name:/n");	fflush(stdin);///	printf("if wrong????????????????????/n");	scanf("%s",save);                        //字符串键入只能用字符串数组接收不能用指针变量//	printf("if wrong????????????????????/n");	fp = fopen(save,"r");//	printf("if wrong????????????????????/n");	if(fp!=NULL)	{		printf("Whether covered: 1.Yse 0.No:/n");		fflush(stdin);		scanf("%d",&i);		if(i)			WriteList(save);		else			Export();		fclose(fp);	}else{		WriteList(save);			}			printf("Export successful! press any key to continue");	fflush(stdin);	getchar();	menu();	}//退出-----------------------------------------------------------------------------------------void Exit(){	exit(0);}//链表相关函数---------------------------------------------------------------------------------struct STUDENT* CreatListHead(){	struct STUDENT* p;	p = (struct STUDENT*)malloc(sizeof(struct STUDENT));//结构体指针创建后一定要初始化!!!	p->next = NULL;	return p;}void ListInsert(char *name,int num,int sex,double score){	//printf("%-10s%-5d %d %4.1f/n",name,num,sex,score);	struct STUDENT *prep,*p;	prep = ListHead;	p=ListHead->next;	printf("%-10s%-5d %d %4.1f/n",name,num,sex,score);	while(p)	{		prep = p;		p =p->next;	}	p = (struct STUDENT*)malloc(sizeof(struct STUDENT));		strcpy(p->name,name);	p->num = num;	p->sex = sex;	p->score = score;	p->next = NULL;	prep->next = p;}void IfListBlank(){	struct STUDENT *prep,*p;	int i=0;	prep = ListHead;	p=ListHead->next;	while(p)	{		prep = p;		p =p->next;		i++;	}	printf("%d",i);}void ExchangeNope(struct STUDENT *p,struct STUDENT *q){	char exchar[10];	int exint;	double exdouble;	strcpy(exchar , p->name);	strcpy(p->name , q->name);	strcpy(q->name , exchar);	exint = p->num;	p->num = q->num;	q->num = exint;	exint = p->sex;	p->sex = q->sex;	q->sex = exint;	exdouble = p->score;	p->score = q->score;	q->score = exdouble;}void DeleteNode(struct STUDENT *d){	struct STUDENT *prep,*p,*del;	prep = ListHead;	p=ListHead->next;	while(p)	{		if(prep==d)		{			prep->next = p->next;			del = p;			printf("%-10s%-5d %d %4.1f(已删除)/n",del->name,del->num,del->sex,del->score);			prep = p->next;			p = prep->next;			free(del);			break;		}else{			prep = p;			p =p->next;		}	}}void WriteList(char *path){	struct STUDENT *p;//	prep = ListHead;	p = ListHead->next;	FILE *fpin;	fpin = fopen(path,"w");	while(p)	{	//	fwrite(p,sizeof(p),1,fpin);		fprintf(fpin,"%-10s%-5d %d %4.1f/n",p->name,p->num,p->sex,p->score);		printf("%-10s%-5d %d %4.1f(已写入)/n",p->name,p->num,p->sex,p->score);		p = p->next;	}	fclose(fpin);}void IfHeadEmpty(){	if(ListHead==NULL){		printf("please import!/n");		getchar();		menu();	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表