Question

In: Computer Science

1. Specification Write a C program to implement a simple calculator that accepts input in the...

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

Solutions

Expert Solution

/*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


Related Solutions

2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a complete MiniMIPS program that accepts a seqeunce of integers at input and after the...
Write a complete MiniMIPS program that accepts a seqeunce of integers at input and after the receipt of each new input value, displays the largest and smallest integers thus far. An input of 0 indicates the end of input values and is not an input value itself. Note that you do not need to keep all integers in memory.
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
PYTHON Write a program that accepts a range of input from the user and checks whether...
PYTHON Write a program that accepts a range of input from the user and checks whether the input data is sorted or not. If the data series is already sorted your program should print “True” or should print “False” otherwise. You should not use any sort function for this program. Input: How many numbers you want to input: 3 # user input 3 Input the number: 5 Input the number: 2 Input the number: 7 Output: False
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and...
Java/Python/C++ Assignment Write a program to implement one round of AES-128 Input is the key and plaintext a. Show the plaintext as a 4x4 matrix b. Show the result after adding RoundKey0 c. Show the result after SubBytes d. Show the result after ShiftRows e. Show the result after MixColumns f. Show the result after first round
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT