// 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.d(), tablen);cout << "插入成功" << endl;}else if (Mode == 2){cout << "请输入需要索引读取的姓名:" << endl;cin >> name;itor1 = find(table.cbegin(), d(), name);if (itor1 != d()){cout << *itor1 << endl;cout << "索引成功" << endl;}else{cout << "索引失败" << endl;}}else if (Mode == 3){cout << "请输入需要索引读取的电话:" << endl;cin >> PhoneNumber;itor1 = find(table.cbegin(), d(), PhoneNumber);if (itor1!d()){cout << *itor1 << endl;cout << "索引成功" << endl;}else{cout << "索引失败" << endl;}}else if (Mode == 4){cout << "请输入需要索引删除的电话:" << endl;cin >> PhoneNumber;itor1 = find(table.cbegin(), d(), PhoneNumber);if (itor1!d()){ase(itor1);cout << "删除成功" << endl;}else{cout << "删除失败" << endl;}}else if (Mode == 5){cout << "请输入需要索引删除的姓名:" << endl;cin >> name;itor1 = find(table.cbegin(), d(), name);if (itor1 != d()){ase(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 << "的释义为:" << planation << 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(), d(), word);if (itor1 != d()){cout << *itor1 << endl;cout << "查询成功" << endl;}else{cout << "无您要查询的单词" << endl;}}else if (Mode == 2){cout << "请输入补充的单词:";cin >> word;cout << "该单词的释义为:";cin >> explanation;dictionaryx.Dictionary_Init(word, explanation);dictionary.d(), dictionaryx);cout << "插入成功" << endl;}else if (Mode == 3){cout << "请输入要删除的单词:";cin >> word;itor1 = find(dictionary.cbegin(), d(), word);if (itor1 != d()){ase(itor1);cout << "删除成功" << endl;}else{cout << "删除失败!" << endl;}}else if (Mode == 4){cout << "退出字典" << endl;break;}}
}int main()
{cout << "Question1" << endl;Question1();cout << "Question2" << endl;Question2();
}
本文发布于:2024-02-01 02:48:08,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170672689033329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |