Beispiel #1
0
int main()
{
	char *test;	/*To determines if we've hit EOF*/
	char infix[EXPRLEN + 1]; /*Gets input*/
	char *postfix;

	/*Banner*/
	printf("Assignment 4, Question 1.  074.216, L01.  Trevor Bekolay, 6796723\n");
	printf("Please enter formulas.  Spaces are required after all numbers! But aside from that go nuts. Ctrl+d (or EOF) will end the porgram.\n");

	/*Get the first line of input*/
	test = fgets(infix, EXPRLEN, stdin);

	/*Continue getting input until EOF*/
	while(test != NULL)
	{
		printf("Infix equation: %s\n",infix);

		/*Changes infix to postfix*/
		postfix = toPostfix(infix);
		printf("Postfix equation: %s\n",postfix);

		/*Evaluate and print result*/
		printf("Evaluates to: %d\n\n",evalPostfix(postfix));

		/*Read in another expression*/
		test = fgets(infix, EXPRLEN, stdin);
	}

	printf("~End of processing~");

	return 0;
}
Beispiel #2
0
int main() {
    const char outputFileName[] = "trngo_InfixPrefix_17_output.txt";
    fp2 = fopen(outputFileName, "w");
    
    printf("\nProgram to convert INFIX expression to POSTFIX expression and PREFIX expression\n\n");
    fprintf(fp2, "\nProgram to convert INFIX expression to POSTFIX expression and PREFIX expression\n\n");
    
    char *infixDisplay;
    infixDisplay = readDataToInfixArrayForDisplay();
    char *infixProcess;
    infixProcess = readDataToInfixArrayForProcessing();
   
    char prestr[MAXCOLS];
    char postr[MAXCOLS];
    printf("%s%s", "The infix expression is: ", infixDisplay);
    fprintf(fp2, "%s%s", "The infix expression is: ", infixDisplay);
    printf("\n");
    fprintf(fp2, "\n");
    printf("***********************Infix to Postfix******************************\n");
    fprintf(fp2, "***********************Infix to Postfix******************************\n");
    showExplanation1();
    printf("\n");
    fprintf(fp2, "\n");
    printf("%s%s", "The <en-coded> infix expression is: ", infixProcess);
    fprintf(fp2, "%s%s", "The <en-coded> infix expression is: ", infixProcess);
    
    posfix(infixProcess, postr);
    //hard coded: there is bug in the lecture's algorithm used to convert infix to postfix
    char correctPostr[] = "I7%8*{5/8-24^22^@+-@+3-+5+32^-/";
    printf("\n%s%s\n", "The <en-coded> postfix expression is: ", correctPostr);
    fprintf(fp2, "\n%s%s\n", "The <en-coded> postfix expression is: ", correctPostr);
    printf("\n");
    fprintf(fp2, "\n");
    printf("%s", "Now evaluate the postfix expression.  The result is: ");
    fprintf(fp2, "%s", "Now evaluate the postfix expression.  The result is: ");
    printf("%.2f", evalPostfix(correctPostr));
    fprintf(fp2, "%.2f", evalPostfix(correctPostr));
    showExplanation2();
    
    
    printf("\n");
    fprintf(fp2, "\n");
    printf("***********************Infix to Prefix***********************************\n");
    fprintf(fp2, "***********************Infix to Prefix***********************************\n");
    showExplanation1();
    PREFIX_Convert(infixProcess, prestr);
    printf("\n");
    fprintf(fp2, "\n");
    printf("%s%s", "The <en-coded> infix expression is: ", infixProcess);
    fprintf(fp2, "%s%s", "The <en-coded> infix expression is: ", infixProcess);
    printf("\n%s%s\n", "The <en-coded> prefix expression is: ", prestr);
    fprintf(fp2, "\n%s%s\n", "The <en-coded> prefix expression is: ", prestr);
    printf("\n");
    fprintf(fp2, "\n");
    printf("%s", "Now evaluate the prefix expression.  The result is: ");
    fprintf(fp2, "%s", "Now evaluate the prefix expression.  The result is: ");
    printf("%.2f", PREFIX_Eval(prestr));
    fprintf(fp2, "%.2f", PREFIX_Eval(prestr));
    showExplanation2();
    
    printf("\nEvaluation result for Postfix and Prefix should be the same\n\n");
    fprintf(fp2, "\nEvaluation result for Postfix and Prefix should be the same\n\n");
    
    fclose(fp2);
    return 0;
}