1、学生信息管理系统
学生信息包括:学号、姓名、年龄、性别、出生年月、地址、电话、等。试设计一学生信息管理系统,使之能提供以下功能:
(1) 系统以(文本)菜单方式工作
(2) 学生信息录入功能(学生信息用文件保存)
(3) 学生信息浏览功能
(4) 查询和排序功能:按学号查询、按姓名查询等
(5) 学生信息的删除与修改
问题:1.在链表中,程序在运行期间只能输出一次最小的学号,而我要的是还能找出链表中剩下的数据中的最小,直到找到链表中的最后一个数据;
2.怎样给链表中的数据按学号排序。
麻烦各位了
#include<iostream>
using namespace std;
struct Student
{int number;
int age;
char name[20];
char sex ;
struct Day_birth
{int year;
int month ;
int day;
}Dbirth;
char adress[20];
char telephone[20];
char E_mail[20];
Student*next;
};
enum Sequence_number
{one=1,two,three,four,fire};
Sequence_number prive() //建立一个菜单输出函数
{int i;
Sequence_number Snumber;
cout<<"序号1--学生信息录入"<<endl;
cout<<"序号2--学生信息浏览"<<endl;
cout<<"序号3--学生信息查询和排序"<<endl;
cout<<"序号4--学生信息的删除和修改"<<endl;
cout<<"序号5--退出系统"<<endl;
cout<<"输入序号:"<<endl;
cin>>i;
Snumber =Sequence_number(i);
return Snumber; }
void date (Student *P1) // 建立一个数据输入函数
{//cout<<"学生学号: "<<endl;cin>>P1->number;
cout<<"学生姓名:"<<endl;cin>>P1->name;
cout<<"学生年龄:"<<endl;cin>>P1->age;
cout<<"学生性别: "<<endl;cin>>P1->sex;
cout<<"学生出生日:"<<endl;cin>>P1->Dbirth.year>>P1->Dbirth.month>>P1->Dbirth.day;
cout<<"学生地址:"<<endl;cin>>P1->adress;
cout<<"学生电话:"<<endl;cin>>P1->telephone;
cout<<"学生E-mail: "<<endl;cin>>P1->E_mail; }
void Information_prive(Student *head) //数据输出函数
{if(head!=NULL)
{ cout<<head->number<<" "<<head->name<<" "<<head->age<<" ";
cout<<head->Dbirth.year<<"."<<head->Dbirth.month<<"."<<head->Dbirth.day<<" "<<head->adress<<" "<<head->telephone[12]<<endl; }
else
cout<<"未输入数据,请重新选择"<<endl;
}
Student*Information_input() //学生信息录入的函数
{Student*head,*P1,*P2;
head=NULL;
P1=P2=new Student;
if(P1!=NULL)
{
cout<<"学生学号:"<<endl;
cin>>P1->number;
if(P1->number!=0)
{date(P1),head=P1;}
else
{delete P1;return head;}
}
else
return head;
while (1)
{
P1=new Student;
if(P1!=NULL)
{cout<<"学生学号:"<<endl; cin>>P1->number;
if(P1->number!=0)
{date(P1);P2->next=P1;P2=P1;}
else
{delete P1;P2->next=NULL;return head;}
}
}
}
void Information_browsing(Student*head) //学生信息浏览函数
{
if(head!=NULL)
{
Student*P=head;
cout<<"\n输出链表数据:\n"<<endl;
if(head !=NULL)
{do
{Information_prive(P);P=P->next;}while(P!=NULL);
}
else
cout<<"此链表为空!\n";
}
else
cout<<"未输入数据,请重新输入"<<endl;
}
Student*Query(Student*head) //学生信息的查询函数
{
if(head!=NULL)
{
Student*P,*P1;
P1=head;
cout<<"输入要查询的学生的学号"<<endl; cin>>P->number;
do {
if(P->number==P1->number)
{
Information_prive(P1);return P1;
}
else
P1=P1->next;
}while(P->number==P1->number);
}
else
;
}
Student*min_number(Student*head) //查找出链表中学号最小的函数
{
if(head!=NULL)
{
Student*P,*PN,*PS,*PL;
PL=head;P=PL;P=P->next;
while(1)
{
if(P!=NULL)
{
if(P->number>PL->number)
{
PS=P;P=P->next;
}
else if(P->number<PL->number)
{
PN=PL;PL=P;
{
if(PN->next!=PL)
PN=PS;
else
;
};
PS=P;P=PL->next;
}
else
cout<<"学生查询已到最后了"<<endl;
}
else
{
PN->next=PL->next;return PL;
}
}
}
else
return head;
}
Student* Sort(Student*head) //对信息进行排序的函数
{
Student*P1,*P2,*P3;
P1=min_number(head);
P2=head; head=P1; P1->next=P2;
while(P2!=NULL)
{
P3=min_number(P2);
if(P2->number!=P3->number)
{
P3->next=P1->next;
P1->next=P3;
P1=P3;
} else
{
P2=P2->next; P1=P1->next;
}
}
cout<<"学生信息已经按学号排好!"<<endl;
return head;
}
void Information_Delete_and_modify(Student*head) //学生信息的删除和修改的函数
{
if(head!=NULL)
{
Student*P1; P1=Query(head);
cout<<"重新输入学生的信息"<<endl;
date(P1);
}
else
cout<<"未输入数据,请重新输入"<<endl;
}
void main ()
{
Sequence_number prive();
void Information_Delete_and_modify(Student* head) ;
Student* Sort(Student* head);
Student*min_number(Student* PL);
Student*Query(Student*head) ;
void Information_browsing(Student*head);
Student*Information_input();
void Information_prive(Student *head);
void date (Student *P1);
Student*head=NULL;
Sequence_number Snumber;
while (1)
{
Snumber=prive();
switch (Snumber) {
case one:head=Information_input();break;
case two:Information_browsing(head);break;
case three:if(head!=NULL)
{
cout<<"学号最小的是:";
Information_prive(min_number(head));
head=Sort(head);
}
else
cout<<"未输入数据,请重新输入"<<endl; break;
case four:Information_Delete_and_modify(head);break;
case fire:exit(0);
}
}
}[b]
[解决办法]
第一个问题:没看懂问题。
第二个问题:你可以直接访问struct中的学号元素,进行比较!
例如:
struct student{ int num struct student *next;}//在函数中做个类似于这样的比较struct student *p1, *p2;int temp;while(p1->next != 0){ p2 = p1; p1->next = p1; if(p1->num > p2->num) { temp = p1->num; p1->num = p2->num; p2->num = temp; }}/*而其他的元素也可以按照这样的形式进行交换...当然,你也可以比较num,进而把*next的地址进行改变*/