Advertisement
myname0

практика_мэп_10

Jul 8th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <map>
  2. #include <fstream>
  3. #include <string>
  4. #include <utility>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     map <string, int> myMap;
  12.     map <int, int> myMap2;
  13.     int last = NULL;
  14.     ifstream in("input.txt");
  15.     string tmp;
  16.     int j = 0;
  17.     int indx = 1;  
  18.     while (in >> tmp)
  19.     {
  20.         bool flag = true;
  21.         for (unsigned int i = 0; i < tmp.size(); i++)
  22.             if (!(tmp[i] == '-' || isdigit(tmp[i])))
  23.                 flag = false;
  24.         if (flag)
  25.         {
  26.             stringstream ss(tmp);
  27.             int temp;
  28.             ss >> temp;
  29.             last = temp;
  30.             if (myMap2.count(temp))
  31.                 (*myMap2.find(temp)).second++;
  32.             else myMap2.insert(pair <int, int>(temp, indx));
  33.         }
  34.         if (!flag)
  35.         {
  36.             string str;
  37.             str = tmp;
  38.             if (myMap.count(str))
  39.                 (*myMap.find(str)).second++;
  40.             else myMap.insert(pair <string, int>(str, indx));
  41.         }
  42.     }
  43.     in.close();
  44.     ofstream out("output.txt");
  45.     if (last != NULL)
  46.     {
  47.         if (myMap.empty())
  48.         {
  49.             out << "There is no words!";
  50.         }
  51.         else
  52.         {
  53.             int m = myMap2[last];
  54.             for (map <string, int>::iterator iter = myMap.begin(); iter != myMap.end(); iter++)
  55.                 if (iter->second == m)
  56.                 {
  57.                     out << iter->first << " - " << iter->second << " entries" << endl;
  58.                     j++;
  59.                 }
  60.         }
  61.         if (j == 0)
  62.             out << "There is no words that occur as many as the last word!";
  63.     }
  64.     else out << "Therer is no numbers!";
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement