📄 c语言版学生档案管理.cpp
字号:
#include "stdio.h"
#include "malloc.h"
#include "iostream.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
int score;
char sex;
struct student *next;
};
int n; //全局变量表示链表长度
struct student *creat(void) //创建学生信息链表
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d %s %d %c",&p1->num,p1->name,&p1->score,&p1->sex);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc (LEN);
scanf("%d %s %d %c",&p1->num,p1->name,&p1->score,&p1->sex);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) //在屏幕上显示所创建的链表
{struct student *p;
printf("学生信息如下:\n",n);
p=head;
if(head!=NULL)
printf("NO. Name Score Sex\n");
while(p!=NULL)
{printf("%-7d %-10s %-8d %-5c\n",p->num,p->name,p->score,p->sex);
p=p->next;
}
}
struct student *dele(struct student *head) //删除记录
{struct student *p1,*p2;
int num1;
p1=head;
printf("输入要删除的学生学号:");
scanf("%d",&num1);
while(num1!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num1==p1->num)
{if(p1==head) head=p1->next;
else
p2->next=p1->next;
printf("学号为%d的学生信息已经被删除\n",p1->num);
free(p1);
n=n-1;
}
else printf("您所要删除的学生信息不存在");
return(head);
}
struct student *insert(struct student *head,struct student *stud) //插入节点
{struct student *p0,*p1,*p2;
p1=head;
p0=stud;
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
if(p0->num==p1->num) printf("此学号的学生信息已经存在\n");
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
void main()
{ struct student *head,*stu;int r,ch;
cout<<"|----------------------------数据库课程设计----------------------------------|"<<endl;
cout<<endl;
cout<<"|-----------------------计算机02-2班 于勇 10号------------------------------|"<<endl;
cout<<endl;
cout<<"|---------------------------学生档案管理系统---------------------------------|"<<endl;
cout<<"|----------------------------------------------------------------------------|"<<endl;
printf("请输入创建档案信息(学生的学号、姓名、成绩、性别):\n");
head=creat(); //创建学生档案
printf("选择要进行的操作:1.显示学生信息 2.删除记录 3.插入新记录 4.退出\n");
scanf("%d",&ch);
while(ch!=4)
{
switch(ch)
{case 1:print(head);break; //打印创建的学生信息
case 2: {head=dele(head); //删除某一记录
printf("删除后");
print(head);
printf("要继续删除操作吗1......yes 0......no?\n");
scanf("%d",&r);
if(r==1)
{head=dele(head);print(head);}
else if(r==0);
else printf("无效输入");} break;
case 3:{printf("输入要插入的学生信息:");
stu=(struct student*)malloc(LEN);
scanf("%d %s %d %c",&stu->num,stu->name,&stu->score,&stu->sex);
while(stu->num!=0)
{head=insert(head,stu);
print(head);
printf("输入下一条要插入的记录信息:");
stu=(struct student*)malloc(LEN);
scanf("%d %s %d %c",&stu->num,stu->name,&stu->score,&stu->sex);}
} break;
default:printf("无效输入 ");
}
printf("选择要进行的操作:1.显示学生信息 2.删除记录 3.插入新记录 4.退出\n");
scanf("%d",&ch);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -