拆分并删除重复项

时间:2015-10-16 04:34:57

标签: c++

好的,我修复了上一个错误。谢谢,但是现在当我想询问用户是否想要再次播放时,我会运行打印菜单功能,但它会跳过"输入一个句子"声明......任何想法为什么?先感谢您。

#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);



    }
    }

1 个答案:

答案 0 :(得分:0)

问题不在于你的循环以及你如何打印每个单词,它是如何删除重复的。将每个单词插入到一个集合中并不会保留它们输入的原始顺序。当您在循环中打印出单词时输出不正确的原因是因为这一行:{{1其中标记声明为tokens.insert(buf);

你甚至有一条评论说&#34;创建矢量来保持我们的话语&#34;这就是你应该做的,因为如果你只是使用它的&#34; .push_back()&#34;那么向量将保留顺序。成员函数以及&#34; find()&#34;用于防止重复插入的功能。

我如何解决它:

  1. set<string> tokens; - &gt; set<string> tokens;

  2. vector<string> tokens;

    1. 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); } - &gt; for(set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it)