c语言学生信息管理系统

2025-03-06 19:23:50
推荐回答(1个)
回答1:

Ixiaofei423#include
#include
#include
#include
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long int num;
char name[20];
int age;
char sex[4];
char address[30];
long int tele_num;
char E_mail[20];
struct student *next;
};int TOTAL_NUM = 0;
struct student *head = NULL;
void record();
void insert(struct student *stu);
void display(struct student *stu);
void displayAll();
void query();
void query_by_num();
void freeAll();
void devise(struct student *p);
//系统主菜单
void main()
{
int choice;
choice = -1;
do
{
printf("\n\n\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ 学生信息管理系统 |\n");
printf("\t\t\t--------------------------------\n");
printf("\t\t\t+ [1]----录入学生信息 |\n");
printf("\t\t\t+ [2]----浏览学生信息 |\n");
printf("\t\t\t+ [3]----查询学生信息 |\n");

printf("\t\t\t+ [0]----退出系统 |\n");
printf("\t\t\t+*·*·*·*·*·*·*·*·*·*·|\n");
printf("\t\t\t--------------------------------\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch(choice)
{
case 0:
freeAll();
break;
case 1:
record();
break;
case 2:
displayAll();
break;
case 3:
query();
break;
default:
printf("\n无效选项!");
break;
}
}
while(choice != 0);
}
//录入学生信息
void record()
{
int i=0;
struct student *p0,*head;
char a;
do
{
p0 = (struct student *)malloc(LEN);
printf("请输入学生的学号:");
scanf("%ld",&p0->num);
fflush(stdin);
printf("请输入学生的姓名:");
scanf("%s",p0->name);
fflush(stdin);
printf("请输入学生的年龄:");
scanf("%d",&p0->age);
fflush(stdin);
printf("请输入学生的性别:");
scanf("%s",p0->sex);
fflush(stdin);
printf("请输入学生的籍贯:");
scanf("%s",p0->address);
fflush(stdin);
printf("请输入学生的电话:");
scanf("%ld",&p0->tele_num);
fflush(stdin);
printf("请输入学生的E-mail:");
scanf("%s",p0->E_mail);
fflush(stdin);printf("\n是否继续输入另外一个学生信息?(y/n)");
a=getchar();
i++;
}while(a=='y' && i<=50);
TOTAL_NUM=i;
head=p0;
} void insert(struct student *stu)
{
struct student *p0, *p1, *p2;

p1 = head;
p0 = stu;

if(head == NULL)
{
head = p0;
p0->next = NULL;
}
else
{
while((p0->num > p1->num)&&(p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if(p0->num <= p1->num)
{
if(head == p1)
head = p0;
else
p2->next = p0;
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
TOTAL_NUM++;
}void display(struct student *p)
{
printf("%ld\t%s\t%d\t%s\t%s\t%ld\t%s\n", p->num, p->name, p->age, p->sex,p->address, p->tele_num, p->E_mail);
}
//浏览学生信息
void displayAll()
{
struct student *p; printf("学生总数:%d\n", TOTAL_NUM);

if(head != NULL)
{
do
{
printf("%10d%10s%10d%10s%10s\n",p->num,p->name,p->age,p->sex,p->E_mail);
p=p->next;
} while(p!=NULL); }
printf("\n");
}
//查询学生信息
void query()
{
int choice;
choice = -1;
do
{
printf("\n");
printf("+--------------------+\n");
printf("| 按学号查询 请按 1 |\n");

printf("| 取消 请按 0 |\n");
printf("+--------------------+\n"); printf("请输入您的选择:");
scanf("%d", &choice);

switch(choice)
{
case 0:
return;
case 1:
query_by_num();
break;

default:
printf("\n无效选项!");
break;
}
}
while(choice != 0);
}
//按学号查询学生信息
void query_by_num()
{
int num;
struct student *p1; printf("请输入学生的学号:");
scanf("%ld", &num);
if(head==NULL)
{
printf("无学生记录!\n");
return;
}
p1=head;
while(num!=p1->num && p1->next!=NULL)
p1=p1->next;
if(num == p1->num)
{
printf("\n学号\t姓名\t年龄\t性别\t出生年月\t地址\t电话\tE-mail\n");
printf("-------------------------------------------------------------------------------");
display(p1);
}
else
printf("没有该学生记录,请核对!");
} void freeAll()
{
struct student *p1, *p2; p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}