每日一练——线性表的顺序表示(数据结构复习)

// 简易学生管理系统
#include<bits/stdc++.h>
using namespace std;
class Student{
    private:
        int people;     // 记录学生人数
        string name[100];
        int age[100];
        int xuehao[100];
    public:
        Student(){
            people = 0;
        }
        void Add(int n);
        void Fix();
        int Delete();
        void Show();
};
void Menu(){
    cout << "******************" << endl
         << "   学生管理系统" << endl
         << "******************" << endl
         << "请选择功能:" << endl
         << "1.添加学生信息" << endl
         << "2.修改学生信息" << endl
         << "3.删除学生信息" << endl
         << "4.显示学生信息" << endl
         << "5.退出" << endl;
}
void Student::Add(int n){
    for(int i = people; i < people + n; i++){
        cout << "请添加学生的姓名:";
        cin >> name[i];
        cout << "请添加学生的年龄:";
        cin >> age[i];
        cout <<"请添加学生的学号(8位):";
        cin >> xuehao[i];        
    }
    people = people + n;
}
void Student::Fix(){
    string old_name;
    int n_1 = 0;
    cout << "请输入你要修改的学生的姓名:";
    cin >> old_name;
    for(int i = 0; i < people; i ++){
        if(old_name == name[i]){
            cout << "请输入修改后学生的姓名:";
            cin >> name[i];
            cout << "请输入修改后学生的年龄:";
            cin >> age[i];
            cout <<"请输入修改后学生的学号(8位):";
            cin >> xuehao[i];
        }
        else{
            n_1 ++;
        }
    }
    if(n_1 >= people) cout << "需要修改的名字不存在." << endl;
}
int Student::Delete(){
    string old_name;
    int n_2;    // 记录要删除的学生的位置
    cin >> old_name;
    for(int i = 0; i < people; i ++){
        if(old_name == name[i]){
            n_2 = i;
        }
    }
    for(int i = n_2; i < people-1; i ++){
        name[i] = name[i+1];
        age[i] = age[i+1];
        xuehao[i] = xuehao[i+1];
    }
    people --;
}
void Student::Show(){
    for(int i = 0; i < people; i ++){
        cout << "姓名:" << name[i] << endl
             << "年龄:" << age[i] << endl
             << "学号:" << xuehao[i] << endl;
    }
}
int main(){
    char num;
    Student stu;
    Menu();
    while(cin>> num){
        if(num == '1'){
            stu.Add(1);
            Menu();
        }
        else if (num == '2'){
            stu.Fix();
            Menu();  
        }
        else if(num == '3'){
            cout << "请输入你要删除的学生的姓名:";
            stu.Delete();
            Menu(); 
        }
        else if(num == '4'){
            stu.Show();
            Menu();
        }
        else if(num == '5'){
            cout << "You have return.";
            break;
        }
        else{
            cout << "Change your number in 1~5.";
            Menu();
        }      
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值