Advertisement
Lawnknome

recConvert

Nov 2nd, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. /***************************************************************************************
  2. * Author:                       Jared Hughes
  3. * Date Created:                 11/1/2014
  4. * Last date Modified:           11/2/2014
  5. * Filename:                     recConvert.cpp
  6. *
  7. * Overview:
  8. *   This program will either take a decimal number or binary number from user input
  9. *   and convert the number to the other option (dec -> bin || bin -> dec).  The third
  10. *   menu option is to simply end the program.
  11. *
  12. * Input:
  13. *   Simple input obtained from the user includes a int data type decimal base 10 number
  14. *   or a binary number (base 2).
  15. *
  16. *       Example: Decimal = 10
  17. *                Binary = 1010
  18. *
  19. * Output:
  20. *   Output of this program will be a simple decimal or binary number given to the user
  21. *   after conversion.
  22. *
  23. *       Example:  Input decimal - > 10
  24. *                 Output binary - > 1010
  25. *
  26. ****************************************************************************************/  
  27. #include <iostream>
  28. #include <cstdlib>
  29. #include <string>
  30. #include <cmath>
  31. using namespace std;
  32.  
  33. string tobinary(int decimal_to);        //function to convert decimal number to binary
  34. int todecimal (string binary_to);       //function to convert binary to decimal
  35.  
  36. int main()
  37. {
  38.     int menu;               //menu selection
  39.     string binary;          //intial binary string to be converted
  40.     int decimal;            //decimal number input to be converted
  41.     char again;             //while loop to repeat program
  42.  
  43.     do
  44.     {
  45.         cout << "\nThis is a number converting program.\n\n";
  46.         cout << "1. Binary to decimal conversion.\n";
  47.         cout << "2. Decimal(as integer) to binary conversion.\n";
  48.         cout << "3. Exit program.\n";
  49.         cout << "\nPlease enter your choice: ";
  50.         cin >> menu;
  51.         cin.ignore(256, '\n');
  52.  
  53.         //Validate that a correct menu choice was made
  54.         while(menu < 1 || menu > 3)
  55.         {  
  56.             cout << "\nPlease select a VALID choice.\n";
  57.             cout << "1. Binary to decimal conversion.\n";
  58.             cout << "2. Decimal to binary conversion.\n";
  59.             cout << "3. Exit program.\n";
  60.             cout << "\nPlease enter your choice: ";
  61.             cin >> menu;
  62.             cin.ignore(256, '\n');
  63.         }
  64.  
  65.         //Binary to decimal
  66.         if(menu == 1)
  67.         {
  68.             cout << "You have selected binary to decimal.\n";
  69.             cout << "Please enter a number to be converted.\n";
  70.             getline(cin, binary);
  71.  
  72.             validate:
  73.  
  74.             for(int x = 0; x < binary.length(); x++)
  75.             {      
  76.                 if(binary.at(x) != '1' && binary.at(x) != '0')
  77.                 {  
  78.                     cout << "Please enter a valid binary number.\n";
  79.                     getline(cin, binary);
  80.                     goto validate;
  81.                 }  
  82.             }
  83.            
  84.             cout << "Your decimal equivalent of " << binary << " is: ";
  85.             cout << todecimal(binary) << endl;
  86.         }
  87.  
  88.         //decimal to binary
  89.         if(menu == 2)
  90.         {
  91.             cout << "You have selected decimal to binary.\n";
  92.             cout << "Please enter an integer to be converted.\n";
  93.            
  94.             while (!(cin >> decimal))
  95.             {
  96.             cin.clear();
  97.             cin.ignore(1000, '\n');
  98.             cout << "Please enter a valid integer: ";
  99.             }  
  100.  
  101.             cout << "Your binary equivalent of " << decimal << " is: ";
  102.             cout << tobinary(decimal) << endl;
  103.  
  104.  
  105.         }  
  106.  
  107.         //end program
  108.         if(menu == 3)
  109.         {
  110.             cout << "This program is concluded.\n";
  111.             return 0;
  112.         }
  113.  
  114.         cout << "Would you like to start again? (Y/N)";
  115.         cin >> again;
  116.         cin.ignore(256, '\n');
  117.         cout << endl;
  118.    
  119.     //repeat program
  120.     }while(again == 'Y' || again == 'y');      
  121.    
  122.     return 0;
  123. }
  124.  
  125. /***********************************************************
  126. * Entry: decimal_to is the input decimal from int main().
  127. *        
  128. * Exit: Will return to main when decimal_to == 0
  129. *       Will return the binary equivalent of decimal_to in
  130. *       a string data type.
  131. *
  132. * Purpose: To convert a binary number to a decimal.
  133. ************************************************************/
  134.  
  135. string tobinary (int decimal_to)
  136. {
  137.     string from_decimal = "";               //string to hold the binary number
  138.  
  139.     if(decimal_to == 0)
  140.     {  
  141.         return from_decimal;
  142.     }
  143.        
  144.     else if((decimal_to % 2) > 0)
  145.     {
  146.         from_decimal += '1';
  147.         (decimal_to /= 2);
  148.         return (tobinary(decimal_to) + from_decimal);
  149.     }
  150.    
  151.     else
  152.     {
  153.         from_decimal += '0';
  154.         (decimal_to /= 2);
  155.         return (tobinary(decimal_to) + from_decimal);
  156.     }  
  157. }
  158.  
  159.  
  160. int todecimal(string binary_to)
  161. {      
  162.     int power_factor;
  163.     long temp_num;
  164.  
  165.     power_factor = binary_to.length()-1;
  166.  
  167.     temp_num = pow(2, power_factor) * (binary_to.at(0) == '1' ? 1 : 0);
  168.  
  169.     if(power_factor > 0)
  170.     {  
  171.         return temp_num + todecimal(binary_to.substr(1, power_factor));
  172.     }
  173.        
  174.     else
  175.     {  
  176.         return temp_num;
  177.     }  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement