简单逆波兰表达式.(华为面试题简版)

之前貌似看过一个题说是计算给定的一个多项式。多项式的数都只有一位.比如:

1+2+3

1*2+4/2之类的。。。

#include "stdio.h"
#include "stdlib.h"
#define    stack_size 100
#define end_array -10
struct stack{
    int cur_data;
    int data[stack_size];
};
//初始化栈
void init_stack(struct stack *q){
    q->cur_data = -1;
}
//入栈
void in_stack(int a,struct stack *q){
    if (q->cur_data == stack_size){
        printf("StackOverflow\n");
        exit(0);
    } else {
        q->cur_data++;
        q->data[q->cur_data] = a;
    }
}
//出栈
int out_stack(struct stack *q){
    int r;
    if (q->cur_data == -1){
        printf("StackEmpty\n");
        
        exit(0);
    } else {
        r = q->data[q->cur_data];
        q->cur_data--;
    }     
    return r;
}
//判断出栈的运算符
int o_stack_check(int a,int b){
    if (a==-1 || a==-2){
        return 0;
    } else if(b==-1 || b==-2){
            return 1;
    } else{
            return 0;
    }
}
//中缀表达式转化为后缀表达式即逆波兰表达式
void con_poland(int *input){
    struct stack pol;
    int i,j=0;
    int t;
    init_stack(&pol);
    for (i=0;input[i]!=end_array;i++){
        if (input[i] >= 0){
            input[j++]=input[i];
        } else if (o_stack_check(input[i],pol.data[pol.cur_data]) || pol.cur_data == -1){
            in_stack(input[i],&pol);
        } else {
            t=out_stack(&pol);
            input[j++]=t;
            in_stack(input[i],&pol);
        }
    }
    while(pol.cur_data>=0){
        input[j++]=out_stack(&pol);
    }
}
//计算逆波兰表达式
int cal_poland(int *input){
    int i;
    int big,small,cal;
    struct stack temp;
    init_stack(&temp);
    for (i=0;input[i]!=end_array;i++){
        if (input[i]>=0){
            in_stack(input[i],&temp);
        } else {
            small = out_stack(&temp);
            big = out_stack(&temp);
            cal = input[i];
            switch((int)cal){
                case -1:    in_stack(big-small,&temp);break;
                case -2:    in_stack(big+small,&temp);break;
                case -3:    in_stack(big*small,&temp);break;
                case -4:    in_stack(big/small,&temp);break;
                default: break;
            }
        }
    }
    return out_stack(&temp);
}
//判断字符是否为'0'~'9'
int is_num(char c){
    if (c>='0' && c<='9')
        return 1;
    else 
        return 0;
}
//字符转整型
int c_to_n(char c){
    int i;
    i = c-48;
    return i;
}
//运算符转某个整数(为了计算方便).
int oper_to_n(char c){
    if (c=='-')    return -1;
    else if (c=='+')    return -2;
    else if (c=='*')    return -3;
    else return -4;
}
//字符数组转成相应的整数数组便于后面的计算
void ca_to_ia(char *s,int *n){
    int i=0;
    int j=0;
    while(s[i] != 'a'){
        if (is_num(s[i])){
            n[j++]=c_to_n(s[i]);    
        } else {
            n[j++]=oper_to_n(s[i]);
        }
        i++;
    }
    n[j]=end_array;
}
int main(){
    char input_s[stack_size];
    char c;
    int input_n[stack_size];
    int i=0;
    struct stack pol;
    init_stack(&pol);
    printf("Enter the bds with the end of a:\n");
    scanf("%c",&c);
    while(c!='a' && i<stack_size){
        input_s[i++]=c;
        scanf("%c",&c);
    }
    input_s[i]='a';
    ca_to_ia(input_s,input_n);
    con_poland(input_n);
    printf("%d",cal_poland(input_n));
}

 

其实就是一个简单的计算器,不过由于数都是只有一位,而且没有考虑的正负号,所以题目难度骤降.就是一个逆波兰即可!上面是自己随便写了一个没有优化,命名什么的都怪乱的.

自己遇到的困难:

1.很长时间没写代码,结构体的传值方式竟然不知道咋搞了。。。

2.计算的时候如果是字符的话相加相减,特别是相乘相除不是很方便,所以就想到了转成整数数组进行计算,但感觉这个方法还是笨了点,肯定有更好的方法.

3.字符与整型之间的转化类似问题不熟。

4.逆波兰表达式的转化与计算方法可参见 http://zh.wikipedia.org/wiki/%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E7%A4%BA%E6%B3%95

 

有时间再修改优化.代码写的很糟糕。很多的不足,希望前辈们能帮忙指出自己的不足之处也让自己得以改正。

转载于:https://www.cnblogs.com/kaikai7/p/3306642.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值