表达式求值

        看了懒猫老师的表达式求值的视频后,自己写了份简易版的C语言版表达式求值,不是最好的算法,以后多多进步,多多改进!https://www.bilibili.com/medialist/play/26340287?from=space&business=space&sort_field=pubtime&spm_id_from=333.999.0.0

#include <stdio.h>
#include <stdlib.h>
#include "biaodashi.cpp"
//主函数
int main(int argc, const char * argv[]) {
    Stack OPTR;
    Stack_num OPND;
    char ch,x,theta;
    double a,b;
    Initstack(OPTR);
    Initstack_num(OPND);
    push(OPTR, '#');
//    printf("%c",precede('*', '('));
    scanf("%c",&ch);
    //ch = getchar();
    do{
        if (isOptr(ch)){
            switch (precede(top(OPTR), ch)) {
                case '<':
                    push(OPTR, ch);
                    scanf("%c",&ch);
                    break;
                case '=':
                    if(ch  == ')' ) x = pop_sym(OPTR);
                    scanf("%c",&ch);
                    break;
                case '>':
                    theta = pop_sym(OPTR);
                    b = pop_num(OPND);
                    a = pop_num(OPND);
                    push_num(OPND, operate(a, theta, b));
                    printf("OPERATE:  %.2f\n",top_num(OPND));
                    break;
            }
        }
        else{
            ch = (ch - '0')*1.0;
            push_num(OPND, ch);
            printf("push is : %.2f\n",top_num(OPND));
            scanf("%c",&ch);
        }
    }while ((ch != '#') || (top(OPTR) != '#'));
    
    printf("the result: %.2f\n",top_num(OPND));
    Destroy(OPTR);
    Destroy(OPND);
    return 0;
}



.c文件

#include "biaodashi.hpp"
#include <stdlib.h>
#include <stdio.h>


//判断是否是数字
bool isOpnd(char ch){
    if (ch >= '0' && ch <= '9') {
        return true;
    }
    else return false;
}
//判断是否是字符
bool isOptr(char ch){
    int i;
    for(i = 0;i < M;i++) {
        if (ch == CalPriority[i][0]) return true;
    }
    return false;
}

void Initstack(Stack &s){
    s.elemtype = (char*)malloc(MaxSize*sizeof(char));
    s.len = -1;
}
void Initstack_num(Stack_num &s){
    s.elemtype = (double*)malloc(MaxSize*sizeof(double));
    s.len = -1;
}

char precede(char a,char b){
    int i,j;
    //寻找角2
    for (i = 0; i < N ; i++) {
        if (CalPriority[0][i] == b) break;
    }
    //寻找角1
    for (j = 0; j < M ; j++) {
        if (CalPriority[j][0] == a) break;
    }
    return CalPriority[j][i];
}


bool isfull(Stack s){
    if(s.len == MaxSize) return true;
    else return false;
}
bool isempty(Stack s){
    if (s.len == -1) return true;
    else return false;
}
bool isfull_num(Stack_num s){
    if (s.len == MaxSize) return true;
    else return false;
}
bool isempty_num(Stack_num s){
    if(s.len == -1) return true;
    else return false;
}

char top(Stack s){
    if(isempty(s)) return  false;
    else return s.elemtype[s.len];
}
double top_num(Stack_num s){
    if(isempty_num(s)) return false;
    else return s.elemtype[s.len];
}

char pop_sym(Stack &s){
    if(isempty(s)) return false;
    else{
        return s.elemtype[s.len--];
    }
}
double pop_num(Stack_num &s){
    if (isempty_num(s)) return false;
    else return s.elemtype[s.len--];
}

bool push_num(Stack_num &s,double ch){
    if(isfull_num(s)) return false;
    else{
        s.elemtype[++s.len] = ch;
        return true;
    }
}
bool push(Stack &s,char ch){
    if(isfull(s)) return false;
    else{
        s.elemtype[++s.len] = ch;
        return true;
    }
}

double operate(double a, char theta ,double b){
    double result;
    switch (theta) {
        case '+':
            result = a + b;
            break;
        case '-':
            result = a - b;
            break;
        case '*':
            result = a * b;
            break;
        case '/':
            result = a / b;
            break;
    }
    return result;
}

void Destroy(Stack &s){
    free(s.elemtype);
}
void Destroy(STACK_num &s){
    free(s.elemtype);
}


.h文件

#ifndef biaodashi_hpp
#define biaodashi_hpp
#include <stdio.h>
#include <stdbool.h>

#define M 8
#define N 8
#define MaxSize 20
typedef struct STACK_ch{
    char *elemtype;
    int len;
}Stack;

typedef struct STACK_num{
    double *elemtype;
    int len;
}Stack_num;


char  CalPriority[M][N]=
{     0 ,'+','-','*','/','(',')','#',
     '+','>','>','<','<','<','>','>',
     '-','>','>','<','<','<','>','>',
     '*','>','>','>','>','<','>','>',
     '/','>','>','>','>','<','>','>',
     '(','<','<','<','<','<','=', 0 ,
     ')','>','>','>','>', 0 ,'>','>',
     '#','<','<','<','<','<', 0 ,'='
};


//辅助函数
char precede(char a,char b);
char top(Stack s);
double top_num(Stack_num s);
bool isOpnd(char ch);       //判断是否是数字
bool isOptr(char ch);       //判断是否是字符
double operate(double a ,char theta ,double b);


//栈函数
void Initstack(Stack &s);
void Initstack_num(Stack_num &s);
bool isfull(Stack s);
bool isempty(Stack s);
bool isfull_num(Stack_num s);
bool isempty_num(Stack_num s);


char pop_sym(Stack &s);
double pop_num(Stack_num &s);

bool push(Stack &s,char ch);
bool push_num(Stack_num &s,double ch);
void Destroy(Stack &s);
void Destroy(STACK_num &s);



#endif /* biaodashi_hpp */

因为我用的Xcode,.h文件为.hpp,  .m文件变成了.cpp文件,所以小伙伴们记得改一下,代码理解不难,感谢懒猫老师!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值