C++的通讯录
#include<iostream>
#include<ctime>
#include <cmath>
#include<string>
#include<cstring>
using namespace std;
struct contact
{
int id;
string name;
unsigned long telNumber;
string gender;
string address;
};
int menu();
int findFriend(string name,contact* Friends, int len);
bool addContact(const contact myFriend, contact* Friends, int friend_id);
bool deleteFriend(string friendName, contact* Friends, int& len);
int exitContact();
void showContact(contact* Friends,int len);
void editContact(string friendName, contact* Friends,int len);
void clearContact(contact* Friends, int len);
int main(){
int friend_id = 0;
contact Friends[1000];
int len = 0;
while(true){
int func = menu();
switch (func)
{
case 1: //添加 完成
{
if(len<1000){
contact myFriend;
string chkString;
long chkInt;
cout<<"请输入联系人姓名:"<<endl;
cin >> chkString;
while(cin.fail()){
cout<<"请检查输入类型是否正确"<<endl;
cin.clear();
cin.ignore();
cout<<"请输入联系人姓名:"<<endl;
cin >> chkString;
}
myFriend.name = chkString;
cout<<"请输入联系人性别:"<<endl;
cin >> chkString;
while(cin.fail()){
cout<<"请检查输入类型是否正确"<<endl;
cin.clear();
cin.ignore();
cout<<"请输入联系人性别:"<<endl;
cin >> chkString;
}
myFriend.gender = chkString;
cout<<"请输入联系人电话:"<<endl;
cin >> chkInt;
while(cin.fail()){
cout<<"请检查输入类型是否正确"<<endl;
cin.clear();
cin.ignore();
cout<<"请输入联系人电话:"<<endl;
cin >> chkInt;
}
myFriend.telNumber = chkInt;
cout<<"请输入联系人住址:"<<endl;
cin >> chkString;
while(cin.fail()){
cout<<"请检查输入类型是否正确"<<endl;
cin.clear();
cin.ignore();
cout<<"请输入联系人住址:"<<endl;
cin >> chkString;
}
myFriend.address = chkString;
addContact(myFriend,Friends,friend_id);
friend_id +=1;
len +=1;
cout<<"添加成功!"<<endl;
}else
cout<<"超出联系人最大数量:1000"<<endl;
break;
}
case 2: //删除 完成
{
string name;
cout << "请输入要删除的联系人的名字: " << endl;
cin >> name;
deleteFriend(name, Friends, len); // 注意,我们现在传递 len 作为引用
break;
}
case 3: //查找
{
string chkString;
string name;
int index;
cout<<"请输入要查找的联系人的名字:"<<endl;
cin>>chkString;
while(cin.fail()){
cout<<"请检查输入类型是否正确"<<endl;
cin.clear();
cin.ignore();
cout<<"请输入联系人姓名:"<<endl;
cin >> chkString;
}
name = chkString;
index = findFriend(name,Friends,len);
if(index == -1)
{
cout << "未收录该联系人" << name << "请确定输入是否正确"<<endl;
}
else
cout<<Friends[index].id+1<<"\t"<<Friends[index].name<<"\t"<<Friends[index].gender<<"\t"<<Friends[index].telNumber<<"\t"<<Friends[index].address<<endl;
break;
}
case 4: //修改 完成
{
string name;
cout << "请输入要修改的联系人名称:" << endl;
cin >> name ;
editContact(name,Friends,len);
cout << "修改成功!" << endl;
break;
}
case 5: //清空 完成
{
clearContact(Friends, len);
friend_id=0;
len = 0;
break;
}
case 6: //显示 完成
{
showContact(Friends,len);
break;
}
case 0: //退出 完成
{
exitContact();
break;
}
default:
break;
}
}
}
int menu(){
int func;
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;
cout<<"请选择您需要的功能:"<<endl;
cin>>func;
return func;
}
bool addContact(const contact myFriend, contact* Friends, int friend_id){
if(sizeof(myFriend) != 0 && Friends != nullptr){
Friends[friend_id].id = friend_id;
Friends[friend_id].name = myFriend.name;
Friends[friend_id].gender = myFriend.gender;
Friends[friend_id].telNumber = myFriend.telNumber;
Friends[friend_id].address = myFriend.address;
return true;
}else
cout<<"请检查联系人是否为空或通讯录是否为空"<<endl;
}
bool deleteFriend(string friendName, contact* Friends, int& len) {
int index = findFriend(friendName, Friends, len);
if (index != -1) {
// Shift elements to the left to overwrite the deleted contact
for (int i = index; i < len - 1; ++i) {
Friends[i] = Friends[i + 1];
Friends[i].id = i; // 更新联系人的ID
}
len--; // Decrease the number of contacts
// 更新删除操作之后的联系人的ID
for (int i = index; i < len; ++i) {
Friends[i].id = i;
}
cout << "联系人删除成功!" << endl;
return true;
} else {
cout << "未找到联系人!" << endl;
return false;
}
}
int findFriend(string name,contact* Friends, int len){
for(int i = -1; i<len;i++){
if(Friends[i].name == name){
return i;
}
else
return i;
}
}
int exitContact(){
cout<<"欢迎下次使用"<<endl;
exit(0);
return 0;
}
void showContact(contact* Friends,int len){
if (sizeof(Friends)!=0 && len!=0){
for(int i=0; i<len; i++){
cout<<Friends[i].id+1<<"\t"<<Friends[i].name<<"\t"<<Friends[i].gender<<"\t"<<Friends[i].telNumber<<"\t"<<Friends[i].address<<endl;
}
}else
cout<<"联系人为空,请检查通讯录!"<<endl;
}
void editContact(string friendName, contact* Friends, int len){
int friend_id = findFriend(friendName, Friends,len);
contact myFriend;
cout<<"请输入联系人姓名:"<<endl;
cin >> myFriend.name;
cout<<"请输入联系人性别:"<<endl;
cin >> myFriend.gender;
cout<<"请输入联系人电话:"<<endl;
cin >> myFriend.telNumber;
cout<<"请输入联系人住址:"<<endl;
cin >> myFriend.address;
addContact(myFriend,Friends,friend_id);
cout<<"修改完成!"<<endl;
}
void clearContact(contact* Friends, int len){
memset(Friends,0,len*sizeof(contact));
cout<<"已清空所有联系人!"<<endl;
}