| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2671 人关注过本帖
标题:我这个中缀表达式转后缀表达式有问题,求修改,编程小白
只看楼主 加入收藏
夙愿000000
Rank: 1
来 自:甘肃张掖
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-4-10
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
我这个中缀表达式转后缀表达式有问题,求修改,编程小白
#include<stdio.h>
#include<iostream.h>
#define TRUE 1
#define FALSE 0
#define max 50
typedef struct
{
    char data[max];
    int top;
}SeqStack;
void chushihua(SeqStack *S)
{
    S->top=-1;
}
void compvalue(SeqStack *S,char exp[],char e[])
{
    int i=0,j=0,k=0;
    while(exp[i++]!='\n')
    {
        if(exp[i]>='0'&&exp[i]<='9'){
            j++;
            e[j]=exp[i];
        }
        if(exp[i]=='('){
            S->top++;
            S->data[S->top]=exp[i];
        }
        if(exp[i]==')')
        {
            while(S->data[S->top]!='('){
                j++;
                e[j]=S->data[S->top];
                S->top--;
            }
            S->top--;
        }
        if(exp[i]=='+'||exp[i]=='-')
        {
            if(S->data[S->top]=='+'||S->data[S->top]=='-'||S->data[S->top]=='*'||S->data[S->top]=='/')
            {
                j++;
                e[j]=S->data[S->top];S->top--;
                S->top++;S->data[S->top]=exp[i];
            }
            else{
                S->top++;
                S->data[S->top]=exp[i];
            }

        }
        if(exp[i]=='*'||exp[i]=='/')
        {
            if(S->data[S->top]=='*'||S->data[S->top]=='/')
            {
                j++;
                e[j]=S->data[S->top];S->top--;
                S->top++;S->data[S->top]=exp[i];
            }
            else
            {
                S->top++;
                S->data[S->top]=exp[i];
        }
    }
    }
        while(S->top!=-1)
        {
            e[j++]=S->data[S->top];
            S->top--;
        }
        while(e[k]!='\n')
        {
            cout<<e[k];
            k++;
        }
}
int main()
{
    SeqStack S;
    chushihua(&S);
    int i=0;
    char exp[20],e[20];
    cout<<"请输入中缀表达式:"<<endl;
    gets(exp);
    chushihua(&S);
    compvalue(&S,exp,e);
    return 0;
}
搜索更多相关主题的帖子: 表达式 
2016-07-05 15:20
word123
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:13
帖 子:333
专家分:1622
注 册:2014-4-5
收藏
得分:20 
兄弟,这个拿去看一下

#include<stdio.h>
#include<iostream.h>
#define max 20
typedef struct
{
    char data[max];
    int top;
}SeqStack;
void chushihua(SeqStack *S)
{
    S->top=-1;
}
void compvalue(SeqStack *S,char exp[],char e[])
{
    int i=0,j=0,k=0;
    while(exp[i]!='\0')//字符串的结束是\0
    {
        if(exp[i]>='0' && exp[i]<='9'){
            e[j]=exp[i];
            j++;
        }else if(exp[i]=='('){
            S->top++;
            S->data[S->top]=exp[i];
        }else if(exp[i]==')'){
            //输出一对括号内的所有运算符
            while(S->data[S->top]!='('){
                e[j]=S->data[S->top];
                S->top--;
                 j++;
            }
            S->top--;//删除栈顶(
        }else if(exp[i]=='+' || exp[i]=='-'){
            //先判断栈是否为空,再比较栈顶符号
            if(S->top!=-1){
                if(S->data[S->top]=='+' || S->data[S->top]=='-' || S->data[S->top]=='*' || S->data[S->top]=='/')
                {
                    e[j]=S->data[S->top];
                    S->top--;
                    S->top++;
                    S->data[S->top]=exp[i];
                    j++;
                }else{
                    //这种情况相当于栈顶是(
                    S->top++;
                    S->data[S->top]=exp[i];
                }
            }else{
                //栈空的处理情况
                S->top++;
                S->data[S->top]=exp[i];
            }
        }else if(exp[i]=='*' || exp[i]=='/'){
            if(S->data[S->top]=='*' || S->data[S->top]=='/')
            {
                //取出栈顶的乘除符号,再把当前乘除符号压入栈顶
                e[j]=S->data[S->top];
                S->top--;
                S->top++;
                S->data[S->top]=exp[i];
                j++;
            }
            else
            {
                //若栈顶是(或加减符号的话,暂时还不能出栈
                S->top++;
                S->data[S->top]=exp[i];
            }
        }
        i++;//指向存储的下一个字符
    }
        while(S->top!=-1)
        {
            e[j++]=S->data[S->top];
            S->top--;
        }
        e[j]='\0';//加上结束符,输出判断

        puts(e);//输出后缀表达式
}

int main()
{
    SeqStack S;
    chushihua(&S);
    int i=0;
    char exp[20],e[20];
    cout<<"请输入中缀表达式:"<<endl;
    gets(exp);
    compvalue(&S,exp,e);
    return 0;
}
2016-07-07 12:07
夙愿000000
Rank: 1
来 自:甘肃张掖
等 级:新手上路
帖 子:10
专家分:0
注 册:2016-4-10
收藏
得分:0 
回复 2楼 word123
谢谢了
2016-07-07 15:11
快速回复:我这个中缀表达式转后缀表达式有问题,求修改,编程小白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024941 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved