C++ Help! You are to create a program that is driven by an input file. The first line of the file will contain the number of subsequent lines in the file.
The subsequent lines in the file will be four pieces of information in each line: The number representation being used in the line (B for binary, H for hex, D for decimal), A first number (in the representation specified in 1) An operand, either + or *, for addition or multiplication respectively A second number (in the representation specified in 1) For each of the lines, you are to output the result of the calculation in the number system specified in (1).
For example, if the line is D 123 + 456, then you will print 579 to the screen. If the line is B 101 + 101, then you will print 1010 to the screen (1010).
When implementing your program, you adhere to the following guidelines: You are allowed to use the string class to read in, for example, binary, decimal, and hex values. This will probably help you parse the individual values character by character. You may NOT use built in C++ conversion libraries such as stol, atoi, std::hex, etc. You MUST do the conversions yourself using for loops, multiply, divide, modulo, and any helper functions to convert between characters and integers. You must implement at least three functions Because the first line of the file is a number telling you how many lines to expect in the input file, you can use a loop to iterate over the inputs.
Full code without pointer
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
void performDecimalOperation(string ,string , string);
void performBinaryOperation(string ,string , string);
void performHexOperation(string,string, string);
string decToHexa(int);
int hexadecimalToDecimal(string);
int main()
{
ifstream infile;
infile.open("data.txt");
if(!infile)
{
cout<<"Unable to opne the
file... Exiting..."<<endl;
return 0;
}
string line,field;
string base[100];
string op[100];
string oprand1[100],oprand2[100];
getline(infile,line);
int totalLine=stoi(line);
int i=0;
while(getline(infile,line))
{
stringstream ss(line);
ss>>field;
base[i]=field;
ss>>field;
oprand1[i]=field;
ss>>field;
op[i]=field;
ss>>field;
oprand2[i]=field;
i++;
}
for(int j=0;j<totalLine;j++)
{
if(base[j]=="D")
performDecimalOperation(oprand1[j],oprand2[j],op[j]);
if(base[j]=="B")
performBinaryOperation(oprand1[j],oprand2[j],op[j]);
if(base[j]=="H")
performHexOperation(oprand1[j],oprand2[j],op[j]);
}
cout<<endl;
return 0;
}
void performDecimalOperation(string op1,string op2, string
opr)
{
int a=stoi(op1);
int b=stoi(op2);
if(opr=="+")
cout<<a+b<<endl;
if(opr=="*")
cout<<a*b<<endl;
}
void performBinaryOperation(string op1,string op2, string
opr)
{
if(opr=="+")
{
string result = ""; // Initialize
result
int s = 0; // Initialize digit
sum
// Travers both strings starting
from last
// characters
int i = op1.size() - 1, j =
op2.size() - 1;
while (i >= 0 || j >= 0 || s
== 1)
{
// Comput sum of
last digits and carry
s += ((i >=
0)? op1[i] - '0': 0);
s += ((j >=
0)? op2[j] - '0': 0);
// If current
digit sum is 1 or 3, add 1 to result
result = char(s
% 2 + '0') + result;
// Compute
carry
s /= 2;
// Move to next
digits
i--; j--;
}
cout<<result<<endl;
}
}
void performHexOperation(string op1,string op2, string opr)
{
int a=hexadecimalToDecimal(op1);
int b=hexadecimalToDecimal(op2);
int answer;
if(opr=="+")
answer=a+b;
if(opr=="*")
answer=a*b;
cout<<decToHexa(answer)<<endl;
}
string decToHexa(int n)
{
// char array to store hexadecimal number
string hexaDeciNum;
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum+= temp + 48;
i++;
}
else
{
hexaDeciNum+= temp + 55;
i++;
}
n = n/16;
}
for (int i = 0; i < n / 2; i++)
swap(hexaDeciNum[i], hexaDeciNum[n - i - 1]);
return hexaDeciNum;
}
int hexadecimalToDecimal(string hexVal)
{
int len = hexVal.length();
// Initializing base value to 1, i.e 16^0
int base = 1;
int dec_val = 0;
// Extracting characters as digits from last character
for (int i=len-1; i>=0; i--)
{
// if character lies in '0'-'9', converting
// it to integral 0-9 by subtracting 48 from
// ASCII value.
if (hexVal[i]>='0' && hexVal[i]<='9')
{
dec_val += (hexVal[i] - 48)*base;
// incrementing base by power
base = base * 16;
}
// if character lies in 'A'-'F' , converting
// it to integral 10 - 15 by subtracting 55
// from ASCII value
else if (hexVal[i]>='A' && hexVal[i]<='F')
{
dec_val += (hexVal[i] - 55)*base;
// incrementing base by power
base = base*16;
}
}
return dec_val;
}
output

If you have any query
regarding the code please ask me in the comment i am here for help
you. Please do not direct thumbs down just ask if you have any
query. And if you like my work then please appreciates with up
vote. Thank You.
C++ Help! You are to create a program that is driven by an input file. The...