c++通讯录

功能实现

#include<iostream>
#include<cstring>
using namespace std;
#define MAX 1000
struct Contact{
	string name;
	string gender;
	int age;
	string phone;
	string address;
};
struct con{
	struct Contact p[MAX];
	int n=0;//通讯录人数 
};
void init(){
	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 add(struct con * c){
	if(c->n>=MAX){
		cout<<"联系人已满,添加失败!!!"<<endl;
	}
	int gen;
	cout<<"请输入姓名:";
	cin>>c->p[c->n].name;
	cout<<"请输入性别(1--男,2--女):"; 
	cin>>gen;
	if(gen==1){
		c->p[c->n].gender="男"; 
	}else{
		c->p[c->n].gender="女";
	}
	cout<<"请输入年龄:";
	cin>>c->p[c->n].age;
	cout<<"请输入联系电话:";
	cin>>c->p[c->n].phone;
	cout<<"请输入家庭住址:";
	cin>>c->p[c->n].address;
	cout<<"添加成功"<<endl; 
	c->n++;
}
void show(struct con * c){
	int i;
	if(c->n==0){
		cout<<"联系人为空"<<endl; 
	}else{
	
	for(i=0;i<c->n;i++)
	cout<<"姓名:"<<c->p[i].name<<"\t性别:"<<c->p[i].gender<<"\t年龄:"
	<<c->p[i].age<<"\t电话:"<<c->p[i].phone<<"\t家庭住址:"<<c->p[i].address<<endl; }
}
void dele(struct con * c){
	int flag=0,j,i;
	string dename;
	if(c->n==0){
		cout<<"联系人为空"<<endl; 
		flag=2;
	}else{
	
	cout<<"请输入你要删除的联系人:"<<endl;
	cin>>dename;
	for(i=0;i<c->n;i++){
		if(c->p[i].name==dename){
			flag=1;
			j=i;
			for(;j<c->n-1;j++){
				c->p[j]=c->p[j+1];
			}
			c->n--;
			break;
		}
	}}
	if(flag==0){
		cout<<"查无此人"<<endl;
	}else if(flag==1){
		cout<<"删除成功"<<endl;
	}
}
void find(struct con *c){
	int i;
	string finame;
	int flag=0;
	if(c->n==0){
		cout<<"联系人为空"<<endl;
		flag=2;
	}else{
	cout<<"请输入你要查询的人"<<endl;
	cin>>finame;
	for(i=0;i<c->n;i++){
		if(c->p[i].name==finame){
			flag=1;
				cout<<"姓名:"<<c->p[i].name<<"\t性别:"<<c->p[i].gender<<"\t年龄:"
				<<c->p[i].age<<"\t电话:"<<c->p[i].phone<<"\t家庭住址:"<<c->p[i].address<<endl; 
			break;
		}
	}}
	if(flag==0){
		cout<<"查无此人"<<endl;
	}
}
void edit(struct con *c){
	int i,gen,flag=0;
	string edname;
	if(c->n==0){
		cout<<"联系人为空"<<endl;
		flag=2;
	}else{
		
	
	cout<<"请输入要修改的联系人"<<endl;
	cin>>edname;
	for(i=0;i<c->n;i++){
		if(c->p[i].name==edname){
			cout<<"请输入姓名:";
			cin>>c->p[i].name;
			cout<<"请输入性别(1--男,2--女):"; 
			cin>>gen;
			if(gen==1){
				c->p[i].gender="男"; 
			}else{
				c->p[i].gender="女";
			}
			cout<<"请输入年龄:";
			cin>>c->p[i].age;
			cout<<"请输入联系电话:";
			cin>>c->p[i].phone;
			cout<<"请输入家庭住址:";
			cin>>c->p[i].address;
			cout<<"修改成功"<<endl; 
			flag=1;
			break;
		}
	} }
	if(flag==0){
		cout<<"查无此人"<<endl; 
	}
	
}
void Clear(struct con *c){
	int i;
	for(i=0;i<c->n;i++){
		c->p[i].name=" ";
		c->p[i].gender=" ";
		c->p[i].age=0;
		c->p[i].phone=" ";
		c->p[i].address=" ";
	}
	cout<<"清空成功"<<endl;
	c->n=0;
}
int main(){
	struct con c;
	c.n=0;
	int n;
	int flag=0;
	while(1){
		init();
		cin>>n;
		switch(n){
			case 1:add(&c);system("pause");system("cls");break;
			case 2:show(&c); system("pause");system("cls");break;
			case 3:dele(&c); system("pause");system("cls");break;
			case 4:find(&c); system("pause");system("cls");break; 
			case 5:edit(&c); system("pause");system("cls");break;
			case 6:Clear(&c); system("pause");system("cls");break;
			case 0:cout<<"退出成功"<<endl;; system("pause");flag=1;break; 
			default:cout<<"输出错误!!!"<<endl;system("pause");system("cls");break; 
		}
		if(flag==1){
			break;
		}
	}

	return 0;
}

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

刘艺金

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

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

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

打赏作者

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

抵扣说明:

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

余额充值