Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You need three files Winput file //command line file .cpp l/output file *linux c

ID: 3873193 • Letter: Y

Question

You need three files Winput file //command line file .cpp l/output file *linux compiler /*program will read from an input file perform conversion and then print in an output file*/ Write a C++ that converts a rational, decimal number to a corresponding value in a foreign base. A foreign base, is constrained to the range 2SS36 Th e program should only cover the basic things we have covered in class. i.e only chapter one and two of the computer organization one class. So use the basic divide and multiplicand to do conversion I/po1.cpp p01.cpp contains functions that process command line arguments and direct the process of reading printing original input values together with corresponding output values Project 1 can be invoked with zero, one, or two program parameters. The first program parameter is the input file name. The second parameter is the output file name. Sample command lines together with corresponding actions by program p01 are shown below. Boldfaced type indicates data entered at the keyboard by the user , converting, and Enter the input file name: i01.dat Enter the output file name: o01.dat S p01 i01.dat Enter the output file name: 001.dat /input file The input file contains pairs on separate lines. The first value in the pair is a rational, decimal number. The second value in the pair is an integer specifying the foreign base where 2-f-36 As an example, consider the following pairs: One line is printed for each pair in the input file. Three values are printed on a line. The first two values are the values read from the input file. The last value is the decimal equivalent of the input values.

Explanation / Answer

#include <iostream>

#include <cstring>

#include <fstream>

using namespace std;

// To return char for a value. For example '2'

// is returned for 2. 'A' is returned for 10. 'B'

// for 11

char reVal(int num)

{

if (num >= 0 && num <= 9)

return (char)(num + '0');

else

return (char)(num - 10 + 'A');

}

// Utility function to reverse a string

void strev(char *str)

{

int len = strlen(str);

int i;

for (i = 0; i < len/2; i++)

{

char temp = str[i];

str[i] = str[len-i-1];

str[len-i-1] = temp;

}

}

// Function to convert a given decimal number

// to a base 'base' and

char* fromDeci(char res[], int base, int inputNum)

{

int index = 0; // Initialize index of result

// Convert input number is given base by repeatedly

// dividing it by base and taking remainder

while (inputNum > 0)

{

res[index++] = reVal(inputNum % base);

inputNum /= base;

}

res[index] = '';

// Reverse the result

strev(res);

return res;

}

int main(int argc, char *argv[] ) {

string inputFileName;

string outputFileName;

if(argc == 3) {

inputFileName = argv[1];

outputFileName = argv[2];

}else if(argc == 2){

inputFileName = argv[1];

outputFileName = inputFileName+"_out";

}else{

do {

cout << "Enter input file name";

getline(cin, inputFileName);

} while(inputFileName.capacity() <= 0);

  

do {

cout << "Enter output file name";

getline(cin, outputFileName);

}while(outputFileName.capacity() <= 0);

}

int number, base;

ifstream infile;

infile.open(inputFileName);

ofstream outfile;

outfile.open(outputFileName);

char res[100];

while (infile >> number >> base)

{

outfile << number <<" "<< base << " "<<fromDeci (res, number, base);

}

infile.close();

outfile.close();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote