Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to implement a four function calculator. The program should pr

ID: 3789305 • Letter: W

Question

Write a C program to implement a four function calculator. The program should prompt the user for a formula. After the user enters a formula, consisting of two floating point values separated by an operator, the program calculates the value of the formula and prints out that result. Operators supported by the calculator are addition (denoted by +), subtraction (denoted by -), multiplication (denoted by *), and division (denoted by /). After the program calculates and prints the result, it should prompt the user to continue with another formula or terminate the program.

Sample Run:

This is a four function calculator.

Please enter a formula similar to "3.14 * 2.1":8+5

The result is 13.000000

Do you want to enter another formula? Enter Y or y to enter another formula or any other character to end the program: Y

Please enter a formula similar to "3.14 * 2.1": 8^6

ERROR: ^ is an invalid operator. This calculator supports only +, -, *, and / Do you want to enter another formula? Enter Y or y to enter another formula or any other character to end the program: y

Please enter a formula similar to "3.14 * 2.1": 9*abc

ERROR: Unable to read your formula. Did you format it properly?

Thank you for using me for your calculations

Explanation / Answer

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
#include <string.h>


int main() {

    char formula[3];
    char op,* user;
    double firstNumber,secondNumber;

do
{
    printf("Please enter a formula similar to "3.14 * 2.1": ");
    scanf("%s",formula);
   
    
firstNumber = int(formula[0] - '0');

op = char(formula[1]);

secondNumber = int(formula[2] - '0');


    switch(op)
    {
  case '+':
     printf(" The result is %.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
     break;

        case '-':
            printf(" The result is %.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf(" The result is %.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf(" The result is %.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / firstNumber);
            break;

        // operator doesn't match any case constant (+, -, *, /)
        default:
            printf(" ERROR: Unable to read your formula. Did you format it properly?");
    }

printf(" Do you want to enter another formula? Enter Y or y to enter another formula or any other character to end the program :");
scanf("%s",user);

}while(strcmp(user,"Y")== 0 || strcmp(user,"y") == 0 );
  
    getch();
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote