实验——学生信息管理系统

实验——学生信息管理系统

大一时,在C语言程序设计实践课中,老师给出许多题目,让我们选择完成,然后我就选择了容易理解的学生信息管理系统题目,完成老师给出的实验。

操作 :利用结构体和链表,对学生信息进行操作。使用菜单的形式,创建链表、输入学生信息、利用文件保存信息、输出学生信息、对学生信息排序、插入学生信息、查询学生信息(按学号查询、按姓名查询)、删除学生信息。
功能不是很多,大家可以在此功能代码上面添加修改信息等功能的代码。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef struct date
{
	int year;
	int month;
	int day;
}DATE;
typedef struct st
{
	int num;
	char name[20];
	char sex;
	DATE birthday;
	int tele;
	char address[20];
	struct st *next;
}STUDENT;

int Menu(void);
void ReserveInfor(STUDENT *L);//将学生信息用文件保存
STUDENT* InputInfor(STUDENT *L);//录入学生信息
STUDENT* PrintInfor(STUDENT *L);//输出学生信息
STUDENT* SwapInfor(STUDENT *L);//为学生信息排序
STUDENT* SearchNum(STUDENT *L,int e);//按学号查询,e为查询的学号值
STUDENT* SearchName(STUDENT *L,char name1[]);//按姓名查询,e为查询的姓名
STUDENT* Delete(STUDENT *L,int num1);//删除信息
STUDENT* InsertInfor(STUDENT *L,int i,STUDENT *stu1);//插入学生的信息,i为插入信息的位置,stu1为插入的值


int main()
{
	STUDENT SqL,stu2;//学生信息结构体变量
	STUDENT *res,*ret,*pos,*pot,*wot,*wos;//函数返回值
	int itemSelected;//res为函数的返回值
	int number,number1,k;//定义学号变量和插入学生信息的位置
	char name2[20];//定义按姓名查找的姓名数组
	while(1)
	{
		itemSelected=Menu();
		switch(itemSelected)
		{
		case 1:res=InputInfor(&SqL);
			ReserveInfor(res);
			printf("录入信息完毕(此功能默认将数据存入文件)\n");break;
		case 2:printf("The student's information is:\n");
			PrintInfor(res);break;
		case 3:ret=SwapInfor(res);
			printf("The abscending order is:\n");
			PrintInfor(ret);break;
		case 4:printf("按学号查询学生信息:\n");
			printf("please input the student's number:");
			scanf("%d",&number);
			fflush(stdin);    //清空输入缓存区
			pos=SearchNum(res,number);
			if(pos!=NULL)
			{
				printf("该学生的信息为:%d %s %c %d/%02d/%02d %d %s\n",pos->num ,pos->name ,pos->sex ,
					pos->birthday .year,pos->birthday .month,pos->birthday .day,pos->tele ,pos->address );
			}
			else
			{
				printf("该学生不存在!\n");
			}
			break;
		case 5:printf("按姓名查询信息:\n");printf("please input student's name:");
			scanf("%s",name2);
			fflush(stdin);
			pot=SearchName(res,name2);
			if(pot!=NULL)
			{
				printf("该学生的信息为:%d %s %c %d/%02d/%02d %d %s\n",pot->num ,pot->name ,pot->sex ,
					pot->birthday .year,pot->birthday .month,pot->birthday .day,pot->tele ,pot->address );
			}
			else
			{
				printf("该学生不存在!\n");
			}
			break;
		case 6:printf("请输入需要删除学生的学号:");
			scanf("%d",&number1);
			fflush(stdin);
			wos=Delete(res,number1);
			PrintInfor(wos);break;
		case 7:printf("请输入需要添加学生的学生信息number,name,sex(sex use char),birth year,birth month,birth day,telephone number,address:\n");
			scanf("%d %s %c %d %d %d %d %s",&stu2.num,stu2.name,&stu2.sex,&stu2.birthday.year,
				&stu2.birthday.month,&stu2.birthday.day,&stu2.tele ,stu2.address );
			fflush(stdin);
			printf("请输入需要将学生信息插入的位置:");
			scanf("%d",&k);
			fflush(stdin);
			wot=InsertInfor(res,k,&stu2);   //在插入学生信息后面,也可以添加函数ReserveInfor()来保存信息。
			printf("输出插入学生信息后的学生表:\n");
			PrintInfor(wot);break;
		case 0:printf("End of the programe!\n");
			exit(0);
		default :printf("Input error!\n");
		}
	}
	return 0;
}


/*函数功能:显示菜单并获得用户键盘输入的选项*/
int Menu(void)
{
	int itemSelected;
	printf("\nManagement for student's information\n");
	printf("1.Input student's information\n");
	printf("2.print student's information\n");
	printf("3.rand student's information\n");
	printf("4.use number to search student's information\n");
	printf("5.use name to search studen's information\n");
	printf("6.delete student's information\n");
	printf("7.insert student's information\n");
	printf("0.end of the programe\n");
	printf("please input your choice:");
	scanf("%d",&itemSelected);
	return itemSelected;
}

/*函数功能:创建链表并输入学生的信息*/
STUDENT* InputInfor(STUDENT *L)
{
	int n;
	STUDENT *tail=NULL,*p=NULL;
	L=(STUDENT *)malloc(sizeof(STUDENT));
	L->next =NULL;
	tail=L;//创建头结点
	printf("please input student's number(when number=-1 end of the programe):");
	while(scanf("%d",&n) && n!=-1)
	{
		p=(STUDENT *)malloc(sizeof(STUDENT));
		p->num =n;
		printf("please input student's name,sex(input char),birth year,birth month,birth day,telephone number,address:\n");
		scanf("%s %c %d %d %d %d %s",p->name,&(p->sex),&(p->birthday.year),
			&(p->birthday.month),&(p->birthday.day),&(p->tele ),p->address  );
		tail->next =p;
		tail=p;
		tail->next =NULL;
		printf("please input student's number:");
	}//输入学生的信息
	return L;
}


/*函数功能:输出学生的信息*/
STUDENT* PrintInfor(STUDENT *L)
{
	STUDENT *p=L;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		printf("%d %s %c %d/%02d/%02d %d %s\n",p->num ,p->name ,p->sex ,p->birthday .year,
			p->birthday .month,p->birthday .day,p->tele ,p->address );
	}
	return L;
}


/*函数功能:根据学号大小,将学生信息按学号大小递增排序*/
STUDENT* SwapInfor(STUDENT *L)
{
	STUDENT *p=L,*q=NULL,*temp;
	//int temp,temp1,temp2,temp3,temp6;
	//char temp4[15],temp5,temp7[20];
	for(p=p->next ;p!=NULL;p=p->next )
	{
		for(q=p->next ;q!=NULL;q=q->next )
		{
			if(p->num >q->num )
			{
				/*temp=p->num ;
				p->num =q->num ;
				q->num =temp;
				temp1=p->birthday .year;
				p->birthday .year=q->birthday .year;
				q->birthday .year=temp1;
				temp2=p->birthday .month;
				p->birthday .month=q->birthday .month;
				q->birthday .month=temp2;
				temp3=p->birthday .day;
				p->birthday .day=q->birthday .day;
				q->birthday .day=temp3;
				strcpy(temp4,p->name) ;
				strcpy(p->name ,q->name );
				strcpy(q->name ,temp4);
				temp5=p->sex ;
				p->sex =q->sex ;
				q->sex =temp5;
				temp6=p->tele ;
				p->tele =q->tele ;
				q->tele =temp6;
				strcpy(temp7,p->address );
				strcpy(p->address ,q->address );
				strcpy(p->address ,temp7);*/
				temp=p;
				p=q;
				q=temp;   // 注释部分便于理解
			}
		}
	}
	return L;
}

/*函数功能:根据学生学号,查询该学生的信息*/
STUDENT* SearchNum(STUDENT *L,int e)
{
	STUDENT *p=L;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		if(p->num == e)
		{
			return p;
		}
	}
	return NULL;
}

/*函数功能:根据学生的姓名,查询该学生的信息*/

STUDENT* SearchName(STUDENT *L,char name1[])
{
	STUDENT *p=L;
	for(p=p->next ;p!=NULL;p=p->next )
	{
		if(strcmp(p->name ,name1) == 0)
		{
			return p;
		}
	}
	return NULL;
}


/*函数功能:根据学生的学号,删除该学生的信息*/
STUDENT* Delete(STUDENT *L ,int num1)
{
	STUDENT *p1,*p2;
	if(L==NULL)
	{
		printf("list is empty!\n");
	}
	p1=L;
	while(num1!=p1->num && p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if(num1==p1->num)
	{
		if(p1==L)
		{
			L=p1->next;
		}
		else
		{
			p2->next=p1->next;
		}
	}
	return L;
}

/*函数功能:在第i个位置插入一个学生的信息*/
STUDENT* InsertInfor(STUDENT *L,int i,STUDENT *stu1)
{
	STUDENT *p=L;
	int j=0;
	while(p!=NULL && j<i-1)
	{
		p=p->next ;
		++j;
	}
	if(!p || j>i-1)
	{
		printf("不在此范围内!\n");
	}
	stu1->next=p->next ;
	p->next = stu1;
	return L;
}
/*函数功能:将学生信息存入文件*/
void ReserveInfor(STUDENT *L)
{
	STUDENT *p;
	FILE *fp;
	p=L->next;
	if((fp=fopen("student.txt","w"))==NULL)
	{
		printf("Can not open the file!\n");
	}
	fprintf(fp,"number\tname\tsex\tbirth year\tbirth month\tbirth day\t\n");
	while(p)
	{
		fprintf(fp,"%d\t%s\t%c\t%d/%02d/%02d\t%d\t%s\n",p->num,p->name,p->sex,p->birthday.year,
			p->birthday.month,p->birthday.day,p->tele ,p->address );
		p=p->next;
	}
	fclose(fp);
}



若代码有错误的地方或是其他代码,请各位指教。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值