中序转逆波兰式

 
  1. 遇到(,直接压栈
  2. 遇到),输出栈中所有元素直到遇到(
  3. 遇到运算符
    1. 如果优先级比栈顶的大则入栈
    2. 否则持续输出栈顶
  4. 遇到数字,直接输出
  5. 输出栈内所有元素
/* 12*(3+4)-6+8/2 */
void caculate(const char * str){
	my_stack<char> s;
	
	int i=0; char ch; 
	char* output = new char[1024]; int j=0;
	while(*(str + i) != '\0'){
		ch = *(str+i);
		if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='(' || ch==')'){
			
			if(ch == '(') s.push(ch) ;
			else if(ch == ')'){
				while(s.top()!='('){
					output[j++] = s.top();
					s.pop();
				} s.pop();
			}
			else {
				while(!s.empty() && s.top() != '(' && getLevel(ch) <= getLevel(s.top())){
					output[j++] = s.top(); s.pop(); //否则栈顶元素出栈 
				} s.push(ch); //操作符优先级比栈顶元素高,则入栈
			}
		}else {
			output[j++] = ch;
			char next = *(str+i+1);
			if(next=='+' || next=='-' || next=='*' || next=='/' || next=='(' || next==')') output[j++]=' ';
		}
		i++;		
	}
	while(!s.empty()){
		output[j++] = s.top();; s.pop();
	}output[j] = '\0';
	
	/* 12 3 4 +*6 -8 2/+ */
	my_stack<int> s2;
	for(j=0; output[j]!='\0';){
		if(output[j]>='0'&&output[j]<='9'){
			int num=0;
			do{
				num*=10; num+=output[j++]-'0';
			}while(output[j]>='0'&&output[j]<='9');
			s2.push(num);
			
		}
		else if(output[j]==' ') j++;
		else if(output[j]=='+' || output[j]=='-' || output[j]=='*' || output[j]=='/'){
			int second = s2.top(); s2.pop();
			int first = s2.top(); s2.pop();
			printf("%d %c %d\n", first, output[j], second);
			if(output[j] == '+') s2.push(first+second);
			else if(output[j] == '-') s2.push(first-second);
			else if(output[j] == '*') s2.push(first*second);
			else s2.push(first/second);
			j++;
		}
	}

	printf("%d", s2.top());
	
	
}