1. 程式人生 > >資料結構 簡單算式表示式求值

資料結構 簡單算式表示式求值

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct 
{
	char *elem;
	int top;
	int size;
 }Sqstack;
 //定義操作符棧和運算元棧 
 Sqstack Ope;
 Sqstack Num;
 
 void Initstack(Sqstack *s) //初始化棧 
 { 
    s->size=50;
    s->top=0;  
    s->elem=(char*)malloc(s->size*sizeof(char));
    if(!s->elem)  
        printf("OVERFLOW");
 }

 void Push(Sqstack *s,char e)  //入棧
 {
 	 if(s->top>=s->size)//棧記憶體不夠時
	 {
	 	char *newelem;
	 	int increment=50;
	 	newelem=(char*)realloc(s->elem,(s->size+increment)*sizeof(char));
	 	s->elem=newelem;
	 	s->size=s->size+increment;
	  } 
	 s->elem[s->top]=e;
	 s->top++; 
 }
 
void Pop(Sqstack *s)//出棧 
{
   s->top--;
}
 
char GetTop(Sqstack *s)//取棧頂元素
{
	char e;
	s->top--;
	e=s->elem[s->top];
	
	return e;
}
void calculate(Sqstack *ope,Sqstack *num)
{  
  
    char num1,num2,op; 
    num2=GetTop(num);  
    num1=GetTop(num);  
    op=GetTop(ope);
    int tmpResult=0;  
    switch(op)
	{  
        case '+':  
            tmpResult=num1+num2;            
            break;  
        case '-':  
            tmpResult=num1-num2;  
            break;  
        case '*':  
            tmpResult=num1*num2;  
            break;  
        case '/':  
            tmpResult=num1/num2;  
            break;  
    }  
    Push(num,tmpResult);//入棧   
}   
void dealExpression()
{   
    Initstack(&Ope);
    Initstack(&Num);
    printf("請輸入正確的中綴表示式(以#號結束):");  
    char onechar; //單個字元 
    scanf("%c",&onechar);  
    while(a!='#')
	{  
        switch(onechar)
		{  
            case '+':  
            case '-':  
                if(Ope.top&&Ope.elem[Ope.top-1]!='(')
				{  
                    calculate(&Ope,&Num);  
                }  
                Push(&Ope,onechar); //入棧 
                scanf("%c",&onechar);  
                break;  
            case '*':  
            case '/':  
                if(Ope.top&&(Ope.elem[Ope.top-1]=='*'||Ope.elem[Ope.top-1]=='/'))
				{  
                    calculate(&Ope,&Num);  
                }  
				Push(&Ope,onechar); //入棧 
                scanf("%c",&onechar);  
                break;  
            case '(':  
                Push(&Ope,onechar); //入棧 
                scanf("%c",&onechar);  
                break;  
            case ')':  
                while(Ope.elem[Ope.top-1]!='(')//當前是')',則Ope棧一定能有'('匹配到,即Ope棧一定不為空 
				{                                  
                    calculate(&Ope,&Num);  
                }  
                Pop(&Ope);//出棧,彈出左括號  
                scanf("%c",&onechar);  
                break;  
            default://onechar為數字,則入num棧  
                char opNum=0;  
                do{  
                    opNum=opNum*10+currentChar-'0';  
                    scanf("%c", &onechar);  
                }while(onechar>='0' && onechar<='9');  
                Push(&Num,opNum); 
                break;  
        }//end switch  
    }  
    while(Ope.top)
	{  
        calculate(&Ope,&Num);  
    }  
    int result;
	result=Num.elem[Num.top-1];  
    printf("計算結果是%d\n", result);  
}  
int main()
{  
    dealExpression();
	return 0;  
}