Question

In: Computer Science

Write a program, which will act as a simple four-function calculator. That is, it will read...

Write a program, which will act as a simple four-function calculator. That is, it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters either an upper- or lower-case C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator.

NOTES:

The person using the calculator has a terrible time using a keyboard and may not always enter a valid operator. If this happens the program should ask for another operator. Regardless of the math operation attempted, the program must not die. If the math operation would cause a fatal error in your program you should check for it and issue an error message rather than attempting the operation.

Please write the code for C programming, not C++ or Java

Solutions

Expert Solution

SimpleCalculator.c

#include<stdio.h>
int main()
{
   // variables declaration
   char operator;
   int operand1=0,operand2=0,result=0;
   printf("\n **** SIMPLE CALCULATOR ****\n\n");
   while(1)
   {
       printf("****************************\n");
       printf("Enter operand 1: ");
       scanf("%d",&operand1); // get first operand
       op:
       printf("Enter an operator: ");
       scanf("%s",&operator); // get an operator
       // check operator is instructing to clear or not
       if(operator=='c' || operator=='C')
       {
           operand1=operand2=0;
           printf("Cleared...!!!\n****************************\n");
           continue;
       }
       else
       // check operator is instructing to turn off calculator or not
       if(operator=='x' || operator=='X')
       {  
           break;  
       }
       else
       // check operator is valid or not
       if(operator!='+' && operator!='-' && operator!='*' && operator!='/')
       {
           printf("Wrong operator. please enter again...!!!\n");
           goto op; // jump to op
       }
       else
       {
           op2:
           printf("Enter operand 2: ");
           scanf("%d",&operand2); // get second operand
           // addition
           if(operator=='+')
           {
               result=operand1+operand2;
           }
           else
           // subtraction
           if(operator=='-')
           {
               result=operand1-operand2;
           }
           else
           // multiplication
           if(operator=='*')
           {
               result=operand1*operand2;
           }
           else
           // division
           if(operator=='/')
           {
               // check operand 2 is 0 or not
               // in the case of division only
               if(operand2==0)
               {
                   printf("Can not divide by zero...!!!\n");
                   goto op2; // jump to op2
               }
               result=operand1/operand2;
           }
           operand1=result;
           // print result
           printf("Result= %d\n",result);
           goto op; // jump to op
       }
   }
   printf("\n******* DONE *******");
   return 0;
}

Output


Related Solutions

For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
Use a switch statement on a char variable to write a function that's a simple calculator...
Use a switch statement on a char variable to write a function that's a simple calculator that keeps running waiting for input Should have a do while loop that continually loops the function, prompting for a new input and new operator, until the calculator returns 0, in which case it will close.
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...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
Complete these steps to write a simple calculator program: 1. Create a calc project directory 2....
Complete these steps to write a simple calculator program: 1. Create a calc project directory 2. Create a mcalc.c source file containing five functions; add, sub, mul, div, and mod; that implement integer addition, subtraction, multiplication, division, and modulo (remainder of division) respectively. Each function should take two integer arguments and return an integer result. 3. Create a mcalc.h header file that contains function prototypes for each of the five functions implemented in mcalc.c. 4. Create a calc.c source file...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT