A simple calculator for writing algorithms every day (application of stack)

For a record, I still didn't know how to start when I wrote it. I felt stuck for a long time, and I have learned it several times.
code show as below:

#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
#define MAX 210
int T;

int main(){
   char s[MAX];
   int i,j;
   double last,now,ans ;
   stack<double> numQE;
   while(gets(s),strcmp(s,"0"))
   {
       years = 0;
       int len ​​=strlen(s);
       char lastOperator=' ';
       for(i = 0 ; i <len ; i++)
       {
           if(s[i]==' ')
           continue ;
           if(s[i]=='*'||s[i]=='/')
           {
               last = numQE.top();
               numQE.pop();
                now =0 ;
               for(j = i+2 ; j <len ;j++)
               {
                    if(s[j]!=' ')
                        now = now*10+(s[j]-'0');
                    else
                        break ;
                }
                if(s[i]=='*')
                    numQE.push(now*last);
                else
                    numQE.push(last/now);
                i = j;
               lastOperator=s[i];

           }
           else if(s[i]=='+'||s[i]=='-')
           {
                lastOperator=s[i];
           }else{
                double  res =0 ;
                for(j = i ; j <len ;j++)
                {
                    if(s[j]!=' ')
                        res = res*10+(s[j]-'0');
                    else
                        break ;
                }
                if(lastOperator=='-')
                    numQE.push(-res);
                else
                    numQE.push(res);
                i = j;
           }
       }
       while(!numQE.empty())
       {
               ans+=numQE.top();
               numQE.pop();
       }
        printf("%.2lf\n",ans);
   }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566360&siteId=291194637