set和multiset小练习

// set容器小练习.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 题目:
//1.设置一个电话簿应用程序,使其能够根据电话号码查找人名(提示:调整运算符 < 和 == ,确保根据电话号码对元素进行比较和排序)。
//2.定义一个multiset来存储单词及其含义,即将multiset用作词典(提示:multiset存储的对象应是一个包含两个字符串的结构,其中一个字符串为单词,另一个字符串是单词的含义)。

#include <iostream>
#include <sstream>
#include <string>
#include <set>
using namespace std;

struct CallTable
{
	string name;
	long PhoneNumber;
	string OutputString;

	CallTable() {};
	void CallTable_Init(const string name, const long PhoneNumber)
	{
		this->name = name;
		this->PhoneNumber = PhoneNumber;
	}
	CallTable(const string name, const long PhoneNumber)
	{
		this->name = name;
		this->PhoneNumber = PhoneNumber;
	}
	operator const char*()
	{
		ostringstream Output;
		Output << name << "的电话为" << PhoneNumber << endl;
		OutputString = Output.str();
		return OutputString.c_str();
	}
	bool operator == (const CallTable &table) const
	{
		return (this->name == table.name) && (this->PhoneNumber == PhoneNumber);
	}
	bool operator == (const string &name) const
	{
		return this->name == name;
	}
	bool operator == (const long &PhoneNumber) const
	{
		return this->PhoneNumber == PhoneNumber;
	}
	bool operator > (const CallTable &table) const
	{
		return this->PhoneNumber > table.PhoneNumber;
	}
	bool operator  < (const CallTable &table) const
	{
		return this->PhoneNumber < table.PhoneNumber;
	}
	friend ostream& operator <<(ostream& output, const CallTable& table);
};
ostream& operator <<(ostream& output, const CallTable& table)
{
	output << table.name << "的电话为" << table.PhoneNumber << endl;
	return output;
}
struct DefaultDescendOrder
{
	bool operator()(const CallTable &table1, const CallTable &table2)
	{
		return table1 > table2;
	}
};

void Question1()
{
	cout << "欢迎使用Phone Table功能" << endl;
	static set<CallTable, DefaultDescendOrder> table;
	set<CallTable, DefaultDescendOrder>::const_iterator itor1;
	CallTable tablen;
	string name;
	long PhoneNumber;
	int Mode = 0;

	/*Mode = 1为输入信息;
	Mode = 2为用姓名索引读取,Mode = 3为用电话索引读取;
	Mode = 4为用电话索引删除,Mode = 5为用姓名索引删除;
	Mode = 6为退出PhoneTable*/
	while (1)
	{
	Recin:cout << "请输入Mode:";
		cin >> Mode;
		if (!(Mode == 1 || Mode == 2 || Mode == 3 || Mode == 4 || Mode == 5 || Mode == 6))
		{
			cout << "输入Mode值无效,请重新输入Mode值" << endl;
			goto Recin;
		}
		itor1 = table.cbegin();


		if (Mode == 1)
		{
			cout << "姓名:"; cin >> name;
			cout << "电话:"; cin >> PhoneNumber;
			tablen.CallTable_Init(name, PhoneNumber);
			table.insert(table.cend(), tablen);
			cout << "插入成功" << endl;
		}
		else if (Mode == 2)
		{
			cout << "请输入需要索引读取的姓名:" << endl;
			cin >> name;
			itor1 = find(table.cbegin(), table.cend(), name);
			if (itor1 != table.end())
			{
				cout << *itor1 << endl;
				cout << "索引成功" << endl;
			}
			else
			{
				cout << "索引失败" << endl;
			}
		}
		else if (Mode == 3)
		{
			cout << "请输入需要索引读取的电话:" << endl;
			cin >> PhoneNumber;
			itor1 = find(table.cbegin(), table.cend(), PhoneNumber);
			if (itor1!=table.end())
			{
				cout << *itor1 << endl;
				cout << "索引成功" << endl;
			}
			else
			{
				cout << "索引失败" << endl;
			}
		}
		else if (Mode == 4)
		{
			cout << "请输入需要索引删除的电话:" << endl;
			cin >> PhoneNumber;
			itor1 = find(table.cbegin(), table.cend(), PhoneNumber);
			if (itor1!=table.end())
			{
				table.erase(itor1);
				cout << "删除成功" << endl;
			}
			else
			{
				cout << "删除失败" << endl;
			}
			
		}
		else if (Mode == 5)
		{
			cout << "请输入需要索引删除的姓名:" << endl;
			cin >> name;
			itor1 = find(table.cbegin(), table.cend(), name);
			if (itor1 != table.end())
			{
				table.erase(itor1);
				cout << "删除成功" << endl;
			}
			else
			{
				cout << "删除失败" << endl;
			}
		}
		else if (Mode == 6)
		{
			cout << "退出Phone Table"s << endl;
			break;
		}
	}
}

struct Dictionary
{
	string word;
	string explanation;
	string OutputString;

	Dictionary() {};
	Dictionary(string word, string explanation)
	{
		this->word = word;
		this->explanation = explanation;
	}
	void Dictionary_Init(string word, string explanation)
	{
		this->word = word;
		this->explanation = explanation;
	}
	bool operator == (const string &word) const
	{
		return this->word == word;
	}
	operator const char*() 
	{
		ostringstream Output;
		Output << this->word << "的解释为" << this->explanation << endl;
		OutputString = Output.str();
		return OutputString.c_str();
	}
	bool operator < (const Dictionary& dictionary) const
	{
		return this->word > dictionary.word;
	}
	friend ostream& operator << (ostream& Output, Dictionary& dic);
};
ostream& operator << (ostream& Output, const Dictionary& dic)
{
	Output << dic.word << "的释义为:" << dic.explanation << endl;
	return Output;
}
struct DefaultAscendOrder 
{
	bool operator()(const Dictionary& dic1, const Dictionary& dic2)
	{
		return dic1 < dic2;
	}
};
/*模式选择;
Mode=1为读取模式;
Mode=2为录入数据模式;
Mode=3为删除数据模式;
Mode=4为退出字典模式;*/
void Question2()
{
	Dictionary dictionaryx, dictionary1("analysis", "理解"), dictionary2("sun", "太阳");
	multiset<Dictionary, DefaultAscendOrder> dictionary{ dictionary1,dictionary2 };
	multiset<Dictionary, DefaultAscendOrder>::iterator itor1;
	string word;
	string explanation;
	int Mode = 0;

	while (1)
	{
		itor1 = dictionary.cbegin();
	Recin:cout << "请选择模式:";
		cin >> Mode;
		if (!(Mode == 1 || Mode == 2 || Mode == 3 || Mode == 4))
		{
			cout << "输入值无效!" << endl;
			goto Recin;
		}

		if (Mode == 1)
		{
			cout << "请输入要查询单词:";
			cin >> word;
			itor1 = find(dictionary.cbegin(), dictionary.cend(), word);
			if (itor1 != dictionary.end())
			{
				cout << *itor1 << endl;
				cout << "查询成功" << endl;
			}
			else
			{
				cout << "无您要查询的单词" << endl;
			}
		}
		else if (Mode == 2)
		{
			cout << "请输入补充的单词:";
			cin >> word;
			cout << "该单词的释义为:";
			cin >> explanation;
			dictionaryx.Dictionary_Init(word, explanation);
			dictionary.insert(dictionary.cend(), dictionaryx);
			cout << "插入成功" << endl;
		}
		else if (Mode == 3)
		{
			cout << "请输入要删除的单词:";
			cin >> word;
			itor1 = find(dictionary.cbegin(), dictionary.cend(), word);
			if (itor1 != dictionary.cend())
			{
				dictionary.erase(itor1);
				cout << "删除成功" << endl;
			}
			else
			{
				cout << "删除失败!" << endl;
			}
		}
		else if (Mode == 4)
		{
			cout << "退出字典" << endl;
			break;
		}
	}
}

int main()
{
	cout << "Question1" << endl;
	Question1();
	cout << "Question2" << endl;
	Question2();
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肥肥胖胖是太阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值