user-0 提问: 用栈实现计算器一个奇怪的问题．．．

回复:
user: user-1
content: 表达式 A - B 中A B之间没有明确的求值顺序，编译器可以以任意顺序对其进行求值 也就是说，你的代码和 i++ + ++i没什么区别

user: user-0
content: 在ＶＳ下运行正确了．．好像真是编译器问题啊

user: user-0
content: 我看过了，栈操作没有问题啊．．身边的人说是编译器优化问题．

user: user-2
content: 应该是你栈的操作弄反了，呵呵，不过楼主，写的不错啊，这是后缀表达式

user: user-0
content: 我是在linux下运行的．  完整代码如下： main.cpp  #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include "stack.h"  int IsOperator(char ch) {     char ops[] = "+-*/^";     int i;     for (i = 0; i < 5; i++)     {         if (ch == ops[i])             return 1;     }     return 0; }   int Precedence(char op) {     if (op == '(')     {         return -1;     }     else if(op == '^') 	return 2;     else if(op == '*' || op == '/')         return 1;     else if(op == '+' || op == '-')         return 0; }  void InFixtoPostFix(char *InFix, char *PostFix) {     int length = 0;     int i = 0;     int j = 0;     length = strlen(InFix);     Stack Experssion = CreateStack(length);     MakeEmpty(Experssion);     for(i = 0; i < length;)     {         if(InFix[i] == '(')             Push(InFix[i++],Experssion);         else if(InFix[i] == ')')	         {         	while(Top(Experssion) != '(')             	{	 			PostFix[j++] = TopAndPop(Experssion); 			PostFix[j++] = ' ';			             	}	 		Pop(Experssion); 		++i;         }         else         {             if(!IsOperator(InFix[i]))             {    while(!IsOperator(InFix[i]) && InFix[i] != '(' && InFix[i] != ')' && i < length) 		{ 			PostFix[j++] = InFix[i++]; 		} 		PostFix[j++] = ' '; 	    }             else             {             	while(IsEmpty(Experssion) == 0 && (Precedence(Top(Experssion))>=Precedence(InFix[i])))             	{         			if(InFix[i] == '^') 				break; 			else 			{	 				PostFix[j++] = TopAndPop(Experssion); 				PostFix[j++] = ' '; 			} 		}				                 Push(InFix[i++],Experssion);             }         }      }      while(IsEmpty(Experssion) == 0)     { 	PostFix[j++] = TopAndPop(Experssion); 	PostFix[j++] = ' ';     }     Free_Stack(Experssion);     PostFix[j] = 0;   //end of the string }  int Calcute(char *PostFix) {     int length = strlen(PostFix);     int i = 0;     ElementType tmp_var = 0;     ElementType result_var = 0;     ElementType index = 0;     Stack Calcute_stack = CreateStack(length);     MakeEmpty(Calcute_stack);     for(i = 0; i < length;)     {         if(IsOperator(PostFix[i]))         {             switch(PostFix[i++])             {             case '+':                 tmp_var = TopAndPop(Calcute_stack) + TopAndPop(Calcute_stack);                 break;             case '-':            		 tmp_var = TopAndPop(Calcute_stack) - TopAndPop(Calcute_stack); 		 tmp_var = -tmp_var;                  break;             case '*':                 tmp_var = TopAndPop(Calcute_stack) * TopAndPop(Calcute_stack);                 break;             case '/':                 tmp_var = 1.0 / TopAndPop(Calcute_stack) * TopAndPop(Calcute_stack);                 break; 	    case '^': 		index = TopAndPop(Calcute_stack);		 		tmp_var = pow(TopAndPop(Calcute_stack), index); 		break;             }             Push(tmp_var,Calcute_stack);	             }          else if(PostFix[i]!= ' ') 	{	 		tmp_var = 0; 		while(PostFix[i]!= ' ') 		{     			tmp_var += PostFix[i++] - '0'; 			tmp_var *= 10; 		} 		tmp_var /= 10;  		Push(tmp_var, Calcute_stack); 	}  	else 		++i;           }      result_var = Top(Calcute_stack);     Free_Stack(Calcute_stack);     return result_var; } int main() {     char InFix[100];     char PostFix[100];     ElementType Result = 0;      while(1)     {         printf("please enter an expression: ");         gets(InFix);         printf("Infix is : %s \n",InFix);         InFixtoPostFix(InFix,PostFix);         printf("PostFix is : %s \n",PostFix);         Result = Calcute(PostFix);         printf("calculation is : %d \n",Result);     }     return 0; }   stack.h  #ifndef _STACK_H #define _STACK_H  struct StackRecord; typedef struct StackRecord *Stack; typedef int ElementType;  int IsEmpty(Stack S); int IsFull(Stack S); Stack CreateStack(int MaxElements); void Free_Stack(Stack S); void MakeEmpty(Stack S); void Push(ElementType X, Stack S); ElementType Top(Stack S); void Pop(Stack S); ElementType TopAndPop(Stack S);  #endif // _STACK_H   stack.cpp  #include <stdio.h> #include <stdlib.h> #include "stack.h"  #define EmptyTOS (-1) #define MinStackSize (1) struct StackRecord { 	int Capacity; 	int TopOfStack; 	ElementType *Array; };  Stack CreateStack(int MaxElements) { 	    Stack S;  		if(MaxElements < MinStackSize) 			printf("Stack Size is too small!\n");  		S = (Stack) malloc(sizeof(struct StackRecord)); 		if(S == NULL) 			printf("Out of Space, malloc Stack S failed\n");  		S->Array = (ElementType *) malloc(sizeof(ElementType)*MaxElements); 		if(S->Array == NULL) 			printf("Out of Space, malloc S->Array failed\n");  		S->Capacity = MaxElements;  		return S; }  void Free_Stack(Stack S) { 	    if(S != NULL) 		{ 			free(S->Array); 			free(S); 		} }  int IsEmpty(Stack S) { 	    return S->TopOfStack == EmptyTOS; }  int IsFull(Stack S) { 	    return S->TopOfStack == S->Capacity - 1; }  void MakeEmpty(Stack S) { 	    S->TopOfStack = EmptyTOS; }  void Push(ElementType X, Stack S) { 	    if(IsFull(S)) 			printf("Stack S is full\n"); 		else 			S->Array[++S->TopOfStack] = X; }  ElementType Top(Stack S) { 	    if(IsEmpty(S)) 		{ 			printf("Stack S is Empty\n"); 			return -1; 		} 		else 			return S->Array[S->TopOfStack]; }  void Pop(Stack S) { 	    if(IsEmpty(S)) 			printf("Stack S is Empty\n"); 		else 			S->TopOfStack--; }  ElementType TopAndPop(Stack S) { 	    if(IsEmpty(S)) 		{ 			printf("Stack S is Empty\n"); 			return -1; 		} 		else 		return S->Array[S->TopOfStack--]; }

user: user-0
content: 不好意思．．．我打错了．以上程序除了最后一个的结果是－１以外，结果都是１．郁闷啊．

user: user-3
content: 除了tmp_var = -(TopAndPop(Calcute_stack) - TopAndPop(Calcute_stack));返回1表示有疑问外,其它应该是对的, 2在上面,1在下面,所以第一个pop的是2,

user: user-0
content: makefile这么写得，这样就关闭编译器优化了吧．．．  infix: main.o stack.o 	g++ -o infix -O0 -g main.o stack.o main.o: main.cpp stack.h 	g++ -c -O0 -g main.cpp stack.o: stack.cpp 	g++ -c -O0 -g stack.cpp clean: 	rm infix main.o stack.o