Question

opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands. The maximum length of any ope
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

#include <iostream>

#include <fstream>

#include <cstring>

#include <cstdlib>

#include <cmath>

using namespace std;

// Function to convert hexadecimal to decimal

int hexadecimalToDecimal(string hexadecimalString)

{

// Stores the length of the string

int len = hexadecimalString.length();

// Initializing base value to 1, i.e 16^0

int base = 1;

int decimalValue = 0;

// Loops from last index position to starting to extract a character from the string

for (int pos = len - 1; pos >= 0; pos--)

{

// Checks if the current character is between '0'-'9' inclusive

// then converts it to integral 0-9 by subtracting 48 from its ASCII value

if (hexadecimalString.at(pos) >= '0' && hexadecimalString.at(pos) <= '9')

{

decimalValue += (hexadecimalString.at(pos) - 48) * base;

// Increases base by 16

base = base * 16;

}// End of if condition

// Checks if the current character is between 'A'-'F' inclusive

// then converts it to integral 10 - 15 by subtracting 55 from its ASCII value

else if (hexadecimalString.at(pos) >= 'A' && hexadecimalString.at(pos) <= 'F')

{

decimalValue += (hexadecimalString[pos] - 55) * base;

// Increases base by 16

base = base * 16;

}// End of else if

}// End of for loop

// Returns the decimal value

return decimalValue;

}// End of function

// Function to perform arithmetic operation based on the operator symbol passed as parameter

void calculate(int firstValue, int secondValue, char operatorSymbol)

{

// Checks the operator symbol and performs appropriate arithmetic operation

switch(operatorSymbol)

{

case '+':

cout<<firstValue<<operatorSymbol<<secondValue<<"="<<(firstValue + secondValue)<<endl;

break;

case '-':

cout<<firstValue<<operatorSymbol<<secondValue<<"="<<(firstValue - secondValue)<<endl;

break;

case '*':

cout<<firstValue<<operatorSymbol<<secondValue<<"="<<(firstValue * secondValue)<<endl;

break;

case '/':

cout<<firstValue<<operatorSymbol<<secondValue<<"="

<<"quotient "<<(firstValue / secondValue)<<", remainder "<<(firstValue % secondValue)<<endl;

break;

case '$':

cout<<firstValue<<operatorSymbol<<secondValue<<"="<<pow(firstValue, secondValue)<<endl;

break;

default:

cout<<"\n Invalid operator!!";

}// End of switch - case

}// End of function

// Function to read file contents

void readFile()

{

// To store the first operand string version

string firstOperand;

// To store the operator

char operatorSymbol;

// To store the second operand string version

string secondOperand;

// To store the first operand integer version

int firstValue;

// To store the second operand integer version

int secondValue;

// To store the expression read from file

string data;

// To store the position of operator symbol

int pos;

// To store the length of the expression

int len;

// ifstream objects declared to read data from files

ifstream fRead;

// Opens the file for reading

fRead.open("HexadecimalArithmetic.txt");

// Checks if the file unable to open for reading display's error message and stop

if(!fRead)

{

cout<<"\n ERROR: Unable to open the file for reading.";

exit(0);

}// End of if condition

// Loops till end of the file

while(!fRead.eof())

{

// Reads an expression

fRead>>data;

// Stores the length of the expression

len = data.length();

// Loops till end of the expression till operator encountered

for(pos = 0; pos < len; pos++)

// Checks if current character is '+' or '-' or '*' or '/' or '$' operator then stop the loop

if(data.at(pos) == '+' || data.at(pos) == '-' || data.at(pos) == '*' || data.at(pos) == '/'

|| data.at(pos) == '$')

break;

// Extracts the first operand from the expression from 0 index position to

// pos (index position of the operator in the expression)

firstOperand = data.substr(0, pos);

// Stores the operator symbol which is at pos index position of the expression

operatorSymbol = data.at(pos);

// Extracts the second operand from the expression from operator index position plus one to

// length of the expression minus 2 (one extra for = symbol)

secondOperand = data.substr(pos+1, (data.length()- firstOperand.length() - 2));

// Calls the function to convert first operand to decimal

firstValue = hexadecimalToDecimal(firstOperand);

// Calls the function to convert second operand to decimal

secondValue = hexadecimalToDecimal(secondOperand);

// Calls the function to calculate

calculate(firstValue, secondValue, operatorSymbol);

}// End of while loop

// Close the file

fRead.close();

}// End of function

// main function definition

int main()

{

// Calls the function to read expression from file

readFile();

return 0;

}// End of main function

Sample Output:

43690+3007=46697
3069+734=3803
256*170=43520
256$5=1.09951e+012
256/15=quotient 17, remainder 1
0-1=-1
-286261249--1985229329=1698968080
-87163471/1171125537=quotient 0, remainder -87163471

Add a comment
Know the answer?
Add Answer to:
opiond it 0 T2419 (3UN) Write a C++ program which performs +,-,,/and $ on hexadecimal operands....
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
  • 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=...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • Write a C program which will display the contents of a file in base-16 (hexadecimal) and...

    Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the following tasks: Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”. Open the file for binary input Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already...

  • C++ program to convert between decimal, hexadecimal, and octal. Please Help!!

    Hi, I need help writing a program that reads in data (hex, octal or decimal values) from an input file and outputs the values in to another base form (hex, octal,decimal) one line at a time depending on the formatting characters provided by the input file. I am posting the code requirements below and an example of what theinput file will look like and what should be output by the program. I only need the one .cpp program file. Thanks...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

    Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • The second phase of your semester project is to write pass one of a two‑pass assembler...

    The second phase of your semester project is to write pass one of a two‑pass assembler for the SIC assembler language program. As with Phase 1, this is to be written in C (not C++) and must run successfully on Linux. Pass one will read each line of the source file, and begin the process of translating it to object code. (Note: it will be to your advantage to have a separate procedure handle reading, and perhaps tokenizing, the source...

  • To write a C++ program to find average of three integers using functions Objectives: To get...

    To write a C++ program to find average of three integers using functions Objectives: To get familiar with functions in C++ Task 1: Study the working of user-defined functions in C++ Task 2: To write a C++ program to find average of three integers using functions Programming Instructions: Make new project (Visual C++ Empty Project) named lab6 and add a C++ file named lab6.cpp to this project. (Some IDE may automatically generate main.cpp or source.cpp, then you just rename it...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

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