• 数据结构-算术表达式求值


    #include<stdio.h>
    #define ERROR 0
    #define OK 1
    #define STACKINCREAMENT 10
    #define STACK_INT_SIZE 100
    #define OVERFLOW -2
    typedef int status;
    typedef char SElemtype;
    #include<malloc.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<windows.h>

    typedef struct{
    SElemtype *base;
    SElemtype *top;
    status stacksize;
    }sqstack;

    typedef struct{
    int *base;
    int *top;
    status stacksize;
    }whstack;

    void init(sqstack *s){
    s->base=(SElemtype*)malloc(STACK_INT_SIZE*sizeof(SElemtype));
    if(!s->base) exit(OVERFLOW);
    s->top=s->base;
    s->stacksize=STACK_INT_SIZE;
    }

    void Seinit(whstack *s){
    s->base=(int*)malloc(STACK_INT_SIZE*sizeof(int));
    if(!s->base) exit(OVERFLOW);
    s->top=s->base;
    s->stacksize=STACK_INT_SIZE;
    }
    char gettop(sqstack *s,SElemtype *e){
    if(s->top==s->base)
    return ERROR;
    e=s->top-1;
    return *e;
    }

    status Segettop(whstack *s,int *e){
    if(s->top==s->base)
    return ERROR;
    e=s->top-1;
    return *e;
    }
    void push(sqstack *s,SElemtype e){

    if(s->top-s->base>=s->stacksize){
    s->base = (SElemtype*)realloc(s->base,(s->stacksize+STACKINCREAMENT)*sizeof(SElemtype));
    if(!s->base) exit(OVERFLOW);
    s->top=s->base+s->stacksize;
    s->stacksize+=STACKINCREAMENT;}
    *s->top++=e;
    }

    void Sepush(whstack *s,SElemtype e){

    if(s->top-s->base>=s->stacksize){
    s->base = (int*)realloc(s->base,(s->stacksize+STACKINCREAMENT)*sizeof(int));
    if(!s->base) exit(OVERFLOW);
    s->top=s->base+s->stacksize;
    s->stacksize+=STACKINCREAMENT;}
    *s->top++=e;
    }

    status pop(sqstack *s,SElemtype *e){
    if(s->top==s->base) return ERROR;
    *e=*--s->top;
    return OK;




    }
    status Sepop(whstack *s,int *e){
    if(s->top==s->base)
    return ERROR;
    *e=*--s->top;
    return OK;


    }

    int char_to_num(char s){
    if(s>=49&&s<=57){
    s=s-48;
    return s;


    }
    else
    return 0;

    }

    char precede(char a,char b){
    int i,j;
    char compare[8][8]={' ','+','-','*','/','(',')','#',
    '+','>','>','<','<','<','>','>',
    '-','>','>','<','<','<','>','>',
    '*','>','>','>','>','<','>','>',
    '/','>','>','>','>','<','>','>',
    '(','<','<','<','<','<','>',' ',
    ')','>','>','>','>',' ','>','>',
    '#','<','<','<','<','<',' ','=',
    };

    for(i=0;i<8;i++)
    if(compare[0][i]==a) break;
    for(j=0;j<8;j++)
    if(compare[j][0]==b) break;

    return compare[j][i];



    }
    int operate(int a,char x,int c){
    int y;
    switch(x)
    {
    case '+':y=a+c;break;
    case '-':y=a-c;break;

    case '*':y=a*c;break;
    case '/':y=a/c;break;

    default:return 0;break;
    }

    return y;

    }

    int evaluateexpression(){


    char c[100];
    sqstack OPTR;
    whstack OPND;
    int a,b,d,i=0;
    char x,theta,ch,y;
    init(&OPTR);

    push(&OPTR,'#');
    Seinit(&OPND);



    printf("请输入表达式(以#结尾): ");
    scanf("%s",c);



    while(1){



    if(char_to_num(c[i]))
    {


    Sepush(&OPND,char_to_num(c[i++]));


    }else if(c[i]=='+'||c[i]=='-'||c[i]=='*'||c[i]=='/'||c[i]=='('||c[i]==')'||c[i]=='#')
    {



    switch(precede(c[i],gettop(&OPTR,&ch)))

    {


    case '<':push(&OPTR,c[i++]);break;

    case '=':pop(&OPTR,&x);break;
    case '>':pop(&OPTR,&theta);
    Sepop(&OPND,&b);
    Sepop(&OPND,&a);
    Sepush(&OPND,operate(a,theta,b)); break;

    default:break;
    }


    }

    if(c[i]=='#'&&gettop(&OPTR,&y)=='#'){



    printf("结果=%d ",Segettop(&OPND,&d));
    return OK;

    }


    }
    }

    int main(){


    printf(" 算术表达式求值 ");

    evaluateexpression();
    getch();


    }

  • 相关阅读:
    7.3---直线是否相交(CC150)
    7.2---蚂蚁相遇问题(CC150)
    5.6---交换整数的奇数位和偶数位(CC150)
    5.5---整数A转成整数B(CC150)
    5.2---小数的二进制表示(CC150)
    5.1---二进制数插入(CC150)
    4.6---找二叉树中序后继(CC150)
    4.5---判断是否是二叉排序树BST(CC150)
    4.4---建立二叉树的链表
    linux中查看java进程
  • 原文地址:https://www.cnblogs.com/xufeng123/p/7822602.html
Copyright © 2020-2023  润新知