Code Monkey home page Code Monkey logo

infix_to_postfix's Introduction

// In main string profInput; string exp;

// Remove spaces from input string and copies chars into new string location for (int i = 0; i < size.profInput; i ++) int j = 0; if (!isspace(profInput[i])) exp[j] = profInput[i]; j++;

// In header file

// Turns infix notation into postfix notation infix2postfix(stack infix){

stack result;

stack operator; // Class Stack in header file

// Run thru end of expression size for (int i = 0; i < infix.size(); i ++){

// If char is a digit or a decimal, copy char into char array result
if ((infix[i] == isdigit()) || (infix[i] == ‘.’)) 
	result += exp[i];

// If char is an open paren, push it onto the operator stack
else if (exp[i] == ‘(‘ )
	s.push();

// If the char is a close paren,
else if (exp[i] == ‘)’ )
	while (!s.empty())		//  While there are still operators left in the stack,
		if (s.top() == ‘(‘ )		// if the top of the stack is an open paren, 
			s.pop();		// remove the open paren					
			break;		// end the loop
		else 
			result += s.top();	// if the top of the stack is not an open paren, 
							// copy the operator at the top of the stack to the result array
			s.pop();			// delete the operator from the top of the stack


// If the char is one of these operators
else if ((exp[i] == ‘+’)||(exp[i] == ‘-’)||(exp[i] == ‘*’)||(exp[i] == ‘/’)||(exp[i] == ‘^’) )
	// If operator stack is empty, push the operator onto the stack
	if (s.empty())
		s.push(exp[i]);
	// If the stack is not empty, 
	else
		while (!s.empty())
			if (!has_higher_precedence)	// and the char at the top of the stack has lower precedence
				s.push(exp[i]);			// add the new operator
				break;				// exit loop
			else 						// If the char at the top of the stack has equal or higher precedence
				result += s.top();		// copy the operator to the result array		
				s.pop();				// delete the operator from the stack		
				s.push(exp[i]);			// add new operator to stack

}

// While the stack is not empty add the operators from the stack // to the result array. // Delete the operator from the array once it has been copied. // Continue until operator stack is empty. while (!s.empty()) { result += s.top(); s.pop(); }

// Determine if operator at the top of the stack has higher precedence // compared to the operator currently being looked at in the expression bool has_higher_precedence (char s.top(), char operator) { int x, y; if (s.top() == ‘(‘) // not a thing return true; if (s.top() == ^) x = 2; if ((s.top() == ‘ * ‘) || (s.top() == ‘/ ‘)) x = 1; if ((s.top() == ‘ + ‘) || (s.top() == ‘- ‘)) x = 0

if (operator == ‘(‘) return false; if (operator == ^) y = 2; if ((operator == ‘ * ‘) || (operator == ‘/ ‘)) y = 1; if ((operator == ‘ + ‘) || (operator == ‘- ‘)) y = 0

if(x > y) return true; else return false;

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.