通讯管理系统

#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
//联系人结构体
struct Person
{
	string m_Name;
	int m_Sex;//1男2女
	int m_Age;
	string m_Phone;
	string m_Adder;
};
struct Addressbooks
{
	struct Person personArray[MAX];
	int m_Size;

};
void addPerson(Addressbooks* abs)
{
	if (abs->m_Size == MAX)
	{
		cout << "通讯录已满,无法添加!" << endl;
		return;
	}
	else
	{
		string name;
		cout << "请输入姓名: " << endl;
		cin >> name;
		abs->personArray[abs->m_Size].m_Name = name;
		cout << "请输入性别: " << endl;
		cout << "1---男" << endl;
		cout << "2---女" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout << "输入错误,请重新输入" << endl;
		}
		//年龄
		cout << "请输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArray[abs->m_Size].m_Age = age;
		//电话
		cout << "请输入联系电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[abs->m_Size].m_Phone = phone;
		//住址
		cout << "请输入住址: " << endl;
		string address;
		cin >> address;
		abs->personArray[abs->m_Size].m_Adder = address;
		//更新人数
	}
		abs->m_Size++;
		cout << "添加成功" << endl;
		system("pause");
		system("cls");//清屏
}
int  isExist(Addressbooks * abs,string name)
{
	for (int i = 0;i <abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
		return -1;
	}
}
void showPerson(Addressbooks * abs)
{
	if (abs->m_Size==0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i <abs->m_Size; i++)
		{
			cout << "姓名: " << abs->personArray[i].m_Name << endl;
			cout << "性别: " << abs->personArray[i].m_Sex << endl;
			cout << "年龄: " << abs->personArray[i].m_Age << endl;
			cout << "电话: " << abs->personArray[i].m_Phone << endl;
			cout << "住址: " << abs->personArray[i].m_Adder << endl;
		}
	}
	system("pause");
	system("cls");//清屏

}
void deletePerson(Addressbooks * abs)
{
	cout << "请输入您要删除联系人的姓名: " << endl;
	string name;
	cin >> name;
	int res = isExist(abs, name);
	if (res != -1)
	{
		for (int i = res; i < abs->m_Size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");//清屏
}
void findPerson(Addressbooks * abs)
{
	cout << "请输入要查找人的姓名" << endl;
	string name;
	cin >> name;
	int res = isExist(abs, name);
	if (res != -1)
	{
		cout << "姓名: " << abs->personArray[res].m_Name << endl;
		cout << "性别: " << abs->personArray[res].m_Sex << endl;
		cout << "年龄: " << abs->personArray[res].m_Age<< endl;
		cout << "电话: " << abs->personArray[res].m_Phone << endl;
		cout << "住址: " << abs->personArray[res].m_Adder << endl;


	}
	else
	{
		cout << "查无此人" << endl;
     }
	system("pause");
	system("cls");//清屏
}
void modifyPerson(Addressbooks* abs)
{
	cout << "请输入要修改人的姓名" << endl;
	string name;
	cin >> name;
	int res = isExist(abs, name);
	if (res != -1)
	{
		string name;
		cout << "请输入姓名:  " << endl;
		cin >> name;
		abs->personArray[res].m_Name = name;
		
		cout << "请输入性别:  " << endl;
		cout << "1--- 男" << endl;
		cout <<"2---女" << endl;
		int sex=0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->personArray[res].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		int age = 0;
		cout << "请输入年龄:  " << endl;
		cin >> age;
		abs->personArray[res].m_Age = age;
		int phone;
		cout << "请输入电话:  " << endl;
		cin >> phone;
		abs->personArray[res].m_Phone = phone;
		string address;
		cout << "请输入地址:  " << endl;
		cin >> address;
		abs->personArray[res].m_Adder = address;
		cout << "修改成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");//清屏
}
void cleanPerson(Addressbooks* abs)
{
	abs->m_Size = 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()
{
	//创建结构体变量
	Addressbooks abs;
	abs.m_Size = 0;
	int select=0;
	while (true)
	{
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(&abs);
			break;
		case 2:
			showPerson(&abs);
			break;
		case 3:
		/* {
			cout << "请输入删除联系人姓名: " << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1)
			{
				cout << "查无此人" << endl;
			}
			else
			{
				cout << "找到此人" << endl;
			}
		}*/
			deletePerson(&abs);
			break;
		case 4:
			findPerson(&abs);
			break;
		case 5:
			modifyPerson(&abs);
			break;
		case 6:
			cleanPerson(&abs);
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;

			break;
		default:
			break;

		}

	}
	



	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值