C++案例
热度: loading...
从C到C++的过渡案例
源码
#include<iostream>
#include<string>
using namespace std;
#define Max 1000
//联系人结构体
struct person {
//姓名
string name;
//性别 1.男 2.女
int sex;
//年龄
int age;
//电话
string phone;
//住址
string addr;
};
//通讯录结构体
struct addressbook{
//通讯录中保存的联系人
struct person personarr[Max];
//当前存的人员个数
int size;
};
//1、 添加联系人
void addperson(addressbook* abs) {
//判满
if (abs->size == Max) {
cout << "通讯录人已满,无法添加!" << endl;
return;
}
else {//添加具体的联系人
//姓名
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personarr[abs->size].name = name;
//性别
cout << "请输入性别: " << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
int sex = 0;
while (1) {
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personarr[abs->size].sex = sex;
break;
}
else
cout << "输入有误,请重新输入!" << endl;
}
//年龄
cout << "请输入年龄: " << endl;
int age = 0;
while (1) {
cin >> age;
if (age >0&& age<150) {
abs->personarr[abs->size].age = age;
break;
}
else
cout << "输入有误,请重新输入!" << endl;
}
//电话
cout << "请输入联系电话: " << endl;
string phone;
cin >> phone;
abs->personarr[abs->size].phone = phone;
//住址
cout << "请输入家庭住址: " << endl;
string addr;
cin >> addr;
abs->personarr[abs->size].addr = addr;
//更新通讯录人数
abs->size++;
cout << "添加成功!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//2、 显示联系人
void showperson(struct addressbook* abs) {
//判断通讯录中人数是否为0
if (abs->size == 0) {
cout << "当前的记录为空!" << endl;
}
else {
for (int i = 0; i < abs->size; i++) {
cout << "姓名: " << abs->personarr[i].name << "\t";
cout << "性别: " << (abs->personarr[i].sex==1?"男":"女") << "\t";
cout << "年龄: " << abs->personarr[i].age << "\t";
cout << "电话: " << abs->personarr[i].phone << "\t";
cout << "住址: " << abs->personarr[i].addr << endl;
}
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//检查联系人是否存在;若存在,返回其具体位置;若不存在,返回-1;
int isexit(addressbook* abs, string name) {
for (int i = 0; i < abs->size; i++) {
//如果找到该用户
if (abs->personarr[i].name == name) {
return i;
}
}
//若没有找到该用户
return -1;
}
//3、 删除指定的联系人
void deleteperson(addressbook* abs) {
cout << "请输入需要删除的联系人" << endl;
string name;
cin >> name;
//ret==-1 没查到
//ret!=-1 查到了
int ret = isexit(abs, name);
if (ret != -1) {
//查找到人,要进行删除操作。
for (int i = ret; i < abs->size; i++) {
//进行数据迁移
abs->personarr[i] = abs->personarr[i + 1];
abs->size--;//更新通讯录中的人员数
cout<<"删除成功!"<<endl;
}
}
else
cout << "查无此人!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//4、 查找指定的联系人信息
void findperson(addressbook* abs){
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
//判断指定的联系人是否存在
int ret = isexit(abs, name);
if (ret != -1) {//找到此人
cout << "姓名: " << abs->personarr[ret].name << "\t";
cout << "性别: " << abs->personarr[ret].sex << "\t";
cout << "年龄: " << abs->personarr[ret].age << "\t";
cout << "电话: " << abs->personarr[ret].phone << "\t";
cout << "住址: " << abs->personarr[ret].addr << endl;
}
else {//未找到此人
cout << "查无此人!" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//5、 修改指定的联系人信息
void modifyperson(addressbook* abs) {
cout << "请输入您要修改的联系人!" << endl;
string name;
cin >> name;
int ret = isexit(abs, name);
if (ret != -1) {//找到指定的联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personarr[ret].name = name;
//性别
cout << "请输入性别:" << endl;
cout << "1------男" << endl;
cout << "2------女" << endl;
int sex = 0;
while (1) {
cin >> sex;
if (sex == 1 || sex == 2) {
abs->personarr[ret].sex = sex;
break;
}
cout << "输入有误,请重新输入!" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
while (1) {
cin >> age;
if (age > 0 && age < 150) {
abs->personarr[ret].age = age;
break;
}
else
cout << "输入有误,请重新输入!" << endl;
}
//电话
cout << "请输入联系电话:" << endl;
string phone;
cin >> phone;
abs->personarr[ret].phone = phone;
//住址
cout << "请输入家庭住址:" << endl;
string addr;
cin >> addr;
abs->personarr[ret].addr = addr;
cout << "修改成功!" << endl;
}
else {//未找到联系人
cout << "查无此人!" << endl;
}
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//6、 清空所有的联系人
void clearperson(addressbook* abs) {
abs->size = 0;//将当前的联系人的数量记为0
cout << "通讯录已清空!" << endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
//菜单界面
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;
}
int main(){
//创建通讯录中的结构体变量
addressbook abs;
//初始化当前的人数
abs.size = 0;
int select = 0;//创建一个用户选择输入的对象
while (1) {
showMenu();
cin >> select;
switch (select) {
case 1://1、添加联系人
addperson(&abs);
break;
case 2://2、显示联系人
showperson(&abs);
break;
case 3://3、删除联系人
{
/*cout << "请输入删除联系人的姓名:" << endl;
string name;
cin >> name;
if (isexit(&abs, name) == -1) {
cout << "查无此人!" << endl;
}
else
cout << "找到了!" << endl;*/
deleteperson(&abs);
}
break;
case 4://4、查找联系人
findperson(&abs);
break;
case 5://5、修改联系人
modifyperson(&abs);
break;
case 6://6、清空联系人
clearperson(&abs);
break;
case 0://0、退出通讯录
cout << "欢迎下次使用!" << endl;
return 0;
break;
}
}
return 0;
}
效果演示
赏