Write a command line C++ program that will read from a file a number in any base
ID: 3589842 • Letter: W
Question
Write a command line C++ program that will read from a file a number in any base and convert it to decimal. The program should read the input from an input file and display the result in another output file. The first line of the input file contains a number N that represents the number of numbers to be converted. The file then contains N lines where each line consists of the base of the number followed by the number (which may contain a radix point). The output file contains N lines where each line starts with the number read followed by its decimal equivalent.
Explanation / Answer
/***********************************************************
* File : dec.cpp
*
* C++ program that will read from a file a number in any base and
* convert it to decimal. The program should read the input from an
* input file and display the result in another output file. The first
* line of the input file contains a number N that represents the number
* of numbers to be converted. The file then contains N lines where each
* line consists of the base of the number followed by the number
* (which may contain a radix point). The output file contains N lines where
* each line starts with the number read followed by its decimal equivalent.
*
******************************************************************/
#include <iostream>
#include <cstdio>
#include <limits>
using namespace std;
//typedef std::numeric_limits< double > dbl;
/* Function to print the Integer part of decimal number */
void printInt(int *a,int k,int base)
{
if (base<10){
for (int i=k-1;i>=0;i--)
cout << a[i];
}else{
for (int i=k-1;i>=0;i--)
{
if (a[i]<10)
cout<<a[i];
else if (a[i]==10)
cout<<"A";
else if (a[i]==11)
cout<<"B";
else if (a[i]==12)
cout<<"C";
else if (a[i]==13)
cout<<"D";
else if (a[i]==14)
cout<<"E";
else cout<<"F";
}
}
}
/* Function to print the reminder of the decimal number */
void printRem(int *a,int k,int base)
{
if (base<10)
{
for(int i=0;i<k;i++){
cout<<a[i];
}
}else{
for(int i=0;i<k;i++)
{
if (a[i]<10)
cout<<a[i];
else if (a[i]==10)
cout<<"A";
else if (a[i]==11)
cout<<"B";
else if (a[i]==12)
cout<<"C";
else if (a[i]==13)
cout<<"D";
else if (a[i]==14)
cout<<"E";
else cout<<"F";
}
}
}
int main ()
{
double num;
int N, i, base;
freopen("input.txt", "r", stdin); /* take input from file input.txt */
freopen("output.txt", "w", stdout); /* print output in the output.txt */
cin>>N;
for(i=0; i<N; i++)
{
cin>>base;
cin>>num;
int *w, *r;
int nw, nr;
w = NULL;
nw = 0;
r = NULL;
nr = 0;
double fp = num - (int)num;
int ip = (int)num;
while (ip != 0) /* Integer part decimal conversion */
{
int *temp = new int[nw + 1];
int remainder;
remainder = ip % base;
ip = ip / base;
if (w != NULL)
for (int j = 0; j < nw; j++)
temp[j] = w[j];
delete [] w;
temp[nw] = remainder;
w = temp;
nw++;
}
while (fp != 0) /* Floating part decimal conversion */
{
int *temp = new int[nr + 1];
double nov;
nov = fp * base;
if (r != NULL)
for (int j = 0; j < nr; j++)
temp[j] = r[j];
temp[nr] = (int)nov;
nr+=1;
delete [] r;
r=temp;
fp = nov - int(nov);
if (nr == 10)
break;
}
//cout.precision(dbl::max_digits10);
cout.precision(10); /* here we can print the exact number by storing in a string array at the time of input */
cout<<num;
cout<<" ";
printInt(w, nw, base);
if (r != NULL) cout<<".";
printRem(r, nr, base);
cout<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.