通讯录 by C++

#include<iostream>
using namespace std;
#include<string>
#define max 1000
struct person{
    string name;
    string age;
    string tel;
    string address;
};
struct addrbook{
    int size ;
    struct person abs[max];
};


void printmenu(){
    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(struct addrbook * a){
    cout << "请输入姓名:" << endl;
    cin >> a->abs[a->size].name;
    cout << "请输入年龄: " << endl;
    cin >> a->abs[a->size].age;
    cout << "请输入电话: " << endl;
    cin >> a->abs[a->size].tel;
    cout << "请输入住址: " << endl;
    cin >> a->abs[a->size].address;
    a->size++;
    cout << "添加成功!" << endl;
    system("pause");
    system("cls");
}
void displayper(struct addrbook * a){
    if(a->size == 0){
    	cout << "********通讯录为空********" << endl;
    	system("pause");
		system("cls"); 
		return; 
	}
    cout << "*********通讯录*********" << endl;
    for(int i = 0;i < a->size;i++){
        cout << "姓名:"<< a->abs[i].name << endl;
        cout << "\t年龄:"<< a->abs[i].age << endl;
        cout << "\t电话:"<< a->abs[i].tel << endl;
        cout << "\t住址:"<< a->abs[i].address << endl;
        cout << "************************" << endl;
    }
    system("pause");
    system("cls");
} 
int checkbook(struct addrbook * a){
    string name;
    cin >> name;
    for(int i = 0;i < a->size;i++){
        if(a->abs[i].name == name){
            return i;
        }
    }
    return -1;
}
void deleteperson(struct addrbook * a){
    cout << "请输入要删除的联系人的姓名: " << endl;
    int ret = checkbook(a);
    if(ret == -1){
        cout << "查无此人!" << endl;
        system("pause");
        system("cls");
        return;
    }
    for(int i = ret;i < a->size-1;i++){
        a->abs[i] = a->abs[i+1];
    }
    a->size--;
    cout << "删除成功!" << endl;
    system("pause");
    system("cls");
    return;
}
void findperson(struct addrbook * a){
    cout << "请输入要查找的联系人的姓名: " << endl;
    int ret = checkbook(a);
    if(ret == -1){
        cout << "查无此人" << endl;
        system("pause");
        system("cls");
        return;
    }
    cout << "查找成功! 信息如下:" << endl;
    cout << "姓名 :" << a->abs[ret].name << endl;
    cout << "\t年龄:"<< a->abs[ret].age << endl;
    cout << "\t电话:"<< a->abs[ret].tel << endl;
    cout << "\t住址:"<< a->abs[ret].address << endl;
    system("pause");
    system("cls");
    return;
}
void modifybook(struct addrbook * a){
    cout << "请输入要修改的联系人的姓名: " << endl;
    int ret = checkbook(a);
    if(ret == -1){
        cout << "查无此人" << endl;
        system("pause");
        system("cls");
        return;
    }
    cout << "请输入姓名:" << endl;
    cin >> a->abs[ret].name;
    cout << "请输入年龄: " << endl;
    cin >> a->abs[ret].age;
    cout << "请输入电话: " << endl;
    cin >> a->abs[ret].tel;
    cout << "请输入住址: " << endl;
    cin >> a->abs[ret].address;
    cout << "修改成功!" << endl;
    system("pause");
    system("cls");
    return;
}
int main(){
    int opr;
    struct addrbook txl;
    txl.size = 0;
    while(1){
        printmenu();
        scanf("%d",&opr);
        switch(opr){
            case 1:
                addperson(&txl);
                break;
            case 2:
                displayper(&txl);
                break;
            case 3:
                deleteperson(&txl);
                break;
            case 4:
                findperson(&txl);
                break;
            case 5:
                modifybook(&txl);
                break;
            case 6:
                if(txl.size == 0){
                    cout << "您的通讯录已为空,无法进行清空操作" << endl;
                    system("pause");
                    system("cls");
                    break;
                }
                cout << "您确定要清空通讯录中的所有信息吗?(无法找回)" << endl;
                cout << "1.确定" << endl <<"2.取消" << endl;
                int opr;
                cin >> opr;
                if(opr == 2){
                    break;
                }
                txl.size = 0;
                cout << "已清空!" << endl;
                system("pause");
                system("cls");
                break;
            case 0:
                cout << "欢迎下次使用!" << endl;
                return 0;
            default:
                cout << "输入有误,请重试" << endl;
			    system("pause");
				system("cls"); 
				break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaozi_512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值