Design a calculator to perform arithmetic operations such as addition, subtraction, multiplication, and division.
Sample command line Input
12 33 +
Sample Output
45
Sample command line Input
12 3 *
Sample Output
36
Implementation of above problem:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int a,b,result;
char opr[10];
if(argc<4){
printf("Some parameters are missing\n");
printf("use prg_name opr value1 value2\n");
return -1;
}
//getting operator
strcpy(opr,argv[3]);
//getting values from arguments
a=atoi(argv[1]);
b=atoi(argv[2]);
switch(opr[0]){
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
default:
printf("Invalid Operator!!!\n");
return -1;
}
printf("\n%d",result);
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: