括号匹配&&计算问题

1.括号匹配

int isMatched(char left,char right){
    if(left == '('&& right == ')'){
        return 1;
    }else if(left == '['&&right ==']'){
        return 1;
    }else if(left == '{'&&right =='}'){
        return 1;
    }else{
        return 0;
    }
}
int isParenthesesBalanced(char exp[]){
    char s[MaxSize];int top=-1;
    for(int i=0;exp[i]!='\0';i++){
        if(exp[i] =='(' ||exp[i] =='{'||exp[i]=='['){
            s[++top]=exp[i];
        }
        if(exp[i] == ')'||exp[i] == '}'||exp[i]==']'){
            if(top ==-1){
                return 0;
            }
            char left=s[top--];
            if(isMatched(left,exp[i]) == 0){
                return 0;
            }
        }
    }
    if(top>-1){
        return 0;
    }
    return 1;
}

 

2.计算问题

 

 

int calF(int m){
    int cum=1;
    int s[MaxSize],top=-1;
    while(m!=0){
        s[++top]=m;
        m/=3;
    }
    while(top!=-1){
        cum*=s[top--];
    }
    return cum;
}

 

posted on 2020-06-25 08:45  二进制dd  阅读(212)  评论(0编辑  收藏  举报

导航