C++——小案例:学生通讯录

发布时间:2024年01月10日

利用C++来实现一个通讯录管理系统

系统中需要实现的功能如下:

- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <string>

using namespace std;

typedef enum Gender{
    male,female
} gender;

typedef struct Student {
    string name;
    int age;
    Gender gender;
    string phone_number;
} student;

#define NUM 100 //student arry total number
typedef struct Addressbook{
    student student_arr[NUM];
    int size; // how many students present in addressbook

} addressbook;


void add_student(addressbook * abs){
    string name;
    int age;
    gender sex;
    string phone_number;

    if (abs->size==NUM) {
        cout << "addressbook is full "<<endl;
        return;
    }
    // add student info
    //name
    cout << "name" << endl;
    cin >> name;
    abs->student_arr[abs->size].name=name;
    //age
    cout << "age" << endl;
    cin >> age;
    abs->student_arr[abs->size].age=age;
    //gender
    cout <<"0-male,1-female"<<endl;
    scanf("%d",&sex);
    if (sex==0||sex==1) {
        abs->student_arr[abs->size].gender=sex;
    }else {
        cout <<"0-male,1-female"<<endl;
        scanf("%d",&sex);
        while (1) {
            if (sex==0||sex==1) {
                abs->student_arr[abs->size].gender=sex;
                break;
        }
    }
    }
    //phone_number
    cout <<"phone_number"<<endl;
    cin >> phone_number;
    abs->student_arr[abs->size].phone_number=phone_number;

    abs->size++;
    cout <<"add successful ! "<<endl;
    system("clear"); // clear screen

}
void show_student(addressbook * abs){
    if (abs->size==0) {
        cout <<"no any student"<<endl;
    }else {
        for (int i=0; i<=abs->size; i++) {
            cout <<"name:"<< abs->student_arr[i].name <<endl;
            cout <<"age:"<< abs->student_arr[i].age <<endl;
            cout <<"sex:"<< abs->student_arr[i].gender <<endl;
            cout <<"phone_number:"<< abs->student_arr[i].phone_number <<endl;
        }
        
    }
    
}
int isExist(addressbook * abs,string name){
    for (int i=0; i<abs->size; i++) {
        if (abs->student_arr[i].name==name) {
            return i;
        }
    }
    return -1;

}
void find_student(){
    
}
void modify_student(){
    
}
void delete_student(){
    
}
void delete_all_students(){
    
}

void showmenu(){

    cout << "***************************" <<endl;
    cout << "1.add student"<< endl;
    cout << "2.show student"<< endl;
    cout << "3.find student"<< endl;
    cout << "4.modify student"<< endl;
    cout << "5.delete student"<< endl;
    cout << "6.delete  all students" << endl;
    cout << "7.exit" << endl;
    cout << "***************************"  <<endl;
}

int main() {
    int select=0;
    // create addressbook struct veriale
    addressbook ads;
    // 
    ads.size=0;
    
    while (1) {
        showmenu();
        cin >> select;
        switch (select) {
        case 1 :
            add_student(&ads);
            break;
        case 2 :
            show_student(&ads);
            break;
        case 3 :
            find_student();
            break;
        case 4 :
            modify_student();
            break;
        case 5 :
            delete_student();
            break;
        case 6 :
            delete_all_students();
            break;
        case 7 :
            goto EXIT;
        default:
            break;
        }
    
    }
    EXIT:
        cout << "thanks for using!" << endl;
    return 0;
}

文章来源:https://blog.csdn.net/weixin_46157873/article/details/135509776
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。