- 为什么这个学生管理系统只有添加功能正确,但插入和其他功能都不能使用呢?
- 该怎样修改
- 如何让初始信息从文件中调用,更新后存入一新文件中
#include<iostream>
#include<stdlib.h>
using namespace std;
int k=1;
struct student{
int num;
char name[30];
struct student *next;
};
class stu{
private:
struct student *head,*tail;
public:
stu(){
head=new student;
head->num=0;
head->name[0]='\0';
};
void create(int n);
void print();
int Insert();
void del();
void destroy();
};
void stu:: create(int n){
struct student *p;
head->next=NULL;
tail=head;
for(int i=0;i<n;i++){
p=new student;
cout<<"input num,name"<<endl;
cin>>p->num>>p->name;
cout<<endl;
p->next=NULL;
tail->next=p;
tail=p;
}
}
void stu::print()
{
struct student *p;
p=head->next;
if(p==NULL){
cout<<"kongbiao"<<endl;
return;
}
while(p!=NULL){
cout<<p->num<<'\t'<<p->name<<endl;
p=p->next;
}
}
int stu::Insert(){
system("cls");
cout<<"请输入要在哪个学号后面插入信息"<<endl;
int sno;
cin>>sno;
struct student *newp=new student,*p;
cout<<"请输入要插入的信息"<<endl;
cin>>newp->num>>newp->name;
p=head->next;
while(p!=NULL){
if(p->num==sno){
newp->next=p->next;
p->next=newp;
break;
}
p=p->next;
}
return 0;
}
void stu::del(){
cout<<"请输入要删除的学号"<<endl;
int sno;
cin>>sno;
struct student *p,*pre;
p=head->next;
pre=head;
while(p!=NULL){
if(p->num==sno){
pre->next=p->next;
delete p;
break;
}
pre=p;
p=p->next;
} }
void stu::destroy(){
struct student *p,*ptr;
p=head;
while(p!=NULL){
ptr=p;
p=ptr->next;
delete ptr;
}
}
void menu()/* 界面*/
{
cout<<"1.添加"<<endl;
cout<<"2.打印"<<endl;
cout<<"3.插入"<<endl;
cout<<"4.删除"<<endl;
cout<<"5.销毁"<<endl;
cout<<"6.退出"<<endl;
int num;
stu STU;
cout<<"请输入你的选择"<<endl;
cin>>num;
switch(num){
case 1:
int n;
cout<<"请输入个数"<<endl;
cin>>n;
STU.create(n);
STU.print();
break;
case 2:
STU.print();
break;
case 3:
STU.Insert();
STU.print();
break;
case 4:
STU.del();
STU.print();
break;
case 5:
STU.destroy();
STU.print();
break;
case 6:
k=0;
cout<<"即将退出程序!"<<endl;
break;
default:
cout<<"请在1~6之间选择"<<endl;
break;
}
}
int main(){
while(k){
menu();
}
system("pause");
return 0;}
OK·1
输入要在哪个学
请输入要插入的信息
wang
exited after 18.23 seconds with retul
品评器
编译日志
V调试按术结果
错误 关门
评时间:
0.55
在这里输入你要搜索的内容
已选择
行歌
1.