栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2.0后缀表达式

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2.0后缀表达式

#include
#include

#include
#include
#define MaxSize 99
//输入的时候一定要输入英文,中文的符号可能算错
char calc[MaxSize],expr[MaxSize],repr[MaxSize],s1[MaxSize];//calc存储中缀表达式,expr为存放前缀表达式的字符串,repr为存放后缀表达式的字符串
int i,t;
struct {
    char data[MaxSize];
    int top;
} Sym,sta;//存符号
struct {
    double data[MaxSize];
    int top;
} Num;//存数值

void InitStack() {//初始化
    Sym.top=Num.top=sta.top=-1;
}
void SymPush() {
    if(Sym.top=0) {
        expr[++t]=Sym.data[Sym.top--];
    } else {
        printf("Sym栈空n");

    }
}
void NumPush() {
    if(Num.top=0) {

            switch(repr[i]) {
                case '+':
                        Num.data[Num.top-1]=Num.data[Num.top-1]+Num.data[Num.top];
                    break;
                case '-':
                        Num.data[Num.top-1]=Num.data[Num.top-1]-Num.data[Num.top];
                    break;
                case '*':
                        Num.data[Num.top-1]=Num.data[Num.top-1]*Num.data[Num.top];
                    break;
                case '/':
                        Num.data[Num.top-1]=Num.data[Num.top-1]/Num.data[Num.top];
                    break;

            }
            Num.top--;
        }
     else {
        printf("Num栈空n");

    }}

char staPop()
  {
     char ch;
   if(sta.top>=0) {
        ch=sta.data[sta.top];
    } else {
        printf("sta栈空n");
}
return ch;}
void staPush() {
    if(sta.top=0) {
        if(calc[i]>='0'&&calc[i]<='9') {
            while((i>=0)&&((calc[i]>='0'&&calc[i]<='9')||(calc[i]=='.'))) {//输入为小数用

                expr[++t]=calc[i--];
            }
            if((i>=0)&&(((i==0&&calc[i]!='(')||(calc[i]=='+'||calc[i]=='-'))&&!(calc[i-1]>='0'&&calc[i-1]<='9')&&calc[i-1]!=')'))
               do {

                expr[++t]=calc[i--];
            }while((i>=0)&&((calc[i]>='0'&&calc[i]<='9')||(calc[i]=='.')));
            expr[++t]=' ';
        } else if(calc[i]==')') {
            SymPush();
        } else if(calc[i]=='(') {
            while(Sym.data[Sym.top]!=')') {
                SymPop();
                expr[++t]=' ';
            }
            Sym.data[Sym.top--]='';
            i--;
        } else if(calc[i]=='+'||calc[i]=='-') {
            while(Sym.top>=0&&Sym.data[Sym.top]!=')'&&Sym.data[Sym.top]!='+'&&Sym.data[Sym.top]!='-') {
                SymPop();
                expr[++t]=' ';
            }
            SymPush();
        } else if(calc[i]=='*'||calc[i]=='/') {

            SymPush();
        } else {
            i--;
        }
    }
    while(Sym.top>=0) {
        SymPop();
        expr[++t]=' ';
    }
    expr[++t]=Sym.data[++Sym.top]='';

    for(i=0; i<=(t-2)/2; i++) {
        ch=expr[i];
        expr[i]=expr[(t-2)-i];
        expr[(t-2)-i]=ch;
    }
   printf("前缀表达式:%sn",expr);

}

void Reversepolish()
{
    char ch;
     int k;
    i=0,k=0,t=-1;
    while(calc[k]!='') {
        k++;
    }
     printf("后缀表达式:");
    while(i<=k){
            if(calc[i]>='0'&&calc[i]<='9') {
               while((i<=k)&&((calc[i]>='0'&&calc[i]<='9')||(calc[i]=='.'))) {//输入为小数用

                repr[++t]=calc[i];
                printf("%c",calc[i++]);
            }
            if((i<=k)&&(((i==0&&calc[i]!='(')||(calc[i]=='+'||calc[i]=='-'))&&!(calc[i-1]>='0'&&calc[i-1]<='9')&&calc[i-1]!=')'))
               do {

               repr[++t]=calc[i];
               printf("%c",calc[i++]);
            }while((i<=k)&&((calc[i]>='0'&&calc[i]<='9')||(calc[i]=='.')));
            printf(" ");//解决了两位数字输出中有空格的问题

            }
            else if(calc[i]=='(')
            {
               staPush();
               i++;

            }
            else if(calc[i]==')')
            {
                while(sta.data[sta.top]!='('&& sta.top>=0) {
                ch=staPop();
                printf("%c ",ch);
                 repr[++t]=ch;
                sta.top--;
                }
                sta.data[sta.top--]='';
                i++;

            }
            else if(calc[i]=='+'||calc[i]=='-')
            {
                while(sta.top>=0&&sta.data[sta.top]!='(') {
                ch=staPop();
                 printf("%c ",ch);
                  repr[++t]=ch;
                 sta.top--;
            }
            staPush();
                i++;

            }
            else if(calc[i]=='*'||calc[i]=='/')
            {

            staPush();
            i++;


        } else {
            i++;

        }

            }
            while(sta.top>=0)
            {
                ch=staPop();
                printf("%c ",ch);
                 repr[++t]=ch;
                sta.top--;
            }
            printf("n");

            for(i=0;i<=t;i++){
        if(repr[i]>='0'&&repr[i]<='9') {
            NumPush();
        } else {
            NumPop();
        }
 }



}

int main() {


    printf("请输入中缀表达式:n");
    InitStack(),gets(calc);
    Polish();
    InitStack();
    Reversepolish();
printf("运算结果为:%gn",Num.data[0]);
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/311560.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号