208k views
5 votes
Write a simple calculator program. Your program should accept as input an expression in the form of: 3 * 2 Use an if / elif ladder style structure to determine what operation needs to be performed on the two numbers. Your program should handle the arithmetic functions Add, Subtract, Multiply, and Divide (Depending on the operator entered). Input: For this assignment you are going to want to take in a string that has the expression separated by a space. Once you have stored the string you will need to split it. This means to get each individual part of the expression. Python has a split function that is part of the string. It will return a list of the pieces that were split. I realize we haven't discussed lists yet so I will give you some code. Suppose you have the expression: str

User Artilheiro
by
4.2k points

1 Answer

6 votes

Answer: Provided in the explanation section

Step-by-step explanation:

provided below is the program to carry out this task, i hope it provides clarity.

#include <iostream>

using namespace std;

int main() {

int n1, n2;

char ch;

cout << "Enter an expression" << endl;

cin >> n1 >> ch >> n2;

switch(ch) {

case '+':

cout << "The sum is " << n1+n2 << endl;

break;

case '-':

cout << "The difference is " << n1-n2 << endl;

break;

case '*':

cout << "The product is " << n1*n2 << endl;

break;

case '/':

cout << "The division is " << n1/n2 << endl;

break;

default:

cout << "Invalid expression" << endl;

}

return 0;

cheers i hope this helped !!

}

User Lizozom
by
4.5k points