All pastes #768803 Raw Edit

Converter

public cpp v1 · immutable
·published 2007-11-10 21:57 UTC
rendered paste body
/*Title: Number ConverterAuthor: VanessaDate: 11/8/07Description: Convert a number that the user gives              between decimal, hexidecimal and binary. */#include<iostream>#include<cstring>#include<math.h>using namespace std;int menu();void decToBin(int dec);void decToHex(int dec);void binToDec(int bin[], int size);void binToHex(int bin[], int size);void hexToDec(int hex);void hexToDec(char hex);void hexToBin(int hex);void hexToBin(char hex);int main(){    int cont = 1;        while(cont)    {        menu();        cont = menu();                   }        return 0;}int menu(){    int choice, dec, hex, bin[8], next, select, numberUsed, index = 0;    char hexa;        cout << "What would you like to do?" << endl;    cout << "1) Convert a decimal number to binary" << endl;    cout << "2) Convert a decimal number to hexadecimal" << endl;    cout << "3) Convert a binary number to decimal" << endl;    cout << "4) Convert a binary number to hexadecimal" << endl;    cout << "5) Convert a hexadecimal number/letter to decimal" << endl;    cout << "6) Convert a hexadecimal number/letter to binary" << endl;    cout << " Please enter 0 to exit." << endl;    cin >> choice;        switch(choice)    {                  case 1:                       cout << "Please enter the decimal number: ";                       cin >> dec;                       decToBin(dec);                       return 1;                       break;                                         case 2:                       cout << "Please enter the decimal number: ";                       cin >> dec;                       decToHex(dec);                       return 1;                       break;                                    case 3:                       int x;                       char c;                       cout << "Please enter the binary number(put a negative number at the end): ";                       cin >> c;                       x =(int)(c - '0');                       while(((x == 0) || (x == 1) ) && (index < 7))                       {                                   bin[index] = x;                                   index++;                                   cin >> c;                                   x =(int)(c - '0');                        }                       numberUsed = index;                       binToDec(bin, numberUsed);                       return 1;                       break;                                          case 4:                       cout << "Please enter the binary number(put a negative number at the end): ";                       cin >> c;                       x =(int)(c - '0');                       while(((x == 0) || (x == 1) ) && (index < 7))                       {                                   bin[index] = x;                                   index++;                                   cin >> c;                                   x =(int)(c - '0');                                                  }                       numberUsed = index;                       binToHex(bin, numberUsed);                       return 1;                       break;                                         case 5:                       cout << "Please enter, " << endl;                       cout << "1) To enter a hexadecimal number" << endl;                       cout << "2) To enter a hexadecimal letter" << endl;                       cin >> select;                       if(select == 1)                       {                                 cout << "Please enter the number: ";                                 cin >> hex;                                 hexToDec(hex);                       }                       if(select == 2)                       {                                 cout << "Please enter the letter: ";                                 cin >> hexa;                                  hexToDec(hexa);                                }                       return 1;                       break;                                    case 6:                       cout << "Please enter, " << endl;                       cout << "1) To enter a hexadecimal number" << endl;                       cout << "2) To enter a hexadecimal letter" << endl;                       cin >> select;                       if(select == 1)                       {                                 cout << "Please enter the number: ";                                 cin >> hex;                                 hexToBin(hex);                       }                       if(select == 2)                       {                                 cout << "Please enter the letter: ";                                 cin >> hexa;                                  hexToBin(hexa);                                }                       return 1;                       break;                                    default:                       return 0;       }}void decToBin(int dec){     int binary[8];          for(int i = 7; i >= 0; i--)     {             binary[i] = dec%2;             dec = dec/2;         }              cout << " The number is: " << binary[0] << binary[1] << binary[2] << binary[3]           << binary[4] << binary[5] << binary[6] << binary[7] << endl << endl;}void decToHex(int dec){     int binary[8];          for(int i = 7; i >= 0; i--)     {             binary[i] = dec%2;             dec = dec/2;         }            double sum = 0, sum2 = 0;      int s = 8;          cout << "The number is: ";          for( int j = 0; j <= 3; j++)     {           if(s >0)          {               sum2 = sum2 + ((binary[s-1])*(pow(2, j)));               s--;           }     }                   for( int j = 4; j <= 7; j++)     {           if(s >0)          {               sum = sum + ((binary[s-5])*(pow(2, j)));               s--;           }     }          if( sum > 0)     {         if(sum < 10)             cout << sum;         else if( sum == 10)             cout << "a";         else if( sum == 11)             cout << "b";         else if( sum == 12)             cout << "c";         else if( sum == 13)             cout << "d";         else if( sum == 14)             cout << "e";         else if( sum == 15)             cout << "f";     }          if(sum2 > 0)     {         if(sum2 < 10)             cout << sum2;         else if( sum2 == 10)             cout << "a";         else if( sum2 == 11)             cout << "b";         else if( sum2 == 12)             cout << "c";         else if( sum2 == 13)             cout << "d";         else if( sum2 == 14)             cout << "e";         else if( sum2 == 15)             cout << "f";     }     cout << endl << endl;     }void binToDec(int bin[], int size){     double sum = 0;      int s = size;           for( int j = 0; j <= 7; j++)     {           if(s >0)          {               sum = sum + ((bin[s-1])*(pow(2, j)));               s--;           }     }          cout << " The number is: " << sum << endl << endl;}void binToHex(int bin[], int size){     double sum = 0, sum2 = 0;      int s = size;          cout << "The number is: ";          for( int j = 0; j <= 3; j++)     {           if(s >0)          {               sum2 = sum2 + ((bin[s-1])*(pow(2, j)));               s--;           }     }                   for( int j = 4; j <= 7; j++)     {           if(s >0)          {               sum = sum + ((bin[s-5])*(pow(2, j)));               s--;           }     }          if( sum > 0)     {         if(sum < 10)             cout << sum;         else if( sum == 10)             cout << "a";         else if( sum == 11)             cout << "b";         else if( sum == 12)             cout << "c";         else if( sum == 13)             cout << "d";         else if( sum == 14)             cout << "e";         else if( sum == 15)             cout << "f";     }          if(sum2 > 0)     {         if(sum2 < 10)             cout << sum2;         else if( sum2 == 10)             cout << "a";         else if( sum2 == 11)             cout << "b";         else if( sum2 == 12)             cout << "c";         else if( sum2 == 13)             cout << "d";         else if( sum2 == 14)             cout << "e";         else if( sum2 == 15)             cout << "f";     }     cout << endl << endl;     }void hexToDec(int hex){     int binary[8];          for(int i = 7; i >= 0; i--)     {             binary[i] = hex%2;             hex = hex/2;         }                       double sum = 0;      int s = 8;           for( int j = 0; j <= 7; j++)     {           if(s >0)          {               sum = sum + ((binary[s-1])*(pow(2, j)));               s--;           }     }          cout << " The number is: " << sum << endl << endl;}void hexToDec(char hex){     int hexa;     if(hex == 'a')            hexa = 10;     else if(hex == 'b')          hexa = 11;     else if(hex == 'c')          hexa = 12;     else if(hex == 'd')          hexa = 13;       else if(hex == 'e')          hexa = 14;      else if(hex == 'f')          hexa = 15;               hexToDec(hexa);}void hexToBin(int hex){     int binary[8];          for(int i = 7; i >= 0; i--)     {             binary[i] = hex%2;             hex = hex/2;         }         cout << " The number is: " << binary[0] << binary[1] << binary[2] << binary[3]           << binary[4] << binary[5] << binary[6] << binary[7] << endl << endl;}void hexToBin(char hex){     int hexa;     if(hex == 'a')            hexa = 10;     else if(hex == 'b')          hexa = 11;     else if(hex == 'c')          hexa = 12;     else if(hex == 'd')          hexa = 13;       else if(hex == 'e')          hexa = 14;      else if(hex == 'f')          hexa = 15;               hexToBin(hexa);     }