ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据结构——简单表达式求值(C语言)

2021-10-02 19:29:55  阅读:314  来源: 互联网

标签:OPRDstack OPTRstack return C语言 else char next 求值 数据结构


#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    double data;
    struct node *next; 
}OPRD,*OPRDstack;//操作数栈
typedef struct NODE
{
    char data;
    struct NODE *next;
}OPTR,*OPTRstack;//操作符栈
OPRDstack Creat_OPRDstack();//建立操作数栈
int empty(OPRDstack H);//操作数栈判空
int push(OPRDstack H,double e);//入操作数栈
double pop(OPRDstack H,double *x);//出操作数栈
double gettop(OPRDstack H);//获得操作数栈顶的值
OPTRstack Creat_OPTRstack();//操作符栈
int Empty(OPTRstack h);//操作符栈判空
int Push(OPTRstack h,char c);//入操作符栈
char Pop(OPTRstack h,char *c);//出操作符栈
char Gettop(OPTRstack h);//获得操作符栈顶的值
int isOPRT(char c);//判断输入的字符是否为操作符
char compare(char a,char b);//比较操作符栈的栈顶字符与读取的下一个字符
double calculate(double left,double right,char operators);//操作数栈连续弹出两个数时将其计算


int main()
{
    OPRDstack H;
    H=Creat_OPRDstack();
    OPTRstack h;
    h=Creat_OPTRstack();
    char operators,x; 
    double left,right,data,value;
    Push(h,'#');
    printf("请输入运算表达式,以#结束\n"); 
    char ch;
    ch=getchar();
    while(ch!='#'||Gettop(h)!='#')
    {
        if(!isOPRT(ch))
        {
            data=ch-'0';
            ch=getchar();
            while(!isOPRT(ch))
            {
                data=data*10+ch-'0';
                ch=getchar();
            }//将字符转换为数字
            push(H,data);
        }
        else
        {
            switch(compare(Gettop(h),ch))
            {
                case '<':Push(h,ch);//如果栈顶的运算符优先级低,新运算符直接入栈
                ch=getchar();break;
                case '=':Pop(h,&x);//如果栈顶运算符的优先级等于读取的运算符优先级,则将栈顶运算符出栈
                ch=getchar();break;
                case '>'://如果栈顶的运算符优先级高,先出栈计算,新运算符再入栈
                operators=Pop(h,&operators);
                right=pop(H,&right);
                left=pop(H,&left);
                push(H,calculate(left,right,operators));break;
                case '!':printf("ERROR!");break;    
            }
        }
    }
    value=gettop(H);
    printf("%lf",value);
}
OPRDstack Creat_OPRDstack()
{
    OPRDstack H=(OPRDstack)malloc(sizeof(OPRD));
    H->next=NULL;
    return H;
}
int empty(OPRDstack H)
{
    if(H->next==NULL) return 1;
    else return 0;
}
int push(OPRDstack H,double e)
{
    OPRDstack p;
    p=(OPRDstack)malloc(sizeof(OPRD));
    p->data=e;
    p->next=H->next;
    H->next=p;
    return true;
}
double pop(OPRDstack H,double *x) 
{
    OPRDstack p;
    if(empty(H)) return 0;
    else{
        p=H->next;
        H->next=p->next;
        *x=p->data;
        free(p);
        return *x;
    }
}
double gettop(OPRDstack H)
{
    if(empty(H)) return 0;
    else{
        OPRDstack e=H->next;
        return e->data;
    }
}
OPTRstack Creat_OPTRstack()
{
    OPTRstack h=(OPTRstack)malloc(sizeof(OPTR));
    h->next=NULL;
    return h;
}
int Empty(OPTRstack h)
{
    if(h->next==NULL) return 1;
    else return 0;
}
int Push(OPTRstack h,char c)
{
    OPTRstack p;
    p=(OPTRstack)malloc(sizeof(OPTR));
    p->data=c;
    p->next=h->next;
    h->next=p;
    return true;
}
char Pop(OPTRstack h,char *c)
{
    OPTRstack p;
    if(Empty(h)) return 0;
    else{
        p=h->next;
        h->next=p->next;
        *c=p->data;
        free(p);
        return *c;
    }
}
char Gettop(OPTRstack h)
{
    if(Empty(h)) return 0;
    else{
        OPTRstack e=h->next;
        return e->data;
    }

int isOPRT(char c)
{
    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#') return 1;
    else return 0;
}
char compare(char a,char b)
{
    if(a=='+')
    {
        if(b=='+'||b=='-'||b==')'||b=='#') return '>';
        else return '<';
    }
    else if(a=='-')
    {
        if(b=='+'||b=='-'||b==')'||b=='#') return '>';
        else return '<';
    }
    else if(a=='*')
    {
        if(b=='(') return '<';
        else return '>';
    }
    else if(a=='/')
    {
        if(b=='(') return'<';
        else return '>';
    }
    else if(a=='(')
    {
        if(b==')') return '=';
        else if(b=='#') return '!';
        else return '<';
    }
    else if(a==')')
    {
        if(b=='(') return '!';
        else return '>';
    }
    else if(a=='#')
    {
        if(b=='#') return '=';
        else if(b==')') return '!';
        else return '<';
    }
}
double calculate(double left,double right,char operators)
{
    switch(operators)
    {
        case '+':
            return 1.0*left+right;
        case '-':
            return 1.0*left-right;
        case '*':
            return 1.0*left*right;
        case '/':
            return 1.0*left/right;    
            
    }
}

标签:OPRDstack,OPTRstack,return,C语言,else,char,next,求值,数据结构
来源: https://blog.csdn.net/qq_52639096/article/details/120588114

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有