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

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 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
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...
Create a program that ask to input two users and the result will vary on their...
Create a program that ask to input two users and the result will vary on their name with similar digits. In a game of F.L.A.M.E.S , it will count and repeat depends on their name that has a similar digit. For an Example (JOE RIZAL) and (JACKLYN BRACKEN) - JOE RIZAL has - 5 similar digits , while JACKLYN BRACKEN has 6 similar digits so a total of 11. F - Friends - 1,7 L - Lover - 2,8 A...
2.29 Domain Lab 4.1 -- Compound Interest Calculator Compound Interest Calculator Your program should ask the...
2.29 Domain Lab 4.1 -- Compound Interest Calculator Compound Interest Calculator Your program should ask the user (in this order) for: Principal … In this equation, it is expressed as PV Annual Interest Rate Time Period (years) … Note that there are 12 compounding periods per year. So your n value in the equation is years multiplied by 12 Your program should then calculate and display: Total Interest Generated Total Future Value SPECIAL NOTE Make sure to round output to...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT