用C++简单的实现一个通讯录管理系统。
全程都是自己完成的。
虽然还有许多不足。
但是基本的功能都实现了。
#include<iostream>
using namespace std;
//电话联系人结构体,最后一个free表示的是该结构体是否是电话联系人,如果是为1,否则为0。
struct Telephone {
string name;
int sex;
int age;
string number;
string address;
int free=0;
};
//函数声明,分别对应七个不同的功能。
void Interface();
void Add(Telephone T[]);
void Display(Telephone T[]);
void Delete(Telephone T[]);
void Search(Telephone T[]);
void Change(Telephone T[]);
void Clear(Telephone T[]);
int main() {
Interface();
Telephone tel[10];
int n;
cout << "请输入操作数:";
cin >> n;
while (1) {
int s = 0;
switch (n) {
case 1:
Add(tel); break;
case 2:
Display(tel); break;
case 3:
Delete(tel); break;
case 4:
Search(tel); break;
case 5:
Change(tel); break;
case 6:
Clear(tel); break;
case 7:
s = 1;
break;
default:
cout << "error";
}
if (s == 1) {
break;
}
cout << "请输入操作数:";
cin >> n;
}
}
//界面显示
void Interface() {
cout << "1,添加联系人" << endl;
cout << "2,显示联系人" << endl;
cout << "3,删除联系人" << endl;
cout << "4,查找联系人" << endl;
cout << "5,修改联系人" << endl;
cout << "6,清空联系人" << endl;
cout << "7,退出通讯录" << endl;
}
//添加联系人
void Add(Telephone T[]) {
int i;
for (i = 0; i < 10; i++) {
if (T[i].free==0) {
break;
}
}
cout << "请输入姓名:";
cin >> T[i].name;
cout << "请输入性别(0:男;1:女):";
cin >> T[i].sex;
cout << "请输入年龄:";
cin >> T[i].age;
cout << "请输入联系电话:";
cin >> T[i].number;
cout << "请输入家庭住址:";
cin >> T[i].address;
T[i].free = 1;
}
//显示联系人
void Display(Telephone T[]) {
int i;
for (i = 0; i < 10; i++) {
if (T[i].free == 1) {
cout << T[i].name << "\t" << T[i].sex << "\t" << T[i].age << "\t" << T[i].number << "\t" << T[i].address << endl;
}
else {
break;
}
}
}
//删除联系人
void Delete(Telephone T[]) {
string deleteName;
cout << "请输入删除的联系人:";
cin >> deleteName;
int i,j,k;
for (i = 0; i < 10; i++) {
if (T[i].name == deleteName) {
break;
}
}
if (i == 10) {
cout << "抱歉,查无此人,无法删除。"<<endl;
}
k = i;
for (j = i+1; j < 10; j++) {
if (T[i].free == 1) {
T[k].name = T[j].name;
T[k].sex = T[j].sex;
T[k].age = T[j].age;
T[k].number = T[j].number;
T[k].address = T[j].address;
T[k].free = T[j].free;
k++;
}
}
T[k-1].free = 0;
}
//查找联系人
void Search(Telephone T[]) {
string searchName;
cout << "请输入联系人:";
cin >> searchName;
int i,s=0;
for (i = 0; i < 10; i++) {
if (T[i].free == 0) {
s = 1;
break;
}
else {
if (T[i].name == searchName) {
cout << T[i].name << "\t" << T[i].sex << "\t" << T[i].age << "\t" << T[i].number << "\t" << T[i].address << endl;
break;
}
}
}
if (s == 1) {
cout << "查无此人!"<<endl;
}
}
//修改联系人
void Change(Telephone T[]) {
string changeName;
cout << "请输入联系人:";
cin >> changeName;
int i, s = 0;
for (i = 0; i < 10; i++) {
if (T[i].free == 0) {
s = 1;
break;
}
else {
if (T[i].name == changeName) {
cout << T[i].name << "\t" << T[i].sex << "\t" << T[i].age << "\t" << T[i].number << "\t" << T[i].address << endl;
cout << "请输入修改后的名字:";
cin >> T[i].name;
cout << "请输入修改后的性别:";
cin >> T[i].sex;
cout << "请输入修改后的年龄:";
cin >> T[i].age;
cout << "请输入修改后的电话号码:";
cin >> T[i].number;
cout << "请输入修改后的地址:";
cin >> T[i].address;
break;
}
}
}
if (s == 1) {
cout << "查无此人!"<<endl;
}
}
//清除联系人
void Clear(Telephone T[]) {
int i = 0;
while (T[i].free == 1) {
T[i].free = 0;
i++;
}
}