C++ Create a program that reads a simple arithmetic expression and evaluates it. More precisely, the program reads from the user a line of text that’s supposed to contain two numbers separated by an arithmetic operator, as in: 35.6 + 12 The program then prints back the expression and its value, as in: 35.6 + 12 = 47.6 The program should verify that the input line contains two numbers, one operator and nothing else. There can be whitespace before the first number, after the second number and between each number and the operator. The operator can be either +, −, *, or /. In case of an error, the program should print: "Invalid expression" and quit. When the program prints the expression, there should be no whitespace before the first number and there should be exactly one blank space before and after the operator and the equal sign. Use string streams.
Addition of Two Numbers
Adding two numbers in programming is essentially the same as adding two numbers in general arithmetic. A significant difference is that in programming, you need to pay attention to the data type of the variable that will hold the sum of two numbers.
C++
C++ is a general-purpose hybrid language, which supports both OOPs and procedural language designed and developed by Bjarne Stroustrup. It began in 1979 as “C with Classes” at Bell Labs and first appeared in the year 1985 as C++. It is the superset of C programming language, because it uses most of the C code syntax. Due to its hybrid functionality, it used to develop embedded systems, operating systems, web browser, GUI and video games.
C++
Create a program that reads a simple arithmetic expression and evaluates it. More precisely, the program reads from the user a line of text that’s supposed to contain two numbers separated by an arithmetic
operator, as in:
35.6 + 12
The program then prints back the expression and its value, as in:
35.6 + 12 = 47.6
The program should verify that the input line contains two numbers, one operator and nothing else. There can be whitespace before the first number, after the second number and between each number and the operator.
The operator can be either +, −, *, or /. In case of an error, the program should print:
"Invalid expression"
and quit.
When the program prints the expression, there should be no whitespace
before the first number and there should be exactly one blank space before
and after the operator and the equal sign.
Use string streams.
Step by step
Solved in 2 steps