Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. typedef map<string, short> KeyMap;
  9. typedef pair<string, short> Key;
  10.  
  11. void processString(const string &s, ofstream &ofile)
  12. {
  13.     KeyMap numberMap;
  14.     Key b2("abc", 2);
  15.     Key b3("def", 3);
  16.     Key b4("ghi", 4);
  17.     Key b5("jkl", 5);
  18.     Key b6("mno", 6);
  19.     Key b7("pqrs", 7);
  20.     Key b8("tuv", 8);
  21.     Key b9("wxyz", 9);
  22.     Key b0(" ", 0);
  23.  
  24.     numberMap.insert(b2);
  25.     numberMap.insert(b3);
  26.     numberMap.insert(b4);
  27.     numberMap.insert(b5);
  28.     numberMap.insert(b6);
  29.     numberMap.insert(b7);
  30.     numberMap.insert(b8);
  31.     numberMap.insert(b9);
  32.     numberMap.insert(b0);
  33.  
  34.     typedef KeyMap::const_iterator iter;
  35.  
  36.     string space;
  37.     iter iterator_result;
  38.     iter j;
  39.     bool found = false;
  40.  
  41.     for(unsigned int i = 0; i < s.size(); i++) {
  42.         for(j= numberMap.begin(); j != numberMap.end(); j++ ) {
  43.             space = "";
  44.             if(j->first.find(s[i]) != string::npos) {
  45.                 found = true;
  46.                 if(j->first.find(s[i+1]) != string::npos)
  47.                     space = " ";
  48.                 break;
  49.             }
  50.         }
  51.         if( found ) {
  52.             for( unsigned int k = 0; k < j->first.size() ; k++) {
  53.                 ofile << j->second;
  54.                 if(s[i] == j->first[k]) {
  55.                     if((s[i] == s[i+1]))
  56.                         ofile << ' ';
  57.                     ofile << space;
  58.                     break;
  59.                 }
  60.  
  61.             }
  62.         }
  63.  
  64.     }
  65.     ofile << endl;
  66.  
  67. }
  68.  
  69. void T9SpellingMain()
  70. {
  71.     ifstream ifile("T9input.txt");
  72.     ofstream ofile("T9output.txt");
  73.     stringstream ss;
  74.     int n;
  75.     string s;
  76.     while(std::getline(ifile, s)) {
  77.         ss.str(s);
  78.         ss >> n;
  79.         for(int i = 1; i <= n; i++) {
  80.             s.clear();
  81.             std::getline(ifile, s);
  82.             ofile << "Case #" << i << ": ";
  83.             processString(s, ofile);
  84.             //ofile << ' ';
  85.         }
  86.         ss.clear();
  87.     }
  88.     if(ifile.is_open())
  89.         ifile.close();
  90.     if(ofile.is_open())
  91.         ofile.close();
  92.  
  93. }
  94.  
  95. int main() {
  96.     T9SpellingMain();
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement