- Đây là code tính trị biểu thức dạng hậu tố, chương trình nó chạy nhưng chạy không đúng, mong mọi người xem sai chổ nào vậy, cảm ơn nhiều!
Code:
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
template <class nodetype>
struct node{
	nodetype data;
	node<nodetype>*next;
};
template< class listtype>
class stack{
public:
	stack();
	~stack();
	void push(listtype value);
	int pop(listtype &dataout);
	int gettopstack(listtype &dataout);
	int size();
	void clear();
	void print();
	int empty();
	
private:
	int count;
	node<listtype>* top;
};
template <class listtype>
stack<listtype>::stack(){
	top=NULL;
	count=0;
}
template <class listtype>
stack<listtype>::~stack(){
	clear();
}
template<class listtype>
void stack<listtype>::push(listtype value){
	node<listtype>* pnew=new node<listtype>();
	pnew->data=value;
	pnew->next=top;
	top=pnew;
	count++;
}
template<class listtype>
int stack<listtype>::pop(listtype &dataout){
	if(count==0)
		return 0;
	node<listtype>*dlp=top;
	top->data=dataout;
	top=top->next;
	count--;
	delete dlp;
	return 1;
}
template<class listtype>
int stack<listtype>::gettopstack(listtype &dataout){
	if(count==0)
		return 0;
	dataout=top->data;
	return 1;
}
template<class listtype>
void stack<listtype>::clear(){
	node<listtype>*p;
	while(top!=NULL)
	{
		p=top;
		top=top->next;
		delete p;
	}
	count=0;
}
template<class listtype>
int stack<listtype>::empty(){
	return (count==0);
}
template<class listtype>
int stack<listtype>::size(){
	return count;
}
template<class listtype>
void stack<listtype>::print(){
	node<listtype>* p;
	p=top;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	char xauvao[80];
	char ch;
	int i=1,len,done=1,ketqua;
	int th1,th2;
	stack<int> stack1;
	cout<<" ********DANH GIA BIEU THUC SANG HAU TO ********* "<<endl;
	do{
		cout<<"Moi ban nhap bieu thuc: "<<endl;
		cin.getline(xauvao,79);
		len=strlen(xauvao);
		if(len!=0)
		{
			cout<<"Cac buoc thuc hien: "<<endl;
			for(int i=0;xauvao[i]!='\0';i++)
			{
				ch=xauvao[i];
				if((ch>='0')&&(ch<='9'))
				{
					stack1.push(ch-'0');
					cout<<"Stack: ";
					stack1.print();
					getch();
				}
				else {
					stack1.pop(th2);
					stack1.pop(th1);
					switch (ch)
					{
					case '+': stack1.push(th1+th2);
						cout<<"Stack: ";
						stack1.print();
						getch();
						break;
					case '-' :  stack1.push(th1-th2);
						cout<<"Stack: ";
						stack1.print();
						getch();
						break;
					case '*' :  stack1.push(th1*th2);
						cout<<"Stack: ";
						stack1.print();
						getch();
						break;
					case '/' :  stack1.push(th1/th2);
						cout<<"Stack: ";
						stack1.print();
						getch();
						break;
					default:
						cout<<" Toan tu khong hop le!";
						exit(1);
					}
				}
			}
			stack1.pop(ketqua);
			cout<<" Ket qua bieu thuc la: "<<ketqua;
		}else done =0;		
	}while(done);
	return 0;
}