Question
#include #include #include #include long long int x , x1 , y , z , i , p , result , j ; char op
#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
I am also unable to to run negative numbers
Implement a program called calc with the following usage interface: calcStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started