求后缀表达式(可用于浮点型)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stack_num
{
  double num;
  struct stack_num *next;
}stack_num_node,*stack_num_link;
typedef struct stack_char
{
  char ch;
  struct stack_char *next;
}stack_char_node,*stack_char_link;

int IsEmptyStackNum(stack_num_link s);
int IsEmptyStackChar(stack_char_link s);
void PushNum(stack_num_link *ss,double e);
void PushChar(stack_char_link *ss,char e);
void PopNum(stack_num_link *ss,double *e);
void PopChar(stack_char_link *ss,char *e);
char GetTopChar(stack_char_link s);
int IsNum(char e);
int ComparePriority(char e1,char e2);
void ChangeChar(char *p);
double computer(double e1,double e2,char ch);
char input(char *p);
double ChangeDouble(char *p);
void InitStackChar(stack_char_link *ss);
void InitStackNum(stack_num_link  *ss);
void PrintChar(char *p,stack_num_link s);

void main()
{
   char str[256],*p;
   system("cls");
   p=str;
   input(p);
   ChangeChar(str);
   getch();

}
void ChangeChar(char *p)
{
  char current_word,stack_word,previous_word;
  char num_word[21],change_str[256];
  int i=0,j=0;
  stack_num_link num_top;
  stack_char_link char_top;

  InitStackNum(&num_top);
  InitStackChar(&char_top);
  previous_word=current_word=*p;
  while(*p)
  {
      previous_word=current_word;
      current_word=*p++;
      if(IsNum(current_word))
      {
   num_word[i++]=current_word;
      }/* end if crrent_word is a number*/
      else if((current_word=='-'||current_word=='+')&&previous_word=='(')
      {
   num_word[i++]=current_word;
      }
      else if(current_word=='(')
      {
   if(i>0)
   {
       num_word[i]='/0';
       PushNum(&num_top,ChangeDouble(num_word));
       i=0;
       change_str[j++]='A';
   }
   PushChar(&char_top,current_word);
      } /*end if current_word is ( */
      else if(current_word==')')
      {
   if(i>0)
   {
       num_word[i]='/0';
       PushNum(&num_top,ChangeDouble(num_word));
       i=0;
       change_str[j++]='A';
   }
   do
   {
        PopChar(&char_top,&stack_word);
        if(stack_word!='(') change_str[j++]=stack_word;
   }while(stack_word!='('&&!IsEmptyStackChar(char_top));
      }/* end else if current=) */
      else
      {
   if(i>0)
   {
       num_word[i]='/0';
       PushNum(&num_top,ChangeDouble(num_word));
       i=0;
       change_str[j++]='A';
   }/* end if i>0 */

   stack_word=GetTopChar(char_top);
   if(stack_word=='(')
   {
       PushChar(&char_top,current_word);
   }
   else
   {
       if(ComparePriority(stack_word,current_word)==1)
       {
    PushChar(&char_top,current_word);
       }
       else
       {

    while(ComparePriority(stack_word,current_word)<=0&&stack_word!='(')
    {
        PopChar(&char_top,&stack_word);
        change_str[j++]=stack_word;
        stack_word=GetTopChar(char_top);
    }
    PushChar(&char_top,current_word);
       }
   }/* end if top_word!=( */
      }/* end if current_word==+*-/) */
  }/* end while */
  change_str[j]='/0';
  PrintChar(change_str,num_top);
  printf("/n/n%s/n/n",change_str);
}

void PrintChar(char *p,stack_num_link s)
{
   stack_num_link top1,q,top2;
   double e,e1,e2,result;
   int i;
   InitStackNum(&top1);
   InitStackNum(&top2);
   while(!IsEmptyStackNum(s))
   {
       q=(stack_num_link)malloc(sizeof(stack_num_node));
       PopNum(&s,&e);
       q->num=e;
       q->next=top1;
       top1=q;
   }
   printf("/n/n/n");
   for(;*p;p++)
   {
      if(*p=='A')
      {

   PopNum(&top1,&e);
   PushNum(&top2,e);
   if(e<0)
   {
       printf("(%lf) ",e);
   }
   else
   {
      printf("%lf ",e);
   }
      }
      else
      {
   PopNum(&top2,&e2);
   PopNum(&top2,&e1);
   PushNum(&top2,computer(e1,e2,*p));
   printf("%c ",*p);
      }
   }
   PopNum(&top2,&result);
   printf("/nThe result is %lf",result);
}

double computer(double e1,double e2,char ch)
{
   double result;
   switch(ch)
   {
       case '+':
    result=e1+e2;
    break;
       case '-':
    result=e1-e2;
    break;
       case '*':
    result=e1*e2;
    break;
       case '/':
    result=e1/e2;
    break;
   }
   return result;
}
double ChangeDouble(char *p)
{
  char *q;
  int flag=0,i;
  double num=0,j=1.0;
  q=p;
  if(*q=='-')
  {
       flag=1;
       q++;
  }
  else if(*q=='+')
  {
       q++;
  }
  while((*q)&&(*q!='.'))
  {
      num=num*10+(*q)-'0';
      q++;
  }
  if(*q=='.')
  {
      q++;
      while((*q)&&(*q)<='9'&&(*q)>='0')
      {
  j=j*0.1;
  i=(*q)-'0';
  num=num+i*j;
  q++;
     }
  }
  if(flag)
  {
      return -num;
  }
  return num;
}
int ComparePriority(char e1,char e2)
{
  int i,m,n;
  int a[4][4]={{0,0,1,1},
        {0,0,1,1},
        {-1,-1,0,0},
        {-1,-1,0,0}};
  char s[4]="+-*/";
  for(i=0;i<4;i++)
  {
      if(s[i]==e1)m=i;
      if(s[i]==e2)n=i;
  }
  return a[m][n];
}

char input(char *p)
{
   *p++='(';
   do
   {
       printf("Input the statement,the length of the statement should less then 250/n/n");
       scanf("%s",p);
   }while(strlen(p)>250);
   strcat(p,")");
}

int IsNum(char e)
{
   if((e<='9'&&e>='0')||(e=='.'))
   {
      return 1;
   }
   return 0;
}
char GetTopChar(stack_char_link s)
{
  return s->ch;
}
void PopChar(stack_char_link *ss,char *e)
{
  stack_char_link p;
  p=*ss;
  if(IsEmptyStackChar(p))
  {
      printf("The char_stack is empty");
      getch();
  }
  else
  {
      *ss=p->next;
      *e=p->ch;
      free(p);
  }
}
void PopNum(stack_num_link *ss,double *e)
{
   stack_num_link p;
   p=*ss;
   if(IsEmptyStackNum(p))
   {
       printf("The num_stack is empty");
       getch();
   }
   else
   {
       *ss=p->next;
       *e=p->num;
       free(p);
   }
}

void PushNum(stack_num_link *ss,double e)
{
  stack_num_link p;
  p=(stack_num_link)malloc(sizeof(stack_num_node));
  if(p)
  {
      p->num=e;
      p->next=*ss;
      *ss=p;
  }
  else
  {
      printf("Num_Memory is overfollow");
      getch();
  }
}

void PushChar(stack_char_link *ss,char e)
{
  stack_char_link p;
  p=(stack_char_link)malloc(sizeof(stack_char_node));
  if(p)
  {
      p->ch=e;
      p->next=*ss;
      *ss=p;
  }
  else
  {
      printf("Char_Memory  is overfollow!");
      getch();
  }
}

int IsEmptyStackChar(stack_char_link s)
{
  return s==NULL;
}

int IsEmptyStackNum(stack_num_link s)
{
  return s==NULL;
}

void InitStackChar(stack_char_link *ss)
{
  *ss=NULL;
}

void InitStackNum(stack_num_link *ss)
{
  *ss=NULL;
}
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值