Exemplo n.º 1
0
int main(int argc, const char * argv[])
{
    string infix,postfix;
    double result;
    cout<<"Enter an infixed expression:- ";
    cin>>infix;
    postfix=InfixToPostfix(infix);
    cout<<"\nPostfix expression:- "<<postfix;
    result=postfixEvaluation(postfix);
    cout<<"\nResult evaluation:- "<<result;
    return 0;
}
Exemplo n.º 2
0
int mybc()
{
	char infix[SIZE];
	int i,j=0;
	push('#');
	gets(infix); //Scan the infix expression
	
	while(strcmp(infix,"quit")) //Scan for infix expressions and convert them to postfix expressions until the user wants to quit.
	{
		//Convert the infix expression to postfix.
		for(i=0;infix[i]!='\0';i++)
		{
			if(isalnum(infix[i])) //If the current symbol is a digit or an alphabet, then add it to the postfix expression.
				sufix[j++]=infix[i];
			else if(infix[i]=='(') //If the current symbol is a (, then push it onto the stack.
				push(infix[i]);
			else if(infix[i]==')') //If the current symbol os a ), then pop all the elements from the stack and add them to the postfix until ( is found.
			{
				while(s[top]!='(')
					sufix[j++]=pop();
				pop();
			}
			else //If the current symbol is an operator, then pop all the elements from the stack which have a priority greater than the the current symbol.
			{
				while(prior(s[top])>=prior(infix[i])) 
					sufix[j++]=pop();
				push(infix[i]);
			}
		}
		while(s[top]!='#') //Pop all the elements from the stack.
			sufix[j++]=pop();
		sufix[j]='\0';

		postfixEvaluation(); //Evaluate the postfix expression.
		j=0;
		top=-1;
		push('#');
		gets(infix);
	}
	
	return 0;
}
Exemplo n.º 3
0
/*************************************************************
 * Function: pa3_main ()
 * Created: February 6th, 2012
 * Last Revised: February 10th, 2012
 * Description: This function runs an evaluation/conversion
 * Input parameters: whether the expression should be saved
 * Returns: N/A
 * Preconditions: an expression needs to be converted/evaluated
 * Postconditions: an expression has been converted/evaluated
 *************************************************************/
void pa3_main (Bool isSaving)
{
	FILE *outFile = NULL;
	char infixString[128] = "", infixBackup[128],
		postfixString[128], postfixBackup[128];
	int endValue = 0.0;
	Bool success = false;

	// flush the buffer (just in case)
	_flushall ();
	fflush(stdin);

	// ask/get for an infix expression
	printf ("Expression to evaluate (positive integers, infix format)? ");
	while (!isdigit (infixString[0]))
	{
		gets (infixString);
	}
	printf ("Converting..");

	// make a backup copy of the inputted expression
	strcpy (infixBackup, infixString);
	printf (".");

	// convert infix to postfix
	success = infixConvertor (infixString, postfixString);
	if (success)
	{
		printf (".");

		// make a backup copy of the postfix expression
		strcpy (postfixBackup, postfixString);
		printf (".");

		// evaluate the expression
		printf (".Converted...Evaluating..");
		endValue = postfixEvaluation (&postfixString[0]);
		printf (".Evaluated!\n\n");

		// print the data
		printf ("%s = %s = %d\n", infixBackup, postfixBackup, endValue);

		// perform any necessary file operations if necessary
		if (isSaving)
		{
			// open the save file (or create one if necessary)
			outFile = fopen ("expressions.txt", "a");

			// print expression data
			fprintf (outFile, "%s = %s = %d\n", infixBackup, postfixBackup, endValue);

			// print that data has been saved
			printf ("Expression data has been saved!\n");

			// close file
			fclose (outFile);
		}
	}
	else
	{
		// if something went wrong, say so
		printf ("Failed to evaluate expression!\n");
	}
	// print a new line
	printf ("\n");
	// pause the screen
	pause_clear (true, false);
}