表达式求解

#include<string.h>
#include<stdio.h>
#include<malloc.h>

#define EOFILE '&'

typedef char SElemType;

#include "stack.h"
Status visit(SElemType * e)
{
  printf("%c", *e);
}

char OP[10]={'+','-','*','/','(',')','#'};
int precede[7][7]={
      1,1,2,2,2,1,1,
      1,1,2,2,2,1,1,
      1,1,1,1,2,1,1,
      1,1,1,1,2,1,1,
      2,2,2,2,2,3,0,
      1,1,1,1,0,1,1,
      2,2,2,2,2,0,3};

int In(char c,char *op)
{
  int i=0;
  while(i<7)
     if(c==op[i++])
       return 1;
  return 0;
}

char Precede(char op,char c)
{
  int pos_op;
  int pos_c;
  int i;
  for(i=0;i<7;i++)
    {
      if(op==OP[i]) pos_op=i;
      if(c==OP[i]) pos_c=i;
    }
  switch(precede[pos_op][pos_c])
    {
      case 1:  return '>';
      case 2:  return '<';
      case 3:  return '=';
    }
}

char Operate(int a,char theta,int b)
{
  switch(theta)
    {
      case '+':return a+b-'0';
      case '-':return a-b+'0';
      case '*':return (a-'0')*(b-'0')+'0';
      case '/':return (a-'0')/(b-'0')+'0';
    }
}

char EvaluateExpression()
{
  SqStack *OPND,*OPTR;
  char c,x,theta;
  char a,b;
  InitStack(&OPTR);  Push(OPTR,'#');
  InitStack(&OPND);
  c=getchar();
  while(c!='#'||GetTop(*OPTR)!='#')
    {
      if(!In(c,OP))
 {Push(OPND,c);c=getchar();}
      else
 switch(Precede(GetTop(*OPTR),c))
   {
     case '<':
       Push(OPTR,c);
       c=getchar();
       break;
     case '=':
       Pop(OPTR,&x);
       c=getchar();
       break;
     case '>':
       Pop(OPTR,&theta);
       Pop(OPND,&b);  Pop(OPND,&a);
       Push(OPND,Operate(a,theta,b));
       break;
   }
    }
  c=GetTop(*OPND);
  DestroyStack(OPTR);
  DestroyStack(OPND);
  return c;
}

main()
{
  char i;
  printf("/n/n/n/nOnly within 0..9 evaluation,input a expression end with symbol #:/n");
  i=EvaluateExpression();
  printf("/nThis expression's result is:   ");
  printf("%d/n/n/n/n",i-'0');
  printf("/n/nWelcome to visit http://zmofun.yeah.net !");
  getch();
}

 

stack.h

#ifndef _STACK_H_
#define _STACK_H_

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

struct STACK
{
  SElemType *base;
  SElemType *top;
  int stacksize;
};

typedef struct STACK SqStack;
typedef struct STACK *pSqstack;
typedef int Status;

Status InitStack(SqStack **S);
void DestroyStack(SqStack *S);
Status ClearStack(SqStack *S);
Status StackEmpty(SqStack S);
Status StackLength(SqStack S);
SElemType GetTop(SqStack S);
Status Push(SqStack *S,SElemType e);
Status Pop(SqStack *S,SElemType *e);
#endif

 

stack.c

#include "stack.h"
Status InitStack(SqStack **S)
{
  (*S)=(SqStack *)malloc(sizeof(SqStack));
  (*S)->base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
  if(!(*S)->base)exit(OVERFLOW);
  (*S)->top=(*S)->base;
  (*S)->stacksize=STACK_INIT_SIZE;
  return OK;
}

void DestroyStack(SqStack *S)
{
 free(S->base);
 free(S);
}

Status ClearStack(SqStack *S)
{
  S->top=S->base;
}

Status StackEmpty(SqStack S)
{
  if(S.top==S.base) return TRUE;
  else
    return FALSE;
}

Status StackLength(SqStack S)
{
  int i;
  SElemType *p;
  i=0;
  p=S.top;
  while(p!=S.base)
    {p++;
     i++;
    }
}

SElemType GetTop(SqStack S)
{
  char a;
  a='/0';        
  if(S.top==S.base) return ERROR;
  return *(S.top-1);
}

Status Push(SqStack *S,SElemType e)
{
 /*
  if(S->top - S->base>=S->stacksize)
   {

     S->base=(SElemType *) realloc(S->base,
     (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
     if(!S->base)exit(OVERFLOW);
     S->top=S->base+S->stacksize;
     S->stacksize += STACKINCREMENT;
   }
  */

  *(S->top++)=e;
  return OK;
}

Status Pop(SqStack *S,SElemType *e)
{
  if(S->top==S->base) return ERROR;
  *e=*--S->top;
  return OK;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值