| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 841 人关注过本帖
标题:算术表达试求值的程序,不知道哪里错了
只看楼主 加入收藏
bigzerg
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2008-5-19
收藏
 问题点数:0 回复次数:0 
算术表达试求值的程序,不知道哪里错了
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

typedef struct
{
  char *base;
  char *top;
  int stacksize;
      
}CharStack;

typedef struct
{
  int *base;
  int *top;
  int stacksize;
      
}IntStack;

void InitCharStack(CharStack *s)//构造符号栈
{
  s->base=(char*)malloc(100*sizeof(char));
  s->top=s->base;
  s->stacksize=100;
}

void InitIntStack(IntStack *s)//构造数字栈
{
  s->base=(int*)malloc(100*sizeof(int));
  s->top=s->base;
  s->stacksize=100;
}

char GetTopChar(CharStack s,char e)//取符号栈顶元素
{
  if(s.top!=s.base)
  {
    e=*(s.top-1);
    return e;
  }
  return '0';
   
}

int GetTopInt(IntStack s,int e)//取栈顶元素
{
  if(s.top!=s.base)
  {
    e=*(s.top-1);
    return e;
  }
  return 0;
   
}

PushChar(CharStack *s,char e)//添加符号元素
{
  if(s->top-s->base>=s->stacksize)
  {
    int a;
    a=s->stacksize;
    s->base=(char*)malloc((a+10)*sizeof(char));
    s->top=s->base+a;
    s->stacksize=a+10;
    *s->top++=e;
  }
  else
  {
    *s->top++=e;
  }
  
}

PushInt(IntStack *s,int e)//添加数字元素
{
  if(s->top-s->base>=s->stacksize)
  {
    int a;
    a=s->stacksize;
    s->base=(int*)malloc((a+10)*sizeof(int));
    s->top=s->base+a;
    s->stacksize=a+10;
    *s->top++=e;
  }
  else
  {
    *s->top++=e;
  }
  
}

void PopChar(CharStack *s,char e)//删除符号栈顶元素
{
  if(s->top==s->base)
  {
      return;
  }
  s->top--;
  e=*s->top;
}

void PopInt(IntStack *s,int e)//删除数字栈顶元素
{
  if(s->top==s->base)
  {
      return;
  }
  s->top--;
  e=*s->top;
}

int IsChar(char c)//判断是否为运算符
{
    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')')
       return 1;
   else
       return 0;

}


int detect(char temp)//搜索矩阵位置
{
    int i=0;
    char oper[7]={'+','-','*','/','(',')','#'};
    for(i=0;i<7;i++)
    {
        if(temp==oper[i])
        {
            return i;
        }
    }
    return -1;
}


char Priority(char temp,char optr)//判断优先级
{
    /**//*
            +  -  *  /  (  )  #
            1  2  3  4  5  6  7
        + 1 <  <  <  <  >  >  >   
        - 2 <  <  <  <  >  >  >
        * 3 >  >  <  <  >  >  >
        / 4 >  >  <  <  >  >  >
        ( 5 >  >  >  >  >  =  0  
        ) 6 <  <  <  <  =  0  >
        # 7 <  <  <  <  >  0  =
    */
    int row ,col;
    char priority[7][7]={/**//*      +      -      *   /      (   )   #                */        
                             {'<','<','<','<','>','>','>'},

                            {'<','<','<','<','>','>','>'},

                            {'>','>','<','<','>','>','>'},

                            {'>','>','<','<','>','>','>'},

                            {'>','>','>','>','>','=','0'},

                            {'<','<','<','<','=','0','>'},

                            {'<','<','<','<','>','0','='},                           
                        };


    row = detect(temp);//找出对应的矩阵下标;
    col = detect(optr);   
//    printf("%d,%d",row,col);
      
    //优先级存储在一个7x7的矩阵中,对应关系上图;
   
    return priority[row][col];

}


int Evaluate(int a,int b,char oper)
{
    int c;
    switch(oper)
    {
        case '+':  c=a+b;break;
        case '-':  c=a-b;break;
        case '*':  c=a*b;break;
        case '/':  c=a/b;break;
        default : exit(-1);break;
    }
    return c;
}

int EvaluateExpression()
{
    int inputInt,topInt;
    char inputChar,topChar,str,x;//str='<' or'>' or'='
    CharStack OPTR;
    IntStack OPND;
    int a,b;
    char theta;
    topChar=' ';
    str='0';//initiate str
    InitCharStack(&OPTR);
    PushChar(&OPTR,'#');
    InitIntStack(&OPND);
   
    scanf("%d",&inputInt);
    PushInt(&OPND,inputInt);
    inputChar=getchar();
    while(inputChar!='#'||GetTopChar(OPTR,topChar)!='#')
    {
    //    if(inputChar=='('||inputChar==')')
    //    {
    //        PushChar(&OPTR,inputChar);
    //        scanf("%d",inputInt);
    //        PushInt(&OPND,inputInt);
    //        inputChar=getchar();
    //    }
         if(inputChar=='+'||inputChar=='-'||inputChar=='*'||inputChar=='/')
        {
          str=Priority(GetTopChar(OPTR,topChar),inputChar);
          switch(str)
          {
          case '<':
                PushChar(&OPTR,inputChar);
                scanf("%d",inputInt);
                PushInt(&OPND,inputInt);
                inputChar=getchar();
                break;
          case '=':
              PopChar(&OPTR,x);
              inputChar=getchar();
              break;
          case '>':
              PopChar(&OPTR,theta);
              PopInt(&OPND,b);
              PopInt(&OPND,a);
              PushInt(&OPND,Evaluate(a,b,GetTopChar(OPTR,topChar)));
              break;
          }//switch
        }//else if
        else
        {
           exit(-1);
        }
        
    }//while
    return GetTopInt(OPND,topInt);

}

main()
{
   int result=0;
   printf("请输入运算式\n");
   result=EvaluateExpression();

   printf("%d",result);

}
搜索更多相关主题的帖子: 算术 求值 
2008-10-24 08:56
快速回复:算术表达试求值的程序,不知道哪里错了
数据加载中...
 
   



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

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