C++学习—通讯录管理系统

说明

本系统学习自黑马程序员C++教学系统。

在其基础上主要进行了如下改动:

1.添加文本来储存通讯录,每次编译读取文本中通讯录,退出系统重写通讯录。

2.使用vector容器储存通讯录,注意容器大小设置


代码

头文件:类和函数声明

#pragma once
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<string>
using namespace std;
class people
{
public:
	string name;
	string sex;
	int age;
	string phone;
	string home;
};
class message
{
public:
	vector <people> ple;
	int number;
	void add_people();
	void show_people();
	void find_people();
	void delete_people();
	void change_people();
	void clear_people();
};
void showmenu();

void load(message *msg);

void down(message *msg);

函数声明文件:

#include"message.h"
//菜单
void showmenu()
{
	cout << "1、添加联系人" << endl;
	cout << "2、显示联系人" << endl;
	cout << "3、删除联系人" << endl;
	cout << "4、查找联系人" << endl;
	cout << "5、修改联系人" << endl;
	cout << "6、清空联系人" << endl;
	cout << "0、退出通讯录" << endl;
}
//加载文件
void load(message* msg)
{
	ifstream infile;
	infile.open("通讯录.txt");
	if (!infile.is_open())
	{
		cout << "通讯录打开失败" << endl;
	}
	string str, str1;
	getline(infile, str);//头文件
	while (!infile.eof())
	{
		getline(infile, str);
		//姓名
		int pos1 = 0, pos2;
		pos2 = str.find(",", pos1);
		str1 = str.substr(pos1, pos2 - pos1);
		msg->ple[msg->number].name = str1;
		//性别
		pos1 = pos2+1;
		pos2 = str.find(",", pos1);
		str1 = str.substr(pos1, pos2 - pos1);
		msg->ple[msg->number].sex = str1;
		//年龄
		pos1 = pos2+1;
		pos2 = str.find(",", pos1);
		str1 = str.substr(pos1, pos2 - pos1);
		msg->ple[msg->number].age = atoi(str1.c_str());
		//电话
		pos1 = pos2+1;
		pos2 = str.find(",", pos1);
		str1 = str.substr(pos1, pos2 - pos1);
		msg->ple[msg->number].phone = str1;
		//家庭住址
		pos1 = pos2+1;
		pos2 = str.find(",", pos1);
		str1 = str.substr(pos1, pos2 - pos1);
		msg->ple[msg->number].home = str1;
		//人数
		msg->number = msg->number + 1;
	}
}
//输出到文件
void down(message* msg)
{
	ofstream outfile;
	outfile.open("通讯录.txt");
	outfile<< "姓名 " << "," << "性别" << "," << "年龄" << "," << " 电话 " << "," << "家庭住址" << endl;
	for (int i = 0; i < msg->number; i++)
	{
		outfile << msg->ple[i].name << "," << msg->ple[i].sex << "," << msg->ple[i].age << "," << msg->ple[i].phone << "," << msg->ple[i].home;
		if (i < msg->number - 1)
			outfile << endl;
	}
}
//添加
void message::add_people()
{
	if (number == 100)
		cout << "通讯录已满"<<endl;
	else
	{
		//名字
		string nm; cout << "请输入姓名:"; cin >> nm; ple[number].name = nm;
		//性别
		string sx; cout  << "请输入性别(男或女):";
		while (true)
		{
			cin >> sx;
			if (sx == "男" || sx == "女")
				break;
			else
				cout << endl << "请重新输入:";
		}
		ple[number].sex = sx;
		//年龄
		int ag; cout << "请输入年龄:"; cin >> ag; ple[number].age = ag;
		//电话
		string tp; cout << "请输入电话:"; cin >> tp; ple[number].phone = tp;
		//家庭住址
		string hm; cout << "请输入家庭住址:"; cin >> hm; ple[number].home = hm;
		//跟新人数
		number = number + 1;
	}
}
//显示
void message::show_people()
{
	cout << "姓名 " << "\t" << "性别" << "\t" << "年龄" << "\t" << " 电话 " << "\t" << "家庭住址" << endl;
	for (int i = 0; i < number; i++)
	{
		cout << ple[i].name << " \t" << ple[i].sex << " \t" << ple[i].age << " \t"  << ple[i].phone << " \t"  << ple[i].home << endl;
	}
}
//查找
void message::find_people()
{
	cout << "请输入查找联系人的姓名:";
	string str; cin >> str;
	for (int i = 0; i < number; i++)
	{
		if (str == ple[i].name)
		{
			cout << "查找结果:   " <<endl<< "姓名 " << "\t" << "性别" << "\t" << "年龄" << "\t" << " 电话 " << "\t" << "家庭住址" << endl;
			cout << ple[i].name << "\t" << ple[i].sex << "\t" << ple[i].age << "\t" << ple[i].phone << "\t" << ple[i].home << endl;
			return;
		}
	}
	cout << "未找到对应联系人信息" << endl;
}
//删除
void message::delete_people()
{
	cout << "请输入删除联系人的姓名:";
	string str; cin >> str;
	for (int i = 0; i < number; i++)
	{
		if (str == ple[i].name)
		{
			cout << "删除联系人:   " << "姓名 " << "\t" << "性别" << "\t" << "年龄" << "\t" << " 电话 " << "\t" << "家庭住址" << endl;
			cout << ple[i].name << "\t" << ple[i].sex << "\t" << ple[i].age << "\t" << ple[i].phone << "\t" << ple[i].home << endl;
			number = number - 1;
			for (int j = i; i < number; i++)
			{
				ple[j].name = ple[j + 1].name; ple[j].sex = ple[j + 1].sex; ple[j].age = ple[j + 1].age; ple[j].phone = ple[j + 1].phone; ple[j].home = ple[j + 1].home;
			}
			cout << "删除联系人成功" << endl;
			return;
		}
	}		
	cout << "未找到对应联系人信息"<<endl;
}
//修改联系人
void message::change_people()
{
	cout << "请输入查找联系人的姓名:";
	string str; cin >> str;
	for (int i = 0; i < number; i++)
	{
		if (str == ple[i].name)
		{
			cout << "请输入需要修改的内容的编号:(1:年龄,2:电话,3,:家庭住址)";
			int i; cin >> i;
			switch (i)
			{
			case 1:
			{
				cout << "请输入修改联系人的新年龄:";
				int j; cin >> j;
				ple[i].age = j;
				break;
			}
			case 2:
			{
				cout << "请输入修改联系人的新电话:";
				string str2; cin >> str2;
				ple[i].phone = str2;
				break;
			}
			case 3:
			{
				cout << "请输入修改联系人的新住址:";
				string str3; cin >> str3;
				ple[i].home = str3;
				break;
			}
			}
			return;
		}
	}
	cout << "未找到对应联系人信息" << endl;
}
//清空
void message::clear_people()
{
	number = 0;
}

主函数文件:

#include"message.h"
using namespace std;
int main()
{
	//通讯录
	message msg;
	//初始化大小,数量
	msg.ple.resize(100);
	msg.number = 0;
	//获取文件中保存的通讯录
	load(&msg);
	//功能
	while (true)
	{
		//菜单
		system("pause");
		system("cls");
		showmenu();
		//选择功能
		cout << "请选择:"; int select; cin >> select;
		switch (select)
		{
			case 1://添加联系人
				msg.add_people();
				break;
			case 2://显示联系人
				msg.show_people();
				break;
			case 3://删除联系人
				msg.delete_people();
				break;
			case 4://查找联系人
				msg.find_people();
				break;
			case 5://修改联系人
				msg.change_people();
				break;
			case 6://清空联系人
				msg.clear_people();
				break;
			case 0:
				down(&msg);
				cout << "欢迎下次使用";
				return 0;
				break;
			default:
				break;
		}
	}
	system("pause"); 
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WHU小疯子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值