In: Computer Science
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation:
calc [operand_1] [operator] [operand_2]
The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%).
Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly
2. Implementation
• The program to be submitted is named calc.c. Use the given template calc.c and fill in your code. Complete functions main()in file calc.c.
• Note that the command-line arguments are all strings. Therefore you need to implement a function to convert a string to an integer to obtain operand_1 and operand_2.
• Sometimes users may forget the command syntax and they may type only the command “calc”. In that case, display the following reminder message:
Usage: calc [operand_1] [operator] [operand_2]
• Other than that, assume that all inputs are valid. No error checking is required on inputs.
• You may define your own variables inside functions main(). Do not use global variables (defined outside functions main()).
• You may define and implement your own function(s) inside file calc.c if needed.
• Do not use any C library functions (e.g., atoi).
• To compile the program, use the following command: gcc –o calc calc.c
• There must be at least a white space between an operand and the operator. That is how the command-line arguments are separated.
3. Sample Inputs/Outputs
See file calc_io.txt for sample inputs and outputs
//////////Calc.c//////////////////Calc.c//////////////////Calc.c//////////////////Calc.c//////// // CALC.C #include #include /***** YOU MAY ADD YOUR OWN FUNCTION(S) HERE. *****/ /* Implement a simple calculator. Input: two operands and one operator as command-line arguments. Output: the result displayed on the standard output. */ void main( int argc, char *argv[] ) { int result = 0; /* stores the result of the arithmetic operation */ /*****************************************/ /***** ADD YOUR CODE BELOW THIS LINE *****/ /***** ADD YOUR CODE ABOVE THIS LINE *****/ /*****************************************/ /**** DO NOT ADD OR CHANGE ANYTHING BELOW THIS LINE ****/ printf( "%d\n", result ); }
//////////////////////////SAMPLE INPUT OUTPUT ///////////////////////////////////////////////////////////////SAMPLE INPUT OUTPUT ////////////////////////////////////////////////////////
indigo 580 % calc Usage: calc [operand_1] [operator] [operand_2] indigo 581 % calc 12 + 10 22 indigo 582 % calc 15 - 10 5 indigo 583 % calc 20 - 55 -35 indigo 584 % calc 20 / 7 2 indigo 585 % calc 20 % 7 6 indigo 586 % calc 50 x 11 550
/*C program to implement a simple calculator.
Input: two operands and one operator as command-line
arguments.
Output: the result displayed on the standard output.
*/
#include <stdio.h>
#include <stdlib.h>
// function to convert the string representing an integer to
int
int convertToInt(char *str)
{
int num = 0,len , i, exp = 1;
len = 0;
// loop to get the number of digits in the string
while(str[len] !='\0')
len++;
// loop to get the maximum exponent
for(i=0;i<len-1;i++)
exp = exp*10;
// loop to convert the string to number
for(i=0;i<len;i++)
{
num += (((int)(str[i]-'0'))*exp);
exp = exp/10;
}
return num;
}
void main( int argc, char *argv[] )
{
int result = 0; /* stores the result of the arithmetic operation
*/
if(argc < 4) // insufficient number of arguments passed,
display error
{
printf("Usage: calc [operand_1] [operator] [operand_2]");
return ;
}else
{
int n1 = convertToInt(argv[1]); // convert the first input to
int
int n2 = convertToInt(argv[3]); // convert the third input to
int
// check the operator passed and perform the operation
if(argv[2][0] == '+')
result = n1 + n2;
else if(argv[2][0] == '-')
result = n1 - n2;
else if(argv[2][0] == 'x')
result = n1 * n2;
else if(argv[2][0] == '/')
result = n1/n2;
else
result = n1%n2;
}
printf( "%d\n", result );
}
//end of program
Output:
calc 12 + 10