1356:计算(calc) stl的实现

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#define Int stack<int>
#define Char stack<char>
using namespace std;
int n;
Int s1;//操作数栈
Char s2;//运算符栈
 
int lev(char x)//运算符优先级
{
    if ((x=='-')||(x=='+')) return 1;
    if ((x=='*')||(x=='/')) return 2;
    if (x=='^') return 3;
    return 0;
}
 
void calculate(Int &s1,Char &s2)//弹出栈顶元素并计算
{
   // /*取出后弹出栈*/
int x,y;
char z;
 y=s1.top();
 s1.pop();
 x=s1.top();
 s1.pop();
 z=s2.top();
 s2.pop();
    ///*根据运算符计算,并压入栈*/
 if(z=='+') s1.push(x+y);
 if(z=='-') s1.push(x-y);
 if(z=='*') s1.push(x*y);
 if(z=='/') s1.push(x/y);
 if(z=='^') s1.push(pow(x,y));
}
 
int c(int x)
{
    return x!=0;
}
char str[1000000];

int main()
{
    int i;
 
    scanf("%s",str+1);
    n=strlen(str+1);
    Int s1;
    Char s2;
    int temp=0;
    bool flag=false;
   for(i=1;i<=n+1;i++)//遍历每个元素,枚举所有情况,这里所有可能的情况较少,枚举不易出错。
   {
       if(str[i]>='0'&&str[i]<='9')
       {
           temp=temp*10+str[i]-'0';
           flag=true;
       }
       else
       {
           if(flag==true)
            {        
           s1.push(temp);
           flag=false;
           temp=0;
           }
              if(str[i]=='(')    s2.push(str[i]);
            if(str[i]==')')
            {
                while(s2.top()!='(')
                calculate(s1,s2);
                s2.pop();
            }

            if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^')
            {
                while(s2.empty()==0&&lev(str[i])<=lev(s2.top())) //这里考虑栈顶是‘(’的情况,实际lev函数中已经规定为了0,所以遇到左括号停止循环,要注意这个设计的细节。
                  calculate(s1,s2);
                 s2.push(str[i]);
            }
            if(str[i]=='\0')
              while(!s2.empty())    calculate(s1,s2);
    
       }
     }
   cout<<s1.top()<<endl;
    return 0;
}

 

//交的作业给修改了一下,手法还没有套路,能写到这样已经不错了,就是少了判断,这里可以看出作者的思路还是非常清晰的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
char a[256];
int stack[100];
char b[100];
int main()
{
    freopen("D:\\in.txt","r",stdin); 
    freopen("D:\\out.txt","w",stdout); 
    gets(a);
    int top1=0,top2=0,i=0,len=strlen(a),ans=0,on=0;
    if(a[len-1]!='@')
    {
        cout<<"NO"<<endl;return 0;
    }

        while(a[i]!='@')

    {
        if('0'<=a[i]&&a[i]<='9')
        {
            ans=ans*10+a[i]-'0';on=1;//考虑多位数字
        }
        else
        {
        if(on==1)//先判断有没有数字需要入栈
        {
            stack[++top2]=ans;
            on=0;
            ans=0;
        }
        if(a[i]=='*')
        {
            if(b[top1]=='*')
            {
                stack[top2-1]=stack[top2-1]*stack[top2];
                top2--;
                top1--;    
            }
            if(b[top1]=='/')
            {
                stack[top2-1]=stack[top2-1]/stack[top2];
                top2--;
                top1--;
            }
             b[++top1]='*';//这里帮你去掉了一次入栈,碰到符号不管是否满足条件实际都要入栈
        }
         if(a[i]=='/')
        {
            if(b[top1]=='*')
            {
                stack[top2-1]=stack[top2-1]*stack[top2];
                top2--;
                top1--;
            
            }
            if(b[top1]=='/')
            {
                stack[top2-1]=stack[top2-1]/stack[top2];
                top2--;
                top1--;
                
            }
            b[++top1]='/';//这里帮你去掉了一次入栈,碰到符号不管是否满足条件实际都要入栈
        }
    if(a[i]=='+')
        {
            if(b[top1]=='*')
            {
                stack[top2-1]=stack[top2]*stack[top2-1];
                top2--;
                top1--;
            
            }
             if(b[top1]=='/')
            {
                stack[top2-1]=stack[top2-1]/stack[top2];
                top2--;
                top1--;
            
            }
            if(b[top1]=='+')
            {
                stack[top2-1]=stack[top2]+stack[top2-1];
                top2--;
                top1--;
            
            }
            if(b[top1]=='-')
            {
                stack[top2-1]=stack[top2-1]-stack[top2];
                top2--;
                top1--;
            }
        b[++top1]='+';//这里帮你去掉了一次入栈,碰到符号不管是否满足条件实际都要入栈
        }
         if(a[i]=='-')
        {
            if(b[top1]=='*')
            {
                stack[top2-1]=stack[top2]*stack[top2-1];
                top2--;
                top1--;
            
            }
            else if(b[top1]=='/')
            {
                stack[top2-1]=stack[top2-1]/stack[top2];
                top2--;
                top1--;
            
            }
            else if(b[top1]=='+')
            {
                stack[top2-1]=stack[top2-1]+stack[top2];
                top2--;
                top1--;
            
            }
            else if(b[top1]=='-')
            {
                stack[top2-1]=stack[top2-1]-stack[top2];
                top2--;
                top1--;
                
            }
                b[++top1]='-';//这里帮你去掉了一次入栈,碰到符号不管是否满足条件实际都要入栈
        }
         if(a[i]=='(')
            b[++top1]='(';
     if(a[i]==')')
        {
            while(b[top1]!='(')
            {
                if(b[top1]=='*')
                    stack[top2-1]=stack[top2-1]*stack[top2];
                else if(b[top1]=='/')
                    stack[top2-1]=stack[top2-1]/stack[top2];
                else if(b[top1]=='+')
                    stack[top2-1]=stack[top2-1]+stack[top2];
                else if(b[top1]=='-')
                    stack[top2-1]=stack[top2-1]-stack[top2];
                top2--;
                top1--;
            }
            top1--;
        }
        }
            
    
        i++;
    }

        if(on==1)//最后是个数字,以为碰到了@停止了运行,所以没有入栈,这里不要忘记补充入栈
        {
            stack[++top2]=ans;
            on=0;
            ans=0;
        }
    while(top1!=0)
    {
        if(b[top1]=='*')
            stack[top2-1]=stack[top2]*stack[top2-1];
        else if(b[top1]=='/')
            stack[top2-1]=stack[top2-1]/stack[top2];
        else if(b[top1]=='-')
            stack[top2-1]=stack[top2-1]-stack[top2];
        else if(b[top1]=='+')
            stack[top2-1]=stack[top2-1]+stack[top2];
        top1--;
        top2--;
    }
    cout<<stack[top2]<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值