Advertisement
warfighter67

c++ Lab5

Oct 8th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <map>
  2. #include <vector>
  3. #include <iostream>
  4. #include <string>
  5. #include <time.h>
  6. using namespace std;
  7.  
  8. typedef map<int, string> phonemap;
  9. void createPhoneMap(phonemap &mapIn);//create the map for phone Keyboard
  10. void PrintAllString(string phoneIn,phonemap mapIn, int size); //print out all the combinations
  11. void PrintMap(phonemap mapIn); //print out the data in  mapIn
  12.  
  13.  
  14. void recurseLoop()
  15. {
  16.    
  17. }
  18.  
  19.  
  20. phonemap MyMap; //global variable
  21. string phonenum;//global string contains user input phone number
  22.  
  23. int main()
  24. {
  25.     int size;
  26.  
  27.     cout<<"Please input the phone number: ";
  28.     cin>>phonenum;
  29.  
  30.     size = phonenum.size();
  31.  
  32.     cout<<endl;
  33.     createPhoneMap(MyMap);
  34.     PrintMap(MyMap);
  35.     cout<<endl;
  36.     PrintAllString(phonenum, MyMap, size);
  37.  
  38.     system("pause");
  39.     return 0;
  40. }
  41.  
  42. void createPhoneMap(phonemap &mapIn)
  43. {
  44.     for(int x = 0; x<10; x++)
  45.     {
  46.         if(x == 0 || x == 1)
  47.         {
  48.             string input = "";
  49.             input.push_back(x + 42);
  50.             mapIn.insert(phonemap::value_type(x, input));
  51.         }
  52.         else if(x > 1 && x < 9)
  53.         {
  54.             string input = "";
  55.             for(int y=0; y<3; y++)
  56.             {
  57.                 input.push_back((x - 2) * 3 + 65 + y);
  58.             }
  59.             mapIn.insert(phonemap::value_type(x, input));
  60.         }
  61.         else //x == 9
  62.         {
  63.             mapIn.insert(phonemap::value_type(x, "VWXYZ"));
  64.         }
  65.     }
  66.     return;
  67. }
  68.  
  69. void PrintAllString(string phoneIn,phonemap mapIn, int size)
  70. {
  71.     phonemap::iterator finder;
  72.     vector<string> sout(size);
  73.  
  74.     //remove any non-digit char from the number
  75.     for(int a=0; a<size; a++)
  76.     {
  77.         if(!isdigit(phoneIn.at(a)))
  78.         {
  79.             phoneIn.erase(a, 1);
  80.             size--;
  81.             a--;
  82.         }
  83.     }
  84.  
  85.     for(int x=0; x<size; x++)
  86.     {
  87.         finder = mapIn.find(phoneIn.at(x) - 48);
  88.         if(finder != mapIn.end())
  89.         {
  90.             sout[x] = finder->second;
  91.             //cout << finder->second << endl;
  92.         }
  93.         else
  94.         {
  95.             cout << "Not found" << endl;
  96.         }
  97.     }
  98.    
  99.     recurseLoop();
  100.  
  101.     //working part for numbers of length 3
  102.  
  103.     /*for(int i = 0; i < sout[0].size(); i++)
  104.     {
  105.         for(int j = 0; j < sout[1].size(); j++)
  106.         {
  107.             for(int k = 0; k < sout[2].size(); k++)
  108.             {
  109.                 cout << sout[0][i] << sout[1][j] << sout[2][k] << endl;
  110.             }
  111.         }
  112.     }*/
  113.  
  114.     return;
  115. }
  116.  
  117. void PrintMap(phonemap mapIn)
  118. {
  119.     cout << "The phone keyboard is: " << endl;
  120.     for(phonemap::iterator i = mapIn.begin(); i != mapIn.end(); i++)
  121.     {
  122.         cout << "<" << i->first << "," << i->second << ">" << endl;
  123.     }
  124.     return;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement