Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: None  |  size: 5.03 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <map>
  6.  
  7. class initialization
  8. {
  9.     private:
  10.         int amount, highestCount;
  11.         std::string brailleLine, str[3];
  12.  
  13.     public:
  14.  
  15.         std::string readLinesFromText(char *file[]);
  16.         void setAmountOfLines(char *file[]);
  17.         int returnAmount() { return amount; }
  18.         void populateTempCharArr(std::string tempStr1);
  19.         int returnHighestCount(){ return highestCount; }
  20.  
  21. };
  22.  
  23. class brailleReading : public initialization
  24. {
  25.     private:
  26.         std::map<int, std::string> numSequence;
  27.  
  28.     public:
  29.         void populateMemoryCharacterMap();
  30.         int stringToNumber(std::string tempString);
  31.         std::string translateBrailleToEnglish(std::string tempPassStr);
  32.         void initiateTranslation(std::string input);
  33.  
  34. };
  35.  
  36. void initialization::setAmountOfLines(char *file[])
  37. {
  38.     std::string line;
  39.     std::ifstream myFile;
  40.     int count = 0, iterator = 0;
  41.  
  42.  
  43.     myFile.open(file[1]);
  44.  
  45.  
  46.     if(myFile.is_open())
  47.     {
  48.         while(myFile.good())
  49.         {
  50.             getline(myFile, line);
  51.  
  52.             for( ; iterator < 3; iterator++)
  53.                 str[iterator] = line;
  54.  
  55.             ++count;
  56.         }
  57.  
  58.         myFile.close();
  59.  
  60.     }
  61.     else
  62.         std::cout << "Unable to open file\n";
  63.  
  64.  
  65.     amount = count - 1;
  66.  
  67. }
  68.  
  69.  
  70. std::string initialization::readLinesFromText(char *file[])
  71. {
  72.     std::ifstream brailleFile;
  73.     int iterator = 0;
  74.     std::string tempStr, tempStr2;
  75.     std::ostringstream stringStream;
  76.  
  77.     brailleFile.open(file[1]);
  78.  
  79.  
  80.     if(brailleFile.is_open())
  81.     {
  82.         while(brailleFile.good())
  83.         {
  84.             getline(brailleFile, brailleLine, '\0');
  85.  
  86.             stringStream << brailleLine;
  87.  
  88.             tempStr += stringStream.str();
  89.  
  90.  
  91.         }
  92.  
  93.         while(iterator < tempStr.length())
  94.         {
  95.             if((tempStr[iterator] == ' ') || (tempStr[iterator] == '.'))
  96.                 tempStr2 += tempStr[iterator];
  97.  
  98.             ++iterator;
  99.  
  100.         }
  101.  
  102.  
  103.         brailleFile.close();
  104.  
  105.     }
  106.     else
  107.         std::cout << "Unable to open file\n";
  108.  
  109.     return tempStr2;
  110.  
  111. }
  112.  
  113. void brailleReading::populateMemoryCharacterMap()
  114. {
  115.     numSequence[1]     = "a";
  116.     numSequence[2]     = "b";
  117.     numSequence[14]    = "c";
  118.     numSequence[145]   = "d";
  119.     numSequence[15]    = "e";
  120.     numSequence[124]   = "f";
  121.     numSequence[1245]  = "g";
  122.     numSequence[125]   = "h";
  123.     numSequence[24]    = "i";
  124.     numSequence[245]   = "j";
  125.     numSequence[13]    = "k";
  126.     numSequence[123]   = "l";
  127.     numSequence[134]   = "m";
  128.     numSequence[1345]  = "n";
  129.     numSequence[135]   = "o";
  130.     numSequence[1234]  = "p";
  131.     numSequence[12345] = "q";
  132.     numSequence[1235]  = "r";
  133.     numSequence[234]   = "s";
  134.     numSequence[2345]  = "t";
  135.     numSequence[136]   = "u";
  136.     numSequence[1236]  = "v";
  137.     numSequence[2456]  = "w";
  138.     numSequence[1346]  = "x";
  139.     numSequence[13456] = "y";
  140.     numSequence[1356]  = "z";
  141.  
  142. }
  143.  
  144. int brailleReading::stringToNumber(std::string tempString)
  145. {
  146.     int result;
  147.  
  148.     std::stringstream convert(tempString);
  149.  
  150.     if( !(convert >> result) )
  151.         result = 0;
  152.  
  153.     return result;
  154.  
  155. }
  156.  
  157. std::string brailleReading::translateBrailleToEnglish(std::string tempPassStr)
  158. {
  159.     int key = 0;
  160.  
  161.     populateMemoryCharacterMap();
  162.     key = stringToNumber(tempPassStr);
  163.  
  164.     for( std::map<int, std::string>::iterator intIter = numSequence.begin(); intIter != numSequence.end(); ++intIter )
  165.     {
  166.         if( (*intIter).first == key )
  167.             return (*intIter).second;
  168.  
  169.     }
  170. }
  171.  
  172. void brailleReading::initiateTranslation(std::string input)
  173. {
  174.         //starting positions for first braille character
  175.     int letter1 = 0, letter2 = input.length() / 3, letter3 = letter2 * 2, letter4 = 1, letter5 = letter2 + 1, letter6 = letter3 + 1;
  176.     std::string numberSeq = "\0";
  177.  
  178.     while( letter1 <= input.length() / 3 )
  179.     {
  180.         if(input[letter1] == '.')
  181.             numberSeq = numberSeq + '1';
  182.  
  183.         if(input[letter2] == '.')
  184.             numberSeq = numberSeq + '2';
  185.  
  186.         if(input[letter3] == '.')
  187.             numberSeq = numberSeq + '3';
  188.  
  189.         if(input[letter4] == '.')
  190.             numberSeq = numberSeq + '4';
  191.  
  192.         if(input[letter5] == '.')
  193.             numberSeq = numberSeq + '5';
  194.  
  195.         if(input[letter6] == '.')
  196.             numberSeq = numberSeq + '6';
  197.  
  198.  
  199.         letter1 += 3;
  200.         letter2 += 3;
  201.         letter3 += 3;
  202.         letter4 = letter1 + 1;
  203.         letter5 = letter2 + 1;
  204.         letter6 = letter3 + 1;
  205.  
  206.        if(numberSeq == "\0")
  207.            std::cout << ' ';      //this will check for spaces and add them
  208.        else
  209.            std::cout << translateBrailleToEnglish(numberSeq);
  210.  
  211.  
  212.         numberSeq = "\0";
  213.  
  214.     }
  215. }
  216.  
  217. int main(int argc, char *argv[])
  218.  {
  219.  
  220.     brailleReading rayCharles;
  221.  
  222.     rayCharles.setAmountOfLines(argv);
  223.     rayCharles.initiateTranslation( rayCharles.readLinesFromText(argv) );
  224.  
  225.     std::cout << '\n';
  226.  
  227.  
  228.  
  229.     return 0;
  230.  
  231. }
clone this paste RAW Paste Data