Click here to Skip to main content
15,843,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i have this program >>i want to add this two Operator (^ and %) but i dont know how
this is the program its work when i dont write in the string post_exp (% or ^) but i want to write them pls help me

What I have tried:

C++
// CPP Program to convert postfix to prefix
#include <iostream>
#include <stack>
using namespace std;

// function to check if character is operator or not
bool isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
case '^':
case '%':
	return true;
}
return false;
}

// Convert postfix to Prefix expression
string postToPre(string post_exp) {
stack<string> s;

// length of expression
int length = post_exp.size();

// reading from right to left
for (int i = 0; i < length; i++) {

	// check if symbol is operator
	if (isOperator(post_exp[i])) {

	// pop two operands from stack
	string op1 = s.top();
	s.pop();
	string op2 = s.top();
	s.pop();
	

	// concat the operands and operator
	string temp = post_exp[i] + op2 + op1;

	// Push string temp back to stack
	s.push(temp);
	}

	// if symbol is an operand
	else {

	// push the operand to the stack
	s.push(string(1, post_exp[i]));
	}
}

// stack[0] contains the Prefix expression
return s.top();
}

// Driver Code
int main() {
string post_exp = "A%BC/-AK/L-*^";
cout << "Prefix : " << postToPre(post_exp);
return 0;
}
Posted
Updated 23-Jun-18 4:56am
v2
Comments
Patrice T 23-Jun-18 5:39am    
And you have a problem or a question ?
كايو تشان 23-Jun-18 9:07am    
problem
Patrice T 23-Jun-18 10:20am    
but we have to guess
CPallini 23-Jun-18 9:09am    
What is the problem? Could you please elaborate?
كايو تشان 23-Jun-18 11:53am    
the problem is that i don't know how put power(^) and Modulo(%) in this program i tried but it did not work

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900