Problem Description
Write a program, enter a prefix expression as a string, and then calculate its value. The input format is: "Operator Object 1 Object 2", where the operator is "+" (addition), "-" (subtraction), "*" (multiplication), or "/" (division), and the operands are integers of not more than 10, separated by a space. Requirements: for addition, subtraction, multiplication, in addition to these four kinds of operations, respectively, design the corresponding function to achieve.
Input format: Enter only one row, which is a prefix expression string.
Output format: Outputs the corresponding calculation results (in the case of division, the C-language "/" operator is used directly, and the result is an integer).
Input/Output Sample
Sample Input
+ 5 2
Sample Output
7
Code:
#include <iostream>
#include <cstdio>
using namespace Std;
int Add (int a, int b)
{
return a + B;
}
int Sub (int a, int b)
{
return a-B;
}
int Div (int a, int b)
{
return a/b;
}
int Mul (int a, int b)
{
return a * b;
}
int main ()
{
char c;
int A, B;
cin>>c>>a>>b;
Switch (c)
{
Case ' + ':
Cout<<add (A, b) <<endl;
Break
Case '-':
Cout<<sub (A, b) <<endl;
Break
Case ' * ':
Cout<<mul (A, b) <<endl;
Break
Case '/':
Cout<<div (A, b) <<endl;
Break
}
return 0;
}
Prefix expression---character + function