C语言学生管理系统

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

typedef struct Student{
	char name[20];
	int  age;
	int  stunum;
	int  score;
}Student;
typedef struct LNode{
    Student stu;
    LNode* Next;
}LNode;

LNode* Ptrl = NULL;

void Panel();
void Input();
void Printout();
void Find();
void Modify();
void Delete();
void Count();
void Save();
void Read();

int main()
{
	while(1){
		Panel();
		char cmp = getch();
		switch (cmp){
			case '1':
				Input();
				break;
			case '2':
				Printout();
				break;
			case '3':
				Find();
				break;
			case '4':
				Modify();
				break;
			case '5':
				Delete();
				break;
			case '6':
				Count();
				break;
			case '7':
				Save();
				break;
			case '8':
				Read();
				break;
			case '0':
				printf("The system is over!\n");
				return 0;
		}
	}
	return 0;
}

void Panel()
{
	printf("***********************\n");
	printf("******功能清单如下*****\n");
	printf("*** [1]录入学生信息 ***\n");
	printf("*** [2]打印学生信息 ***\n");
	printf("*** [3]查找学生信息 ***\n");
	printf("*** [4]修改学生信息 ***\n");
	printf("*** [5]删除学生信息 ***\n");
	printf("*** [6]统计学生人数 ***\n");
	printf("*** [7]保存学生信息 ***\n");
	printf("*** [8]读入学生信息 ***\n");
	printf("*** [0]退出学生系统 ***\n");
	printf("***********************\n");
}


void Input()
{
	LNode* p = (LNode*)malloc(sizeof(LNode));
	p->Next  = NULL;
	if(Ptrl == NULL){
		Ptrl = p;
	}
	else{
		p->Next = Ptrl;
		Ptrl    = p;
	}
    printf("Please input student's name:\n");
    scanf("%s",p->stu.name);
    printf("Please input student's age:\n");
    scanf("%d",&p->stu.age);
    printf("Please input student's studentnumber:\n");
    scanf("%d",&p->stu.stunum);
    printf("Please input student's score:\n");
    scanf("%d",&p->stu.score);
    printf("The operation is finished.\n");
    system("pause");
    system("cls");
}

void Printout()
{
	LNode* p = Ptrl;
	if(p != NULL){
		while(p != NULL){
		printf("**************************************\n");
		printf("name\tage\tstudentnumber\tscore\n");
		printf("%s\t%d\t%d\t\t%d\n",
			p->stu.name,
			p->stu.age,
			p->stu.stunum,
			p->stu.score);
		p = p->Next;
		}
		printf("**************************************\n");
	}
	else{
		printf("Empty!\n");
	}
	system("pause");
	system("cls");
}

void Find()
{
	char name[20];
	int stunum;
	LNode* p=Ptrl;

	printf("Please input the name of the student which you want to find.\n");
	scanf("%s",name);
	printf("Please input the studentnumber of the student which you want to find.\n");
	scanf("%d",&stunum);
	while(p != NULL){
		if(p->stu.stunum == stunum || 0 ==strcmp(p->stu.name,name)){
			printf("name\tage\tstudentnumber\tscore\n");
			printf("%s\t%d\t%d\t\t%d\n",
				p->stu.name,
				p->stu.age,
				p->stu.stunum,
				p->stu.score);
				break;
		}
		p = p->Next;
	}
	if(p == NULL){
		printf("This information does not exist.\n");
	}
	system("pause");
	system("cls");
}

void Modify()
{
	int stunum;
	LNode* p = Ptrl;
	
	printf("Please input the studentnumber of the student which you want to modify.\n");
	scanf("%d",&stunum);
	while(p != NULL){
		if(p->stu.stunum == stunum){
			printf("The changed name:\n");
			scanf("%s",p->stu.name);
			printf("The changed studentname:\n");
			scanf("%d",&p->stu.stunum);
			printf("The changed score:\n");
			scanf("%d",&p->stu.score);
			break;
		}
		p = p->Next;
	}
	if(p == NULL){
		printf("This information does not exist.\n");
	}
	system("pause");
	system("cls");
}

void Delete()
{
	int stunum;
	LNode* d1;
	LNode* d2;
	
	printf("Please input the studentnumber of the student which you want to delete.\n");
	scanf("%d",&stunum);
	if(Ptrl->stu.stunum == stunum){
		d1 = Ptrl;
		Ptrl = Ptrl->Next;
		free(d1);
		printf("The operation is finished.\n");
		system("pause");
		system("cls");
	}
	else{
		LNode*	p = Ptrl;
		while(p->Next != NULL){
			if(p->Next->stu.stunum == stunum){
				d2 = p->Next;
				p->Next = p->Next->Next;
				free(d2);
				printf("The operation is finished.\n");
			}
		}
		if(p == NULL){
			printf("This information does not exist.\n");
		}
		system("pause");
		system("cls");
	}
}

void Count()
{
	LNode* p = Ptrl;
	int count=0;
	
	while(p != NULL){
		p = p->Next;
		count++;
	}
	printf("The total number of the students is %d\n",count);
	system("pause");
	system("cls");
}

void Save()
{
	FILE* fp = fopen("D:\\text2.txt","w");
	LNode* p;
	
	if(fp == NULL){
		printf("Failed to open file.\n");
		system("pause");
		system("cls");
		return;
	}
	p = Ptrl;
	while(p != NULL){
		fwrite(&p->stu,sizeof(Student),1,fp);
		p = p->Next;
	}
	fclose(fp);
	printf("Data is successfully saved.\n");
	system("pause");
	system("cls");
}

void Read()
{
	FILE* fp = fopen("D:\\text2.txt","r");
	LNode* p;
	
	if(fp == NULL){
		printf("Failed to open file.\n");
		system("pause");
		system("cls");
		return;
	}
	Student stu;
	while(fread(&stu,sizeof(Student),1,fp)){
		p = (LNode*)malloc(sizeof(LNode));
		p->Next == NULL;
		memcpy(p,&stu,sizeof(Student));
	}
	if(Ptrl == NULL){
		Ptrl = p;
	}
	else{
		p->Next = Ptrl;
		Ptrl = p;
	}
	fclose(fp);
	printf("Data is successfully loaded.\n");
	system("pause");
	system("cls");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值