#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
using namespace std;
#include <io.h>
#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;
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 del_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+
[4]----删除学生信息
+\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_by_num();
break;
case 4:
del_by_num();
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);
insert(p0);
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);
}
//浏览学生信息
void displayAll()
{
struct student *p;
printf("学生总数:%d\n", TOTAL_NUM);
if(head != NULL)
{ p=head;//少了这个
do
{
printf("%10d%10s%10d%10s%10s\n",p->num,p->name,p->age,p->sex);
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
\n");
printf("-------------------------------------------------------------------------------
");
display(p1);
}
else
printf("没有该学生记录,请检查!");
}
void
del_by_num()// 定义数据删除函数 //
{
int num;
struct student *p1;
printf("请输入要删除的学生学号:");
scanf("%ld", &num);
if(num==p1->num)
{
p1=head;
while(num!=p1->num && p1->next!=NULL)
p1=p1->next;
p1=head;
printf("删除成功!%ld\n",num);
display(p1=NULL);
num=num-1;
}
else
cout<<"输出无效"<<endl;
}
void freeAll()
{
struct student *p1, *p2;
p1=p2=head;
while(p1)
{
p2=p1->next;
free(p1);
p1=p2;
}
}