Jump to content

Hope This Helps.


Somerset
 Share

Recommended Posts

// This program will let the operator select routines to convert  
// an octal number to decimal, a decimal number to octal, convert 
// a hex to decimal, decimal to hex, find the square root of a
// number, convert keyboard characters to decimal and octal       
// values, or add, subtract, multiply, or divide numbers.         
#include <iostream.h>

void coct_dec (),cdec_oct (),chex_dec (),cdec_hex ();
double absolute_value (double);
void sq_root (),key_value (),arith_value ();

void main ()
{
char crlf_char;
char dis_patch;
// print the menu  
loop1:  cout << "\n\n\n\n\n\n\n\n       MENU";
cout << "\n       ----";
cout << "\n\n       0 = Convert octal number to decimal.";
cout << "\n\n       1 = Convert decimal number to octal.";
cout << "\n\n       2 = Convert hex to decimal.";
cout << "\n\n       3 = Convert decimal to hex.";
cout << "\n\n       4 = Find the square root of a number.";
cout << "\n\n       5 = Convert keyboard to decimal and octal.";
cout << "\n\n       6 = Add, subtract, multiply, or divide.";
cout << "\n\n       7 = Exit program.";
// get menu selection from operator  
loop2:  cout << "\n\n       Enter menu number (0 - 7): ";
loop3:  dis_patch = cin.get ();

if (dis_patch == '\n')
goto loop3;
// now there is only six items on the menu  
if (dis_patch > '7')
{
 cout << "Input the correct menu number!";
goto loop2;
}

// get the number and dispatch to the appropriate function.  
crlf_char = cin.get (); //Get that crlf 
if (dis_patch == '0')
coct_dec ();
if (dis_patch == '1')
cdec_oct ();
if (dis_patch == '2')
chex_dec ();
if (dis_patch == '3')
cdec_hex ();
if (dis_patch == '4')
sq_root ();
if (dis_patch == '5')
key_value ();
if (dis_patch == '6')
arith_value ();

if (dis_patch == '7')
return;
else goto loop1;

}

// function to convert an octal number to decimal  
void coct_dec ()
{               
char ascii_char;
char octal_buf[11] = {'0','0','0','0','0','0','0','0','0','0','0'};
char crlf_char;
char octal_num;
int dec_count;
int index;
long int dec_value;
// prompt the operator to enter the octal number. the octal  
// characters are then stored in an a character buffer until  
// the numbers are terminated with a crlf.  
loop1:  cout << "\n\n       Enter octal number: ";
index = 0;                

do
{
++index;
ascii_char = cin.get ();
octal_buf[index] = ascii_char;
}
while (ascii_char != '\n');                
dec_count = 1;
dec_value = 0;

// we are going to figure this out in binary. mask each bit,   
// starting at the low order bit and calulate the decimal     
// number. if the bit is there, add in the decimal count for   
// that position or update the decimal count for the next      
// binary postion.                                            

do
{

--index; // trash crlf in buffer 
// mask the first binary bit of the ascii character and   
// calculate  
octal_num = octal_buf[index];
octal_num = (octal_num & 1);
if (octal_num == 1)
dec_value = (dec_value + dec_count);
dec_count = (dec_count + dec_count);
// mask the second binary bit of the ascii character and   
// calculate  
octal_num = octal_buf[index];
octal_num = (octal_num & 2);
if (octal_num == 2)
dec_value = (dec_value + dec_count);
dec_count = (dec_count + dec_count);
// mask the third binary bit of the ascii character and  
// calculate  
octal_num = octal_buf[index];
octal_num = (octal_num & 4);
if (octal_num == 4)
dec_value = (dec_value + dec_count);
dec_count = (dec_count + dec_count);

}                
// the end of the buffer contains a crlf  
while (index >= 2); //Last thing in the buffer is \n 

cout << "\n       The answer is: " << dec_value;

cout << "\n\n       Try another number 'y or n': ";
       
// if the answer is no return to menu else ask for another   
// number  
ascii_char = cin.get ();
crlf_char = cin.get (); // get that crlf  

if (ascii_char == 'n')
return;

else goto loop1;

}
// function to convert decimal to octal  
void cdec_oct ()
{
char crlf_char;
char next_digit;
char ascii_char;
int count;
long int dec_num;
int con_num[10] = {0,0,0,0,0,0,0,0,0,0};
// get the decimal number from the operator and save it in a   
// decimal value  
loop1:  cout << "\n\n       Enter decimal number: ";

cin >> dec_num;

cout << "\n       ";
cout << dec_num;               
cout << " in decimal is "; 
count= 0;

// each octal digit is the remainder of the base divided by   
// the base these are saved in a buffer for calculation to     
// an ascii character  
do
{
++count;
con_num[count] = (dec_num % 8);
dec_num = (dec_num / 8);
}               
while (dec_num != 0);

// get the digit from the buffer and make the ascii character                  

do
{
next_digit = (con_num[count] + '0');
cout << next_digit;
--count;
}
while (count != 0);
cout << " in octal.";
cout << "\n\n       Try another number (y or n): ";

crlf_char = cin.get (); // get that crlf  
ascii_char = cin.get ();
// if the answer is no return to menu else ask for another   
// number  

if (ascii_char == 'n')
return;

else goto loop1;
}
// This routine will convert a hex value to decimal  
void chex_dec ()
       
{               
char ascii_char;
char hex_buf[11] = {'0','0','0','0','0','0','0','0','0','0','0'};
char crlf_char;
int dec_count;
int index;
long int dec_value;
// prompt the operator to enter the hex number. the hex  
// characters are then stored in an a character buffer until  
// the numbers are terminated with a crlf.  
loop1:  cout << "\n\n       Enter hex number: ";
index = 0;                

do
{
++index;
ascii_char = cin.get ();
if (ascii_char <= '9')
hex_buf[index] = ascii_char - '0';
else hex_buf[index] = ((ascii_char - '0') - 7);
}
while (ascii_char != '\n');                
dec_count = 1;
dec_value = 0;

// we are going to figure this out in binary. mask each bit,   
// starting at the low order bit and calulate the decimal     
// number. if the bit is there, add in the decimal count for   
// that position or update the decimal count for the next      
// binary postion.                                            

do
{

--index; // trash crlf in buffer 
// get the ascii character out of the buffer and   
// calculate  
if (hex_buf[index] != 0)
dec_value = (dec_value + (hex_buf[index] * dec_count));
dec_count = (dec_count * 16);

}                
// the end of the buffer contains a crlf  
while (index >= 2); //Last thing in the buffer is \n 

cout << "\n       The answer is: " << dec_value;

cout << "\n\n       Try another number 'y or n': ";
       
// if the answer is no return to menu else ask for another   
// number  
ascii_char = cin.get ();
crlf_char = cin.get (); // get that crlf  

if (ascii_char == 'n')
return;

else goto loop1;

}
// function to convert decimal to hex  
void cdec_hex ()
{
char crlf_char;
char next_digit;
char ascii_char;
int count;
long int dec_num;
int con_num[10] = {0,0,0,0,0,0,0,0,0,0};
// get the decimal number from the operator and save it in a   
// decimal value  
loop1:  cout << "\n\n       Enter decimal number: ";

cin >> dec_num;

cout << "\n       ";
cout << dec_num;               
cout << " in decimal is "; 
count= 0;

// each octal digit is the remainder of the base divided by   
//the base these are saved in a buffer for calculation to     
// an ascii character  
do
{
++count;
con_num[count] = (dec_num % 16);
dec_num = (dec_num / 16);
}               
while (dec_num != 0);

// get the digit from the buffer and make the ascii character                  

do
{
if (con_num[count] > 9)
next_digit = (con_num[count] + '0' + 7);
else
next_digit = (con_num[count] + '0');

cout << next_digit;
--count;
}
while (count != 0);
cout << " in hex.";
cout << "\n\n       Try another number (y or n): ";

crlf_char = cin.get (); // get that crlf  
ascii_char = cin.get ();
// if the answer is no return to menu else ask for another   
// number  

if (ascii_char == 'n')
return;

else goto loop1;
}
// calculate the absolute value of a decimal number. if the   
// number is less than 0 then negate the number.             
double absolute_value(float func1,float func2,float func3)
{
double func_num;
func_num = (func1 * func2 - func3);
if (func_num < 0)
func_num = -func_num;
func_num = func_num;
return (func_num);
}
// function to find the square root of a number  
void sq_root ()
{
char ascii_char;
char crlf_char;
int index;
double epsilon;
float guess;
float dec_num;
// get the decimal number from the operator and save it.  
// if guess squared minus the number is greater than or    
// equal to one then thats the number else set guess to    
// the nmuber divided by guess divided by 2 guess again.  
loop1:  guess = 1.0;     // initialize some variables  
epsilon = .00001;
index = 0;
cout << "\n\n       Input decimal number: ";

cin >> dec_num;

if (dec_num < 0)
{
cout << "Cannot square root a negative number";
goto loop1;
}
   while ((absolute_value(guess,guess,dec_num) >= epsilon) && (index <= 20))
{
guess = (((dec_num / guess) + guess) / 2.0);
index = index + 1;
}
 
cout << "\n       Square root is: " << guess;
cout << "\n\n       Try another number (y or n): ";

crlf_char = cin.get (); // get that crlf  
ascii_char = cin.get ();
// if the answer is no then return to menu else ask for next   
// number  
if (ascii_char == 'n')
return;

else goto loop1;
}
// function to convert a keyboard character to its octal
// and then decimal value.  
void key_value ()
{
char ascii_char;
char crlf_char;
char next_digit;
int count;
int oct_num;
int dec_num;
int dec_count;
int con_num[4] = {0,0,0,0};
// prompt the operator for the keyboard character  
loop1:  for (count = 0; count <= 3; ++count)
con_num[count] = 0;
cout << "\n\n       Enter the Keyboard Character: ";

ascii_char = cin.get ();
crlf_char = cin.get (); // get that crlf  
// convert keyboard character to decimal and let it print  
cout << "\n\n       Keyboard Character:  '";
cout << ascii_char;
cout << "'  ";
cout << "is  "; 
 
       //convert character to octal
count = 0;
do
{
++count;
con_num[count] = (ascii_char % 8);
ascii_char = (ascii_char / 8); 
}
while (ascii_char != 0);
       //get the digits from the buffer and print the octal number
do
{
next_digit = (con_num[count] + '0');
cout << next_digit;
--count;
}
while (count > 0);
 
cout << "  in octal and  ";

       // convert octal number to decimal and let it print  
      
dec_num = 0;
dec_count = 1;
count = 1;
do
{
oct_num = (con_num[count] & 1);
if (oct_num == 1)
dec_num = (dec_num + dec_count);
dec_count = (dec_count + dec_count);
oct_num = (con_num[count] & 2);
if (oct_num == 2)
dec_num = (dec_num + dec_count);
dec_count = (dec_count + dec_count);
oct_num = (con_num[count] & 4);
if (oct_num == 4)
dec_num = (dec_num + dec_count);
dec_count = (dec_count + dec_count);

++count;
}
while (count <= 3);
cout << dec_num;
cout << "  in decimal.";

// prompt the operator for a 'y' or a 'n'  
cout << "\n\n       Try another character (y or n): ";

// if the answer is no return to the menu else ask for  
// another keyboard character  
ascii_char = cin.get ();
crlf_char = cin.get (); // get that crlf  
if (ascii_char == 'n')
return;
goto loop1;
}
// function to add, substract, multiply, or divide  
void arith_value ()
{       
char ascii_char;
char crlf_char;
float dec_numa;
float dec_numb;
float dec_num;
// get either a + b, a - b, a * b, or a / b from the operator  
loop1:  cout << "\n\n       Arithmetic operations\n";
cout << "       Examples: '8+2, 9-2, 7*7, or 8/2': ";

cin >> dec_numa >> ascii_char >> dec_numb;
// if it was a + b then add and thats the number a + b = c  
if (ascii_char == '+')

{
dec_num = (dec_numa + dec_numb);
cout << "\n\n       The answer is: ";
cout << dec_numa;
cout << " " << ascii_char << " ";
cout << dec_numb;
cout << " = ";
cout << dec_num;
}
// if it was a - b then subtract and thats the number a - b = c  

if (ascii_char == '-')
{
dec_num = (dec_numa - dec_numb);
cout << "\n\n       The answer is: ";
cout << dec_numa;
cout << " " << ascii_char << " ";
cout << dec_numb;
cout << " = ";
cout << dec_num;
}

// if it was a * b then multiply and thats the number a * b = c  
if (ascii_char == '*')
{
dec_num = (dec_numa * dec_numb);
cout << "\n\n       The answer is: ";
cout << dec_numa;
cout << " " << ascii_char << " ";
cout << dec_numb;
cout << " = ";
cout << dec_num;
}

// if it was a / b then  divide and thats the number a / b = c  
if (ascii_char == '/')
{
dec_num = (dec_numa / dec_numb);
cout << "\n\n       The answer is: ";
cout << dec_numa;
cout << " " << ascii_char << " ";
cout << dec_numb;
cout << " = ";
cout << dec_num;
}

cout << "\n\n       Try some more arithmatic 'y or n': ";
      
crlf_char = cin.get (); // get that crlf  
ascii_char = cin.get ();
// if the answer is no then return to menu else ask for next   
// number  
if (ascii_char == 'n')
return;
else goto loop1;
}

i read in here somewhere i dunno where or remmber but here is a c++ code for converting this to that and back again. not just trying to build up my post counts. :whistle:

Link to comment
Share on other sites

I recognize that code, I looked at that code 9 or 10 months ago when I was trying to figure out how to write a base converter. I didn't like how he did it. My converter can convert freely between base 2 (binary) and base 36 to any other base in that range. If Jon wants code that can do that, I can update mine as it's already using double's and only needs changed from using std::string to Variant or AString types. It needs updated anyway, when I wrote it, I barely knew what I was doing so I'm surprised it works at all (but it does)...

Mine's not too big, 2 functions. One to convert from any base to base 10, the other to convert from base 10 to anything else.

Edited by Valik
Link to comment
Share on other sites

The code looks too complicated for me. There is inbuilt functionality within C++ to perform the conversions between octal, hexadecimal and decimal as well as perform a square root (actually there is also an inbuilt function in C and C++ called fabs which provides the absolute value of a floating point number).

Here is my simplified version of your code

#include <iostream>
#include <iomanip>
#include <sstream>
#include <math.h>

static void sq_root ();
static void key_value ();
static std::string DecimalToOctal(long value);
static std::string DecimalToHex(long value);
static long OctalToDecimal(const std::string& value);
static long HexToDecimal(const std::string& value);
static void cdec_oct();
static void cdec_hex();
static void chex_dec();
static void coct_dec();
static void key_value();

int main()
{
    int dis_patch;
    // Repeat for ever

    while(true)
    {
        // print the menu  
        std::cout << "\n\n\n\n\n\n\n       MENU"
                   << "\n       ----"
                   << "\n\n       0 = Convert octal number to decimal."
                   << "\n\n       1 = Convert decimal number to octal."
                   << "\n\n       2 = Convert hex to decimal."
                   << "\n\n       3 = Convert decimal to hex."
                   << "\n\n       4 = Find the square root of a number."
                   << "\n\n       5 = Convert keyboard to decimal and octal."
                   << "\n\n       6 = Add, subtract, multiply, or divide."
                   << "\n\n       7 = Exit program."
                   << std::endl;    // Flushes the buffer

        // get menu selection from operator  
        std::cout << "\n\n       Enter menu number (0 - 7): ";
        std::cin >> dis_patch;
        switch(dis_patch)
        {
        case 0: coct_dec (); break;
        case 1: cdec_oct (); break;
        case 2: chex_dec (); break;
        case 3: cdec_hex (); break;
        case 4: sq_root ();  break;
        case 5: key_value (); break;
        //case 6: arith_value (); break;
        case 7: return 0;
        default:
            std::cout << "Input the correct menu number!" << std::endl;
        }
    }
    return 0;
}

// function to find the square root of a number  
static void sq_root ()
{
    float dec_num;
    do
    {
        std::cout << "\n\n       Input decimal number: ";
        std::cin >> dec_num;
        if (dec_num < 0)
            std::cout << "Cannot square root a negative number" << std::endl;
        else
        {
            std::cout << "\n       Square root is: " << sqrt(dec_num)
                      << "\n\n       Try another number (y or n): ";
        }
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

static std::string DecimalToOctal(long value)
{
    std::stringstream Temp;
    Temp << std::oct << value;
    return Temp.str();
}

static std::string DecimalToHex(long value)
{
    std::stringstream Temp;
    Temp << std::hex << value;
    return Temp.str();
}

static long OctalToDecimal(const std::string& value)
{
    long result;
    std::stringstream Temp(value);
    Temp >> std::oct >> result;
    return result;
}

static long HexToDecimal(const std::string& value)
{
    long result;
    std::stringstream Temp(value);
    Temp >> std::hex >> result;
    return result;
}

// function to convert decimal to octal  
static void cdec_oct ()
{
    long dec_num;
    do
    {
        std::cout << "\n\n       Enter decimal number: ";
        std::cin >> dec_num;

        std::cout << "\n       " << dec_num
                  << " in decimal is "
                  << DecimalToOctal(dec_num)
                  << " in octal."
                  << "\n\n       Try another number (y or n): ";
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

// function to convert decimal to hex
static void cdec_hex ()
{
    long dec_num;
    do
    {
        std::cout << "\n\n       Enter decimal number: ";
        std::cin >> dec_num;

        std::cout << "\n       " << dec_num
                  << " in decimal is "
                  << DecimalToHex(dec_num)
                  << " in hex."
                  << "\n\n       Try another number (y or n): ";
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

// This routine will convert a hex value to decimal  
static void chex_dec()
{       
    std::string input;
    do
    {
        std::cout << "\n\n       Enter hex number: ";
        std::cin >> input;
        std::cout << "\n       The answer is: " << HexToDecimal(input)
                  << "\n\n       Try another number 'y or n': ";
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

// This routine will convert an octal value to decimal  
static void coct_dec()
{       
    std::string input;
    do
    {
        std::cout << "\n\n       Enter oct number: ";
        std::cin >> input;
        std::cout << "\n       The answer is: " << OctalToDecimal(input)
                  << "\n\n       Try another number 'y or n': ";
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

// function to convert a keyboard character to its octal
// and then decimal value.  
static void key_value()
{
    char ascii_char;
    do
    {
        std::cout << "\n\n       Enter the Keyboard Character: ";
        std::cin >> ascii_char; 
        std::cin.get (); // get and ignore that crlf  
        std::cout << "\n\n       Keyboard Character:  '"
                  << ascii_char
                  << "'  "
                  << "is  "
                  << DecimalToOctal(ascii_char)
                  << "  in octal and  "
                  << static_cast<long>(ascii_char)
                  << "  in decimal."
                  << "\n\n       Try another number 'y or n': ";
        std::cin.get(); // get and ignore that crlf  
    } while (std::cin.get() == 'y');
}

The only major change I would make to function arith_value is to get rid of the goto statement.

GrahamS

Link to comment
Share on other sites

The problem with those functions is that they are part of iostream which is part of the standard C++ library which Jon isn't using (Too much code bloat). Also, as you mentioned, you can only convert between a few specific types.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...