Ejemplo n.º 1
0
 /**
  * @param expression: a vector of strings;
  * @return: an integer
  */
 int evaluateExpression(vector<string> &expression) {
     if (expression.empty()) {
         return 0;
     }
     vector<string> postfix;
     infixToPostfix(expression, postfix);
     return evaluatePostfixExpression(postfix);
 }
Ejemplo n.º 2
0
int main(){
  char charArray[50];
  printf("Print expression: ");
  gets(charArray);
  int answer = evaluatePostfixExpression(charArray);
  printf("The value of the expression is: %d\n", answer);
  return 0;
}
Ejemplo n.º 3
0
int main(void)
{
	char infix[50] = {'(','3','-','1',')','*','8','+','2','\0'};
	char infix2[50];
	char postfix[50] = {'\0'};
	int i = 0, result = 0;
	char cont = 'y';

	while (toupper(cont) == 'Y')
	{
		//Clear infix2
		for(i = 0; i < 50; i++)
		{
			infix2[i] = '\0';
		}
		//Clear postfix
		for(i = 0; i < 50; i++)
		{
			postfix[i] = '\0';
		}

		//Get expression from user
		_flushall();
		printf("Enter an expression to convert to postfix (no spaces): ");
		gets(infix2);

		//Convert to postfix
		convertToPostfix(infix2, postfix);

		//Print postfix expression
		printf("\nPostfix: ");
		i = 0;
		while(postfix[i] != '\0')
		{
			printf("%c ", postfix[i]);
			i++;
		}

		//Calculate result using postfix expression
		result = evaluatePostfixExpression(postfix);

		//Print result
		printf("\nResult: %d\n", result);
		do{
			_flushall();
			printf("\nWould you like to continue? <Y or N>: ");
			scanf("%c", &cont);
			if(toupper(cont) != 'Y' && toupper(cont) != 'N')
			{
				printf("Invalid option, please choose Y or N\n");
			}
		}while(toupper(cont) != 'Y' && toupper(cont) != 'N');
	}
	return 0;
}
Ejemplo n.º 4
0
int main(void)
{
	string postfix("62+5*84/-");
	string postfix2("562*+9/");
	int result = 0;
	Stack stack1;

	result = evaluatePostfixExpression(postfix2, stack1);
	cout << result << endl;

	
	
	return 0;
}
Ejemplo n.º 5
0
int computePostfix(char* postfix){
	return evaluatePostfixExpression(postfix);;
}