You are not allowed to use any bit operations in coding the programs. Your code
ID: 3590574 • Letter: Y
Question
You are not allowed to use any bit operations in coding the programs. Your code will be tested against some input data. If possible avoid using WIN32 APIs and use POSIX APIs. When submitting your assignment, submit only one source code file for each of the questions. 1. Write C/C++ ary numbers to decimal and e program should read the input from an input file and display the result program that will convert bin hexadecimal formats. Th in another output file. The first line of the input file contains a number N that represents the number of binary numbers to be converted. The file then contains N lines where each line is a binary number (which may contain a radix point). The output file contains N lines where each line starts with the binary number followed by the decimal equivalent followed by the hexadecimal equivalent. See the below examples for input and output. Please note that you are not allowed to use any C/C++ binary/bit operators. Sample output 10110011 01.0101 0001.1 Sample input 179 5.3125 5.5 17.511.8 15 B3 10110011 101.0101 10001.1Explanation / Answer
Please copy the below main.cpp file
main.cpp
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
int main (int argc, char *argv[])
{
vector<string> list;
ifstream in_stream;
string line;
in_stream.open(argv[1]);
in_stream >> line;
//cout<<line;
stringstream total(line);
int n=0;
total >> n;
long* binary = NULL;
binary = new long[n];
long* decimal = NULL;
decimal = new long[n];
string* hexdecimal = NULL;
hexdecimal = new string[n];
int count=0;
for (int i=0; i<n; i++) {
binary[i] = 0;
decimal[i] = 0; // Initialize all elements to zero.
hexdecimal[i] = "";
}
while(!in_stream.eof())
{
in_stream >> line;
stringstream item(line);
item >> binary[count];
count++;
// cout<<line;
//list.push_back(line);
}
in_stream.close();
for (int i=0; i<n; i++) {
//cout<< binary[i] <<" ";
long bin, dec = 0, rem, num, base = 1;
num = binary[i];
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
//cout<< dec <<" ";
decimal[i] = dec;
// char array to store hexadecimal number
char hexaDeciNum[100];
int k = dec;
// counter for hexadecimal number array
int m = 0;
while(k!=0)
{
int temp = 0;
temp = k % 16;
if(temp < 10)
{
hexaDeciNum[m] = temp + 48;
m++;
}
else
{
hexaDeciNum[m] = temp + 55;
m++;
}
k = k/16;
}
string s="";
// printing hexadecimal number array in reverse order
for(int j=m-1; j>=0; j--){
cout << hexaDeciNum[j];
//s=s+hexaDeciNum[j];
}
// cout << " " << s;
hexdecimal[i]=s;
}
ofstream myfile ("output.txt");
if (myfile.is_open())
{
cout<<"conversions output: ";
for (int i=0; i<n; i++) {
myfile << "Binary number:" << binary[i] <<" Decimal:" <<decimal[i]<<" hexdecimal:"<<hexdecimal[i]<<" ";
}
myfile << "End of file. ";
myfile.close();
}
else cout << "Unable to open file";
//cout<<"read";
}
input.txt
2
10111100
11011111
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.