#include<bits/stdc++.h>
using namespace std;
const int N = 114514 ;
int z[N] ;
stack<int> sum;
string s;
int p(int i){
	int o=1,cum=0;
	for(;s[i]!='.'&&i!=-1&&s[i]!='+'&&s[i]!='-'&&s[i]!='*'&&s[i]!='/';i--,o*=10){
		cum+=(s[i]-'0')*o;
	}
	return cum;
}
int main()
{
	int a,b;
	getline(cin,s);
	for(int i=0;s[i]!='@';i++){
		if(s[i]>='0'&&s[i]<='9') continue;
		if(s[i]=='.'){ 
			sum.push(p(i-1));
			continue;
		}
		if(s[i]=='+'){
			a = sum.top();
			sum.pop();
			b = sum.top();
			sum.pop();
			sum.push(b+a);
		}
		if(s[i]=='-'){
			a = sum.top();
			sum.pop();
			b = sum.top();
			sum.pop();
			sum.push(b-a);
		}if(s[i]=='*'){
			a = sum.top();
			sum.pop();
			b = sum.top();
			sum.pop();
			sum.push(b*a);
		}if(s[i]=='/'){
			a = sum.top();
			sum.pop();
			b = sum.top();
			sum.pop();
			sum.push(b/a);
		}
	}
	cout << sum.top() ;
	return 0;
}
//怨深,泣动 

3 comments

  • @ 2024-3-16 16:55:06
    //这个看着好理解
    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 114514;
    int a[N];
    int i;
    int now;
    int main()
    {
    	char s;
    	while((s = getchar()) != '@')
    	{
    		if(s >= '0' && s <= '9')
    		{
    			now = 10 * now; 
    			now += s - '0'; 	
    		}
    		else if(s == '.')
    		{
    			a[++i] = now;
    			now = 0;
    	
    		}	
    		else if(s == '+')
    		{
    			a[i - 1] = a[i - 1] + a[i];
    			a[i] = 0;
    			i--; 
    		}
    			else if(s == '-')
    		{
    			a[i - 1] = a[i - 1] - a[i];
    			a[i] = 0;
    			i--; 
    		}
    			else if(s == '*')
    		{
    			a[i - 1] = a[i - 1] * a[i];
    			a[i] = 0;
    			i--; 
    		}
    			else if(s == '/')
    		{
    			a[i - 1] = a[i - 1] / a[i];
    			a[i] = 0;
    			i--; 
    		}
    		else break;
    	}
    	cout << a[1];
    	return 0;
    }
    
    • @ 2024-3-14 21:52:01

      这个好像更简单

      #include<bits/stdc++.h>
      using namespace std;
      
      stack<long long> stk;
      
      int main()
      {
      	string a;
      	cin >> a;
      	long long n = 0;
      	for(int i = 0; i < a.size(); i++)
      	{
      		if(a[i] == '.') continue;
      		else if(a[i] == '@')
      		{
      			cout << stk.top() << endl;
      			return 0;
      		}
      		else if(a[i] >= '0' && a[i] <= '9')
      		{
      			n += a[i] - '0';
      			if(a[i + 1] == '.')
      			{
      				stk.push(n);
      				n = 0;	
      			}
      			else
      			{
      				n *= 10;
      			}
      		}
      		else if(a[i] == '+')
      		{
      			long long temp1 = stk.top();
      			stk.pop();
      			long long temp2 = stk.top();
      			stk.pop();
      			stk.push(temp1 + temp2);
      		}
      		else if(a[i] == '-')
      		{
      			long long temp1 = stk.top();
      			stk.pop();
      			long long temp2 = stk.top();
      			stk.pop();
      			stk.push(temp2 - temp1);
      		}
      		else if(a[i] == '*')
      		{
      			long long temp1 = stk.top();
      			stk.pop();
      			long long temp2 = stk.top();
      			stk.pop();
      			stk.push(temp1 * temp2);
      		}
      		else if(a[i] == '/')
      		{
      			long long temp1 = stk.top();
      			stk.pop();
      			long long temp2 = stk.top();
      			stk.pop();
      			stk.push(temp2 / temp1);
      		}	
      	}	
      	
      	return 0;
      }
      
      • @ 2024-3-14 21:09:27

        👎

        • 1