哪有毛病,点点

P1449 后缀表达式

半云_ @ 2017-03-02 17:37:43

#include<cstdio>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<iostream>
using namespace std;
char ch[1120];
int ans=0,top=0;
int heap[10000];
int main(){
        gets(ch);
        int i=0;
        while(i<strlen(ch)-1){
        if (ch[i]>='0' && ch[i]<='9'){
            while(ch[i]!='.'){
            ans+=ans*10+ch[i]-'0';
            i++;
            }
            heap[++top]=ans;
            ans=0;    
        }
        else {
                      if (ch[i]=='+'){
                          int tt=heap[top]+heap[top-1];
                          top--;
                          heap[top]=tt;
                      }
                      if (ch[i]=='-'){
                          int tt=heap[top-1]-heap[top];
                          top--;
                          heap[top]=tt;
                      }
                      if (ch[i]=='*'){
                          int tt=heap[top]*heap[top-1];
                          top--;
                          heap[top]=tt;
                      }
                      if (ch[i]=='/'){
                          int tt=heap[top-1] / heap[top];
                          top--;
                          heap[top]=tt;
                      }
              }
    i++;          
    }
    cout<<heap[top];
    return 0;
}

by 半云_ @ 2017-05-16 16:58:55

求大神


by JKPS @ 2017-07-17 14:42:28

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
char s[1010];
stack<int> st;
int main(){
    char t='\0',ttt[200],pos=0;
    int shu,t1,t2,tans;
    while(t!='@')
    {
        t=getchar();
        if(t>='0'&&t<='9')
        {
            ttt[pos++]=t;     
            continue;
        }
        else if(t=='.')
            {
                ttt[pos]='\0';pos=0;    
                sscanf(ttt,"%d",&shu); 
                st.push(shu);
                continue;
            }
            else if(t!='@')
            {
                t1=st.top(); st.pop();
                t2=st.top(); st.pop();
                switch(t){
                case '+':st.push(t2+t1);break;
                case '-':st.push(t2-t1);break;
                case '*':st.push(t2*t1);break;
                case '/':st.push(t2/t1);break;
                }
                continue;
            }
    }
        cout<<st.top();
        return 0;
}

|