Knowee
Questions
Features
Study Tools

Charlie, a novice programmer, is developing a simple calculator program to perform basic arithmetic operations. He needs your assistance in refining the program.Write a program that takes an operator ('+', '-', '*', or '/') and two operands as input. The program should then perform the corresponding operation and display the result. If the input is an invalid operator, the program should display an appropriate error message.Function Specifications:double add(double x, double y)double subtract(double x, double y)double multiply(double x, double y)double divide(double x, double y)Input format :The first line of input consists of a character c, representing the operator (+, -, *, /).The second line consists of two space-separated double values a and b, representing the operands.Output format :

Question

Charlie, a novice programmer, is developing a simple calculator program to perform basic arithmetic operations. He needs your assistance in refining the program.Write a program that takes an operator ('+', '-', '*', or '/') and two operands as input. The program should then perform the corresponding operation and display the result. If the input is an invalid operator, the program should display an appropriate error message.Function Specifications:double add(double x, double y)double subtract(double x, double y)double multiply(double x, double y)double divide(double x, double y)Input format :The first line of input consists of a character c, representing the operator (+, -, *, /).The second line consists of two space-separated double values a and b, representing the operands.Output format :

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple program in C++ that fulfills your requirements:

#include <iostream>
using namespace std;

double add(double x, double y) {
    return x + y;
}

double subtract(double x, double y) {
    return x - y;
}

double multiply(double x, double y) {
    return x * y;
}

double divide(double x, double y) {
    if(y != 0)
        return x / y;
    else {
        cout << "Error! Division by zero is not allowed.";
        return 0;
    }
}

int main() {
    char operator;
    double num1, num2;

    cout << "Enter operator: ";
    cin >> operator;
    cout << "Enter two operands: ";
    cin >> num1 >> num2;

    switch(operator) {
        case '+':
            cout << "Result: " << add(num1, num2);
            break;
        case '-':
            cout << "Result: " << subtract(num1, num2);
            break;
        case '*':
            cout << "Result: " << multiply(num1, num2);
            break;
        case '/':
            cout << "Result: " << divide(num1, num2);
            break;
        default:
            cout << "Error! Invalid operator.";
            break;
    }

    return 0;
}

This program first takes an operator and two operands as input. Then, it uses a switch statement to call the appropriate function based on the operator. If the operator is invalid, it displays an error message. The divide function also checks if the second operand is zero before performing the division to avoid a division by zero error.

This problem has been solved

Similar Questions

Write a program that does basic arithmetic operations (addition, subtraction, multiplication,and division). The inputs to the program are two numbers (in double format) and theoperation required. Provide a function for each operation and the identifiers for addition,subtraction, multiplication, and division are ‘+’, ‘-‘, ‘*’, and ‘/’ respectively

Write a program to enter two numbers and print the arithmeticoperations like +,-,*,**, /, // and % using function

.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.

John's little brother is struggling with math. He decided to design a calculator with basic operations such as addition, subtraction, and multiplication.The calculator should receive two numbers and an operator from the user. It should operate according to the operator entered and display the result. Help him implement the calculator using the Switch statement.

You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.Evaluate the expression. Return an integer that represents the value of the expression.Note that:The valid operators are '+', '-', '*', and '/'.Each operand may be an integer or another expression.The division between two integers always truncates toward zero.There will not be any division by zero.The input represents a valid arithmetic expression in a reverse polish notation.The answer and all the intermediate calculations can be represented in a 32-bit integer. Example 1:Input: tokens = ["2","1","+","3","*"]Output: 9Explanation: ((2 + 1) * 3) = 9

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.