Question

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
//defintion of function string_to_int
int string_to_int(char str[])
{
    int temp,n=0,i;
    //loop to convert the string to int
    //In this loop we read the each character and
    //subtract that character from '0' ascii value
    //example - character is '4' read from string
    // 4 ascii value is - 52
    // 0 ascii value is - 48
    //subtract 52 - 48 = 4.In this way we convert the each string into digit.
    //Then we use below logic to convert digits into number
    for(i = 0;str[i] != '\0';i++)
    {
       temp = str[i] - '0';
       n = n *10 + temp;
    }
    //return converted number
    return n;
}
//main starts here
int main(int argc,char* argv[])
{
     char op;
     int num1,num2,result = 0;
     //argc stores the number of arguments
     //argv[0] - first argument which is executable file name
     //argv[1] = num1
     //argv[2] = operator
     //argv[3] = num2
     //calling the function string_to_int
     //return value stored in num1
     //if command line arguments syntax is wrong
     //it displays the error message
     if(argc <= 3)
     {
          printf("Usage: calc [operand_1] [operator] [operand_2]");
          return 0;
     }
     num1 = string_to_int(argv[1]);
     num2 = string_to_int(argv[3]);
     op = argv[2][0];
     switch(op)
     {
        case '+':
           result = num1 + num2;
           break;
        case '-':
           result = num1 - num2;
           break;
        case 'x':
              result = num1 * num2;
              break;
        case '/':
              result = num1 / num2;
              break;
        case '%':
              result = num1 % num2;
              break;
        default :
              printf("Invalid operator");
              return 0;
      }
      printf("%d\n",result);
      return 0;
}

1 #include<stdio.h> 2 //defintion of function string_to_int 3 int string_to_int(char str[]), int temp, n=0,i; 7/loop to conve----- - -- - - --- - - -- - - - int main(int argc, char* argv[]), Y char op; int num1, num2, result = 0; //argc stores the nuop = argv[2] [0]; switch(op) case +: result = num1 + num2; break; case -: result = num1 - num2; break; case x: result =| 12 + 101115 - 1020 - 55|- 3520/7n20%750 x 11550

Add a comment
Know the answer?
Add Answer to:
1. Specification Write a C program to implement a simple calculator that accepts input in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write a C program as follows: Single source code file Requests the user to input two...

    Write a C program as follows: Single source code file Requests the user to input two integer numbers Requests the user to make a choice between 0 (add), 1 (subtract), or 2 (multiply) Declares three separate functions Uses a pointer to these three functions to perform the requested action Outputs the result to the screen Submit your program source code file to this assignment. Sample Output Enter first integer number: 15 Enter second integer number: 10 Enter Choice: 0 for...

  • Question:Write a C program to input an integer k. Compute 10^k and store the result in...

    Question:Write a C program to input an integer k. Compute 10^k and store the result in a double variable. Display the result on the standard output using printf. Implementation ° The program is named lab4c . c. Use the given template lab4c .c (BELOW ) and fill in your code. ° Assume that the input integer k is small enough so that 10^k can be stored in a double variable named my_double. ° Display on the standard output the result...

  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT