Question

In: Computer Science

Write a program that acts as a basic calculator Assume the starting result is 0 Ask...

Write a program that acts as a basic calculator

  • Assume the starting result is 0
  • Ask the user for the operation to be performed
    • 0 – Exit
    • 1 – Add
    • 2 – Subtract
    • 3 – Multiply
    • 4 – Divide (Assume input will not be 0)
  • Keep on performing the operation and showing the result until the user inputs the ‘0’ (exit) operation.
  • You need to have everything written as functions
    • E.g. - You should only have the options listed once in the code
  • You should have 4 different functions for each operation.
    • Any 2 of these functions should not return anything but instead use parameter passing by reference/pointers.
    • The other 2 functions should pass the parameter by value and have return statements (make sure to have a way to restrict the users to not make any changes to the original result variable).
  • Your main function should be just the following lines

please use #include studio not ios.stream

int main(int argc, char* argv[])

{

    calculator();

    return 0;

}

/* Sample run of the program - Input and Output */

0 - Exit

1 - Add

2 - Subtract

3 - Multiply

4 - Divide

Please select an option : 1

Please enter the value : 5

Result = 5.000000

0 - Exit

1 - Add

2 - Subtract

3 - Multiply

4 - Divide

Please select an option : 2

Please enter the value : 1

Result = 4.000000

0 - Exit

1 - Add

2 - Subtract

3 - Multiply

4 - Divide

Please select an option : 3

Please enter the value : 3

Result = 12.000000

0 - Exit

1 - Add

2 - Subtract

3 - Multiply

4 - Divide

Please select an option : 4

Please enter the value : 2

Result = 6.000000

0 - Exit

1 - Add

2 - Subtract

3 - Multiply

4 - Divide

Please select an option : 0

Solutions

Expert Solution

#include <stdio.h>
float add(float a,float b){return a+b;}
float sub(float a,float b){return a-b;}
void mul(float *a,float b){*a=(*a)*b;}
void divi(float *a,float b){*a =( *a)/b;}
void claculator()
{ float result=0;
float inp=0;
int opt=-1;
while(1)
{
printf("0-Exit\n1-Add\n2-Substarct\n3-Multiply\n4-Divide\n");
printf("Please select an option :");
scanf("%d",&opt);

if(opt==0)
break;
printf("Please enter the value : ");
scanf("%f",&inp);
if(opt==1)
result=add(result,inp);
if(opt==2)
result=sub(result,inp);
if(opt==3)
mul(&result,inp);
if(opt==4)
divi(&result,inp);
printf("result =%f\n",result);
}
}
int main()
{
claculator();

return 0;
}


Related Solutions

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,...
In PYTHON : as basic as possible Write a program that acts as a cash register....
In PYTHON : as basic as possible Write a program that acts as a cash register. The program will prompt how many items they are buying. Then they will input the price of each item (these should be decimal numbers). The program will display the SUBTOTAL, TAX ADDED (13%), and the TOTAL (with tax). Make sure you include $ signs and round to two decimal places ] Sample Output: How many items are you buying? 3 Enter in a price:...
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 program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Write a C# program to ask the user for an undetermined amount ofnumbers (until 0...
Write a C# program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum.
Write a program and ask a user to enter a numeric value between 0 - 99....
Write a program and ask a user to enter a numeric value between 0 - 99. Your program will spell out the numbers into words. You must use at least three switch cases in addition to multiple if statements. Each missing switch statement will reduce your grade for this problem by 20%. Note: The total number of if statements and switch cases should not exceed 28. Additional if/case statement will further reduce your grade by 5%. in c++ please
In this project you will create a basic console based calculator program. The calculator can operate...
In this project you will create a basic console based calculator program. The calculator can operate in two modes: Standard and Scientific modes. The Standard mode will allow the user to perform the following operations: (+, -, *, /) add, subtract, multiply, and divide The Scientific mode will allow the user to perform the same functionality as the Standard add, subtract, multiply, and divide (+, -, *, / ) plus the following: sin x, cos x, tan x. (sin x,...
Count the number of 1’s and 0’s Write a MIPS program that will ask the user...
Count the number of 1’s and 0’s Write a MIPS program that will ask the user to enter an integer, and then output the number of 1’s and 0’s that are present in the integer’s signed 32-bit binary representation. For example, 15 has a binary representation of 0000 0000 0000 0000 0000 0000 0000 1111, which has 28 zeroes and 4 ones. We have provided you the starter code that deals with the input/output logic. The integer input is saved...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT