Prefix expression---character + function

Source: Internet
Author: User
Tags mul

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.