#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> long l
ID: 3870868 • Letter: #
Question
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
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]<='1')
{
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]<='7')
{
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]<='9')
{
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<16; 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<5)
{
printf("ERROR Too few arguments ") ;
return 0;
}
op = argv[1][0] ;
for(i=0;argv[2][i]!='';i++)
{
str1[i] = argv[2][i] ;
}
for(i=0;argv[3][i]!='';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<0)
{
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: 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 examplesExplanation / Answer
Please run Your code using below command
gcc -o test test.c -lm
-lm option will make sure power function is available
Thanks, I am able to run it. Please let me know if you face any issue. Comment it and I will surely respond.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.