首页 > 代码库 > C语言实现四则混合运算

C语言实现四则混合运算

 
 
#include<iostream>
usingnamespace std;
 
typedefstruct SqStack{
 int* base;
 int* top;
 intsize;
}SqStack;
 
#define STACK_INIT_SIZE  100
#define STACKINCREMENT   20
 
voidInitSqStack(SqStack &t);
voidpush(SqStack &t, intn);
int out(SqStack &t);
int Empty( SqStack &t);
charGetTop(SqStack &t);
charPrecede(char ,char);
 
int main(){
    charch;
 SqStack OPTR;InitSqStack(OPTR);
 SqStack OPND;InitSqStack(OPND);
    push(OPTR,‘=‘);
     
  
 ch=getchar();
 
 if(ch==‘-‘){        //第一个数为负数
  intm=0;
  ch=getchar();
   while(ch<=‘9‘&&ch>=‘0‘){
    m=m*10+ch-48;
    ch=getchar();                          
       }
    push(OPND,m*(-1));
 }
    
  
  
 while(ch!=10){
  intm=0; int flag=1;   char ch1=ch;
 
        if(ch1==‘(‘){  
   charch2;
   push(OPTR,ch);
   while((ch=getchar())==‘(‘)
                 push(OPTR,ch);
    
   ch2=ch;
   if(ch2==‘-‘){               //出现负数
    flag=-1;
    ch=getchar();
    }
       
  while(ch<=‘9‘&&ch>=‘0‘){
      m=m*10+ch-48;
      ch=getchar();                          
         }
   
       
  if(ch==‘)‘)              //形如(-1)的负数 还有形如(-1+2*5  中的负数
     {
     out(OPTR);
     ch=getchar();
     }
 }
 
 else{
     while(ch<=‘9‘&&ch>=‘0‘){
    m=m*10+ch-48;
    ch=getchar();                          
     }
 }
 
  if(m)
    push(OPND,m*flag);
 
  switch(Precede(GetTop(OPTR),ch)){
                  
        case‘<‘:push(OPTR,ch);ch=getchar();break;
          
        case‘=‘:out(OPTR);ch=getchar();break;
           
        case‘>‘:{
                     intm2=out(OPND),m1=out(OPND);
                                  //注意m1和m2的顺序  后一个操作数先出来用m2接收
                     switch(out(OPTR))
                     {
                          case‘+‘:push(OPND,m1+m2);break;
                                   case‘-‘:push(OPND,m1-m2);break;
                                   case‘*‘:push(OPND,m1*m2);break;
                                   case‘/‘:push(OPND,m1/m2);break;
                      }
        
             break;
       
     }
    }
 }
 printf("%d",out(OPND));
  
  
 getchar();
 
  
}
 
   
 
voidpush(SqStack &t, intn){              
 if((t.top-t.base)>=t.size){
  t.base=(int*)realloc(t.base,(t.size+STACKINCREMENT)*sizeof(int));
  if(!t.base)
   exit(1);
 }
  *(t.top)=n;
     t.top++;
  
}         
 
intout(SqStack &t){            
 t.top--;
 return*(t.top);
}           
 
intEmpty( SqStack &t){
 intm=(t.top-t.base); 
 returnm;
}   
 
charGetTop(SqStack &t){
   return*(t.top-1);
}      
 
charPrecede(char c,char d)
{
    charyouxian[7][7]=
    {
        {‘>‘,‘>‘,‘<‘,‘<‘,‘<‘,‘>‘,‘>‘},
        {‘>‘,‘>‘,‘<‘,‘<‘,‘<‘,‘>‘,‘>‘},
        {‘>‘,‘>‘,‘>‘,‘>‘,‘<‘,‘>‘,‘>‘},
        {‘>‘,‘>‘,‘>‘,‘>‘,‘<‘,‘>‘,‘>‘},
        {‘<‘,‘<‘,‘<‘,‘<‘,‘<‘,‘=‘,‘>‘},
        {‘>‘,‘>‘,‘>‘,‘>‘,‘>‘,‘>‘,‘>‘},
        {‘<‘,‘<‘,‘<‘,‘<‘,‘<‘,‘>‘,‘=‘}
    };
    charopt[7]={‘+‘,‘-‘,‘*‘,‘/‘,‘(‘,‘)‘,‘=‘};
    intm,n,i;
    for(i=0;i<7;i++)
    {
        if(c==opt[i])
        {
            m=i;
        }
        if(d==opt[i])
        {
            n=i;
        }
    }
 returnyouxian[m][n];
}
 
voidInitSqStack(SqStack &t){
 t.base=(int*)(malloc(sizeof(int)*STACK_INIT_SIZE));
 if(!t.base)
   exit(1);
 t.top=t.base;
 t.size=STACK_INIT_SIZE;
}  

C语言实现四则混合运算