程序设计与算法(二)算法基础 括号表达式

对于一个表达式的完整定义如下:

#include <bits/stdc++.h>
#define LL long long
using namespace std;

char s[10005];
int n, tot=1;
int bds(int &tot);

int yz(int &tot){
    if(s[tot]=='('){
        //++ (
        int ans=bds(++tot);
    }
    else{
        int ans=0;
        while(s[tot]<='9'&&s[tot]>='0'){
            ans=ans*10+s[tot]-'0';
            tot++;
        }
        return ans;
    }
}

int xiang(int &tot){
    int ans=yz(tot);
    bool vis=true;
    while(vis){
        char op=s[tot];
        if(op=='*'||op=='/'){
            //++ 运算符号
            int s1=yz(++tot);
            if(op=='*'){
                ans*=s1;
            }
            else{
                ans/=s1;
            }
        }
        else{
            vis=false;
        }
    }
    return ans;
}

int bds(int &tot){
    int ans=xiang(tot);
    bool vis=true;
    while(vis){
        char op=s[tot];
        if(op=='+'||op=='-'){
            //++ 运算符号
            int s1=xiang(++tot);
            if(op=='+'){
                ans+=s1;
            }
            else{
                ans-=s1;
            }
        }
        else {
            vis=false;
            tot++;//++ )
        }
    }
    return ans;
}

int main(){

    scanf("%s", s+1);
    n=strlen(s+1);
    printf("%d\n", bds(tot));

    return 0;
}
全部评论

相关推荐

想问各位佬,这是挂了吗
糖糖p:同学,瞅瞅我司,医疗独角兽,校招刚开,名额有限,先到先得,我的主页最新动态,绿灯直达,免笔试~
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
08-26 10:41
门头沟学院 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-08 18:20
职场水母:这题思路是什么,我目前想的一个暴力方法就是先把这个链表遍历一遍,用哈希表存储出现次数,然后再根据哈希表来一个一个删除节点,
点赞 评论 收藏
分享
在投简历的柠檬精很想...:可以明确说,问的东西几乎是简历上的东西。你写的确实有点模糊。面试可能会问你一些常用的通信的问题,差分信号走线之类的,单片机最小系统啥的,模电,数电,基本电源,buck,boost,ldo之类的吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务