简单的学生管理系统:C++描述

简单的学生管理系统

#include<iostream>
using namespace std;
#include<string>
// 联系人结构体
struct Person{
    string name;
    int sex;
    int age;
    int phone;
    string address;
};
// 通讯录结构体
#define MAX 100 //最多存放100人
struct Addressbooks{
    Person personArray[MAX];
    // 记录当前通讯录的人员个数。
    int size;
};
// 展示菜单
void showMenu(){
    cout<<"**********************"<<endl;
    cout<<"****1,添加联系人*******"<<endl;
    cout<<"****2,显示联系人*******"<<endl;
    cout<<"****3,删除联系人*******"<<endl;
    cout<<"****4,查找联系人*******"<<endl;
    cout<<"****5,修改联系人*******"<<endl;
    cout<<"****6,清空联系人*******"<<endl;
    cout<<"****0,退出     ********"<<endl;
    cout<<"**********************"<<endl;
}

// 添加联系人
void addperson(Addressbooks * abs){
    if (abs->size==MAX){
        cout<<"通讯录已满,无法添加"<<endl;
        return ;
    }
    else{
        // 输入姓名
        string name;
        cout<<"请输入姓名: "<<endl;
        cin>>name;
        abs->personArray[abs->size].name=name;

        // 输入性别
        int sex;
        cout<<"请输入性别,输入为1是男生,输入为2是女生"<<endl;
        // 如果输入的是1或或者2,那么退出循环,因为这个是正确的值
        while(true){
            cin>>sex;
            if(sex==1||sex==2){
                abs->personArray[abs->size].sex=sex;
                break;
            }
            cout<<"输入有误,请重新输入"<<endl;
        }

        // 输入年龄
        cout<<"请输入年龄"<<endl;
        int age;
        cin>>age;
        abs->personArray[abs->size].age=age;

        // 输入电话
        cout<<"请输入电话"<<endl;
        int tele;
        cin>>tele;
        abs->personArray[abs->size].phone=tele;

        // 输入住址
        cout<<"请输入住址"<<endl;
        string address;
        cin>>address;
        abs->personArray[abs->size].address=address;

        // 更新人数
        abs->size++;
        cout<<"添加成功"<<endl;

        system("pause");
        system("cls");


    }
}

// 显示联系人
void showPerson(Addressbooks * abs){
    // 判断人数是否为0
    if (abs->size==0){
        cout<<"当前的记录为空"<<endl;
    }else{
        for(int i=0;i<abs->size;i++){
            cout<<"name= "<<abs->personArray[i].name<<"\t";
            cout<<"sex= "<<abs->personArray[i].sex<<"\t";
            cout<<"age= "<<abs->personArray[i].age<<"\t";
            cout<<"phone= "<<abs->personArray[i].phone<<"\t";
            cout<<"address= "<<abs->personArray[i].address<<endl;
        }
    }
    system("pause");
    system("cls");
}

// 检查联系人是否存在
int isExist(Addressbooks *abs,string name){
    for(int i=0;i<abs->size;i++){
        if(abs->personArray[i].name==name){
            return i;
            // 返回在数组中的下标编号
        }
    }
    return -1;
}

// 删除联系人
void deletPerson(Addressbooks * abs){
    cout<<"请输入你要删除的联系人的姓名"<<endl;
    string name;
    cin>>name;
    int ret=isExist(abs,name);
    if (ret==-1)
    {
        cout<<"联系人并不存在"<<endl;
    }else{
        for(int i=ret;i<abs->size;i++){
            // 数据前移动
            abs->personArray[i]=abs->personArray[i+1];
        }
        abs->size--;
        cout<<"删除成功"<<endl;

    }
    

    if (abs->size==0){
        cout<<"并没有存储着任何联系人"<<endl;
        
    }
    system("pasue");
    system("cls");
}

// 查找联系人
void findPerson(Addressbooks * abs){
    string name;
    cout<<"请输入你要查找的联系人"<<endl;
    cin>>name;

    int ret=isExist(abs,name);
    if(ret!=-1){
        cout<<"name= "<<abs->personArray[ret].name<<"\t";
        cout<<"sex= "<<abs->personArray[ret].sex<<"\t";
        cout<<"age= "<<abs->personArray[ret].age<<"\t";
        cout<<"phone= "<<abs->personArray[ret].phone<<"\t";
        cout<<"address= "<<abs->personArray[ret].address<<endl;

    }
    else{
        cout<<"未找到联系人"<<endl;
    }
    system("pasue");
    system("cls");

}

// 修改联系人
void modfiyPerson(Addressbooks * abs){
    cout<<"请输入你要修改的联系人"<<endl;
    string name;
    cin>>name;
    
    int ret=isExist(abs,name);
    if (ret!=-1){
        cout<<"请输入你要修改的值"<<endl;
        cout<<"name= "<<endl;
        cin>>name;
        abs->personArray[ret].name=name;

        cout<<"sex= "<<endl;
        int sex;
        while(true){
            cin>>sex;
            if(sex==1||sex==2){
                abs->personArray[abs->size].sex=sex;
                break;
            }
            cout<<"输入有误,请重新输入"<<endl;
        }

        cout<<"age= "<<endl;
        int age; cin>>age;
        abs->personArray[ret].age=age;

        cout<<"phone= "<<endl;
        int phone; cin>>phone;
        abs->personArray[ret].phone=phone;

        cout<<"address= "<<endl;
        int address; cin>>address;
        abs->personArray[ret].address=address; 
    }
    else{
        cout<<"未找到联系人"<<endl;
    }
    system("pasue");
    system("cls");

}

// 清空联系人
void cleanPerson(Addressbooks *abs){
    abs->size=0;
    cout<<"通讯录已经清空"<<endl;
    system("pause");
    system("cls");
}


int main(){
    // 创建选项输入数字
    int select=0;
    // 创建通讯录结构体变量
    Addressbooks abs;
    // 初始化通讯录当前人数的个数
    abs.size=0;
    while(true){
        showMenu();
        cin>>select;
        switch (select)
        {
            case 1:
                // 修改实参
                addperson(&abs);
                break;
            case 2:
                showPerson(&abs);
                break;
            case 3:
                deletPerson(&abs);
                break;
            case 4:
                findPerson(&abs);
                break;
            case 5:
                modfiyPerson(&abs);
                break;
            case 6:
                cleanPerson(&abs);
                break;
            case 0:
                cout<<"欢迎下次使用"<<endl;
                system("pause");
                return 0;
                break;
            default:
                break;
        }
    }
}
posted @ 2021-04-24 10:54  Zeker62  阅读(67)  评论(0编辑  收藏  举报