论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

一个将任意精度的中序表达式转化成后序表达式并求值的程序

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:45:26

    前些时间,偶写了个将中序表达式转化成后序表达式的程序,不过只能对单字符进行操作,现在改良了一下,能够对任意精度的数值进行运算,并且能够处理任意多的括弧(不过我并没有显式的答应输入[]或者{},只要将它们都成()就可以实现[]或者是{}的操作了),由于程序中间有小数点,所以在写成后序的时候,为了以便于区分输入的数据,我用下划线将它们隔离了开来。

   由于时间的关系,这个程序还有很多待改进的地方,例如:本程序不支持负数的运算,不能够报错,因此我正在努力的实现上述的功能,还在考虑它的容错功能,例如:计算3+5的时候,我们不小心多输入了几个+号,我们也能够得到我们所期望的数值8,此外也有考虑用二叉树实现此运算的功能等等,总之,诸多不善之处还望大家海涵!

-------偶的QQ:37170732 邮箱:owenstone@sina.com.cn 大家有什么意见不妨和我交流一下。

------------------------------------下面是程序代码部分-----------------------------------------

#include <stdio.h>
#include <conio.h>
#include <string.h>
#define NumberType double
#define NULL 0
#define DataType char
typedef struct Node *PNode;
typedef struct LinkList *PLinkList;
typedef struct LinkStack *PLinkStack;

struct Node
{
  DataType info;
  PNode link;
};
struct LinkStack
{ PNode base;
  PNode top;
};
struct LinkList
{ PNode base;
  PNode top;
};

PLinkList Create_LinkListPointer(void)
{PLinkList pllist;
 pllist=(PLinkList)malloc(sizeof(struct LinkList));
 if(pllist==NULL) pllist=(PLinkList)realloc(sizeof(struct LinkList));
 else pllist->top=pllist->base=NULL;
 return (pllist);
}

PLinkList Create_LinkList(PLinkList pllist)
{pllist->top=(PNode)malloc(sizeof(struct Node));
 if(pllist->top==NULL) pllist->top=(PNode)realloc(sizeof(struct Node));
 else {pllist->base=pllist->top;
  pllist->base->link=pllist->top->link=NULL;
 }
 return (pllist);
}

PLinkList Create_LinkListNode(PLinkList pllist)
{pllist->top->link=(PNode)malloc(sizeof(struct Node));
 if(pllist->top->link==NULL) pllist->top->link=(PNode)realloc(sizeof(struct Node));
 else{ pllist->top=pllist->top->link;
  pllist->top->link=NULL;
 }
 return(pllist);
}


PLinkStack Create_LinkStackPointer(void)
{PLinkStack plstack;
 plstack=(PLinkStack)malloc(sizeof(struct LinkStack));
 if(plstack==NULL) plstack=(PLinkStack)realloc(sizeof(struct LinkStack));
 else plstack->top=plstack->base=NULL;
 return (plstack);
}

PLinkStack Create_LinkStack(PLinkStack plstack)
{plstack->top=(PNode)malloc(sizeof(struct Node));
 if(plstack->top==NULL) plstack->top=(PNode)realloc(sizeof(struct Node));
 else {plstack->base=plstack->top;
  plstack->top->link=plstack->base->link=NULL;
 }
 return(plstack);
}

PLinkStack Create_LinkStackNode(PLinkStack plstack)
{plstack->top->link=(PNode)malloc(sizeof(struct Node));
 if(plstack->top->link==NULL) plstack->top->link=(PNode)malloc(sizeof(struct Node));
 else{plstack->top=plstack->top->link;
  plstack->top->link=NULL;
 }
 return(plstack);
}


PLinkStack push_stack(PLinkStack plstack,DataType i)
{plstack->top->link=(PNode)malloc(sizeof(struct Node));
 if(plstack->top->link==NULL) plstack->top->link=(PNode)realloc(sizeof(struct Node));
 else {plstack->top=plstack->top->link;
       plstack->top->link=NULL;
       plstack->top->info=i;
      }
 return (plstack);
}


PLinkStack pop_stack_all(PLinkList pllist,PLinkStack plstack)
{PNode p;
 while(plstack->top!=plstack->base){
  p=plstack->base;
  while(p->link!=plstack->top) p=p->link;
  Create_LinkListNode(pllist);
  pllist->top->info=plstack->top->info;
  plstack->top=plstack->top->link;
  plstack->top=p;
 }
 return (plstack);
}

PLinkList print_llist(PLinkList pllist)
{PNode temp;
 temp=pllist->base;
 while(temp!=pllist->top){
  printf("%c",temp->link->info);
  temp=temp->link;
 }
 printf("\n");
 return (pllist);
}

int bracket_included(PLinkStack plstack)
{PNode p,q;
 q=p=plstack->base;
 while(p!=plstack->top)
 {if( p->link->info=='(' || p->link->info==')') q=p->link;
  p=p->link;
 }
 if(q->info=='(' || q->info ==')') return (1);
 else return (0);
}

int islower_included(PLinkStack plstack)
{PNode p,q;
 q=p=plstack->base;
 while(p!=plstack->top){
  if(p->link->info=='+' || p->link->info=='-' ) q=p->link;
  p=p->link;
 }
 if(q!=plstack->base &&(q->info=='+' || q->info =='-')) return (1);
 else return (0);
}

int bracket_included_and_lower(PLinkStack plstack)
{PNode p,q;
 p=plstack->base ;
 while(p!=plstack->top){
  if(p->link->info==')' ) q=p->link;
  p=p->link;
 }
 p=q;
 while(p!=plstack->top){
  if(p->link->info=='+' || p->link->info=='-') q=p->link;
  p=p->link;
 }
 if(q->info=='+' || q->info=='-') return (1);
 else return (0);
}

PLinkList push_LinkList(PLinkList pllist,DataType i)
{
 Create_LinkListNode(pllist);
 pllist->top->info=i;
 return(pllist);
}

PLinkList push_underline(PLinkList pllist)
{Create_LinkListNode(pllist);
 pllist->top->info='_';
 return (pllist);
}

PLinkStack pop_stack_until_bracket(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
 p=plstack->base;
 while(p!=plstack->top){
  if(p->link->info=='(') q=p->link;
  p=p->link;
 }
 p=q;
 while(plstack->top!=p){
  q=plstack->base;
  while(q->link!=plstack->top) q=q->link;
  Create_LinkListNode(pllist);
  pllist->top->info=plstack->top->info;
  plstack->top=plstack->top->link;
  plstack->top=q;
 }
 return (plstack);
}

PLinkStack pop_until_lower(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
 p=plstack->base;
 while(p!=plstack->top){
  if(p->link->info=='+' || p->link->info=='-') q=p->link;
  p=p->link;
 }
 p=q;
 while(plstack->top!=p){
  q=plstack->base;
  while(q->link!=plstack->top) q=q->link;
  Create_LinkListNode(pllist);
  pllist->top->info=plstack->top->info;
  plstack->top=plstack->top->link;
  plstack->top=q;
 }
 return (plstack);
}
PLinkStack pop_stack_until_bracket_lower(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
 p=plstack->base;
 while(p->info!='(') p=p->link;
 q=p;
 while(plstack->top!=q){
 p=q;
 while(q->link!=plstack->top) q=q->link;
 Create_LinkListNode(pllist);
 pllist->top->info=plstack->top->info;
 plstack->top=plstack->top->link;
 plstack->top=q;
 }
 return (plstack);
}

PLinkStack pop_one(PLinkStack plstack)
{PNode p;
 p=plstack->base;
 while(p->link!=plstack->top) p=p->link;
 plstack->top=plstack->top->link;
 plstack->top=p;
 return (plstack);
}

PLinkStack pop_all(PLinkList pllist,PLinkStack plstack)
{PNode p;
 if(!isOperator(pllist->top->info)&&pllist->top->info!='_') push_underline(pllist);
 while(plstack->top!=plstack->base){
  p=plstack->base;
  while(p->link!=plstack->top) p=p->link;
  Create_LinkListNode(pllist);
  pllist->top->info=plstack->top->info;
  plstack->top=plstack->top->link;
  plstack->top=p;
 }
 return (plstack);
}
int isOperator(DataType i)
{
  if(i=='+' || i=='-' || i=='*' || i=='/' || i=='(' || i==')') return (1);
  else return (0);
}

int isPoint_included(PNode p)
{
 PNode q,temp;
 q=p;
 while(q->link->info!='_'){
  if(q->info=='.') temp=q;
  q=q->link;
 }
 if(temp->info=='.') return (1);
 else return (0);
}

int Calculator_underline(PNode p)
{
 PNode q;
 int i;
 i=0;
 q=p;
 while(!isOperator(q->link->info)){
  if(q->link->info=='_') i++;
  q=q->link;
 }
 return (i);
}

NumberType Calculator(PLinkList pllist)
{
 PNode p,q,temp,temp1,swap;
    int i,j;
 NumberType t,t1,t2,tmark1,tmark2,m;
 t=t1=t2=0;
 q=p=pllist->base;
 while(!isOperator(p->link->info)) p=p->link;/* find p */
 while(p!=pllist->top){
  /**************************************************************/
  /**************************************************************/
  if(!isOperator(p->info)&&p->link==pllist->top){
    if(q==pllist->base){                     /* find q */
         while(q!=p){
         if(q->info=='_') temp=q;
         q=q->link;
      }
         q=temp;
   }
   temp1=temp=pllist->base;
      while(temp!=q){                         /* find temp */
         if(temp->info=='_') temp1=temp;
         temp=temp->link;
   }
      temp=temp1; temp1=pllist->base;
      /*////////// Calculate function /////////////////////*/
      swap=q; i=0;       /* calculate t1 */
   if(isPoint_included(q)){           /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){     /*doesn't include point*/
          while(swap->link->info!='_') {swap=swap->link; i++;}
          swap=q;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
      swap=temp; t2=0;i=0;   /* calculate t2 */
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)(swap->link->info-'0');
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
        t2=t2+m;
            swap=swap->link;
    }
    if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') t=t1+t2;
   else if(p->link->info=='-') t=t2-t1;
   else if(p->link->info=='*') t=t1*t2;
   else if(p->link->info=='/') t=t2/t1;
      /*///// Deal with the left //////////////////*/
     q=temp;
     p=p->link;
  }
  /*******************************************************************/
  /*******************************************************************/
 else if(!isOperator(p->info)&&isOperator(p->link->link->info)){
      if(q==pllist->base){                     /* find q */
         while(q!=p){
         if(q->info=='_') temp=q;
         q=q->link;
      }
         q=temp;
   }
   temp1=temp=pllist->base;
      while(temp!=q){                         /* find temp */
         if(temp->info=='_') temp1=temp;
         temp=temp->link;
   }
      temp=temp1; temp1=pllist->base;
      /*////////// Calculate function /////////////////////*/
      swap=q; i=0;       /* calculate t1 */
   if(isPoint_included(q)){           /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){     /*doesn't include point*/
          while(swap->link->info!='_') {swap=swap->link; i++;}
          swap=q;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }

      swap=temp; i=0;   /* calculate t2 */
   if(isPoint_included(temp)){    /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') t=t1+t2;
   else if(p->link->info=='-') t=t1-t2;
   else if(p->link->info=='*') t=t1*t2;
   else if(p->link->info=='/') t=t2/t1;
      /*///// Deal with the left //////////////////*/
     q=temp;
     p=p->link;
  }
     /***********************************************************************************/
  /***********************************************************************************/
     else if(!isOperator(p->info)&&!isOperator(p->link->link->info)&&Calculator_underline(p->link)==1){
   if(q==pllist->base){                     /* find q */
         while(q!=p){
         if(q->info=='_') temp=q;
         q=q->link;
      }
         q=temp;
   }
   temp1=temp=pllist->base;
      while(temp!=q){                         /* find temp */
         if(temp->info=='_') temp1=temp;
         temp=temp->link;
   }
      temp=temp1; temp1=pllist->base;
      /*////////// Calculate function /////////////////////*/
      swap=q; i=0;       /* calculate t1 */
   if(isPoint_included(q)){           /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){     /*doesn't include point*/
          while(swap->link->info!='_') {swap=swap->link; i++;}
          swap=q;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
      swap=temp; i=0;   /* calculate t2 */
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') t=t1+t2;
   else if(p->link->info=='-') t=t1-t2;
   else if(p->link->info=='*') t=t1*t2;
   else if(p->link->info=='/') t=t2/t1;
      /*///// Deal with the left //////////////////*/
     q=temp;
     temp=p=p->link;
     /***note*** Dealt withe the second part*/
     while(!isOperator(p->link->info)) p=p->link;
     swap=temp; t1=0;i=0;   /* calculate t1 */
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
   swap=temp;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
        t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') t=t+t1;
   else if(p->link->info=='-') t=t-t1;
   else if(p->link->info=='*') t=t*t1;
   else if(p->link->info=='/') t=t/t1;
      /*///// Deal with the left //////////////////*/
     p=p->link;
  }
  /************************************************************/
  /************************************************************/
  else if(isOperator(p->info)&&isOperator(p->link->info)){
   temp=temp1=pllist->base; t2=0;i=0;   /*find temp */
   while(temp!=q){
    if(temp->info=='_') temp1=temp;
    temp=temp->link;
   }
   temp=temp1;
   swap=temp;
   if(isPoint_included(temp)){
    while(swap->link->info!='.') {swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0||i==-1) {t2=t2 +(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   /*calculate t*/
   if(p->link->info=='+') t=t+t2;
   else if(p->link->info=='-') t=t-t2;
   else if(p->link->info=='*') t=t*t2;
   else if(p->link->info=='/') t=t2/t;
   /* dealt with the left */
   temp=swap=pllist->base;
   while(swap!=q){
    if(swap->info=='_') temp=swap;
    swap=swap->link;
   }
   q=temp;
   p=p->link;
  }
  /*****************************************************************/
  /*****************************************************************/
  else if(isOperator(p->info) && !isOperator(p->link->info)&&Calculator_underline(p->link)==1){
   temp=p;
   while(!isOperator(p->link->info)) p=p->link;
   swap=temp;
   t1=t2=0;i=0;
   if(isPoint_included(temp)){
    while(swap->link->info!='.') {swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0||i==-1) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
    /*calculate t*/
   if(p->link->info=='+') t=t+t1;
   else if(p->link->info=='-') t=t-t1;
   else if(p->link->info=='*') t=t*t1;
   else if(p->link->info=='/') t=t/t1;
   /* dealt with the left */
   temp=swap=pllist->base;
   while(swap!=q){
    if(swap->info=='_') temp=swap;
    swap=swap->link;
   }
   q=temp;
   p=p->link;
  }
  /*******************************************************************/
  /********************************************************************/
  else if(isOperator(p->info)&&!isOperator(p->link->info)&&Calculator_underline(p->link)==2){
   PNode ID;
   temp1=temp=pllist->base;
      while(temp!=q){                         /* find temp */
         if(temp->info=='_') temp1=temp;
         temp=temp->link;
   }
      ID=temp=temp1;
   temp1=q=p;
   while(!isOperator(p->link->info)) p=p->link;
   while(q->link!=p){
    if(q->link->info=='_') temp=q->link;
    q=q->link;
   }
   q=temp;
   temp=temp1;
   swap=q; t1=0;i=0;       /* calculate t1 */
   if(isPoint_included(q)){           /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0 ||i==-1) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){     /*doesn't include point*/
          while(swap->link->info!='_') {swap=swap->link; i++;}
          swap=q;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0 || i==-1) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
      swap=temp; t2=0;i=0;   /* calculate t2 */
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0 ||i==-1) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') tmark1=t1+t2;
   else if(p->link->info=='-') tmark1=t1-t2;
   else if(p->link->info=='*') tmark1=t1*t2;
   else if(p->link->info=='/') tmark1=t2/t1;

   p=p->link;
   if(p->link->info=='+') t=t+tmark1;
   else if(p->link->info=='-') t=t-tmark1;
   else if(p->link->info=='*') t=t*tmark1;
   else if(p->link->info=='/') t=t/tmark1;
   p=p->link;
   q=ID;
  }
  /********************************************************************/
  /*********************************************************************/
  else if(isOperator(p->info)&&!isOperator(p->link->info)&&Calculator_underline(p->link)==1){
   temp1=p;
   while(!isOperator(p->link->info)) p=p->link;
   swap=temp1; t1=0;i=0;   /* calculate t1 */
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
   swap=temp1;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
        t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   if(p->link->info=='+') t=t+t1;
   else if(p->link->info=='-') t=t-t1;
   else if(p->link->info=='*') t=t*t1;
   else if(p->link->info=='/') t=t/t1;
   p=p->link;
  }
  /** ****************************************************************/
  /********************************************************************/
  else if(!isOperator(p->info) && !isOperator(p->link->link->info)&&Calculator_underline(p->link)==2){
   PNode ID;
   if(q==pllist->base){                     /* find q */
         while(q!=p){
         if(q->info=='_') temp=q;
         q=q->link;
      }
         q=temp;
   }
   swap=temp=pllist->base; /*find temp */
   while(temp->link!=q){
    if(temp->link->info=='_') swap=temp;
    temp=temp->link;
   }
   ID=temp=swap;
   swap=q;
   t1=t2=0;i=0;
   if(isPoint_included(q)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}/*calculate t1*/
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=q;
          while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   swap=temp;
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
         while(swap->link->info!='_'&&--i!=0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
    /*calculate t*/
   if(p->link->info=='+') tmark1=t1+t2;
   else if(p->link->info=='-') tmark1=t1-t2;
   else if(p->link->info=='*') tmark1=t1*t2;
   else if(p->link->info=='/') tmark1=t2/t1;
      temp1=p=p->link;
     /*//////////////////////dealt with the second part/////////*/
   t1=t2=0;i=0;
     while(!isOperator(p->link->info)) p=p->link;
     while(q->link!=p){                                /*find p*/
      if(q->link->info=='_') temp=q->link;
      q=q->link;
     }
     q=temp;
     temp=temp1;
     if(isPoint_included(q)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=q;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t1=t1+m;
     swap=swap->link;
    }
    if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t1=t1+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(q)){
   swap=q;
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=q;
   while(swap->link->info!='_'&&--i>0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t1=t1+m;
            swap=swap->link;
    }

    if(i==0||i==-1) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
   swap=temp;
   if(isPoint_included(temp)){   /*include point */
    while(swap->link->info!='.'){swap=swap->link;i++;}
    swap=temp;
    while(swap->link->info!='.'&&--i!=0){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*10;
     t2=t2+m;
     swap=swap->link;
    }
    if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
    i=1;
    while(swap->link->info!='_'){
     j=i;
     m=(NumberType)swap->link->info-'0';
     while(j--!=0) m=m*1/10;
     t2=t2+m;
     swap=swap->link;
     i++;
    }
   }
   else if(!isPoint_included(temp)){
    while(swap->link->info!='_'){swap=swap->link;i++;}
          swap=temp;
   while(swap->link->info!='_'&&--i>0) {
      j=i;
      m=(NumberType)swap->link->info-'0';
      while(j--!=0) m=m*10;
             t2=t2+m;
            swap=swap->link;
    }
    if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
   }
    /*calculate tmark2*/
   if(p->link->info=='+') tmark2=t1+t2;
   else if(p->link->info=='-') tmark2=t1-t2;
   else if(p->link->info=='*') tmark2=t1*t2;
   else if(p->link->info=='/') tmark2=t2/t1;
   p=p->link;
    /*calculate t*/
   if(p->link->info=='+') t=tmark1+tmark2;
   else if(p->link->info=='-') t=tmark1-tmark2;
   else if(p->link->info=='*') t=tmark1*tmark2;
   else if(p->link->info=='/') t=tmark1/tmark2;
   /*dealt with the left*/
   p=p->link;
   q=ID;
  }
 }
 printf("The result is %f\n",t);
 return(t);
}

void main()
{
 char s[100];
 int i;
 PLinkList pllist;
 PLinkStack plstack;
 pllist=Create_LinkListPointer();
 plstack=Create_LinkStackPointer();
 Create_LinkList(pllist);
 Create_LinkStack(plstack);
 printf("Input the operator:\n");
 scanf("%s",&s);
 for(i=0;i<strlen(s);i++){
 if(!isOperator(s[i])) push_LinkList(pllist,s[i]);
 else if(isOperator(s[i]) && plstack->top==plstack->base) {
      if(!isOperator(pllist->top->info)) push_underline(pllist);
      push_stack(plstack,s[i]);
  }
  else {
   if(s[i]=='(') push_stack(plstack,s[i]);
   else if(plstack->top->info=='('){
       push_underline(pllist);
       push_stack(plstack,s[i]);
   }
   /*//////////////////////////////////////////////////////////////////////////////////*/
   else if((s[i]=='+' || s[i]=='-') && (plstack->top->info=='*' || plstack->top->info=='/') && bracket_included(plstack)){
 if(!isOperator(pllist->top->info)) push_underline(pllist);
 pop_stack_until_bracket(pllist,plstack);
 push_stack(plstack,s[i]);
   }
   else if((s[ i]=='+' || s[i]=='-') && (plstack->top->info=='*' || plstack->top->info=='/') && !bracket_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_all(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   /*////////////////////////////////////////////////////////////////////////////////////////////////////*/
   else if((s[i]=='*' || s[i]=='/')&&(plstack->top->info=='+'|| plstack->top->info=='-')){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    push_stack(plstack,s[i]);
   }
   /*////////////////////////////////////////////////////////////////////////////////////////////////////*/
   else if((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    push_stack(plstack,s[i]);
   }
   /*///////////////////////////////////////////////////////////////////////////////////*/
   else if(((s[i]=='+' && plstack->top->info=='-') || (s[i]=='-' && plstack->top->info=='+')) && bracket_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='+' && plstack->top->info=='-') || (s[i]=='-' && plstack->top->info=='+')) && !bracket_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_all(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')) && bracket_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')) && !bracket_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_all(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   /*///////////////////////////////////////////////////////////////////////////////*/
   else if(((s[i]=='*'&& plstack->top->info=='/')||(s[i]=='/'&& plstack->top->info=='*'))&&bracket_included(plstack)&&bracket_included_and_lower(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket_lower(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info=='/') || (s[i]=='/' && plstack->top->info=='*')) && bracket_included(plstack) && !bracket_included_and_lower(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info =='/') ||(s[i]=='/' && plstack->top->info=='*')) && !bracket_included(plstack) && islower_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_until_lower(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info=='/') ||(s[i]=='/' && plstack->top->info=='*')) && !bracket_included(plstack) && !islower_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_all(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*'&& plstack->top->info=='*')||(s[i]=='/'&& plstack->top->info=='/'))&&bracket_included(plstack)&&bracket_included_and_lower(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket_lower(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info=='*') || (s[i]=='/' && plstack->top->info=='/'))&& bracket_included(plstack) && !bracket_included_and_lower(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info =='*') ||(s[i]=='/' && plstack->top->info=='/'))&& !bracket_included(plstack) && islower_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_until_lower(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   else if(((s[i]=='*' && plstack->top->info=='*') ||(s[i]=='/' && plstack->top->info=='/'))&& !bracket_included(plstack) && !islower_included(plstack)){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_all(pllist,plstack);
    push_stack(plstack,s[i]);
   }
   /*////////////////////////////////////////////////////////////////////////////////////*/
   else if(s[i]==')'){
    if(!isOperator(pllist->top->info)) push_underline(pllist);
    pop_stack_until_bracket(pllist,plstack);
    pop_one(plstack);
   }
  }
 }
 pop_all(pllist,plstack);
 print_llist(pllist);
 Calculator(pllist);
 getch();
}

视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058