| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 483 人关注过本帖
标题:利用栈表达式求值没有结果?
只看楼主 加入收藏
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
结帖率:8.33%
收藏
 问题点数:0 回复次数:2 
利用栈表达式求值没有结果?
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define int_size 100
#define incre_size 10
#define MAX 25
typedef int Status;
typedef char SElemType;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;

}SqStack;
//定义全局变量

Status InitStack(SqStack *S)
{
    S->base=(SElemType *)malloc(sizeof(SElemType));
    if(!S->base)
        exit(ERROR);
    S->top=S->base;
    S->stacksize=int_size;
    return OK;
}
Status Push(SqStack *S,SElemType e)
{
    if(S->top-S->base>=int_size)
    {
         S->base=(SElemType *)realloc(S->base,(S->stacksize+incre_size)*sizeof(SElemType));
         if(!S->base)
             exit(ERROR);
         S->top=S->base+S->stacksize;
         S->stacksize+=incre_size;
    }
    *S->top++=e;
    return OK;
}
/*void print(SElemType c)
{
   printf("%c",c);
}
void StackTravese(SqStack *S,void (*SElemType)())
{
     SElemType *p=S->base;
     while(S->top>p)
         visit(*p++);
     printf("\n");
}*/
Status Pop(SqStack *S,SElemType &e)
{
     if(S->top==S->base)
         exit(ERROR);
     e=*--S->top;
     return OK;
}
void ClearStack(SqStack *S)
{
    S->top=S->base;
}
void DestroyStack(SqStack *S)
{
    free(S->base);
    S->top=S->base=NULL;
    S->stacksize=0;
}
Status StackEmpty(SqStack *S)
{
     if(S->top==S->base)
         return OK;
     else
         return ERROR;
}
Status GetTop(SqStack *S,SElemType &e)
{
     if(S->top>S->base)
     {
         e=*--S->top;
         return OK;
     }
     else
         return ERROR;
}
SElemType Precede(SElemType c1,SElemType c2)
{
int i=0,j=0;
static char array[49]={
'>', '>', '<', '<', '<', '>', '>',
'>', '>', '<', '<', '<', '>', '>',
'>', '>', '>', '>', '<', '>', '>',
'>', '>', '>', '>', '<', '>', '>',
'<', '<', '<', '<', '<', '=', '!',
'>', '>', '>', '>', '!', '>', '>',
'<', '<', '<', '<', '<', '!', '='};
switch(c1)
{
/* i为下面array的横标 */
case '+' : i=0;break;
case '-' : i=1;break;
case '*' : i=2;break;
case '/' : i=3;break;
case '(' : i=4;break;
case ')' : i=5;break;
case '#' : i=6;break;
}

switch(c2)
{
/* j为下面array的纵标 */
case '+' : j=0;break;
case '-' : j=1;break;
case '*' : j=2;break;
case '/' : j=3;break;
case '(' : j=4;break;
case ')' : j=5;break;
case '#' : j=6;break;
}
return (array[7*i+j]); /* 返回运算符 */
}
SElemType Operate(SElemType a,SElemType theta,SElemType b)
{
      switch(theta)
      {
          case '+':return a+b;
          case '-':return a-b;
          case '*':return a*b;
      }
      return a/b;
}
Status In(SElemType c)
{
     switch(c)
     {
         case '+':
         case '-':
         case '*':
         case '/':
         case '(':
         case ')':
         case '#': return OK;
         default: return ERROR;

     }
}
SElemType EvaluateExpression()
{
   SqStack OPTR,OPND;//OPTR为运算符,OPND为运算数
   SElemType a,b,c,x;
   InitStack(&OPTR);
   InitStack(&OPND);
   Push(&OPTR,'#');
   c=getchar();
   GetTop(&OPTR,x);
   while(c!='#' || x!='#')
   {
       if(In(c))
           switch(Precede(x,c))
           {
               case '<':Push(&OPTR,c);
                        c=getchar();
                        break;
               case '=':Pop(&OPTR,x);
                        c=getchar();
                        break;
               case '>':Pop(&OPTR,x);
                        Pop(&OPND,b);
                        Pop(&OPND,a);
                        Push(&OPND,Operate(a,x,b));
           }
       else if(c>='0' && c<='9')
       {
            Push(&OPND,c-48);
            c=getchar();
       }
       else
       {
           printf("出现非法字符\n");
           exit(ERROR);
       }
       GetTop(&OPTR,x);
   }
   Pop(&OPND,x);
   if(!StackEmpty(&OPND))
   {
       printf("表达式不正确\n");
       exit(ERROR);
   }
   return x;
}
int main()
{
    int rel;
    printf("请输入算术表达式(输入的值在0-9之间,中间运算值和输出的结果在-128-129之间:\n");
    rel=EvaluateExpression();
    printf("%d\n",rel);
    return 0;
}
搜索更多相关主题的帖子: include return 表达式 
2013-02-26 19:28
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:0 
好长,~楼主懂得调试不?你问题那步和你的不符合,调试下到那个位置看看咯

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-02-27 19:07
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
收藏
得分:0 
就是调不出来啊,郁闷啊
2013-02-28 09:40
快速回复:利用栈表达式求值没有结果?
数据加载中...
 
   



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

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