C语言_增删查改排序

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

1.我的废话

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

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

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

还有-,-,编写软件–>VC++6.0

2.截图

1.添加数据在这里插入图片描述
2.按学号查找在这里插入图片描述
3.按姓名查找在这里插入图片描述
4.排序后输出在这里插入图片描述
5.删除学生信息在这里插入图片描述在这里插入图片描述

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");
}
	
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,我们可以使用MySQL提供的API来实现MySQL数据库的增删查改操作。以下是一些基本的MySQL操作示例: 1. 连接MySQL数据库 ```c #include <mysql.h> MYSQL *conn; conn = mysql_init(NULL); /* 连接数据库 */ if (!mysql_real_connect(conn, "localhost", "root", "password", "database_name", 0, NULL, 0)) { printf("%s\n", mysql_error(conn)); exit(1); } ``` 2. 插入数据 ```c char *query; query = "INSERT INTO table_name (column1, column2, ...) VALUES ('value1', 'value2', ...)"; if (mysql_query(conn, query)) { printf("%s\n", mysql_error(conn)); exit(1); } ``` 3. 查询数据 ```c MYSQL_RES *result; MYSQL_ROW row; char *query; int num_fields; query = "SELECT * FROM table_name"; if (mysql_query(conn, query)) { printf("%s\n", mysql_error(conn)); exit(1); } result = mysql_store_result(conn); num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { for(int i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(result); ``` 4. 更新数据 ```c char *query; query = "UPDATE table_name SET column1='value1', column2='value2', ... WHERE condition"; if (mysql_query(conn, query)) { printf("%s\n", mysql_error(conn)); exit(1); } ``` 5. 删除数据 ```c char *query; query = "DELETE FROM table_name WHERE condition"; if (mysql_query(conn, query)) { printf("%s\n", mysql_error(conn)); exit(1); } ``` 注意:以上代码示例仅供参考,实际使用时需要根据具体情况进行修改。同时,在使用MySQL API操作数据库时,需要注意SQL注入攻击等安全问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值