用C语言写一个班级管理系统,要求用到指针,结构体

2025-03-01 21:53:05
推荐回答(3个)
回答1:

/*用C语言写一个班级管理系统,要求用到指针,结构体
有姓名,出生日期,和性别等项目。
能输入、修改、查询、删除、按出生日期排序等功能从大到小*/
#include
#include
#include
typedef struct Date
{
int year;
int month;
int day;
}Date;
typedef struct stu
{
int id;
char name[20];
Date date;
char sex[5];
struct stu *next;
}stu;
int compare(Date d1,Date d2)/*比较两个日期的大小,以方便下面按年龄从大到小插入。即存入学生信息时,就是按顺序存入,故直接输出即可。*/
{
if(d1.year>d2.year)
{
return 1;
}
else if(d1.year {
return 0;
}
else
{
if(d1.month>d2.month)
{
return 1;
}
else if(d1.month {
return 0;
}
else
{
if(d1.day>d2.day)
{
return 1;
}
else if(d1.day {
return 0;
}
else
{
return 2;
}
}
}
}
//建表,即输入
void insert(stu *head,stu *temp)
{
stu *p,*q;
p=q=(stu*)malloc(sizeof(struct stu));
if(head->next==NULL)
{
temp->next=head->next;
head->next=temp;
}
else
{
p=q=(stu *)malloc(sizeof(struct stu));
q=head;
p=head->next;
while(p->next!=NULL&&compare(p->date,temp->date)==0)//要插入的学生年龄较小
{
q=p;
p=p->next;
}
if(p->next==NULL&&compare(p->date,temp->date)==0)/*当走到最后一个节点时,才找到要插入的学生年龄大于最后一个节点p->date,单独处理*/
{
temp->next=p->next;
p->next=temp;
}
else
{
temp->next=p;
q->next=temp;
}
}
}
//修改
void revise(stu *head,int num)
{
stu *p=(stu*)malloc(sizeof(struct stu));
p=head;
while(p->next!=NULL&&p->id!=num)
{
p=p->next;
}
if(p->next==NULL&&p->id!=num)//走到最后一个节点但是p->id!=num证明该生不在班级信息中
{
printf("没有找到要修改的学生的信息,该生不存在!\n");
}
else
{
int choose1;
while(1)
{
printf("请选择要修改的项:0 退出 1 修改id 2 修改姓名 3 修改年龄 4 修改 性别\n");
scanf("%d",&choose1);
switch(choose1)
{
case 0:
{
return;
}
case 1:
{
printf("输入想要修改的id号:");
int num1;
scanf("%d",&num1);
p->id=num1;
break;
}
case 2:
{
printf("输入想要修改的姓名:");
char name1[20];
scanf("%s",name1);
strcpy(p->name,name1);
break;
}
case 3:
{
printf("输入想要修改的出生日期:");
Date date1;
scanf("%d%d%d",&date1.year,&date1.month,&date1.day);
p->date.year=date1.year;
p->date.month=date1.month;
p->date.day=date1.day;
break;
}
case 4:
{
printf("输入想要修改的性别:");
char sex1[5];
scanf("%s",sex1);
strcpy(p->sex,sex1);
break;
}
}
}
}
}
//删除
void Delete(stu *head,int num)
{
stu *p,*q;
p=q=(stu*)malloc(sizeof(struct stu));
q=head;
p=head->next;
while(p->next!=NULL&&p->id!=num)
{
q=p;
p=p->next;
}
if(p->next==NULL&&p->id!=num)
{
printf("没有找到要修改的学生的信息,该生不存在!\n");
}
else
{
q->next=p->next;
delete p;
printf("已成功将该生信息删除!\n");
}
}
void display(stu *head)
{
stu *p=(stu*)malloc(sizeof(struct stu));;
p=head->next;
while(p!=NULL)
{
printf("学号:%d",p->id);
printf("姓名:%s ",p->name);
printf("年龄%d_%d_%d ",p->date.year,p->date.month,p->date.day);
printf("性别%s\n",p->sex);
p=p->next;
}
if(p==NULL)
{
printf("现在系统中还没有存入学生信息!\n");
}
}
void main()
{
stu *head=(stu *)malloc(sizeof(struct stu));
head->next=NULL;
int choose;
while(1)
{
printf("请选择您的操作: 0 退出 1 添加 2 修改 3 删除 4 查找\n");
scanf("%d",&choose);
switch(choose)
{
case 0:
{
return;
}
case 1:
{
stu*temp=(stu*)malloc(sizeof(struct stu));
printf("输入学号:");
scanf("%d",&temp->id);
printf("输入姓名:");
scanf("%s",temp->name);
printf("输入出生日期:");
scanf("%d%d%d",&temp->date.year,&temp->date.month,&temp->date.day);
printf("输入性别");
scanf("%s",temp->sex);
insert(head,temp);
break;
}
case 2:
{
printf("请输入想要修改的学生的学号:");
int num;
scanf("%d",&num);
revise(head,num);
break;
}
case 3:
{
printf("请输入想要删除的学生的学号:");
int num;
scanf("%d",&num);
Delete(head,num);
break;
}
case 4:
{
display(head);
break;
}
}
}
}
/*程序可能健壮性不很好,但只要按要求输入就可以了,其他的小地方你自己改吧,呵呵,希望对你有帮助*/

回答2:

用一个二维数组就可以了,你先试试,有疑问在找我 ,我QQ837333118

回答3:


你是中南的么
我是中南的
刚做了一个实习
和你的一模一样
我的QQ562888025
粘在这里乱码了
有空切磋下