Reverse Polish Notation Calculator

Öncelikle Reverse Polish Notation ya da Ters Polonya Notasyonu nedir onu açıklayayım. Reverse Polish Notation(RPN) parantez ve köşeli ayraç kullanmadan matematiksel ifade yazmak için kullanılacak bir yol olarak Jan Lukasiewicz tarafından 1920 yılında geliştirilmiştir. Kaynak: HP. Daha ayrıntılı bilgiyi burada bulabilirsiniz

Reverse Polish Notation ile yazılmış bir aritmetik ifadeyi hesaplayan hesap makinası programının amacı C++ ile Stack yapılarını kullanmaktır: İndir

FilenameFilesizeLast modified
ReversePolishNotation.exe97.7 KiB2019/05/08 21:25
ReversePolishNotation.txt3.8 KiB2019/05/08 21:25
reversepolishnotation.cpp4.8 KiB2019/05/08 21:25
reversepolishnotation.jpg11.1 KiB2019/05/08 21:25

Kaynak Kodu:

Reverse Polish Notation ile yazılmış bir aritmetik ifadeyi hesaplayan program (Stack yapılarını kullanılıyor).

#include <iostream.h>
#include <stdlib.h>
const int MaxStackSize=256;
 
class Stack{ //a Stack is defined
	private:
		int StackList[MaxStackSize]; //Stores the values of stack items
		int TypeList[MaxStackSize];  //Stores the type of items i.e. 1 for operators 0 for operands
		int top;
	public:
		Stack(void);  //contructor
		int Pop(void);            //methods for stack
		void Push(int item1,int item2);
		void ClearStack(void);
		int StackEmpty(void);
		int StackFull(void);
		int CheckType(void);//CheckType is used to check the item is operator or operand
};
Stack::Stack(void):top(-1){}    // constructor
 
int Stack::Pop(void){
	int temp;
 
	if(top==-1){              //for error detection
		cout<<"Stack is Empty";
			 //	exit(1);
	}
	temp=StackList[top];
	top--;
 
	return temp;
}
 
void Stack::Push(int value,int type) //Stack method:Push to enter new items
{
	if(top==MaxStackSize-1){        //for error detection
		cout<<"Stack is Full";
			 //	exit(1);
	}
	top++;
	StackList[top]=value;   // value is entered to Stacklist array
	TypeList[top]=type;    // type is entered to Typelist array
 
}
 
void Stack::ClearStack(void){
	top=-1;
}
 
int Stack::StackEmpty(void){
	  return top==-1;
}
 
int Stack::StackFull(void){
	  return top==MaxStackSize-1;
}
 
int Stack::CheckType(void){    //CheckType is used to check the item is operator or operand
	return TypeList[top];      //returns 1 if it is an operator,returns 0 if it is an operand
}
 
 
int main(void)
{
 
Stack s,s2,s3;
char mystring[MaxStackSize];
char temp[MaxStackSize];
int i,k;
int operand1,operand2;
 
	k=0;
	i=0;
 
	//the first screen for the user
	cout<<"Enter an expression in 'Reverse Polish Notation' to be calculated"<<endl;
	cout<<"Then Press Enter"<<endl<<endl;
	cout<<"Format will be as: a b + c d + * "<<endl;
	cout<<"e.g: 15 12 - 20 13 + *"<<"    (Put a space between each operands and operators)"<<endl<<endl;
	cout<<"-->";
 
	cin.getline(mystring,256); //
 
	while (1)
	{
 
		switch(mystring[i]){   //detects whether the item is an operator or a part of operand.
		  case '+':        // detects an addition operation
			s.Push(1,1);    //the Pushed value for addition is 1
			break;
 
		  case '-':        // detects an substraction operation
			s.Push(2,1);     //the Pushed value for substraction is 2
			break;
 
		  case '*':        // detects an multiplication operation
			s.Push(3,1);     //the Pushed value for  multiplicationis 3
			break;
 
		  case '/':        // detects an division operation
			s.Push(4,1);    //the Pushed value for division  is 4
			break;
 
		  case ' ':       //takes the entered operand to the temp then push it to stack s
			{
				for(int j=k;j>0;j--) //takes the operand until space and put it to the temp
				{
					temp[k-j]=mystring[i-j];
					temp[k]='\0';
				} //for
 
			  if(!(temp[1]=='+' || temp[1]=='-'|| temp[1]=='*'|| temp[1]=='/'))
			  //to prevent a push operation for a space after an operator
 
				s.Push(atoi(temp),0);//convert the string in temp (the operand)and push it to the stack s
 
				k=0;
 
				break;
			 }
 
			case '\0': //put for the NULL in my string(for the last item in the expression)
			{
				for(int j=k;j>0;j--) //takes the operand until NULL and put it to the temp
				{
					temp[k-j]=mystring[i-j];
					temp[k]='\0';
				} //for
 
			  if(!(temp[1]=='+' || temp[1]=='-'|| temp[1]=='*'|| temp[1]=='/'))
           //to prevent a push operation for a NULL after an operator
 
				s.Push(atoi(temp),0);  //convert the string in temp(the operand) and push it to the stack s
 
				k=0;
			}
				break;
 
			default:			break;
 
 
 
		}//switch
 
	  if	(mystring[i]==NULL)
	  break;
 
 
			i++;
			k++;
 }
 
	//used for reversing the stack s and store it to s2
	int temp2;
	while(!s.StackEmpty())
	{
		temp2=s.CheckType();
		s2.Push(s.Pop(),temp2);
	}//while
 
	//s2 is transferred to s3 to calculate the expression
	while(!s2.StackEmpty()) //it leaves out of the while when all the items are popped from s2
	{
		if(!s2.CheckType())      //checks the top item of the s3 stack whether it is a operator
			s3.Push(s2.Pop(),0);     //it pops operands from the top of the stack s3 sends them to the s3
 
		else //if the top of the s2 stack is operator
		{
			operand2=s3.Pop();    //take the top of s3 as operand2
			operand1=s3.Pop();    //take the new top of s3 as operand2
 
			switch(s2.Pop()){  //perform the operator on operands(1:add 2:subtract 3:multiply 4:divide)
			 case 1 :
				s3.Push(operand1+operand2,0);  //put the result of the addition to s3 again
				break;
			 case 2 :
				s3.Push(operand1-operand2,0);   //put the result of the substraction to s3 again
				break;
			 case 3 :
				s3.Push(operand1*operand2,0);   //put the result of the multiplication to s3 again
				break;
			 case 4 :
				s3.Push(operand1/operand2,0);    //put the result of the division to s3 again
				break;
			}//switch
 
		}//else
 
	}//while
 
cout<<endl<<"Result:"<<s3.Pop();//the last item in the s3 stack is the result
 
return (0);
} 

  • projelerim/programlama/cplusplus/reverse_polish_notation.txt
  • Son değiştirilme: 2019/05/08 21:25
  • (Dışarıdan düzenle)