Question

Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The...

Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The

maximum length of any operand or a solution is 40 digits. The input will be in the

following format:

Op1 op op2 =

There is no space between operands and operator.

Note

5/2 = quotient 2, remainder 1

2$3 = 8

The output should be of the form

2*3=6.

Read date from a file.

TEST DATA (input.txt):

AAAA+BBF=

BFD+2DE=

100*AA=

100$5=

100/F=

10000000000000-1=

AAAAABBBBBCCCCCDDDDDEEEEEFFFFF-ABCDEF0123456789ABCDEF=

FACDFDB1/45CDF521=

I am posting code below that I tried but It is not working like they want in output please can someone help me out with this. When you test this program you can see the output in output file.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct brokenString {
   string leftOp;
   string rightOp;
};

int hexToDec(char);
string decToHex(int, string&);
brokenString splitUpString(string);
string addition(brokenString);
string subtraction(brokenString);
string multiplication(brokenString);
string division(brokenString);
string power(brokenString);

int main() {
   ifstream inputFile;
   ofstream outputFile;
   string equation;
   brokenString splitEquation;

   inputFile.open("input.txt");
   outputFile.open("output.txt");

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << addition(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << addition(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << multiplication(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << power(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << division(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << subtraction(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << subtraction(splitUpString(equation)) << endl;

   getline(inputFile, equation, '=');
   outputFile << equation << " = " << division(splitUpString(equation)) << endl;

   outputFile.close();
   inputFile.close();

   system("PAUSE");
   return 0;
}

int hexToDec(char value) {
   switch (value) {
   case 'A':
       return 10;
   case 'B':
       return 11;
   case 'C':
       return 12;
   case 'D':
       return 13;
   case 'E':
       return 14;
   case 'F':
       return 15;
   default:
       return atoi(&value);
   }
}

string decToHex(int value, string& convertedDec) {
   int remainder;
   string convertedReversed;

   remainder = value % 16;

   if (remainder > 9) {
       switch (remainder) {
       case 10:
           convertedDec.push_back('A');
           break;
       case 11:
           convertedDec.push_back('B');
           break;
       case 12:
           convertedDec.push_back('C');
           break;
       case 13:
           convertedDec.push_back('D');
           break;
       case 14:
           convertedDec.push_back('E');
           break;
       case 15:
           convertedDec.push_back('F');
           break;
       }
   }
   else {
       convertedDec.push_back('0' + remainder);
   }

   if (value / 16)
       return decToHex(value / 16, convertedDec);
   else
       for (int i = convertedDec.size() - 1; i > -1; i--) {
           convertedReversed.push_back(convertedDec[i]);
       }

   return convertedReversed;
}

brokenString splitUpString(string myString) {
   brokenString temp;
   int i = 0;

   while (myString[i] != '+' &&
       myString[i] != '-' &&
       myString[i] != '/' &&
       myString[i] != '*' &&
       myString[i] != '$')
   {
       if (myString[i] != '\n') {
           temp.leftOp.push_back(myString[i]);
       }
       i++;
   }

   i++;
   for (i; i < myString.size(); i++)
       temp.rightOp.push_back(myString[i]);

   return temp;
}

string addition(brokenString myEquation) {
   string output = "", dummy = "";
   int op1, op2;
   bool done = false;
   int i, j, sum, carry = 0;

   i = myEquation.leftOp.size() - 1;
   j = myEquation.rightOp.size() - 1;

   //cout << myEquation.leftOp[i] << endl << myEquation.rightOp[j] << endl;
   for (i; i > -1; i--) {
       op1 = hexToDec(myEquation.leftOp[i]);

       if (j < 0)
           op2 = 0;
       else
           op2 = hexToDec(myEquation.rightOp[j]);

       sum = op1 + op2 + carry;
       output.insert(0, decToHex(sum, dummy));

       if (dummy.size() > 1) {
           carry = 1;
           output.erase(output.begin());
       }
       else
           carry = 0;

       j--;
       dummy.clear();
   }
   if (carry == 1)
       output.insert(0, "1");
   return output;
}

string subtraction(brokenString myEquation) {
   string output;


   return output;
}

string multiplication(brokenString myEquation) {
   string output = "0";
   vector<string> products;
   int op2, counter = 0;
   brokenString tempProduct;
   int b = myEquation.leftOp.size();
   op2 = hexToDec(myEquation.rightOp[myEquation.rightOp.size() - 1]);

   for (int i = myEquation.rightOp.size() - 1; i > -1; i--) {
       for (int j = myEquation.leftOp.size() - 1; j > -1; j--) {
           tempProduct.rightOp = myEquation.leftOp[j];
           for (int k = 0; k < op2; k++) {
               tempProduct.leftOp = output;
               output = addition(tempProduct);
           }
       }
       op2 = hexToDec(myEquation.rightOp[i]);
   }

   /*for (int i=0; i < products.size(); i++) {
           tempProduct.leftOp = output;
           tempProduct.rightOp = products[i];
           output = addition(tempProduct);
   }*/

   return output;
}

string division(brokenString myEquation) {
   string output;


   return output;
}

string power(brokenString myEquation) {
   string output;


   return output;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We need to make one small change in your decToHex() function. It cannot to be recursive given the return value in your funciton is the reversed string. The point here is that the string must be reversed in the very end, when you do recursion, the string is reversed at every recursive funciton call. We will rectify this using a while loop instead pf recursive function call.

Our altered decToHex() functions is:

string decToHex(long value) {
int remainder;
string convertedDec;
string convertedReversed;

while(value>0)
{
remainder = value % 16;

if (remainder > 9) {
switch (remainder) {
case 10:
convertedDec.push_back('A');
break;
case 11:
convertedDec.push_back('B');
break;
case 12:
convertedDec.push_back('C');
break;
case 13:
convertedDec.push_back('D');
break;
case 14:
convertedDec.push_back('E');
break;
case 15:
convertedDec.push_back('F');
break;
}
}
else {
convertedDec.push_back('0' + remainder);
}
value /= 16;
}
for (int i = convertedDec.size() - 1; i > -1; i--) {
convertedReversed.push_back(convertedDec[i]);
}
return convertedReversed;
}

Everything else is fine. Now we will write the definition for functions performing hexadecimal arithmetic.

We will follow this algorithm for all our functions:
1.) convert the operands to decimal values

2.) perform the desired operation

3.) convert the result back to hexadecimal


string addition(brokenString myEquation) {
string output;
long op1, op2;
bool done = false;
int i, j;
long sum;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
op1=0;
op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}

//cout << myEquation.leftOp[i] << endl << myEquation.rightOp[j] << endl;
sum = op1+op2;
output = decToHex(sum);
return output;
}

string subtraction(brokenString myEquation) {
string output;
int i, j;
long diff;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
diff = op1-op2;
output = decToHex(diff);
return output;
}

string multiplication(brokenString myEquation) {
string output;
int i, j;
long mul;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}   
mul = op1*op2;
output = decToHex(mul);
return output;
}

string division(brokenString myEquation) {
string output;
int i, j;
long div;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
div = op1/op2;
output = decToHex(div);
return output;
}

string power(brokenString myEquation) {
string output;
int i, j;
long power;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
cout<<"\n"<<op1<<" "<<op2<<"\n";
power = pow(op1, op2);
cout<<power;
output = decToHex(power);
return output;
}

So those are our functions peforming hexadecimal arithmetic.

The entire program looks like:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

struct brokenString {
string leftOp;
string rightOp;
};

int hexToDec(char);
string decToHex(int, string&);
brokenString splitUpString(string);
string addition(brokenString);
string subtraction(brokenString);
string multiplication(brokenString);
string division(brokenString);
string power(brokenString);

int main() {
ifstream inputFile;
ofstream outputFile;
string equation;
brokenString splitEquation;

inputFile.open("input.txt");
outputFile.open("output.txt");

getline(inputFile, equation, '=');
outputFile << equation << " = " << addition(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << addition(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << multiplication(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << power(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << division(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << subtraction(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << subtraction(splitUpString(equation)) << endl;

getline(inputFile, equation, '=');
outputFile << equation << " = " << division(splitUpString(equation)) << endl;

outputFile.close();
inputFile.close();

system("PAUSE");
return 0;
}

int hexToDec(char value) {
switch (value) {
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;
default:
return atoi(&value);
}
}

string decToHex(long value) {
int remainder;
string convertedDec;
string convertedReversed;

while(value>0)
{
remainder = value % 16;

if (remainder > 9) {
switch (remainder) {
case 10:
convertedDec.push_back('A');
break;
case 11:
convertedDec.push_back('B');
break;
case 12:
convertedDec.push_back('C');
break;
case 13:
convertedDec.push_back('D');
break;
case 14:
convertedDec.push_back('E');
break;
case 15:
convertedDec.push_back('F');
break;
}
}
else {
convertedDec.push_back('0' + remainder);
}
value /= 16;
}
for (int i = convertedDec.size() - 1; i > -1; i--) {
convertedReversed.push_back(convertedDec[i]);
}
return convertedReversed;
}

brokenString splitUpString(string myString) {
brokenString temp;
int i = 0;

while (myString[i] != '+' &&
myString[i] != '-' &&
myString[i] != '/' &&
myString[i] != '*' &&
myString[i] != '$')
{
if (myString[i] != '\n') {
temp.leftOp.push_back(myString[i]);
}
i++;
}

i++;
for (i; i < myString.size(); i++)
temp.rightOp.push_back(myString[i]);
return temp;
}

string addition(brokenString myEquation) {
string output = "", dummy = "";
long op1, op2;
bool done = false;
int i, j;
long sum;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
op1=0;
op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}

//cout << myEquation.leftOp[i] << endl << myEquation.rightOp[j] << endl;
sum = op1+op2;
output = decToHex(sum);
return output;
}

string subtraction(brokenString myEquation) {
string output;
int i, j;
long diff;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
diff = op1-op2;
output = decToHex(diff);
return output;
}

string multiplication(brokenString myEquation) {
string output;
int i, j;
long mul;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}   
mul = op1*op2;
output = decToHex(mul);
return output;
}

string division(brokenString myEquation) {
string output;
int i, j;
long div;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
div = op1/op2;
output = decToHex(div);
return output;
}

string power(brokenString myEquation) {
string output;
int i, j;
long power;
i = myEquation.leftOp.size() - 1;
j = myEquation.rightOp.size() - 1;
long op1=0;
long op2=0;
for(i; i>-1; i--)
{
op1 += hexToDec(myEquation.leftOp[i])*pow(16,myEquation.leftOp.size()-1-i);
}
for(j; j>-1; j--)
{
op2 += hexToDec(myEquation.rightOp[j])*pow(16,myEquation.rightOp.size()-1-j);
}
cout<<"\n"<<op1<<" "<<op2<<"\n";
power = pow(op1, op2);
cout<<power;
output = decToHex(power);
return output;
}

Output.txt file:

That concludes our solution, if you need more information, please reach out to me in comments.

Add a comment
Know the answer?
Add Answer to:
Write a C++ program which performs +, -, *, / and $ on hexadecimal operands. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can sombody tell me what wrong for this code to work?? It is c++ btw. thanks!...

    Can sombody tell me what wrong for this code to work?? It is c++ btw. thanks! #include <iostream> #include <string> using namespace std; double op1; double op2; string ADD; string SUBTRACT; string MULTIPLY; string DIVIDE; string selection; string multiply; string divide; string add; string subtract; string "*"; string "+"; string "-"; string "/"; int main() { do { cout << "--------WELCOME TO THE CALCULATOR----------" << endl; cout << "Enter your first operand: "; cin >> op1; cout << "Enter your...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • Use the file processing example program shown at the bottom. Modify the program to enter the...

    Use the file processing example program shown at the bottom. Modify the program to enter the grades, and count number of grades. Also get total and average grade. Use the following data for grades: 79, 99, 85, 97, 88, 95, 100, 87, 94 EXAMPLE OUTPUT Total: 9 grades Total grade sum: 824 Average grade: 92 Letter grade: A / Using Break, and Continue #include "stdafx.h" #include using namespace std; int main() { int i = 0; for (int x =...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands....

    opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands. The maximum length of any operand or a solution is 40 digits. The input will be in the following format: Opl op op2- There is no space between operands and operator. I Note 5/2-quotient 2, remainder 1 2$3-8 The output should be of the form 2 3-6 Read date from a file Upload the source code, executable and output TEST DATA: AAAA+BBF- BFD+2DE 100...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • I am trying to understand the following code I found in a website so that I...

    I am trying to understand the following code I found in a website so that I can use it for a project that evaluates boolean expressions. The code is below but I think it is in java because I do not understand it. Can you help me describe what it is doing in c++ so that I can understand it better? The link to the code is here: https://stackoverflow.com/questions/16762057/algorithm-to-evaluate-value-of-boolean-expression The code is below: public static boolean evaluateBool(String s) { Stack<Object>...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT