program that reads an unsigned number in some base and outputs the equivalent value in a target base. The input file providing these values will be organized to allow for comment lines and blank lines along with data lines. Use only a single source file so I can more easily script the downloading and compiling of your submissions.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

Develop a C++ program that reads an unsigned number in some base and outputs the
equivalent value in a target base. The input file providing these values will be organized to
allow for comment lines and blank lines along with data lines. Use only a single source file so I
can more easily script the downloading and compiling of your submissions.
Your program must require a single command line argument, the name of an input file. This
input file will consist of any number of lines, each falling into one of three varieties:
1. Comment - These lines always start with an at-symbol ('@') and should be completely
ignored.
2. Blank line - Used to visually organize the input file and should be completely ignored.
3. Data line - These are the ones your program will process and are described below.
Data lines will all have exactly 3 items separated by a single space character and end with a
newline:
1. an input base (2 through 36) identified in decimal. Digits representing coefficients
between 10 and 35 will use the letters 'A'-'Z' and/or 'a'-'z' (i.e., they could be upper or
lower case)
2. an input value in that base whose binary equivalent will never be more than 30 bits
long
3. an output base (2 through 36) identified in decimal
For each Data line from the input file, you must output:
1. the line number of the input file this data line is on (starting the file with line 1… same as
a text editor would show)
2. a colon followed by a space
3. the equivalent of the input value in the output base using upper case letters A-Z for
any coefficients with the value of 10-35. NEVER print any leading zeros
a. eg, instead of 000123, you would print 123
b. only exception is if the value itself is zero, as in the example below
Example input file Expected output to console

Input:
@ a first example
8 70 4
2 011100 16
7 0 21
11 3A 10

Output:
2: 320
4: 1C
5: 0
6: 43

Expert Solution
Step 1
C program
 
main.c
 
#include<stdio.h> // library for standard input-output
#include<math.h> // library for pow function
#include<string.h> // library for strings
#include<ctype.h> // library for character types
 
 
// function that takes a data line and prints the output
void data_line(char line[])
{
// int for input base
int input_base;
// string for value
char value[35];
// int for output base
int output_base;
 
// read input base, value, output base from data line
sscanf(line,"%d %s %d",&input_base,value,&output_base);
 
// calculate the length of value
int n = strlen(value);
 
// int to store the decimal value for value with input base
int decimal=0;
 
// convert the value from input base to decimal
for(int i=n-1;i>=0;i--)
{
// if it is a digit
if(value[i]>='0' && value[i]<='9')
{
decimal += pow(input_base,n-i-1)*(value[i]-'0');
}
// if it is a character
else
{
// convert character to lower case
value[i]=tolower(value[i]);
decimal += pow(input_base,n-i-1)*(value[i]-'a'+10);
}
}
 
// array to store value with output base
char output[35];
 
// int to store count of output
int k=0;
 
// repeat until decimal value is 0
while(decimal>0)
{
// calculate the remainder
int rem = decimal%output_base;
 
// convert the remainder to its corresponding character
if(rem>=10)
output[k]=('A'+rem-10);
else
output[k]=('0'+rem);
 
// increase the count
k++;
 
// calculate the remaining decimal value
decimal = decimal/output_base;
}
 
 
// if count is 0 print 0
if(k==0)
{
printf("0\n");
}
else
{
// print the output to console
for(int i=k-1;i>=0;i--)
printf("%c",output[i]);
printf("\n");
}
}
 
// main function
int main(int argc,char *argv[])
{
// open the file in read mode
// input file is in first argument of command line
FILE *fp = fopen(argv[1],"r");
 
// char array used to read every line in input file
char line[100];
 
// int to store the line number to print to console
int line_no = 1;
 
// read until EOF (end of file)
while(fscanf(fp,"%[^\n]%*c",line)!=EOF)
{
// check if the current line is a data line
// if there is a data line then definitely first character will be a digit
if(line[0]>='0' && line[0]<='9')
{
// print the line number
printf("%d: ",line_no);
// call the function to convert from input base to output base
data_line(line);
}
 
// increase the line number
line_no++;
}
 
// close the file finally
fclose(fp);
}
 
 
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education