<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Need some help while implementing RPN]]></title><description><![CDATA[<p dir="auto">I tried to implement this algorithm on c++. I tried almost 8 hrs. 2 hrs for understanding algorithm ,that it is very hard . But that was easy. And almost roughly 6 hours of coding.<br />
I want to implement this in plain C++. And can use <code>deque</code> and <code>vector</code><br />
And this is not my homework or any task!!!  I am just trying to learn something extra since I don't have machine to run higher stuff like QT</p>
<p dir="auto">I haven't visited any implementation before this of the same thing. I tried to implement it on JavaScript at first and I <strong>completed</strong> it but couldn't complete this in c++</p>
<p dir="auto">If you are free then try compiling this</p>
<p dir="auto">some <strong>cout</strong> are just for debugging purpose and <strong>try catch</strong> blocks are also for the same. I got tired of seeing this thing so posting here</p>
<p dir="auto">ALSO rate my coding style out of 10<br />
LOL</p>
<pre><code>#include &lt;iostream&gt;
#include&lt;vector&gt;
#include &lt;string&gt;
#include &lt;string.h&gt;
#include&lt;algorithm&gt;
#include&lt;cmath&gt;
#include&lt;map&gt;
#include&lt;deque&gt;

using  std::cout,std::cin, std::string, std::endl;
// THIS MUST BE COMPILED WITH C++17, Because comma separated value are supported with it
// g++ main.cpp -std=c++17

#define LEFT_TO_RIGHT  "LEFT_TO_RIGHT"
#define RIGHT_TO_LEFT  "RIGHT_TO_LEFT"
// can use const too

std::map&lt;string,int&gt; precedenceDeclaration;
std::map&lt;string,string&gt; associativeDeclaration;

void fillFirst(){
	//
	precedenceDeclaration.insert(std::make_pair("(",0));
	precedenceDeclaration.insert(std::make_pair("[",0));
	precedenceDeclaration.insert(std::make_pair("{",0));
	precedenceDeclaration.insert(std::make_pair("+",1));
	precedenceDeclaration.insert(std::make_pair("-",1));
	precedenceDeclaration.insert(std::make_pair("/",2));
	precedenceDeclaration.insert(std::make_pair("*",2));
	precedenceDeclaration.insert(std::make_pair("%",2));
	precedenceDeclaration.insert(std::make_pair("^",3));
	precedenceDeclaration.insert(std::make_pair(")",4));
	precedenceDeclaration.insert(std::make_pair("}",4));
	precedenceDeclaration.insert(std::make_pair("]",4));

	associativeDeclaration.insert(std::make_pair("+",LEFT_TO_RIGHT));
	associativeDeclaration.insert(std::make_pair("-",LEFT_TO_RIGHT));
	associativeDeclaration.insert(std::make_pair("*",LEFT_TO_RIGHT));
	associativeDeclaration.insert(std::make_pair("/",LEFT_TO_RIGHT));
	associativeDeclaration.insert(std::make_pair("%",LEFT_TO_RIGHT));
	associativeDeclaration.insert(std::make_pair("^",RIGHT_TO_LEFT));

}
int precedence(string op){
	if(precedenceDeclaration.count(op) &lt; 1){
		throw "character is invalid for precedence";
	}
	return precedenceDeclaration[op];
}
std::string associativity(string toFind){
	if(associativeDeclaration.count(toFind) &lt; 1){
		throw "Character is invalid for associativity";
	}
	return associativeDeclaration[toFind];
}
bool isNumber(string value){
	for(auto v : value){
		if(std::isdigit(v) == false){
			return false;
		}
	}
	return true;
}
std::string last(std::deque&lt;string&gt; arr){
	if(arr.size() == 0 ){
		throw "Cannot get the last element of empty structure";
		// throw 10;
	}

	return arr.at(arr.size() - 1);
}
bool isValidCharacter(string value){
	try{
		if(precedence(value) &gt;= 0){
			return true;
		}
	}catch(string err){
		std::cout&lt;&lt;err;
		std::cout&lt;&lt;"The character is not valid character!!!!!!!!!!";
	}

	return false;
}
bool isValidOperator(string value){
	try{
		if(precedence(value) &gt; 0 &amp;&amp; precedence(value) &lt; 4){
			return true;
		}
	}catch(string err){
		std::cout&lt;&lt;err;
		std::cout&lt;&lt;"The character is not valid operator!!!!!1";
	}

	return false;
}
float convertToNumber(string str){
	return atof(str.c_str());
}
string convertToStringS(char s){
	return string({s});
}
float evaluateOperation(string op1 , char operatorInput, string op2){
	float operand1 = convertToNumber(op1);
	float operand2 = convertToNumber(op2);

	switch(operatorInput){
		case '+':
		 return operand1 + operand2;
		 break;
		case '-':
			return operand1 - operand2;
		break;
	}

	return 500;
}
template &lt;typename Data&gt; void display(Data val){
	std::cout&lt;&lt;"\n{ ";
	for(auto v:val){
		std::cout&lt;&lt;" '"&lt;&lt;v&lt;&lt;"',";
	}
	std::cout&lt;&lt;" }";
}

void RPN(string equation);


int main(){

	fillFirst();
	string operation = "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3";
	RPN(operation);
}
/*
[
  '3', '4', '2', '*',
  '+', '1', '5', '-',
  '2', '3', '^', '^',
  '/'
]

*/


void RPN(string equation){
	std::deque&lt;std::string&gt; stack,output;


	for(auto eq:equation){
		string currentCharacter = convertToStringS(eq);
		if(currentCharacter == " " || currentCharacter == "."){
			continue;
		}

		if(isNumber(currentCharacter)){
			output.push_back(currentCharacter);
		}else if(isValidCharacter(currentCharacter)){
			std::cout&lt;&lt;"\nOperator = "&lt;&lt;currentCharacter&lt;&lt;std::endl;
			int currentPrecedence = precedence(currentCharacter);

			if(currentPrecedence == 0  ){
				output.push_back(currentCharacter);
			}else if(currentPrecedence == 4){
				
				while(precedence(last(stack)) != 0){
					string lastStack = last(stack);
					stack.pop_back();
					output.push_back(lastStack);
					if(stack.size() == 0 ){
						break;
					}
				}				
				if(stack.size() != 0){stack.pop_back(); display(stack);}
			}

			auto performBack = [&amp;](){
				if(stack.size() != 0){
				while(currentPrecedence &lt; precedence(last(stack))){
				std::cout&lt;&lt;"inside performBack "&lt;&lt;std::endl;
				display(stack);
				display(output);
					if(stack.size() == 0 )
						break;

					string lastWord = last(stack);
					output.push_back(lastWord);
					stack.pop_back();
				}
			}
				stack.push_back(currentCharacter);
			};

/////////////////////////////////////////////////////////////////////////////////////////////////////////
		THAT THROW IS JUST FOR CHECKING!!!!!!!!!!!!!!!!
			if(isValidOperator(currentCharacter)){
				if(stack.size() == 0 ){
					stack.push_back(currentCharacter);
				}else if(currentPrecedence &gt; precedence(last(stack)) || precedence(last(stack)) == 0){
					stack.push_back(currentCharacter);
				}else if(currentPrecedence &lt; precedence(last(stack))){
					performBack();
					/*
					while(stack.size() != 0 || currentPrecedence &gt; precedence(last(stack))){
						string lastWord = last(stack);
						output.push_back(lastWord);
						stack.pop_back();
					}
					*/
				}else if(currentPrecedence == precedence(last(stack))){
					
					if(associativity(currentCharacter) == LEFT_TO_RIGHT){
						performBack();
					}else if(associativity(currentCharacter) == RIGHT_TO_LEFT){
						stack.push_back(currentCharacter);
					}
					
				}
			}

		}

	display(output);
	display(stack);
	}
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/129816/need-some-help-while-implementing-rpn</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 22:59:09 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/129816.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 27 Aug 2021 15:05:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Need some help while implementing RPN on Sat, 28 Aug 2021 04:44:40 GMT]]></title><description><![CDATA[<p dir="auto">Hello guys<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  <a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a>  <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>  <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>  @AnneRanch<br />
I hope to get some thoughts on this!</p>
]]></description><link>https://forum.qt.io/post/678173</link><guid isPermaLink="true">https://forum.qt.io/post/678173</guid><dc:creator><![CDATA[Thank You]]></dc:creator><pubDate>Sat, 28 Aug 2021 04:44:40 GMT</pubDate></item></channel></rss>