首页 > 试题广场 >

Emacs计算器

[编程题]Emacs计算器
  • 热度指数:3005 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
Emacs号称神的编辑器,它自带了一个计算器。与其他计算器不同,它是基于后缀表达式的,即运算符在操作数的后面。例如“2 3 +”等价于中缀表达式的“2 + 3”。
请你实现一个后缀表达式的计算器。

输入描述:
输入包含多组数据。

每组数据包括两行:第一行是一个正整数n (3≤n≤50);紧接着第二行包含n个由数值和运算符组成的列表。

“+-*/”分别为加减乘除四则运算,其中除法为整除,即“5/3=1”。


输出描述:
对应每一组数据,输出它们的运算结果。
示例1

输入

3
2 3 +
5
2 2 + 3 *
5
2 2 3 + *

输出

5
12
10
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <exception>
#include <iomanip>
#include <memory>
#include <sstream>
#include <list>

using namespace std;

int main(int argc, char** argv)
{
	//freopen("in.txt", "r", stdin);
	int n;
	while (cin >> n)
	{
		vector<string> pre;
		string s;
		for (int i = 0; i < n; ++i)
		{
			cin >> s;
			pre.emplace_back(s);
		}
		while (pre.size() > 1)
		{
			for (int i = 0; i < pre.size(); ++i)
			{
				string op = pre[i];
				if (op == "+" || op == "-" || op == "*" || op == "/")
				{
					int x = stoi(pre[i - 2]), y = stoi(pre[i - 1]);
					switch (op[0])
					{
					case '+': x += y; break;
					case '-': x -= y; break;
					case '*': x *= y; break;
					case '/': x /= y; break;
					default:break;
					}
					pre[i] = to_string(x);
					pre.erase(pre.begin() + i - 2, pre.begin() + i);
					break;
				}
			}
		}

		cout << pre[0] << endl;
	}

	return 0;
}

发表于 2017-07-11 11:11:20 回复(0)
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
    stack<int> s1;
    int n;
    while(cin>>n)
    {
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            if(s[0]>='0'&&s[0]<='9'){
                int num=0;
                for(int i=0;i<s.length();i++)
                    num=num*10+s[i]-'0';
                s1.push(num);
            }
            else{
                int x=s1.top();s1.pop();
                int y=s1.top();s1.pop();
                if(s=="+")       s1.push(x+y);
                else if(s=="-")  s1.push(y-x);
                else if(s=="*")  s1.push(x*y);
                else if(s=="/")  s1.push(y/x);
            }
        }
        cout<<s1.top()<<endl;
    }
    return 0;
}

编辑于 2020-03-14 13:35:06 回复(0)
import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){				
			int n=in.nextInt();
			Stack<String> sk=new Stack<String>();
			for (int i = 0; i < n; i++) {
				String s=in.next();
				switch (s) {
				case "+":
					sk.push(String.valueOf(Integer.valueOf(sk.pop())+Integer.valueOf(sk.pop())));
					break;
				case "-":
					sk.push(String.valueOf(-(Integer.valueOf(sk.pop())-Integer.valueOf(sk.pop()))));
					break;
				case "*":
					sk.push(String.valueOf(Integer.valueOf(sk.pop())*Integer.valueOf(sk.pop())));
					break;
				case "/":
					sk.push(String.valueOf(Integer.valueOf(sk.pop())/Integer.valueOf(sk.pop())));
					break;
				default:
					sk.push(s);
					break;
				}
			}
			System.out.println(sk.pop());
		}
	}
}
数组指针问题,感觉输入没问题呀!

发表于 2017-05-08 10:12:53 回复(1)
  #include<iostream> 
  #include<string> 
  #include<stack> 
  #include<queue> 
  using namespace std; 
  

  int atoi(string c); 
  

  int main() 
  { 
      //cout<<string::npos<<endl; 
  

      int n; 
      //int res=0; 
      string c[50]; 
      string sign="+-*/"; 
      /*if(sign.find("+")!=string::npos) 
      { 
          cout<<"+ is in the sign"<<endl; 
      }*/ 
      while(cin>>n) 
      { 
          queue<string> ss1; 
          stack<int> ss; 
          ss.push(1); 
          for(int i=0;i<n;i++) 
          { 
              cin>>c[i]; 
              ss1.push(c[i]); 
          } 
          //cout<<ss1.front()<<endl; 
  

          int a,b; 
          while(!ss1.empty()) 
          { 
              string cc=ss1.front(); 
              //cout<<cc<<endl; 
              ss1.pop(); 
              if(sign.find(cc)==string::npos) 
              { 
                  ss.push(atoi(cc)); 
                  //cout<<ss.top()<<endl; 
                  continue; 
              } 
              else 
              { 
                  b=ss.top(); 
                  //cout<<a<<endl; 
                  ss.pop(); 
  

                  a=ss.top(); 
                  //cout<<b<<endl; 
                  ss.pop(); 
                  //cout<<cc<<endl; 
                  if(cc=="+") 
                  { 
                      //cout<<1<<endl; 
                      ss.push(a+b); 
                      //cout<<ss.top()<<endl; 
                  } 
                  else if(cc=="-") 
                  { 
                      ss.push(a-b); 
                  } 
                  else if(cc=="*") 
                  { 
                      ss.push(a*b); 
                  } 
                  else if(cc=="/") 
                  { 
                      if(b==0) 
                      { 
                          return 0; 
                      } 
                      ss.push(a/b); 
                  } 
              } 
          } 
          //cout<<n<<endl; 
          int res=ss.top(); 
          ss.pop(); 
          cout<<res<<endl; 
  

      } 
      return 0; 
  } 
  int atoi(string c) 
  { 
      int len=c.length(); 
      int res=0; 
      int i; 
      for(i=0;i<len;i++) 
      { 
          res=res*10+(c[i]-'0'); 
      } 
      //cout<<res<<endl; 
      return res; 
  } 
  

  

  

编辑于 2019-07-30 17:33:48 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <stack>

using namespace std;

int main()
{
    int n;
    while (cin >> n)
    {
        vector<string> calc(n);
        for (int i = 0; i < n; ++i)
        {
            cin >> calc[i];
        }
        
        string ops("+-*/");
        stack<int> st; //用int会溢出 long long不会 但int能过 long long不能过
                 //不知道为啥溢出才是正确的
        for (int i = 0; i < n; ++i)
        {
            if (ops.find(calc[i]) != string::npos)
            {
                char op = calc[i][0];
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                
                int res = 0;
                switch (op)
                {
                case '+':
                    res = left + right;
                    break;
                case '-':
                    res = left - right;
                    break;
                case '*':
                    res = left * right;
                    break;
                case '/':
                    res = left / right;
                    break;
                default:
                    cout << "bug" << endl;
                    break;
                }
                
                st.push(res);
            }
            else
            {
                int num = stoi(calc[i]);
                st.push(num);
            }
        }
        
        cout << st.top() << endl;
    }
}


编辑于 2022-05-21 14:06:12 回复(0)
是哦。。我咋没想到用栈呢。。。
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            String str = sc.nextLine();
            int[] nums = new int[n];
            char[] arr = sc.nextLine().toCharArray();
            int i = 0 , size = 0;
            for(char ch : arr){
                if(ch == '+'){
                    add(nums,--size);
                    --size;
                }else if(ch == '-'){
                    sub(nums,--size);
                    --size;
                }else if(ch == '*'){
                    mul(nums,--size);
                    --size;
                }else if(ch == '/'){
                    div(nums,--size);
                    --size;
                }else if(ch != ' '){
                    nums[size] = nums[size] * 10 + ch - '0';
                }else{
                    size++;
                }
            }
            System.out.println(nums[0]);
        }
    }
    private static void add(int[] nums,int index){
        nums[index-1] = nums[index-1] + nums[index];
        nums[index] = 0;
    }
    private static void sub(int[] nums,int index){
        nums[index-1] = nums[index-1] - nums[index];
        nums[index] = 0;
    }
    private static void mul(int[] nums,int index){
        nums[index-1] = nums[index-1] * nums[index];
        nums[index] = 0;
    }
    private static void div(int[] nums,int index){
        nums[index-1] = nums[index-1] / nums[index];
        nums[index] = 0;
    }
}

发表于 2022-05-20 19:58:25 回复(0)
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        stack<int> s;
        for(int i = 0; i < n; i++)
        {
            string num;
            cin >> num;
            if(num[0] >= '0' && num[0] <= '9')
                s.push(atoi(num.c_str()));
            else
            {
                int back = s.top();
                s.pop();
                int front = s.top();
                s.pop();
                if(num == "+")
                    s.push(front + back);
                if(num == "-")
                    s.push(front - back);
                if(num == "*")
                    s.push(front * back);
                if(num == "/")
                    s.push(front / back);
            }
        }
        cout << s.top() << endl;
    }
    return 0;
}

发表于 2021-11-26 10:51:18 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = Integer.parseInt(sc.nextLine());
            String[] str = sc.nextLine().split(" ");
            Stack<String> stack = new Stack<>();
            int sum = 0;
            for(int i = 0; i < n; i++){
                if(!str[i].equals("+") && !str[i].equals("-") 
                   && !str[i].equals("*") && !str[i].equals("/")){
                    stack.push(str[i]);
                }
                else{
                    int a = Integer.parseInt(stack.pop());
                    int b = Integer.parseInt(stack.pop());
                    if(str[i].equals("+")){
                        sum = a + b;
                    }
                    if(str[i].equals("-")){
                        sum = b - a;
                    }
                    if(str[i].equals("*")){
                        sum = a * b;
                    }
                    if(str[i].equals("/")){
                        sum = b / a;
                    }
                    stack.push(String.valueOf(sum));
                }
            }
            System.out.println(stack.pop());
        }
    }
}

发表于 2021-07-30 19:13:30 回复(0)
// 利用vector实现 原理和 stack一样;
// write your code here cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        // 把 n个字符先 放进 s (vector) 里
        vector s(n);
        for(int i =0; i<n; i++)
        {
            cin>>s[i];
        }
        //从s里取数据 先判断 再 操作;
        vector v;
        for(auto e:s)  // &e
        {
            if(e=="+"|| e=="-" || e=="*" || e=="/") //'+'
            {
                int second = v.back();
                v.pop_back();
                int first  = v.back();
                v.pop_back();

                if(e =="+")
                    v.push_back(first+second);
                if(e =="-")
                    v.push_back(first-second);
                if(e =="*")
                    v.push_back(first*second);
                if(e =="/")
                    v.push_back(first/second);
            }
            else
            {
                v.push_back(stoi(e));
            }
        }
        cout<<v.back()<<endl;
    }
    return 0;
}

编辑于 2021-06-12 17:17:16 回复(0)
  1. 哪里写错了??求救!!
    // write your code here cpp
    #include <iostream>
    #include <vector>
    #include <stack>
    #include <sstream>
    using namespace std;
    int main()
    {
    	int n = 0;
    	while (cin >> n)
    	{
    		vector<char> num(n, '0');
    		stack<int> st;
    		for (int i = 0; i < n; i++)
    		{
    			cin >> num[i];
    		}
    		for (int i = 0; i < n; i++)
    		{
    			if (num[i] == '+' || num[i] == '-' || num[i] == '*' || num[i] == '/')
    			{
    				if (num[i] == '+')
    				{
    					int num1 = st.top();
    					st.pop();
    					int num2 = st.top();
    					st.pop();
    					st.push(num1 + num2);
    				}
    				if (num[i] == '*')
    				{
    					int num1 = st.top();
    					st.pop();
    					int num2 = st.top();
    					st.pop();
    					st.push(num1 * num2);
    				}
    				if (num[i] == '-')
    				{
    					int num1 = st.top();
    					st.pop();
    					int num2 = st.top();
    					st.pop();
    					st.push(num2 - num1);
    				}
    				if (num[i] == '/')
    				{
    					int num1 = st.top();
    					st.pop();
    					int num2 = st.top();
    					st.pop();
    					st.push(num2 / num1);
    				}
    			}
    			else
    			{
    				stringstream ss;
    				int tmp = 0;
    				ss << num[i];
    				ss >> tmp;
    				st.push(tmp);
    			}
    			
    		}
    		int result = st.top();
    		cout << result << endl;
    	}
    }

发表于 2021-03-12 15:33:26 回复(1)
// write your code here cpp
#include<cstdio>
(802)#include<stack>
#include<cstring>
(803)#include<cctype>
using namespace std;
int main(){
    char str[60][20];
    int n;
    while(scanf("%d",&n)!=EOF){
        stack<int> s;
        //scanf("%s",str);
        //int len=strlen(str);
        for(int i=0;i<n;++i){
            scanf("%s",str[i]);
        }
        for(int i=0;i<=n-1;++i){
            if(isdigit(str[i][0])){
                int sum=0;
                for(int j=0;j<strlen(str[i]);++j){
                    sum=sum*10+str[i][j]-'0';
                }
                s.push(sum);
            }else{
                int a=s.top();
                s.pop();
                int b=s.top();
                s.pop();
                int sum;
                if(str[i][0]=='+')
                    sum=a+b;
                else if(str[i][0]=='-')
                    sum=b-a;
                else if(str[i][0]=='*')
                    sum=a*b;
                else
                    sum=b/a;
                s.push(sum);
            }
        }
        printf("%d\n",s.top());
    }
}

发表于 2020-03-20 00:22:43 回复(0)
#include<iostream>
(720)#include<string>
#include<stack>
using namespace std;

int main()
{
	int n = 0;
    stack<int> arr;
	while (cin >> n)
	{
		string str;
		getline(cin, str);
		getline(cin, str);
		for (int i = 0; i < str.size(); ++i)
		{
			if(str[i] >= '0' && str[i] <= '9')
			{
                int num = 0;
				while(str[i] != ' ')
                {
                    num = num*10 + str[i] - '0';
                    ++i;
                }
                
                arr.push(num);
			}
			else if (str[i] == '+' || str[i] == '-'
				|| str[i] == '*' || str[i] == '/')
			{
				int x = arr.top();
				arr.pop();
				int y = arr.top();
				arr.pop();
				if (str[i] == '*')
					arr.push(x * y);
				else if(str[i] == '/')
					arr.push(y / x);
				else if (str[i] == '-')
					arr.push(y - x);
				else
					arr.push(x + y);
			}
		}
		cout << arr.top() << endl;
	}
	return 0;
}

发表于 2020-03-14 19:07:52 回复(0)
真不知道这是哪错了  #include<iostream>



(720)#include<vector>
#include<stack>
(850)#include <string>
using namespace std;
int main()
{
	int n = 0;
	while (cin >> n)
	{
		getchar();
		string str;
		getline(cin, str);
		stack<int> si;
		for (int i = 0; i < str.size(); i++)
		{

			if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
			{
				int a = si.top();
				si.pop();
				int b = si.top();
				si.pop();
				if (str[i] == '+')
				{
					si.push(a + b);
				}
				else if (str[i] == '-')
				{
					si.push(b - a);
				}
				else if (str[i] == '*')
				{
					si.push(b*a);
				}
				else
				{
					if (a)
						si.push(b / a);
				}
			}
			else if (str[i] != ' ')
				si.push(str[i] - '0');

		}
		cout << si.top() << endl;
	}
}

编辑于 2020-03-12 21:51:56 回复(1)
#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main()
{
    int n;
    while (cin >> n)
    {
        getchar();
        stack<int> st;
        for (size_t i = 0; i<n; i++)
        {
            int sum = 0;
            string ch;//运用string而不是用char的原因是,char只能存10以下的字符,当输入的字符
            //大于10,就会出现错误
            cin >> ch;
            if (ch[0] == '+' ||ch[0] ==  '-' || ch[0] == '*' ||ch[0] == '/')
            {
                int right = st.top();//栈结构,自底向上,所以取值时,需要先取右操作数
                st.pop();
                int left = st.top();
                st.pop();
                if (ch[0] == '+')
                    sum = left + right;
                if (ch[0] == '-')
                    sum = left - right;
                if (ch[0] == '*')
                    sum = left*right;
                if (ch[0] == '/')
                {
                    if (right != 0)
                        sum = left / right;
                    else
                        return 0;
                }
                st.push(sum);
            }
            else
                st.push(atoi(ch.c_str()));
        }
        cout << (int)st.top() << endl;
    }
    return 0;
}

发表于 2019-07-26 19:55:34 回复(0)
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        vector<string> v;
        v.resize(n);
        stack<int> s;
        string str;
        for(int i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        for(int i = 0; i < n; i++)
        {
            str = v[i];
            if(!(str == "+" || str == "-" || str == "*" || str == "/"))
                //字符串转整数,注意是c语言的函数,要加上c_str
                s.push(atoi(str.c_str()));
            else
            {
                int right = s.top();
                s.pop();
                int left = s.top();
                s.pop();
                //把每次运算完的和入栈
                if(str == "+")
                    s.push(right+left);
                else if(str == "-")
                    s.push(left-right);
                else if(str == "*")
                    s.push(left*right);
                else if(str == "/")
                {
                    if(right != 0)
                        s.push(left/right);
                    else
                        return 0;
                }
            }
            str = "";
        }
        cout << s.top() << endl;
    }
    return 0;
}

发表于 2019-07-26 16:41:19 回复(0)
import java.util.Scanner;  import java.util.Stack; public class EmacsCal {   public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
      while (in.hasNext()) {
            String nn = in.nextLine(); int n = Integer.parseInt(nn);
            String[] str = new String[n]; for(int i=0; i<n; i++){
                str[i] = in.nextLine();
            }

            Stack<Integer> stk = new Stack<Integer>(); for(int i=0; i<n; i++) {
                String[] res = str[i].split(" "); int len = res.length; for(int j=0; j<len; j++) { if(!res[j].equals("+") && !res[j].equals("-") && !res[j].equals("*")&& !res[j].equals("/")) {
                        stk.push(Integer.parseInt(res[j]));
                    }else if (res[j].equals("+")) {
                        stk.push(stk.pop()+stk.pop());
                    }else if (res[j].equals("-")) {
                        stk.push((stk.pop()-stk.pop())*(-1));
                    }else if (res[j].equals("*")) {
                        stk.push(stk.pop()*stk.pop());
                    }else if (res[j].equals("/")) { int a = stk.pop(); int b = stk.pop();
                        stk.push(b/a);
                    }
                }
                System.out.println(stk.peek());
            }

        }
    }
}

想不通为啥过不了,但本地IDEA能过
发表于 2019-07-20 23:05:43 回复(1)
基本思路:利用栈实现后缀运算。遍历表达式,若为则数字入栈,若为运算符,则栈顶元素与次栈顶元素根据运算符运算,上述两元素出栈,运算结果入栈。最后栈顶元素即为后缀算术表达式的运算结果。
注意:
1.输入有空格,所以使用cin获取string(以空格为结束)每个数字和运算符均使用string保存 这样遍历vector<string>的每个元素即可
2.字符串与字符不同。"a"为字符串 ‘a’为字符 即便只有一个字符的字符串也不能与字符比较。所以是判断运算符为"+" "-" "*" "/"
3.+和*没有操作数要求,但是-和/要求操作数保持先后关系。因为使用栈,所以对于a-b,a先入栈,b**栈,所以栈顶元素为b,次栈顶为a
#include<iostream> //for cout endl
#include<stack> //for stack
#include<string>// for string
#include<vector>//for vector
#include<algorithm>//for pow
using namespace std;
int computeSuffix(vector<string>);//计算后缀表达式 int整数多于1位 + - * /
int str2dec(string);
int main()
{
    int n = 0;
    while (cin >> n)
    {
        vector<string> str;
        for (int i = 0; i < n; i++)
        {
            string temp;
            cin >> temp;
            str.push_back(temp);
        }
        int res = computeSuffix(str);
        cout << res << endl;
    }
    return 0;
}
int computeSuffix(vector<string> str)
{
    int size = str.size();
    stack<int> st;
    for (int i = 0; i < size; i++)
    {
        if (str[i] != "+"&&str[i] != "-"&&str[i] != "*"&&str[i] != "/")
            st.push(str2dec(str[i]));
        if (str[i] == "+")
        {
            int a = st.top();
            st.pop();
            int b = st.top();
            st.pop();
            st.push(a + b);
        }
        if (str[i] == "-")
        {
            int a = st.top();
            st.pop();
            int b = st.top();
            st.pop();
            st.push(b - a);
        }
        if (str[i] == "*")
        {
            int a = st.top();
            st.pop();
            int b = st.top();
            st.pop();
            st.push(a * b);
        }
        if (str[i] == "/")
        {
            int a = st.top();
            st.pop();
            int b = st.top();
            st.pop();
            st.push(b / a);
        }
    }
    return st.top();
}
int str2dec(string str)
{
    int size = str.size();
    int res = 0;
    for (int i = 0; i < size; i++)
        res += (str[i] - '0')*pow(10, size-i-1);
    return res;
}

发表于 2019-05-20 16:09:06 回复(0)
一开始将数据设置成long long型,只能说我考虑多了,反而错了!
#include <bits/stdc++.h>
using namespace std; 

int atoi(string s) {
    int res = 0;
    for(int i = 0; i < s.size(); ++i) {
        res *= 10;
        res += s[i] - '0';
    }
    return res;
}

int main()
{
    int n;
    while(cin >> n) {
        vector<string> v(n);
        stack<int> st;
        for(int i = 0; i < n; ++i) {
            cin >> v[i];
            if(v[i] != "+" && v[i] != "-" && v[i] != "*" && v[i] != "/") {
                st.push(atoi(v[i]));
            }
            else {
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                if (v[i] == "+") 
                    st.push(left + right);
                else if (v[i] == "-")
                    st.push(left - right); 
                else if (v[i] == "*")
                    st.push(left * right);
                else if (v[i] == "/") {
                    if (right != 0)
                        st.push(left / right); 
                    else
                        return 0;
                }
            }
        }
        cout << st.top() << endl;
    }
    return 0;
}

发表于 2018-02-20 16:16:11 回复(0)
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int atoi(string c)
{
    int len = c.length();
    int res = 0;
    int i = 0;
    for(i=0;i<len;i++){
        res = res*10 + (c[i]-'0');
    }
    return res;
}

int main()
{
    int n;
    string c;
    string sign = "+-*/";
    while(cin >> n){
        stack<int> ss;
        int a,b;
        while(n--){
            cin >> c;
            if(sign.find(c) == string::npos){
                ss.push(atoi(c));
                continue;
            }
            else{
                a = ss.top();
                ss.pop();
                b = ss.top();
                ss.pop();
                if(c == "+")
                    ss.push(a+b);
                else if(c == "-")
                    ss.push(b-a);
                else if(c == "*")
                    ss.push(a*b);
                else
                    ss.push(b/a);
            }
        }
        int res = ss.top();
        ss.pop();
        cout << res << endl;
    }
    return 0;
}

发表于 2016-01-21 15:43:05 回复(0)
// write your code here cpp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int atoi(char s[]) {
    int res=0;
    int i=0;
    if(s[i]=='-') i++;
    while(s[i]) {
        res = res*10+ s[i]-'0';
        i++;
    }
    if(s[0]=='-') res *= -1;
    return res;
}
int cal(int a, int b, char op[])
{
    switch(op[0]) {
        case '+': return a+b;
        case '-': return a-b;
        case '*': return a*b;
        case '\/': return  b!=0? a/b:-1;
        default:return -1;
    }
}
int main(int argc, char* argv[])
{
    int n;
    while(scanf("%d",&n)!=EOF) {
        char a[n][32];
        int i;
        for(i=0;i<n;i++) {
            scanf("%s",a[i]);
        }
        
        int res=0;
        int num=0;
        int ops=n/2;
        
        while(num<ops) {
            for(i=0;i<n;i++) {
                if((a[i][0]<'0' || a[i][0]>'9')&& a[i][1]==0) {
                   res =  cal(atoi(a[i-2]),atoi(a[i-1]), a[i]);
                   sprintf(a[i-2],"%d",res);
                   for(int j=i+1;j<n;j++) {
                      strcpy(a[j-2],a[j]);
                   }
                   num++;
                   n-=2;
                   break;
                } 
            }   
        }
        printf("%s\n",a[0]);
    }
    return 0;
}
/*
input:
49
37 66 66 39 8 - * - 74 41 - 98 - 79 + * 38 / 10 - 60 58 + 14 99 64 * / 46 - - 24 * + 23 / 88 46 + 8 * 46 / 59 / + 22 + +
49
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + + + + + + + + + + + + + + + + + + + + + + + +
output:
198
25
*/

发表于 2015-12-27 18:29:40 回复(0)