前缀表达式

来源:互联网 发布:中文模糊匹配软件 编辑:程序博客网 时间:2024/06/16 07:43
问题描述  编写一个程序,以字符串方式输入一个前缀表达式,然后计算它的值。输入格式为:“运算符 对象1 对象2”,其中,运算符为“+”(加法)、“-”(减法)、“*”(乘法)或“/”(除法),运算对象为不超过10的整数,它们之间用一个空格隔开。要求:对于加、减、乘、除这四种运算,分别设计相应的函数来实现。  输入格式:输入只有一行,即一个前缀表达式字符串。  输出格式:输出相应的计算结果(如果是除法,直接采用c语言的“/”运算符,结果为整数)。  输入输出样例    样例输入+ 5 2    样例输出7
#include<iostream>using namespace std;int Jia(int a,int b){    return a+b;}int Jian(int a,int b){    return a-b;}int Cheng(int a,int b){    return a*b;}int Chu(int a,int b){    return a/b;}int main(){    char ch;    int a,b;    cin>>ch>>a>>b;    if(ch=='+')cout<<Jia(a,b);    else if(ch=='-')cout<<Jian(a,b);    else if(ch=='*')cout<<Cheng(a,b);    else if(ch=='/')cout<<Chu(a,b);    return 0;}