[英]Split words and remove duplicates
好吧,我修复了先前的错误。 谢谢,但是现在当我想问用户是否想再次玩游戏时,我运行了打印菜单功能,但是它跳过了“输入句子”语句...为什么有任何想法? 先感谢您。
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <set>
#include <vector>
using namespace std;
void clearScreen();
void printMenu();
int main()
{
//Variables
string s;
char selection;
string w;
string buf;
char select2;
cout << "Enter a paragraph or a sentence : " ; //Get sentence
getline(cin, s);
//Menu
cout << "" << endl;
cout << " Menu " << endl;
cout <<" ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: " ; //Get selection
cin >> selection;
cout << "" << endl;
stringstream ss(s);// Insert the string into a stream
vector<string> tokens;// Create vector to hold words
switch (selection) //Switch statement
{
//If A is chose convert to uppercase
case 'a':
case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for(int i=0; s[i]!='\0'; i++)
{
s[i]=toupper(s[i]);
}
cout << "This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'b':
case 'B':
//If B is chose convert to lowercase
cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i=0; s[i] !='\0'; i++)
{
s[i]=tolower(s[i]);
}
cout << "This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'c':
case 'C':
//If C is chosen delete spaces
cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for(int i=0; i<s.length(); i++)
if(s[i] == ' ') s.erase(i,1);
cout <<"This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'd':
case 'D':
//If D is chosen split words and remove duplicates
cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
while (ss >> buf)
{
if(find(tokens.begin(), tokens.end(), buf) == tokens.end())
tokens.push_back(buf);
}
cout << "This is it: " << endl;
for (vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
cout << "- " << *it << " " << endl;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'e':
case 'E':
//If E is chosen allow the user to search for a word
cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
cout << "" << endl;
s.find(w);
if ( s.find( w ) != std::string::npos )
{
cout << w << " was found in the paragraph. " << endl;
}
else
{
cout << w << " was not found in the paragraph. " << endl;
}
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
}
return 0;
}
//Function prologue
/*********************************************************************
* clearScreen () *
* The purpose of this function is to clear the screen! *
*********************************************************************/
//This clears the screen
void clearScreen()
{
cout << "\33[H\33[2J";
// clears the screen
}
//Function prologue
/*********************************************************************
* printMenu() *
* The purpose of this function is to print the menu again if *
* the user selects Y or y! *
*********************************************************************/
void printMenu()
{
//Variables
string s;
char selection;
string w;
string buf;
char select2;
cout << "Enter a paragraph or a sentence : " ; //Get sentence
getline(cin, s);
//Menu
cout << "" << endl;
cout << " Menu " << endl;
cout <<" ------------------------" << endl;
cout << "" << endl;
cout << "A -- Convert paragraph to all caps " << endl;
cout << "B -- Convert paragraph to all lowercase " << endl;
cout << "C -- Delete whitespaces " << endl;
cout << "D -- Split words & remove duplicates " << endl;
cout << "E -- Search a certain word " << endl;
cout << "" << endl;
cout << "Please select one of the above: " ; //Get selection
cin >> selection;
cout << "" << endl;
stringstream ss(s);// Insert the string into a stream
vector<string> tokens;// Create vector to hold words
switch (selection) //Switch statement
{
//If A is chose convert to uppercase
case 'a':
case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl;
cout << "" << endl;
for(int i=0; s[i]!='\0'; i++)
{
s[i]=toupper(s[i]);
}
cout << "This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'b':
case 'B':
//If B is chose convert to lowercase
cout << "You chose to convert the paragragh to all lowercase" << endl;
cout << "" << endl;
for (int i=0; s[i] !='\0'; i++)
{
s[i]=tolower(s[i]);
}
cout << "This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'c':
case 'C':
//If C is chosen delete spaces
cout << "You chose to delete the whitespaces in the paragraph" << endl;
cout << "" << endl;
for(int i=0; i<s.length(); i++)
if(s[i] == ' ') s.erase(i,1);
cout <<"This is it: " << s << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'd':
case 'D':
//If D is chosen split words and remove duplicates
cout << "You chose to split the words & remove the duplicates in the paragraph" << endl;
cout << "" << endl;
while (ss >> buf)
{
if(find(tokens.begin(), tokens.end(), buf) == tokens.end())
tokens.push_back(buf);
}
cout << "This is it: " << endl;
for (vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
cout << "- " << *it << " " << endl;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
case 'e':
case 'E':
//If E is chosen allow the user to search for a word
cout << "You chose to search for a certain word in the paragraph. " << endl;
cout << "" << endl;
cout << "Enter the word you want to search for: ";
cin >> w;
cout << "" << endl;
s.find(w);
if ( s.find( w ) != std::string::npos )
{
cout << w << " was found in the paragraph. " << endl;
}
else
{
cout << w << " was not found in the paragraph. " << endl;
}
cout << "" << endl;
cout <<"Would you like to try another? Y/N \n ";
cin >> select2;
clearScreen ();
if (select2 == 'Y' || select2 == 'y')
{
printMenu ();
break;
}
else if (select2 != 'Y' || select2 != 'y')
exit(0);
}
}
问题不是循环,而是如何打印每个单词,而是如何删除重复项。 将每个单词插入到集合中并不会保留它们输入的原始顺序。在循环中打印单词时,输出不正确的原因是由于以下这一行: tokens.insert(buf);
其中的标记被声明为set<string> tokens;
您甚至有一条注释,上面写着“创建向量来容纳我们的话”,这是您应该做的,因为如果您只使用其“ .push_back()”成员函数和“ find( )”功能,以防止插入重复项。
我如何解决它:
set<string> tokens;
-> vector<string> tokens;
while (ss >> buf) { // check to make sure vector doesn't contain "buf" if(find(tokens.begin(), tokens.end(), buf) == tokens.end()) tokens.push_back(buf); }
for(set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
-> for(vector<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.