不太完整的c++可存档通讯录

2024年7月21日 | 分类: 【编程】
//#ifndef coutAIO_H
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#define MAX 1000//最大

using namespace std;
long long sz[10000][10000];//数组,用于分解整数
long long jc(long long a) {//阶乘(!a)
	if (a > 1) {
		return jc(a - 1) * a;
	}
	return 1;
}
long long zsws(long long a) {//求整数位数
	short b = 0;
	while (a) {
		a /= 10;
		b++;
	}
	return b;
}
void fj(long long a) {//分解整数各位
	long long b = zsws(a), i;
	while (b--) {
		sz[0][b] = a % 10;
		a /= 10;
	}
}
void xycfb(long long x = 9, long long y = 9) {//把xy乘法表存在sz里。
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++) {
			sz[i][j] = (i + 1) * (j + 1);
		}
	}
}
void scsz(long long x = 9, long long y = 9) {//输出数组
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++) {
			cout << sz[i][j] << "\t";
		}
		cout << endl;
	}
}
void sccfb(long long a = 9, long long b = 9) {//输出ab乘法表
	xycfb(a, b);
	scsz(a, b);
}
long long fbnq(long long a) {//斐波那契数列求和
	if (a > 2)
		return fbnq(a - 1) + fbnq(a - 2);
	return 1;
}
void printsz(char* arr[]) {//打印数组
	long long i = 0;
	for (; *arr != NULL; arr++) {
		cout << i << " " << *arr << endl;//第几个被输出与
		i++;
	}
}
//以上新添
void gotoxy(int y, int x)
{
	COORD  coord;
	coord.X = x;
	coord.Y = y;
	HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(a, coord);
}
void color(int a)//打印颜色
{
	/*亮白*/    if (a == 0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	/*蓝色*/    if (a == 1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
	/*绿色*/    if (a == 2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
	/*紫色*/    if (a == 3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
	/*红色*/    if (a == 4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
	/*黄色*/    if (a == 5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
	/*深蓝色*/    if (a == 6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
	/*土黄色or金黄色*/    if (a == 7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN);
	/*灰色接近白*/    if (a == 8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void bukaqingping() {
	gotoxy(0, 0);/*
	for (int i = 0; i < 50; i++) {
		cout << "																																																		                                                                                                       " << endl;
	}*/
	system("cls");
	gotoxy(0, 0);
}
template<typename o>
void swap(o & a, o & b) {
	o temp = a;
	a = b;
	b = temp;
}
long long numberaio(string n) {
	while (true) {
		int i = 0;
		if (n[i] == '\0')
		{
			return i;
		}
		i++;
	}
}
int zfzh(string s) {//字符转换
	int num = 0;
	for (int i = 0; i < s.size(); ++i) {
		num = 10 * num + (s[i] - '0');
	}
	return num;
}
void coutAIO(string b, int truefalse0 = 0, bool truefalse1 = 0) {//说的话,速度,是否回车。
	int i = 0, s = 0;
	long long a = numberaio(b);
	while (a != 0) {
		color(2);
		/*
		s = rand() % 9;
		if (a != 0 && s == 0)
		{
			color(0);
		}
		if (a != 0 && s == 1)
		{
			color(1);
		}
		if (a != 0 && s == 2)
		{
			color(2);
		}
		if (a != 0 && s == 3)
		{
			color(3);
		}
		if (a != 0 && s == 4)
		{
			color(4);
		}
		if (a != 0 && s == 5)
		{
			color(5);
		}
		if (a != 0 && s == 6)
		{
			color(6);
		}
		if (a != 0 && s == 7)
		{
			color(7);
		}
		if (a != 0 && s == 8)
		{
			color(8);
		}*/
		cout << b[i];
		a--;
		i++;

		if (truefalse0 == 1) {
			Sleep(10);
		}
		if (truefalse0 == 2) {
			Sleep(20);
		}
		if (truefalse0 == 3) {
			Sleep(40);
		}
		if (truefalse0 == 4) {
			Sleep(100);
		}
		if (truefalse0 == 5) {
			Sleep(300);
		}
		if (truefalse0 == 6) {
			Sleep(1000);
		}
	}
	if (truefalse1 == 0) {
		cout << endl;
	}
}/*
void full_screen()
{
	HWND hwnd = GetForegroundWindow();
	int cx = GetSystemMetrics(SM_CXSCREEN);            // 屏幕宽度 像素
	int cy = GetSystemMetrics(SM_CYSCREEN);            // 屏幕高度 像素

	LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE);   // 获取窗口信息
	// 设置窗口信息 最大化 取消标题栏及边框 //
	//SetWindowLong(hwnd,GWL_STYLE,( l_WinStyle |WS_MAXIMIZE| WS_POPUP) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);//
	SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//联系人结构体
struct people {
	string m_Name;//姓名
	int m_Sex;//性别
	int m_Age;//年龄
	string m_Phone;//电话
	string m_Addr;//住址
};
//通讯录结构体
struct Addressbook {
	Addressbook() {
		m_Size = 0;
	}
	people peopleArray[MAX];//联系人数组
	int m_Size;//人数个数
};
void showMenu() {//显示面板
	coutAIO("************************", 0);
	coutAIO("***** 1.添加联系人 *****", 0);
	coutAIO("***** 2.显示联系人 *****", 0);
	coutAIO("***** 3.删除联系人 *****", 0);
	coutAIO("***** 4.查找联系人 *****", 0);
	coutAIO("***** 5.修改联系人 *****", 0);
	coutAIO("***** 6.清空通讯录 *****", 0);
	coutAIO("***** 0.退出通讯录 *****", 0);
	coutAIO("************************", 0);
}

void AddPeople(Addressbook * abs) {
	if (abs->m_Size == MAX)
	{
		coutAIO("通讯录已满,无法添加。");
		return;
	}
	else {
		string name;
		coutAIO("请输入您朋友的姓名:", 1, 1);
		cin >> name;
		abs->peopleArray[abs->m_Size].m_Name = name;
		int sex;
		while (true)
		{
			coutAIO("请认真输入您朋友的性别,1男 2女:", 1, 1);
			cin >> sex;
			if (sex != 1 && sex != 2)
			{
				coutAIO("再说一遍,1是男,2是女。", 3);
				continue;
			}
			abs->peopleArray[abs->m_Size].m_Sex = sex;
			break;
		}

		int age;
		while (true)
		{
			age = 0;
			coutAIO("请认真输入您朋友的年龄:", 1, 1);
			cin >> age;
			if (age >= 200)
			{
				coutAIO("嗯,活得真久", 3);
				continue;
			}
			else if (age < 0)
			{
				coutAIO("哇哦,这个人竟然没出生。", 1);
				continue;
			}
			else {
				abs->peopleArray[abs->m_Size].m_Age = age;
				break;
			}
		}

		string phone;
		coutAIO("请认真输入您朋友的联系电话:", 1, 1);
		cin >> phone;
		abs->peopleArray[abs->m_Size].m_Phone = phone;

		string address;
		coutAIO("请认真输入您朋友的住址:", 1, 1);
		cin >> address;
		abs->peopleArray[abs->m_Size].m_Addr = address;

		abs->m_Size++;
	}
}

void showPeople(Addressbook * abs) {
	if (abs->m_Size == 0)
	{
		bukaqingping();
		coutAIO("当前记录为空");
		Sleep(1000);
		bukaqingping();
	}
	else {
		for (size_t i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->peopleArray[i].m_Name << "\t";
			cout << "性别:" << (abs->peopleArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->peopleArray[i].m_Age << "\t";
			cout << "电话:" << abs->peopleArray[i].m_Phone << "\t\t";
			cout << "住址:" << abs->peopleArray[i].m_Addr << "\t";

			cout << endl;
		}
	}
	Sleep(abs->m_Size * 1000);
	system("pause");
	bukaqingping();
}

int isExist(Addressbook * abs, string name, int aa = 1) {
	int i, flag = 0, kn[MAX] = { 0 }, flag2 = 0, temp;

	for (i = 0; i < abs->m_Size; i++)
	{
		if (abs->peopleArray[i].m_Name == name)
		{
			kn[flag2] = i;
			flag++;
			flag2++;
		}
	}
	temp = flag;
	if (flag == 1 && aa == 0) {
		cout << "姓名:" << abs->peopleArray[kn[0]].m_Name << "\t";
		cout << "性别:" << (abs->peopleArray[kn[0]].m_Sex == 1 ? "男" : "女") << "\t";
		cout << "年龄:" << abs->peopleArray[kn[0]].m_Age << "\t";
		cout << "电话:" << abs->peopleArray[kn[0]].m_Phone << "\t\t";
		cout << "住址:" << abs->peopleArray[kn[0]].m_Addr << "\t";

		cout << endl;
		system("pause");
		bukaqingping();

	}
	if (flag > 1 && aa == 1) {
		flag = 1;
		cout << "请问您想删除哪个" << name << "?" << endl;
		for (size_t i = 0; i < abs->m_Size; i++)
		{
			if (abs->peopleArray[i].m_Name == name)
			{
				cout << flag << ".\t";
				cout << "姓名:" << abs->peopleArray[i].m_Name << "\t";
				cout << "性别:" << (abs->peopleArray[i].m_Sex == 1 ? "男" : "女") << "\t";
				cout << "年龄:" << abs->peopleArray[i].m_Age << "\t";
				cout << "电话:" << abs->peopleArray[i].m_Phone << "\t\t";
				cout << "住址:" << abs->peopleArray[i].m_Addr << "\t";

				cout << endl;
				flag++;
			}
		}
		cin >> flag2;
		while (flag2 > flag) {
			coutAIO("别闹!");
			cin >> flag2;
		}
		return kn[flag2];
	}
	if (flag > 1 && aa == 0) {
		cout << "有" << flag << "个名为" << name << "的联系人,以下是人员名单:" << endl;
		flag = 1;
		for (size_t i = 0; i < abs->m_Size; i++)
		{
			if (abs->peopleArray[i].m_Name == name)
			{
				cout << flag << ".\t";
				cout << "姓名:" << abs->peopleArray[i].m_Name << "\t";
				cout << "性别:" << (abs->peopleArray[i].m_Sex == 1 ? "男" : "女") << "\t";
				cout << "年龄:" << abs->peopleArray[i].m_Age << "\t";
				cout << "电话:" << abs->peopleArray[i].m_Phone << "\t\t";
				cout << "住址:" << abs->peopleArray[i].m_Addr << "\t";

				cout << endl;
				flag++;
			}
		}
		Sleep(1500);
		system("pause");
		return 0;
	}
	if (flag > 1 && aa == 2) {
		cout << "有" << flag << "个名为" << name << "的联系人。您想改哪个?" << endl;
		flag = 1;
		for (size_t i = 0; i < abs->m_Size; i++)
		{
			if (abs->peopleArray[i].m_Name == name)
			{
				cout << flag << ".\t";
				cout << "姓名:" << abs->peopleArray[i].m_Name << "\t";
				cout << "性别:" << (abs->peopleArray[i].m_Sex == 1 ? "男" : "女") << "\t";
				cout << "年龄:" << abs->peopleArray[i].m_Age << "\t";
				cout << "电话:" << abs->peopleArray[i].m_Phone << "\t\t";
				cout << "住址:" << abs->peopleArray[i].m_Addr << "\t";

				cout << endl;
				flag++;
			}
			cin >> flag2;
			while (flag2 > flag) {
				coutAIO("别闹!");
				cin >> flag2;
			}
			return kn[flag2];
		}
		return 0;
	}
	if (temp == 1) {
		return kn[0];
	}
	bukaqingping();
	coutAIO("查无此人");
	Sleep(1500);
	bukaqingping();
	return -1;
}

void save(Addressbook * abs) {
	ofstream ofs;
	ofs.open("MyFriend.txt", ios::out);
	for (size_t i = 0; i < abs->m_Size; i++)
	{
		ofs << abs->peopleArray[i].m_Name << ""
			<< abs->peopleArray[i].m_Sex << " "
			<< abs->peopleArray[i].m_Age << " "
			<< abs->peopleArray[i].m_Phone << " "
			<< abs->peopleArray[i].m_Addr << endl;
	}
	ofs.close();
}

void read(Addressbook * abs) {
	ifstream ifs;
	ifs.open("MyFriend.txt", ios::in);
	if (!ifs.is_open()) {
		return;
	}
	int i = 0, buf1, flag = 0, j = 0, k = 0, j1 = 0, temp = 0;
	string buf = "", flag2 = "";
	while (ifs >> buf) {//读入,后面的思想是找到空格后读入它前面的参数。
		buf1 = (int)buf[i];
		temp = numberaio(buf);
		for (i = 0; i < temp; i++)
		{
			if (buf1 == 32 && flag == 0) {
				for (j = 0; j < i; j++)
				{
					abs->peopleArray[i].m_Name[j] = buf[i];
				}
				flag++;
				continue;
			}
			if (buf1 == 32 && flag == 1) {
				for (k = j; k < i; k++)
				{
					flag2[j1] = buf[k];
					j1++;
				}
				abs->peopleArray[i].m_Sex = zfzh(flag2);
				flag++;
				j = ++k;
				j1 = 0;
				flag2 = "";
				continue;
			}
			if (buf1 == 32 && flag == 2) {
				for (k = j; k < i; k++)
				{
					flag2[j1] = buf[k];
					j1++;
				}
				abs->peopleArray[i].m_Age = zfzh(flag2);
				flag++;
				j = ++k;
				j1 = 0;
				flag2 = "";
				continue;
			}
			if (buf1 == 32 && flag == 3) {
				for (k = j; k < i; k++)
				{
					abs->peopleArray[i].m_Phone[k] = buf[i];
				}
				flag++;
				j = ++k;
				continue;
			}
			if (flag == 4) {
				for (k = j; k < temp; k++)
				{
					abs->peopleArray[i].m_Addr[k] = buf[i];
				}
				return;
			}
		}
		i++;
		flag = 0;
	}
	ifs.close();
}

void deletePeople(Addressbook * abs) {
	coutAIO("请输入需要删除联系人的姓名:");
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		for (size_t i = ret; i < abs->m_Size; i++)
		{
			abs->peopleArray[i] = abs->peopleArray[i + 1];
		}
		abs->m_Size--;
		bukaqingping();
		coutAIO("删除成功");
		Sleep(1500);
	}
}

void findPerson(Addressbook * abs) {
	coutAIO("请输入您需要查找的联系人:");
	string name;
	cin >> name;
	isExist(abs, name, 0);
}

void modifyPeople(Addressbook * abs) {
	string name;
	coutAIO("请输入您要修改的联系人:", 1, 1);
	cin >> name;
	int ret = isExist(abs, name, 2);
	if (ret == -1) {
		return;
	}
	abs->peopleArray[ret].m_Name = name;
	int sex;
	while (true)
	{
		coutAIO("请认真输入您朋友的性别,1男 2女:", 1, 1);
		cin >> sex;
		if (sex != 1 && sex != 2)
		{
			coutAIO("再再言兑一一遍遍,11是是男男,22是是女子。", 3);
			continue;
		}
		abs->peopleArray[ret].m_Sex = sex;
		break;
	}

	int age;
	while (true)
	{
		age = 0;
		coutAIO("请认真输入您朋友的年龄:", 1, 1);
		cin >> age;
		if (age >= 200)
		{
			coutAIO("口恩,水舌得真久", 3);
			continue;
		}
		else if (age < 0)
		{
			coutAIO("口圭口我,这个人竟然没出生。", 1);
			continue;
		}
		else {
			abs->peopleArray[ret].m_Age = age;
			break;
		}
	}

	string phone;
	coutAIO("请认真输入您朋友的联系电话:", 1, 1);
	cin >> phone;
	abs->peopleArray[ret].m_Phone = phone;

	string address;
	coutAIO("请认真输入您朋友的住址:", 1, 1);
	cin >> address;
	abs->peopleArray[ret].m_Addr = address;
}

void my_cls(Addressbook * abs) {
	abs->m_Size = 0;
	coutAIO("已清空");
}



int main()
{
	srand((unsigned)time(NULL));
	system("title 通讯录管理系统");
	int num;
	Addressbook abs;
	read(&abs);
	while (true)
	{
		showMenu();
		cin >> num;
		switch (num)
		{
		case 1:
			AddPeople(&abs);
			break;
		case 2:
			showPeople(&abs);
			break;
		case 3:
			deletePeople(&abs);
			break;
		case 4:
			findPerson(&abs);
			break;
		case 5:
			modifyPeople(&abs);
			break;
		case 6:
			my_cls(&abs);
			break;
		case 0://直接结束主函数
			coutAIO("欢迎下次使用", 1);
			save(&abs);
			system("pause");
			bukaqingping();
			system("pause");
			exit(0);
			break;
		default:
			coutAIO("抱歉", 1, 1);
			coutAIO(",", 5, 1);
			coutAIO("暂时没有其他功能。", 1);
			system("pause");
			break;
		}

		bukaqingping();
	}
	return 0;
}