算术表达式求值@C/C++

来源:互联网 发布:单片机湿度程序 编辑:程序博客网 时间:2024/06/08 18:11
#include <stdafx.h>
#include 
<malloc.h>

#define MAXSIZE 20


bool Error=false;

typedef 
struct
{
//定义两个指针,分别指向栈的顶和栈底
int* top;
int* head;
}
 stack;


//栈的初始化
stack* init()
{
    stack s;
    s.head
=(int *)malloc(MAXSIZE*sizeof(int));
    s.top
=s.head;
    
return &s;
}

//取栈顶操作
int pop(stack* s)
{
    
int retNumber=0;
    
if(s->head==s->top)
    
{
        Error
=true
    }

    
else
    
{
        s
->top=s->top-1;
        retNumber
=*(s->top);
    }

    
return retNumber;
}


//压栈操作
void push(const int number,stack* s)
{
    
if((s->top-s->head)>=MAXSIZE)
    
{
        Error
=true;
    }

    
else
    
{
        
*(s->top)=number;
        s
->top=s->top+1;
    }

}

bool InDigit(char c)
{
    
if(c>47 && c <58)
        
return true;
    
return false;
}


bool InOp(char c)
{
    
if(c=='+'||c=='-'||
        c
=='*'||c=='/'||
        c
=='('||c==')'||
        c
=='='||c=='@')
        
return true;
    
return false;
}


bool Allow(char c)
{
    
if(InOp(c)||InDigit(c)||c==' ' ||c==' ')
        
return true;
    
return false;
}

#define BUFSIZE 6
int GetNumber(char* ch)
{
    
int numberCache=0;
    
int index=0;
    
char charCache=*ch;
    
while(InDigit(*ch)&&index<BUFSIZE)
    
{
        charCache
=*ch;
        Error
=(index>=1&&charCache==0)?true:false;
        numberCache
=numberCache*10+(charCache-48);
        ch
++;
    }

    
return numberCache;
}


int GetPow(char op)
{
    
int retcd=0;
    
switch(op)
    
{
    
case '(': retcd=1;break;
    
case '+':
    
case '-': retcd=2;break;
    
case '*'
    
case '/': retcd=3;break;
    
case ')': retcd=4;break;
    
case '@': retcd=5;break;
    }

    
return retcd;
}


/* 如果op1 的优先级大于 op2 的优先级返回 true */
bool CmpPow(char op1,char op2)
{
    
return GetPow(op1)-GetPow(op2)>=0?true:false;
}


int solution(char op1,int number1,int number2)
{
    
switch(op1)
    
{
    
case '@':
        
return op1;
    
case '+'
        
return (number1+number2);
        
break;
    
case '-'
        
return (number1-number2);
        
break;
    
case '*'
        
return (number1*number2);
        
break;
    
case '/'
        
if(number2!=0)
            
return (number1/number2);
        
else {Error=true;return 0;}
        
break;
    }

}


int main(int argc, char* argv[])
{
    
char buf[BUFSIZE];
    
int count=0;
    
int bufnumber=0;
    
char c,sign;
    
bool PronenessCharIsDigit=true;
    stack numberS;
    stack opS;

    
//numberS=init();        //初始化两个栈
    
//opS=init();
    numberS.head=(int *)malloc(MAXSIZE*sizeof(int));
    numberS.top
=numberS.head;

    opS.head
=(int *)malloc(MAXSIZE*sizeof(int));
    opS.top
=opS.head;

    printf(
"请输入运算表达式( 以 '=' 号结束 ): ");
    c
=getchar();
    push((
int)'@',&opS);
    
do
    
{
        
if(InOp(c))
        
{
            
if(PronenessCharIsDigit==true)
            
{
                buf[count]
='#';
                bufnumber
=GetNumber(buf);
                
if(Error==true)
                    printf(
"Error: check your number string !");
                
/*如果当前字符是操作符者将数字缓冲区内的内容送入数字栈*/
                push(bufnumber,
&numberS);   
            }

            
switch(c)
            
{
            
case '('
                push((
int)c,&opS); 
                
break;
            
case '+':
            
case '-'
            
case '*'
            
case '/':
                
while(CmpPow(sign=(char)pop(&opS),c) == true)
                
{
                    
int n[2];
                    
if(Error)
                    
{printf("Error: Too much operator ! ");break;}
                    
if(sign=='@')break;
                    n[
1]=pop(&numberS);
                    n[
0]=pop(&numberS);
                    push(solution(sign,n[
0],n[1]),&numberS);
                }

                push((
int)sign,&opS);
                push((
int)c,&opS);
                
break;
            
case ')'
                
while( (sign=(char)pop(&opS)) != '(' )
                
{
                    
int n[2];
                    
if(Error)
                    
{printf("Error: not match :'(' or ')' !   ");break;}
                    n[
1]=pop(&numberS);
                    n[
0]=pop(&numberS);
                    push(solution(sign,n[
0],n[1]),&numberS);
                }

                
break;
            
case '='
                
while( (sign=(char)pop(&opS)) != '@')
                
{
                    
int n[2];
                    
if(Error)
                    
{printf("Error: Maybe the expression is Too long !  ");break;}
                    n[
1]=pop(&numberS);
                    n[
0]=pop(&numberS);
                    push(solution(sign,n[
0],n[1]),&numberS);
                    
                }

                printf(
"a 运算结果为:%d ",pop(&numberS));
                printf(
"请输入运算表达式( 以 '=' 号结束 ): ");
                push((
int)sign,&opS);
                
break;
            }

            count
=0;
            PronenessCharIsDigit
=false;
        }

        
else if(InDigit(c))
        
{
            PronenessCharIsDigit
=true;
            buf[count
++]=c;
            Error
=count>=BUFSIZE?true:false;
        }


    }
while((c=getchar())!=EOF && !Error);

    printf(
" 够笨的...有错误,难道你没发现 ? ");
    
return 0;
}


 

原创粉丝点击