Ask Question
16 May, 08:45

Write a simple arithmetic expression translator that reads in expressions such as 25.5 + 34.2 and displays their value. Each expression has two numbers separated by an arithmetic operator. (Hint: Use a switch statement with the operator symbol (type char) as a selector to determine which arithmetic operation to perform on the two numbers. For a sentinel, enter an expression with zero for both operands.)

+1
Answers (1)
  1. 16 May, 11:05
    0
    Following are the program in the C+ + Programming Language.

    //header file

    #include

    //header file

    #include

    //namespace

    using namespace std;

    //set main function

    int main ()

    {

    //set float type variables

    float a, s, m, d, c, b, g;

    //print message and get variable from the user

    cout<<" Enter The First Number : ";

    cin>>c;

    //print message and get variable from the user

    cout<<" Enter The Second Number: ";

    cin>>b;

    again:

    //get variable from the user for Operation

    cout<<" Enter the Operation which you want : ";

    char choice;

    cin>>choice;

    //Addition

    a=c+b;

    //Subtraction

    s=c-b;

    //Multiplication

    m=c*b;

    //Division

    d=c/b;

    //Modulus

    g=fmod (c, b);

    //set switch statement

    //here is the solution

    switch (choice)

    {

    case '+':

    cout<<"Addition: "<
    goto again;

    break;

    case '-':

    cout<<"Subtraction: "<
    goto again;

    break;

    case '*':

    cout<<"Multiplication: "<
    goto again;

    break;

    case '/':

    cout<<"Division: "<
    goto again;

    break;

    case '%':

    cout<<"Modulus: "<
    goto again;

    break;

    default:

    cout<<"Exit";

    break;

    }

    return 0;

    }Explanation:

    Here, we define the required header files then, we set main function.

    set float type variables a, s, m, d, c, b, g. Get input from the user for operation in the variable c, d. Set character type variables in which we get input from the user for operations. Then, we perform some operations. Set switch statement which display the output according to the user an if user input wrong then statement breaks.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a simple arithmetic expression translator that reads in expressions such as 25.5 + 34.2 and displays their value. Each expression has ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers