好难好难啊 2021-12-16 19:09 采纳率: 33.3%
浏览 63
已结题

关于学生信息管理系统的问题C++

  1. 为什么这个学生管理系统只有添加功能正确,但插入和其他功能都不能使用呢?
  2. 该怎样修改
  3. 如何让初始信息从文件中调用,更新后存入一新文件中
#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;}


img

img

    OK·1
   输入要在哪个学
  请输入要插入的信息
    wang
           exited after 18.23 seconds with retul
品评器
                编译日志
                        V调试按术结果
                   错误 关门
                   评时间:
                             0.55
        在这里输入你要搜索的内容
                  已选择
                               行歌

1.

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-12-17 14:11
    关注

    把void stu::create(int n) 函数修改下,改为从文件里载入链表就可以了,其余的修改如下,供参考:

    #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 struct student; //修改
            head->num = 0;
            head->name[0] = '\0';
            head->next = NULL;  //修改
        };
        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 struct student;  //修改
            cout << "input num,name" << endl;
            cin >> p->num >> p->name;
            cout << endl;
            p->next = NULL;
            tail->next = p;
            tail = p;
        }
    }
    void stu::print()
    {
        if (head->next == NULL) { //修改
            cout << "kongbiao" << endl;
            return;
        }
        struct student* p;
        p = head->next;
        while (p != NULL) {
            cout << p->num << '\t' << p->name << endl;
            p = p->next;
        }
    }
    int stu::Insert() {
        system("cls");
        if (head->next == NULL) { //修改
            cout << "kongbiao" << endl;
            return -1;
        }
        cout << "请输入要在哪个学号后面插入信息" << endl;
        int sno;
        cin >> sno;
        struct student* p = head->next;
        while (p != NULL){ 
            if (p->num == sno) {
                struct student* newp = new struct student; //修改 
                newp->next = NULL;                      
                cout << "请输入要插入的信息:num,name" << endl;
                cin >> newp->num >> newp->name;
                newp->next = p->next; 
                p->next = newp;
                break;
            }
            p = p->next;
        }
        if (p == NULL)                                   //修改
            printf("学号:%d 记录未找到,插入未成功!\n", sno);
        return 0;
    }
    void stu::del() {
        if (head->next == NULL) {       //修改
            cout << "kongbiao" << endl;
            return;
        }
        struct student* p, * pre;
        p = head->next;
        pre = head;
        cout << "请输入要删除的学号" << endl;
        int sno;
        cin >> sno;
        while (p != NULL) {
            if (p->num == sno) {
                pre->next = p->next;
                delete p;
                printf("学号:%d 记录删除成功!\n", sno);//修改
                return;  //break;                        //修改
            }
            pre = p;
            p = p->next;
        }
        if (p == NULL)                                  //修改
            printf("学号:%d 记录未找到,删除未成功!\n", sno);
    }
    void stu::destroy() {
        struct student* p, * ptr;
        //p = head;             //修改
        while (head->next != NULL) {
            ptr = head->next;
            head->next = ptr->next;
            delete ptr;
        }
    }
    void menu(stu STU)/* 界面*/   //修改
    {
        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() 
    {
        stu STU;    //修改
        while (k) {
            menu(STU);
        }
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月19日
  • 已采纳回答 12月17日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程