天天看點

Arithmetic Problems(字尾表達式)

Arithmetic Problems(字尾表達式)

傳送門

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
#define mst(a) memset(a,0,sizeof a)
int main(){
	double n;
    char c;
    stack<double>s;
    double ans=0;
        int f=0,jg=0;
    cin>>n;
    s.push(n);
    while(cin>>c){
        cin>>n;
        if(f) continue;
        if(c=='/') s.push(n);
        else if(c=='*') s.push(-n);
        else if(c=='-'){
             double x=s.top();
             s.pop();
            s.push(x*n);
            //printf("x*n=%lf\n",x*n);
        }
        else if(c=='+'){
            if(n==0){
                f=1;
                continue;
            } 
            double x=s.top();
            s.pop();
            s.push(x/n);
            //printf("x/n=%lf\n",x/n);
        }
    }
     if(!f){
         while(s.size()){
             //cout<<s.top()<<endl;
             ans+=s.top();
             s.pop();
         }
         printf("%.2lf\n",ans);
     }
     else  puts("Cannot be divided by 0");
	return 0;
} 
           
#include<cstdio>
#include<iostream>
using namespace std;
double a[N]=6e5+5;
int main(){
	double n,ans=0;
	int cnt=0,f=0;
	char c;
	cin>>n;
	a[++cnt]=n;
	while(cin>>c>>n){
		if(c=='/') a[++cnt]=n;
		else if(c=='*') a[++cnt]=-n;
		else if(c=='-') a[cnt]*=n;
		else {
			if(!n) f=1;
			else a[cnt]/=n;
		}
	}
	if(f) puts("Cannot be divided by 0");
	else{
		for(int i=1;i<=cnt;i++) ans+=a[i];
		printf("%.2lf\n",ans);
	}
}