Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include long long int x , x1 , y , z , i , p , result , j ; char op

image text in transcribed

image text in transcribed

#include

#include

#include

#include

long long int x , x1 , y , z , i , p , result , j ;

char op , str1[64] , str2[64] , str[64] , res ;

char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F'} ;

long long int b2d(char c[20])

{

z = strlen(c) ;

y = 0 ;

p = 0 ;

for(i=z-1;i>=0;i--)

{

if(c[i]>='0' && c[i]

{

y = y + pow(2,p)*(c[i]-'0') ;

p++;

}

}

if(c[0]=='-')y=-y;

return y ;

}

long long int o2d(char c[20])

{

z = strlen(c) ;

y = 0 ;

p = 0 ;

for(i=z-1;i>=0;i--)

{

if(c[i]>='0' && c[i]

{

y = y + pow(8,p)*(c[i]-'0') ;

p++;

}

}

if(c[0]=='-')y=-y;

return y ;

}

long long int d2d(char c[20])

{

z = strlen(c) ;

y = 0 ;

p = 0 ;

for(i=z-1;i>=0;i--)

{

if(c[i]>='0' && c[i]

{

y = y + pow(2,p)*(c[i]-'0') ;

p++;

}

}

if(c[0]=='-')y=-y;

return y ;

}

long long int x2d(char c[20])

{

y = 0 ;

z = 0 ;

for(i=strlen(c)-1; i >= 0; i--) {

for(j=0; j

if(c[i] == hexDigits[j]){

y += j*pow(16, z);

}

}

z++;

}

if(c[0]=='-')y=-y;

return y ;

}

void d2b(long long int num)

{

if(num==0)

{

printf("0");

return ;

}

y = 0 ;

while(num>0)

{

str[y++] = num%2+'0' ;

num = num/2 ;

}

printf("%s",str) ;

}

void d2o(long long int num)

{

if(num==0)

{

printf("0");

return ;

}

y = 0 ;

while(num>0)

{

str[y++] = num%8+'0' ;

num = num/8 ;

}

printf("%s",str) ;

}

void d2x(long long int num)

{

if(num==0)

{

printf("0");

return ;

}

y = 0 ;

while(num>0)

{

str[y] = num%16 +'0';

if(num%16==10)

{

str[y] = 'A' ;

}

if(num%16==11)

{

str[y] = 'B' ;

}

if(num%16==12)

{

str[y] = 'C' ;

}

if(num%16==13)

{

str[y] = 'D' ;

}

if(num%16==14)

{

str[y] = 'E' ;

}

if(num%16==15)

{

str[y] = 'F' ;

}

y++;

num = num/16 ;

}

printf("%s",str) ;

}

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

if(argc>5)

{

printf("EROOR Too many arguments ") ;

return 0;

}

if(argc

{

printf("ERROR Too few arguments ") ;

return 0;

}

op = argv[1][0] ;

for(i=0;argv[2][i]!='\0';i++)

{

str1[i] = argv[2][i] ;

}

for(i=0;argv[3][i]!='\0';i++)

{

str2[i] = argv[3][i] ;

}

res = argv[4][0] ;

if(str1[0]=='-')y = 1 ;

if(str1[y]=='b')

{

x = b2d(str1) ;

}

else if(str1[y]=='o')

{

x = o2d(str1) ;

}

else if(str1[y]=='x')

{

x = x2d(str1) ;

}

else

{

x = d2d(str1) ;

}

y = 0 ;

if(str2[0]=='-')y = 1 ;

if(str2[y]=='b')

{

x1 = b2d(str2) ;

}

else if(str2[y]=='o')

{

x1 = o2d(str2) ;

}

else if(str2[y]=='x')

{

x1 = x2d(str2) ;

}

else

{

x1 = d2d(str2) ;

}

if(op=='+')

{

result = x + x1 ;

}

else if(op=='-')

{

result = x - x1 ;

}

if(result

{

printf("-") ;

result = -result ;

}

printf("%c",res) ;

if(res=='b')

{

d2b(result) ;

}

else if(res=='o')

{

d2o(result) ;

}

else if(res=='x')

{

d2x(result) ;

}

else

{

printf("%lld",result) ;

}

return 0 ;

}

When I run this program, I get some errors and I'm not sure how to fix them

image text in transcribed

I am also unable to to run negative numbers

Implement a program called calc with the following usage interface: calc The first argument, , is either the string"" for addition, or "", for subtraction. If you want to implement multiplication, then can also be the string "". (If you do implement multiplication, make sure to say so in your readme.pdf file so that the TAs know to check your program for this functionality.) The next two arguments, and are 64-bit, two's-complement integers. Each of these numbers will be given in the form of which can be interpreted as: a base indicator where b means that the number is a binary number, o means octal, r means hexadecimal and d means decimal. dndn1..dido are the digits of the number. A decimal number may be preceded by a minus sign. Note that a minus sign is neither meaningful nor necessary for binary, octal or hexadecimal numbers as the bit pattern for these representations already covers positve and negative quantities The final argument, , gives the base for the resulting output number. Like the base indicator for the input numbers, this argument can be one of four strings: "b" for binary, "o" for octal, "d" for decimal, and "r" for hexadecimal. Your program should output the answer in the same form as the input numbers (that is, the output number should follow the regular expression given above) Some examples

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

Which of the following is NOT a relational operator? 1. =

Answered: 1 week ago