Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************************
- * Author: Jared Hughes
- * Date Created: 11/1/2014
- * Last date Modified: 11/2/2014
- * Filename: recConvert.cpp
- *
- * Overview:
- * This program will either take a decimal number or binary number from user input
- * and convert the number to the other option (dec -> bin || bin -> dec). The third
- * menu option is to simply end the program.
- *
- * Input:
- * Simple input obtained from the user includes a int data type decimal base 10 number
- * or a binary number (base 2).
- *
- * Example: Decimal = 10
- * Binary = 1010
- *
- * Output:
- * Output of this program will be a simple decimal or binary number given to the user
- * after conversion.
- *
- * Example: Input decimal - > 10
- * Output binary - > 1010
- *
- ****************************************************************************************/
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <cmath>
- using namespace std;
- string tobinary(int decimal_to); //function to convert decimal number to binary
- int todecimal (string binary_to); //function to convert binary to decimal
- int main()
- {
- int menu; //menu selection
- string binary; //intial binary string to be converted
- int decimal; //decimal number input to be converted
- char again; //while loop to repeat program
- do
- {
- cout << "\nThis is a number converting program.\n\n";
- cout << "1. Binary to decimal conversion.\n";
- cout << "2. Decimal(as integer) to binary conversion.\n";
- cout << "3. Exit program.\n";
- cout << "\nPlease enter your choice: ";
- cin >> menu;
- cin.ignore(256, '\n');
- //Validate that a correct menu choice was made
- while(menu < 1 || menu > 3)
- {
- cout << "\nPlease select a VALID choice.\n";
- cout << "1. Binary to decimal conversion.\n";
- cout << "2. Decimal to binary conversion.\n";
- cout << "3. Exit program.\n";
- cout << "\nPlease enter your choice: ";
- cin >> menu;
- cin.ignore(256, '\n');
- }
- //Binary to decimal
- if(menu == 1)
- {
- cout << "You have selected binary to decimal.\n";
- cout << "Please enter a number to be converted.\n";
- getline(cin, binary);
- validate:
- for(int x = 0; x < binary.length(); x++)
- {
- if(binary.at(x) != '1' && binary.at(x) != '0')
- {
- cout << "Please enter a valid binary number.\n";
- getline(cin, binary);
- goto validate;
- }
- }
- cout << "Your decimal equivalent of " << binary << " is: ";
- cout << todecimal(binary) << endl;
- }
- //decimal to binary
- if(menu == 2)
- {
- cout << "You have selected decimal to binary.\n";
- cout << "Please enter an integer to be converted.\n";
- while (!(cin >> decimal))
- {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Please enter a valid integer: ";
- }
- cout << "Your binary equivalent of " << decimal << " is: ";
- cout << tobinary(decimal) << endl;
- }
- //end program
- if(menu == 3)
- {
- cout << "This program is concluded.\n";
- return 0;
- }
- cout << "Would you like to start again? (Y/N)";
- cin >> again;
- cin.ignore(256, '\n');
- cout << endl;
- //repeat program
- }while(again == 'Y' || again == 'y');
- return 0;
- }
- /***********************************************************
- * Entry: decimal_to is the input decimal from int main().
- *
- * Exit: Will return to main when decimal_to == 0
- * Will return the binary equivalent of decimal_to in
- * a string data type.
- *
- * Purpose: To convert a binary number to a decimal.
- ************************************************************/
- string tobinary (int decimal_to)
- {
- string from_decimal = ""; //string to hold the binary number
- if(decimal_to == 0)
- {
- return from_decimal;
- }
- else if((decimal_to % 2) > 0)
- {
- from_decimal += '1';
- (decimal_to /= 2);
- return (tobinary(decimal_to) + from_decimal);
- }
- else
- {
- from_decimal += '0';
- (decimal_to /= 2);
- return (tobinary(decimal_to) + from_decimal);
- }
- }
- int todecimal(string binary_to)
- {
- int power_factor;
- long temp_num;
- power_factor = binary_to.length()-1;
- temp_num = pow(2, power_factor) * (binary_to.at(0) == '1' ? 1 : 0);
- if(power_factor > 0)
- {
- return temp_num + todecimal(binary_to.substr(1, power_factor));
- }
- else
- {
- return temp_num;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement