需求
代码
#include <iostream>
using namespace std;
#include <string> //c++ 中调用string类型 字符串时需要加,否则编译会报错-----引入一个头文件
#include <ctime>//使用时间头文件
struct Tongxl {
string name;
string sex;
int age;
string telephone;
string address;
};
Tongxl add() {
Tongxl num1;
string name;
string sex;
int age;
string telephone;
string address;
cout << "请输入联系人姓名:" << endl;
cin >> name;
cout << "请输入" << name << "的性别:" << endl;
cin >> sex;
cout << "请输入" << name << "的年龄:" << endl;
cin >> age;
cout << "请输入" << name << "的电话:" << endl;
cin >> telephone;
cout << "请输入" << name << "的地址:" << endl;
cin >> address;
num1.name = name;
num1.sex = sex;
num1.age = age;
num1.telephone = telephone;
num1.address = address;
return num1;
};
void look(Tongxl *num1, int len) {
for (int j = 0; j < len; j++) {
if (num1[j].name != "") {
cout << "姓名:" << num1[j].name << " \t" << "性别:" << num1[j].sex << " \t" << "年龄:" << num1[j].age << " \t" << "电话:" << num1[j].telephone << " \t" << "地址:" << num1[j].address << " \t" << endl;
}
}
}
//得用地址传参修改
int del(Tongxl *num1, int len) {
string name2;
cout << "请输入删除的姓名";
cin >> name2;
int ii = 0;
for (int i = 0; i < len; i++) {
if (num1[i].name == name2) {
num1[i] = {};
ii++;
for (int jj = i+1; jj < len; jj++) {
if (num1[jj].name != name2 && num1[jj].name != "") {
num1[i] = num1[jj];
num1[jj] = {};
break;
}
}
}
else if (num1[i].name == "") {
for (int jj = i + 1; jj < len; jj++) {
if (num1[jj].name != name2 && num1[jj].name != "") {
num1[i] = num1[jj];
num1[jj] = {};
break;
}
}
}
}
return (len - ii) > 0?len - ii:0;
}
void cz(Tongxl *num1, int len) {
string name1;
cout << "请输入查找的姓名";
cin >> name1;
for (int i = 0; i <= len; i++) {
if (num1[i].name == name1) {
cout << "姓名:" << num1[i].name << " \t" << "性别:" << num1[i].sex << " \t" << "年龄:" << num1[i].age << " \t" << "电话:" << num1[i].telephone << " \t" << "地址:" << num1[i].address << " \t" << endl;
}
}
}
void qk(Tongxl *num1, int len) {
for (int i = 0; i <= len; i++) {
num1[i] = {};
}
}
void xg(Tongxl *num1, int len) {
string name1;
cout << "请输入修改的人姓名" << endl;
cin >> name1;
cout << "修改前信息" << endl;
for (int i = 0; i <= len; i++) {
if (num1[i].name == name1) {
cout << "姓名:" << num1[i].name << " \t" << "性别:" << num1[i].sex << " \t" << "年龄:" << num1[i].age << " \t" << "电话:" << num1[i].telephone << " \t" << "地址:" << num1[i].address << " \t" << endl;
}
}
string name2;
string sex;
int age;
string telephone;
string address;
cout << "请输入联系人姓名:" << endl;
cin >> name2;
cout << "请输入" << name2 << "的性别:" << endl;
cin >> sex;
cout << "请输入" << name2 << "的年龄:" << endl;
cin >> age;
cout << "请输入" << name2 << "的电话:" << endl;
cin >> telephone;
cout << "请输入" << name2 << "的地址:" << endl;
cin >> address;
cout << "修改后信息" << endl;
for (int i = 0; i <= len; i++) {
if (num1[i].name == name1) {
num1[i].name = name2;
num1[i].sex = sex;
num1[i].age = age;
num1[i].telephone = telephone;
num1[i].address = address;
cout << "姓名:" << num1[i].name << " \t" << "性别:" << num1[i].sex << " \t" << "年龄:" << num1[i].age << " \t" << "电话:" << num1[i].telephone << " \t" << "地址:" << num1[i].address << " \t" << endl;
}
}
}
int main() {
Tongxl t[1000];
int i = 0;
Ks:
system("cls");//清除屏幕
cout << "************************************\t" << endl;
cout << "******** 1.添加联系人 ********\t" << endl;
cout << "******** 2.显示联系人 ********\t" << endl;
cout << "******** 3.删除联系人 ********\t" << endl;
cout << "******** 4.查找联系人 ********\t" << endl;
cout << "******** 5.修改联系人 ********\t" << endl;
cout << "******** 6.清空联系人 ********\t" << endl;
cout << "******** 7.退出通讯录 ********\t" << endl;
cout << "************************************\t" << endl;
int lx;
cout << "请选择操作类型:" << endl;
cin >> lx;
//int *p = &t;
if (lx==1) {
if (i >= 1000) {
cout << "最多1000个" << endl;
}
else {
t[i]=add();
i++;
}
}else if (lx == 2) {
look(t, i);
system("pause");
}else if (lx == 3) {
int bh=del(t, i);
i = bh;
system("pause");
}else if (lx == 4) {
cz(t, i);
system("pause");
}else if (lx == 5) {
xg(t, i);
system("pause");
}else if (lx == 6) {
qk(t,i);
}else if (lx == 7) {
return 0;
}
goto Ks;
return 0;
}
演示
1.添加联系人
2.查看联系人
重复1操作,多添加几个联系人
4.查找联系人
5.修改联系人
查看所有联系人
3删除联系人
删除后查看剩余联系人
6清空联系人
清除完后查看剩余联系人
7退出