void Postfix_Evaluator_Strategy::Build_Expression_Strategy()
{
	bool quit=false;
	bool converted=false;
	std::ifstream Exprfile;
	Exprfile.open("testExpressions.txt");

	clock_t t;
	t=clock();
	do
	{
		Array<Command *> postfix;
		
		std::string infix;
		
		Stack_Expr_Command_Factory factory;
		
		try
		{
			quit = false;
			int result=0;
			
			getline(Exprfile, infix);
			if (infix.compare("QUIT") == 0)
			{
				quit = true;
				break;
			}
			//call infix_to_postfix
			converted = infix_to_postfix(infix, factory, postfix);
			if(converted)
			{
				Array_Iterator<Command *> arr_iter(postfix);
		
				if(evaluate_postfix(arr_iter, result))
					std::cout <<"\nExpression Value : " <<result;
				
			}
			else
			{
				std::cout<<"\nInvalid expression";
			}		
		}
		catch(std::exception &e)
		{
			std::cout<<e.what()<<"\n";			
		}
		
	}
	while(!quit);

	t= (float)(clock()-t)/(CLOCKS_PER_SEC/1000);
	cout<<"\n Time : "<<t<<" millisecs";
	Exprfile.close(); 
}
int main( int argc, char* argv[] )
{
    char* string;
    string = malloc(100*sizeof(char));
    int answer;
    fgets(string, 100*sizeof(char), stdin);
    answer = evaluate_postfix(string);
   // printf("%d",answer);

    return 0;
}
Esempio n. 3
0
int main(void) {
	while(fgets(buf, MAX_LEN, stdin) != NULL) {
		buf[strcspn(buf, "\n")] = '\0';
		if(!strncmp(buf, "\0", 1)) continue;
		if(!strncmp(buf, "0\0", 2)) break;
		if(match_parentheses(buf)) { printf("Incorrectly matched parentheses\n"); continue;}
		normalize_exp(buf);
		if(infix2postfix(buf)) continue;
		printf("%d\n", evaluate_postfix(buf));
	}
}
Esempio n. 4
0
/* returns the final answer */
double evaluate(const char * str) {
	char * strbuffer; /* mutable buffer for string (modified in calls to strtok()) */
	double ans; /* answer to return */
	struct token_queue queue_infix, queue_postfix;

	/* copy str into mutable buffer */
	strbuffer = strcpy((char *)malloc(strlen(str)+1),str);

	/* get queue of tokens in infix order from string buffer */
	queue_infix = expr_to_infix(strbuffer);

	/* get queue of tokens in postfix order from infix-ordered queue */
	queue_postfix = infix_to_postfix(&queue_infix);

	/* get answer from postfix-ordered queue */
	ans = evaluate_postfix(&queue_postfix);

	free(strbuffer); /* free memory from heap */
	return ans;
}