欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言_增删查改排序

程序员文章站 2022-05-08 14:33:11
...

C语言_学生信息管理系统_增删查改排序

1.我的废话

看了C语言链表后,自己动手写了一下.

但是,暂时不能将数据写入本地文件,因为 C语言_文件操作 那部分知识还不是很熟0.0

所以只能实现命令框程序。

2.截图

1.添加数据C语言_增删查改排序
2.按学号查找C语言_增删查改排序
3.按姓名查找C语言_增删查改排序
4.排序后输出C语言_增删查改排序
5.删除学生信息C语言_增删查改排序C语言_增删查改排序

3.源码

/* 
*  学生信息管理系统

*  学生信息
	{
		学号		
		姓名
		性别
		专业
	}
	  
		
*  功能介绍
* 1、增
* 2、删
* 3、查   
* 4、改
* 5、排序后,列表显示

 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student{
	char name[10];//名字
	char student_number[20];//学号
	char gender[10];//性别
	char major[30];//专业
	struct student *link;//链表“钩子”
	};

struct student *HEAD;
struct student *head;
struct student *END;

void chooce();//功能选择菜单
struct student *Add();//添加
void Delete();//删除
void Find();//查找
void Change();//更改
void Display();//展示
void sort();//排序,根据学号
int  retrieve(char *num);//检索相同学号
void exchange(char *x,char *y);//交换x和y
void print(struct student *p);//打印结构体中学生信息

int len = 0;//链表长度,学生人数

//主函数
int main()
{
	HEAD = (struct student *)malloc(sizeof(struct student));

	HEAD->link =  NULL;
	printf("welcome to student information management system!\n\n");

	chooce();

	free(HEAD);
	system("pause");
	return 0;
}

void chooce()
{
	int n,f=0;
	do{
		printf(	"******************************************************\n"
				"******  Please select the following functions:  ******\n"
				"******************************************************\n"
				"************  1.Add student information     **********\n"
				"************  2.Delete student informatio   **********\n"
				"************  3.Find student informatio     **********\n"
				"************  4.Change student informatio   **********\n"
				"************  5.Display all student informatio  ******\n"
				"************  6.log out           ********************\n"
				"******************************************************\n");

		printf("\nPlease enter the serial number:");
		scanf("%d",&n);
		switch (n)
		{
		case 1:
			printf("len = %d.  \n",len);//输出总人数
			
			if(len==0)   //首地址分配
			{
				HEAD =	Add();

			}
			else
			{
				//END=(struct student *)malloc(sizeof(struct student));
				//不许要开辟END空间,因为他只是用于传递链表头的地址
				END=HEAD;

				while(END->link!=NULL)//移动链表到最后一个结构体
				{	
					END=END->link;
				}

				END->link = Add();

			}

			putchar('\n');
				

			
			break;
			
		case 2:
			Delete();
			putchar('\n');
			break;

		case 3:
			Find();
			putchar('\n');
			break;

		case 4:
			Change();
			putchar('\n');
			break;

		case 5:
			Display();
			putchar('\n');
			break;

		case 6:
			f=1;
			break;

		default :printf("Please enter a valid number!\n");
		}
	}while(f==0);
}

//添加学生
struct student *Add()//结构体函数,返回一个结构体指针
{
	struct student *currently,*next;
	char flag;
	char flag_0;
	
	//head = (struct student *)malloc( sizeof(struct student));
	//不需要开辟head空间,因为他只是用于传递一个链表头地址
	
	currently =  (struct student *)malloc( sizeof(struct student) );
	
LEA:
	printf("\nPlease enter the student id number: ");
	scanf("%s",&currently->student_number);
	getchar();

	if(retrieve(currently->student_number)==1)
		{
			printf("\nSorry,the student number already exists!\n");
			printf("Do you want to continue entering other student information?(y/any other key)\n");
			scanf("%c",&flag_0);
			if(flag_0 == 'y')
			{
				goto LEA;
			}
			else
				return 0;
		}
	
	printf("Please enter the student's name: ");
	scanf("%s",currently->name);
		
	printf("Please enter the gender of this student: ");
	scanf("%s",currently->gender);

	printf("Please enter the major of this student: ");
	scanf("%s",currently->major);
	getchar();//吸收 回车(多余的字符)

	currently->link=NULL;
	
	head = currently;
	
	if(len == 0)
		HEAD = head;
	else
		END->link = head;

	
	len++;
	
	printf("ÊÇ·ñ¼ÌÐøÊäÈ룺(y/n)\n");
	scanf("%c",&flag);	
	

	while(flag=='y')
	{
		next=(struct student *)malloc(sizeof(struct student));//循环生成链表中的结构体
		
		printf("\nPlease enter the student id number: ");
		scanf("%s",next->student_number);
		getchar();
		
		if(retrieve(next->student_number)==1)
		{
			printf("\nSorry,the student number already exists!\n");
			printf("Do you want to continue entering other student information?(y/any other key)\n");
			scanf("%c",&flag_0);
			if(flag_0 == 'y')
				continue;
			else
				break;
		}

		printf("Please enter the student's name: ");
		scanf("%s",next->name);
	
		printf("Please enter the gender of this student: ");
		scanf("%s",next->gender);

		printf("Please enter the major of this student: ");
		scanf("%s",next->major);
		getchar();

		currently->link=next;
		next->link=NULL;
		len++;

		currently=next;
	
		//   free(next);释放next的同时会将原本链表中的结构体释放

		printf("Whether to continue enter?(y/any other key)\n");

		scanf("%c",&flag);	

	}
	printf("\n");


	//free(currently);释放的同时会将原本链表中的结构体释放

	system("pause");
	return head;
}

//检索,是否有相同学号的学生
int  retrieve(char *num)
{
	struct student *p;
	p=HEAD;
	
	while(1)
	{
		if(strcmp(p->student_number,num)==0)
			return 1;
		if(p->link!=NULL)
			p = p->link;
		else
			break;
	}
	
	return 0;
}



//删除一个学生信息
void Delete()
{
	int flag_0=0;
	char flag_1;
	struct student *p,*p_0,*p_1;
	p=(struct student *)malloc(sizeof(struct student));
	
	p_1 = HEAD;
	p_0 = p_1;


	putchar('\n');
	printf(	"-------------------------------------------------------------------------------\n"
			"|| Enter the student ID to find the student information you want to delete.  ||\n"
			"-------------------------------------------------------------------------------\n");

	printf("Please enter the student id of the student you are looking for: ");
	scanf("%s",p->student_number);
	getchar();
	
	printf("\n");
	while(1)
	{
		if(strcmp(p->student_number,p_1->student_number)==0)	
		{
			flag_0 = 1;
			print(p_1);
			break;
		}	
		if(p_1->link==NULL)
			break;
		else
		{
			p_0 = p_1;
			p_1 = p_1->link;
		}
	}
	if(flag_0 == 1)
	{
		printf("Do you want delete the information of student number %s.\n",p->student_number);
		printf("Please press 'y' or any key.\n");
		scanf("%c",&flag_1);
		if(flag_1 == 'y')
		{
			if(p_1 == HEAD)
				HEAD = HEAD->link;
			else
				p_0->link = p_1->link;

			free(p_1);

			len--;
			printf("OK,delete successfully!\n");
		}
		else 
			printf("Delete cancel,return to the general menu.\n");
	}
	else
		printf("Sorry,there are no student with student id %s\n",p->student_number);

	//free(p_1);//释放p_1的同时会将原本链表中的结构体释放
	free(p);
	system("pause");
}




//查找学生,1.学号;2.姓名
void Find()
{
	int n_1=0,flag_0=0;
	struct student *p,*p_0;
	p=(struct student *)malloc(sizeof(struct student));
	
	p_0=HEAD;

	putchar('\n');
	printf(	"------------------------------------\n"
			"||  1.Search by student number.   ||\n" 
			"||  2.Search by name.             ||\n"
			"------------------------------------\n"
			"Please enter the corresponding number:");
	scanf("%d",&n_1);
	switch(n_1)
	{
	case 1:
		printf("Please enter the student id of the student you are looking for: ");
		scanf("%s",p->student_number);
		
		printf("\n");
		while(1)
		{
			if(strcmp(p->student_number,p_0->student_number)==0)	
			{
				flag_0 = 1;
				print(p_0);
				break;
			}
			if(p_0->link == NULL)
				break;
			else
				p_0 = p_0->link;
				
		}
		if(flag_0 == 0)
			printf("Sorry,there are no student with student id %s\n",p->student_number);
		else
			break;

	case 2:
		printf("Please enter the name of the student you are looking for: ");
		scanf("%s",p->name);

		putchar('\n');
		while(1)
		{
			if(strcmp(p->name,p_0->name)==0)	
			{
				flag_0=1;
				print(p_0);
				printf("\n");
			}
			if(p_0->link != NULL)
				p_0=p_0->link;					
			else
				break;	
		}


		if(flag_0==0)
			printf("Sorry,there are no student named %s!\n",p->name);
		else
			printf("These are the student whose names are %s\n",p->name);

		break;

	default:printf("Please enter the correct number!\n");
	}
	
	free(p);
	system("pause");
}





void Change()
{
	int flag_0=0;
	int flag_2=0;
	char flag_1;
	int n_2;
	
	struct student *p,*p_1;
	
	p=(struct student *)malloc(sizeof(struct student));
	
	p_1 = HEAD;


	putchar('\n');
	printf(	"-------------------------------------------------------------------------------\n"
			"|| Enter the student ID to find the student information you want to change.  ||\n"
			"-------------------------------------------------------------------------------\n");

	printf("Please enter the student id of the student you are looking for: ");
	scanf("%s",p->student_number);
	getchar();
	
	printf("\n");
	while(1)
	{
		if(strcmp(p->student_number,p_1->student_number)==0)	
		{
			flag_0 = 1;
			print(p_1);
			printf("\n");
			break;
		}
		
		if(p_1->link!=NULL)
		{
			p_1 = p_1->link;
		}
		else
			break;
	}
	if(flag_0 == 1)
	{
		printf(	"------------------------------------\n"
				"|| You want to change student:    ||\n"
				"------------------------------------\n"
				"||  1.ID number.                  ||\n" 
				"||  2.name.                       ||\n"
				"||  3.gender.                     ||\n"
				"||  4.major.                      ||\n"
				"||  5.get back.                   ||\n"
				"------------------------------------\n"
				"Please enter the corresponding number:");
		scanf("%d",&n_2);
		switch(n_2)
		{
		case 1:
LEB:
			printf("Change the Id number to:");
			scanf("%s",p->student_number);
			getchar();
				
			if(retrieve(p->student_number )==1)
			{
				printf("\nSorry,the student number already exists!\n");
				printf("Do you want to continue change his/her information?(y/any other key)\n");
				scanf("%c",&flag_1);
				if(flag_1 == 'y')
					goto LEB;
				else
					flag_2 = 1;
			}

			break;
		case 2:
			printf("Change the name to:");
			scanf("%s",p->name);
			strcpy(p_1->name,p->name);
			break;
		case 3:
			printf("Change the gender to:");
			scanf("%s",p->gender);
			strcpy(p_1->gender ,p->gender );
			break;
		case 4:
			printf("Change the major to:");
			scanf("%s",p->major );
			strcpy(p_1->major ,p->major );
			break;
		
		case 5: 
			flag_2 = 1;
			break;
		default:printf("Please enter the correct number!\n");
			flag_2 = 1;
		}
		
		free(p);

		if(flag_2 == 0)
		{
			printf("Change the success!\n");

			print(p_1);
		}
		
	}		
	else		
		printf("Sorry,there are no student with student id %s\n",p->student_number);
	system("pause");
}			
			
			
//列表展示			
void Display()
{

	int i;

	struct student *p;
	
	p=(struct student *)malloc(sizeof(struct student));
	
	p = HEAD;
	
	
	sort();//先按学号排序,再输出


	printf("\nThere are %d student in all!\n",len);
	
	printf("***** student number ***** name ***** gender ***** majior *****\n");
	
	for(i=0;i<len;i++)
	{
		printf("      %-21s%-11s%-13s%-12s \n",p->student_number,p->name,p->gender,p->major);

//可以输出链表的‘钩子’,用于调试查看
//printf("%s\n",p->link );
		
		p=p->link;
	}
	system("pause");
}	
	
	
	
//排序,按学号从低到高排序
void sort()
{	
	struct student *p,*q;
	
	p = HEAD;

	for(p=HEAD ; ; p = p->link)
	{
		for(q=p ; ; q = q->link)
		{
			if(strcmp(p->student_number ,q->student_number ) > 0)
			{
				exchange(p->student_number ,q->student_number );
				exchange(p->name ,q->name );
				exchange(p->gender ,q->gender );
				exchange(p->major ,q->major );
			}
			if(q->link == NULL)
				break;

		}
		if(p->link == NULL)
			break;
	}

}	
	

//交换x和y	
void exchange(char *x,char *y)
{
	char * m;

	m = (char *)malloc(50*sizeof(char));

	strcpy(m , x);
	strcpy(x , y);
	strcpy(y , m);

	free(m);
}


//打印学生信息
void print(struct student *p)
{
	printf("student number:%s\n",p->student_number );
	printf("student name:%s\n",p->name);
	printf("student gender:%s\n",p->gender);
	printf("student major:%s\n",p->major);
	printf("\n");
}