| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 579 人关注过本帖
标题:表达式求值
只看楼主 加入收藏
呆呆521
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2014-5-30
结帖率:0
收藏
 问题点数:0 回复次数:0 
表达式求值
此编程的操作数栈显示有问题,请大神帮忙改正




#include"malloc.h"
#include"stdio.h"
#include"ctype.h"   //判断是否为字符的函数 的头文件
#define MAXSIZE 100
typedef int elmtype;
typedef struct sqstack sqstack;//由于sqstack不是一个类型,struct sqstack才是
char ch[7]={'+','-','*','/','(',')','#'};
int f1[7]={3,3,5,5,1,6,0}; //栈内元素优先级                                       
int f2[7]={2,2,4,4,6,1,0};  //栈外元素优先级
int n=0;
struct sqstack  
 
{
     elmtype stack[MAXSIZE];
     int top;
};
 Initstack(sqstack *s)//置空栈
{
    s->top=-1;
}
 StackEmpty(sqstack S)//判断空栈
{
     if(S.top==-1)
        return 1;
     else
        return 0;
}
int Push(sqstack *s,elmtype x)//入栈
{
     if(s->top==MAXSIZE-1)
     {  
        printf("ERROR,overflow!\n");
        return 0;
     }
     else
     {
        s->top++;
        s->stack[s->top]=x;
        return 1;
     }
}
int Pop(sqstack *s,elmtype *x)//出栈
{
    if(s->top==-1)
    {
        printf("ERROR,underflow!\n");
        return 0;
    }
    else
    {
         *x=s->stack[s->top];
         s->top--;
         return 1;
    }
}
elmtype Gettop(sqstack s)//取栈顶元素
{
    if(s.top==-1)
    {
        printf("ERROR,underflow\n");
        return 0;
    }
    else
        return s.stack[s.top];
}
elmtype f(char c)
{
    switch(c)
    {
        case'+':
             return 0;
        case'-':
             return 1;
        case'*':
             return 2;
        case'/':
             return 3;
        case'(':
             return 4;
        case')':
             return 5;
        default:
             return 6;
    }
}
void visitoptr(sqstack c) //对栈进行遍历
{
      elmtype s;
      sqstack m;
      int i=0;
      Initstack(&m);
      if(StackEmpty(c))
      printf("0");
      while(!StackEmpty(c))
      {
           Pop(&c,&s);
           Push(&m,s);
      }
      while(!StackEmpty(m))
      {
           i++;
           Pop(&m,&s);
           Push(&c,s);
           printf("%c",ch[s]);
      }
      printf("\t\t");
}
void visitopnd(sqstack c)//对栈进行遍历
{
      elmtype s;
      sqstack m;
      int i=0;
      Initstack(&m);
      if(StackEmpty(c))
      printf("0");
      while(!StackEmpty(c))
      {
           Pop(&c,&s);
           Push(&m,s);
      }
      while(!StackEmpty(m))
      {
           i++;
           Pop(&m,&s);
           Push(&c,s);
           printf("%d",s);
      }
      printf("\t\t");   
}
void printstep()
{
    n++;
    printf("%d",n);
    printf("\t\t");
}   
char Compare(char c1,char c2)
{
     int i1=f(c1);
     int i2=f(c2);//把字符变为数字
     if(f1[i1]>f2[i2])
            return'>';
     else if(f1[i1]<f2[i2])
            return '<';
        else
            return '=';     
}
int Operate(elmtype a,elmtype t,elmtype b)
{
    int sum;
    switch(t)
    {
         case 0:
            sum=a+b;
            break;
         case 1:
            sum=a-b;
            break;
         case 2:
            sum=a*b;
            break;
         default:
            sum=a/b;            
    }
    return sum;
}
EvaluateExpression()
{
   char c;
   int i=0,sum=0;
   int k=1,j=1;//设置了开关变量
   elmtype x,t,a,b;
   sqstack OPTR,OPND;
   Initstack(&OPTR);
   Push(&OPTR,'#');//压入栈
   Initstack(&OPND);
   c=getchar();
   printf("*************表达式求值***************\n");
   printf("step");
   printf("\t\t");
   printf("OPTR");
   printf("\t\t");
   printf("OPND");
   printf("\t\t");
   printf("操作\n");
   printstep();
   printf(" #\n");
   /*while(c!='#'||ch[Gettop(OPTR)]!='#')
   {
      if(isdigit(c))
      {
          sum=0;
          while(isdigit(c))
          {
              if(!j)
              {
                  sum=sum*10-(c-'0');
              }
              else
                  sum=sum*10+(c-'0');
                  c=getchar();
          }
          Push(&OPND,sum);//如果还是数字先不压栈,转化为十进制在压栈
          j=1;
      }
      else
            if(k)
            {
                switch(Compare(ch[Gettop(OPTR)],c))
                {
                  case'<':Push(&OPTR,f(c));//把他们整形化
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("Push(&OPTR,%c)\n",c);
                    c=getchar();
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("getchar()=%c\n",c);
                    break;
                  case'=':Pop(&OPTR,&x);      
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("Pop(&OPND,%d)\n",x);
                    c=getchar();
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("getchar()=%c\n",c);
                  break;
                  case'>':Pop(&OPTR,&t);
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("Pop(&OPTR,%c)\n",ch[t]);
                    Pop(&OPND,&a);//注意这里谁先出栈
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("Pop(&OPND,%d)\n",a);
                    Push(&OPND,Operate(a,t,b));
                    printstep();
                    visitoptr(OPTR);
                    visitoptr(OPND);
                    printf("Push(&OPND,Operate(%d,%c,%d))\n",a,ch[t],b);
                  break;
                }
            }
   }
       return(Gettop(OPND));
}*/
main()
{
      int result;
      printf("************欢迎使用表达式求值************");
      printf("请输入你的表达式(以#结束):\n");
      result=EvaluateExpression();
}
搜索更多相关主题的帖子: include 表达式 优先级 元素 
2014-10-07 14:05
快速回复:表达式求值
数据加载中...
 
   



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

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