void main ( ) { char infix [30]; printf ("\n Enter the infix expression:\n"); gets (infix); infix_to_postfix (infix); }
int main() { printf(HELP_TEXT); char infix[MAX_STRING]; do { printf(">>> "); fgets(infix, MAX_STRING, stdin); /* Exit input loop if user enters 'quit' */ if (strcmp(infix, "quit\n") == 0) { break; } char postfix[2 * MAX_STRING]; infix_to_postfix(infix, postfix); double result = evaluate(postfix); if (isnan(result)) { printf("ERROR: expression is not well formed\n"); } else { printf("postfix: %s\n", postfix); if (!isfinite(result)) { printf("division by 0 occured\n"); } else { printf("%lg\n", result); } } } while (1); return 0; }
int main() { char string[50] = {0}; int str_length; int i; puts("--Exercise 5.1 : numerical expression calculator----------"); puts("examples)"); puts(" * 3*4"); puts(" * 9+2-5"); puts(" * (5*4)/2"); puts(" * (1+2+3)*(4+5)"); while (1) { puts("Enter 'q' if you want to exit."); printf("input: "); scanf("%s", string); fflush(stdin); if (string[0] == 'q') break; infix_to_postfix(string); printf("in postfix: %s\n", string); printf("result: %d\n\n", eval(string)); } return 0; }
// function to evaluate expression float evaluate(char *exp, int value){ int exp_length = strlen(exp); //evaluate postfix function infix_to_postfix(exp); //printf("%s\n",postfix); int i=0,to=-1; float result; float arr[100]; int length = strlen(postfix); while(length > 0){ if(postfix[i] =='+' || postfix[i] =='-' || postfix[i] =='*' || postfix[i] =='/' || postfix[i] =='^'){ float secondOperand=arr[to--]; float firstOperand=arr[to--]; char operator=postfix[i]; result = computation(firstOperand,operator,secondOperand); //printf("result%f\n",result); arr[++to] = result; i++; } else{ if(postfix[i] == 'x' || postfix[i] == 'y') arr[++to]= value; else arr[++to] = postfix[i] - '0'; i++; } length--; } return arr[to]; }
int main(void) { char buf[50] = "3+4*2/(1-5)"; char out[50] = {0}; infix_to_postfix(buf, out, strlen(buf)); return 0; }
int main(void) { char inf[] = "2*682-3*2+4/2", new_arr[100] = {0}; infix_to_postfix(inf, new_arr, 100); printf("%s\n", new_arr); printf("%d\n", parse_postfix(new_arr)); return 0; }
float evaluate_exp(const char *exp) { char str[INPUT_SIZE]; float result; infix_to_postfix(str, exp); result = parse_postfix(str); return result; }
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(); }
main() { long int value; top=-1; printf("Enter infix : "); gets(infix); infix_to_postfix(); printf("Postfix : %s\n",postfix); value=eval_post(); printf("Value of expression : %ld\n",value); }/*End of main()*/
Token Instruction::eval(const std::string& expression) { // 1. Split string into a token list tokenization(expression); // 2. Convert tokens from infix notation to postfix notation infix_to_postfix(); // 3. Eval postfixed expression return eval_postfix(); }
int main() { long int value; top=-1; clrscr(); printf("Enter the postfix expression\n"); gets(infix); infix_to_postfix(); printf("Postfix =%s\n",postfix); value=eval_post(); printf("Value of expression:%ld\n",value); return 0; }
int main() { int t, i; scanf("%d", &t); for (i = 0; i < t; i++) { char e[1000000]; scanf("%s", e); int l; l = strlen(e); char r[l]; printf("%s\n",infix_to_postfix(e,l,r)); } return 0; }
int main(){ char infix[MAXN]; char postfix[MAXN]; setPres('+', 1); setPres('-', 1); setPres('*', 2); setPres('/', 2); while(scanf(" %s", infix) == 1){ printf("Infix: [%s] ", infix); infix_to_postfix(infix, postfix); printf("Postfix: [%s]\n", postfix); } return 0; }
int main (int argc, char *argv[]) { int result; if (argc != 2) { printf ("usage: ./infix_postfix_calculations \"expression\"\n"); return 1; } char * expr = argv[1]; char * postfix = infix_to_postfix (expr); printf ("postfix: %s\n", postfix); result = postfix_evaluation (postfix); printf ("result: %d\n", result); return 0; }
int main() { int testnos, len; char ch; scanf("%d\n", &testnos); while (testnos--) { stack_pointer = -1; len = 0; ch = '\n'; while(scanf("%c", &ch) > 0 && ch != '\n') { expr[len++] = ch; } infix_to_postfix(len); //printbuffer(len); } return 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; }
static int papi_load_derived_events (char *pmu_str, int pmu_type, int cidx, int preset_flag) { SUBDBG( "ENTER: pmu_str: %s, pmu_type: %d, cidx: %d, preset_flag: %d\n", pmu_str, pmu_type, cidx, preset_flag); char pmu_name[PAPI_MIN_STR_LEN]; char line[LINE_MAX]; char name[PATH_MAX] = "builtin papi_events_table"; char *event_file_path=NULL; char *event_table_ptr=NULL; int event_type_bits = 0; char *tmpn; char *tok_save_ptr=NULL; FILE *event_file = NULL; hwi_presets_t *results=NULL; int result_size = 0; int *event_count = NULL; int invalid_event; int line_no = 0; /* count of lines read from event definition input */ int derived = 0; int res_idx = 0; /* index into results array for where to store next event */ int preset = 0; int get_events = 0; /* only process derived events after CPU type they apply to is identified */ int found_events = 0; /* flag to track if event definitions (PRESETS) are found since last CPU declaration */ #ifdef PAPI_DATADIR char path[PATH_MAX]; #endif if (preset_flag) { /* try the environment variable first */ if ((tmpn = getenv("PAPI_CSV_EVENT_FILE")) && (strlen(tmpn) > 0)) { event_file_path = tmpn; } /* if no valid environment variable, look for built-in table */ else if (papi_events_table) { event_table_ptr = papi_events_table; } /* if no env var and no built-in, search for default file */ else { #ifdef PAPI_DATADIR sprintf( path, "%s/%s", PAPI_DATADIR, PAPI_EVENT_FILE ); event_file_path = path; #else event_file_path = PAPI_EVENT_FILE; #endif } event_type_bits = PAPI_PRESET_MASK; results = &_papi_hwi_presets[0]; result_size = PAPI_MAX_PRESET_EVENTS; event_count = &_papi_hwd[cidx]->cmp_info.num_preset_events; } else { if ((event_file_path = getenv( "PAPI_USER_EVENTS_FILE" )) == NULL ) { SUBDBG("EXIT: User event definition file not provided.\n"); return PAPI_OK; } event_type_bits = PAPI_UE_MASK; results = &user_defined_events[0]; result_size = PAPI_MAX_USER_EVENTS; event_count = &user_defined_events_count; } // if we have an event file pathname, open it and read event definitions from the file if (event_file_path != NULL) { if ((event_file = open_event_table(event_file_path)) == NULL) { // if file open fails, return an error SUBDBG("EXIT: Event file open failed.\n"); return PAPI_ESYS; } strncpy(name, event_file_path, sizeof(name)-1); name[sizeof(name)-1] = '\0'; } else if (event_table_ptr == NULL) { // if we do not have a path name or table pointer, return an error SUBDBG("EXIT: Both event_file_path and event_table_ptr are NULL.\n"); return PAPI_ESYS; } /* copy the pmu identifier, stripping commas if found */ tmpn = pmu_name; while (*pmu_str) { if (*pmu_str != ',') *tmpn++ = *pmu_str; pmu_str++; } *tmpn = '\0'; /* at this point we have either a valid file pointer or built-in table pointer */ while (get_event_line(line, event_file, &event_table_ptr)) { char *t; int i; // increment number of lines we have read line_no++; t = trim_string(strtok_r(line, ",", &tok_save_ptr)); /* Skip blank lines */ if ((t == NULL) || (strlen(t) == 0)) continue; /* Skip comments */ if (t[0] == '#') { continue; } if (strcasecmp(t, "CPU") == 0) { if (get_events != 0 && found_events != 0) { SUBDBG( "Ending event scanning at line %d of %s.\n", line_no, name); get_events = 0; found_events = 0; } t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) { PAPIERROR("Expected name after CPU token at line %d of %s -- ignoring", line_no, name); continue; } if (strcasecmp(t, pmu_name) == 0) { int type; SUBDBG( "Process events for PMU %s found at line %d of %s.\n", t, line_no, name); t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) { SUBDBG("No additional qualifier found, matching on string.\n"); get_events = 1; } else if ((sscanf(t, "%d", &type) == 1) && (type == pmu_type)) { SUBDBG( "Found CPU %s type %d at line %d of %s.\n", pmu_name, type, line_no, name); get_events = 1; } else { SUBDBG( "Additional qualifier match failed %d vs %d.\n", pmu_type, type); } } continue; } if ((strcasecmp(t, "PRESET") == 0) || (strcasecmp(t, "EVENT") == 0)) { if (get_events == 0) continue; found_events = 1; t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) { PAPIERROR("Expected name after PRESET token at line %d of %s -- ignoring", line_no, name); continue; } SUBDBG( "Examining event %s\n", t); // see if this event already exists in the results array, if not already known it sets up event in unused entry if ((res_idx = find_event_index (results, result_size, t)) < 0) { PAPIERROR("No room left for event %s -- ignoring", t); continue; } // add the proper event bits (preset or user defined bits) preset = res_idx | event_type_bits; SUBDBG( "Use event code: %#x for %s\n", preset, t); t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) { // got an error, make this entry unused papi_free (results[res_idx].symbol); results[res_idx].symbol = NULL; PAPIERROR("Expected derived type after PRESET token at line %d of %s -- ignoring", line_no, name); continue; } if (_papi_hwi_derived_type(t, &derived) != PAPI_OK) { // got an error, make this entry unused papi_free (results[res_idx].symbol); results[res_idx].symbol = NULL; PAPIERROR("Invalid derived name %s after PRESET token at line %d of %s -- ignoring", t, line_no, name); continue; } /****************************************/ /* Have an event, let's start assigning */ /****************************************/ SUBDBG( "Adding event: %s, code: %#x, derived: %d results[%d]: %p.\n", t, preset, derived, res_idx, &results[res_idx]); /* results[res_idx].event_code = preset; */ results[res_idx].derived_int = derived; /* Derived support starts here */ /* Special handling for postfix and infix */ if ((derived == DERIVED_POSTFIX) || (derived == DERIVED_INFIX)) { t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) { // got an error, make this entry unused papi_free (results[res_idx].symbol); results[res_idx].symbol = NULL; PAPIERROR("Expected Operation string after derived type DERIVED_POSTFIX or DERIVED_INFIX at line %d of %s -- ignoring", line_no, name); continue; } // if it is an algebraic formula, we need to convert it to postfix if (derived == DERIVED_INFIX) { SUBDBG( "Converting InFix operations %s\n", t); t = infix_to_postfix( t ); results[res_idx].derived_int = DERIVED_POSTFIX; } SUBDBG( "Saving PostFix operations %s\n", t); results[res_idx].postfix = papi_strdup(t); } /* All derived terms collected here */ i = 0; invalid_event = 0; results[res_idx].count = 0; do { t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ((t == NULL) || (strlen(t) == 0)) break; if (strcasecmp(t, "NOTE") == 0) break; if (strcasecmp(t, "LDESC") == 0) break; if (strcasecmp(t, "SDESC") == 0) break; SUBDBG( "Adding term (%d) %s to derived event %#x, current native event count: %d.\n", i, t, preset, results[res_idx].count); // show that we do not have an event code yet (the component may create one and update this info) // this also clears any values left over from a previous call _papi_hwi_set_papi_event_code(-1, -1); // make sure that this term in the derived event is a valid event name // this call replaces preset and user event names with the equivalent native events in our results table // it also updates formulas for derived events so that they refer to the correct native event index if (is_event(t, results[res_idx].derived_int, &results[res_idx], i) == 0) { invalid_event = 1; PAPIERROR("Error finding event %s, it is used in derived event %s", t, results[res_idx].symbol); break; } i++; } while (results[res_idx].count < PAPI_EVENTS_IN_DERIVED_EVENT); /* preset code list must be PAPI_NULL terminated */ if (i < PAPI_EVENTS_IN_DERIVED_EVENT) { results[res_idx].code[results[res_idx].count] = PAPI_NULL; } if (invalid_event) { // got an error, make this entry unused papi_free (results[res_idx].symbol); results[res_idx].symbol = NULL; continue; } /* End of derived support */ // if we did not find any terms to base this derived event on, report error if (i == 0) { // got an error, make this entry unused papi_free (results[res_idx].symbol); results[res_idx].symbol = NULL; PAPIERROR("Expected PFM event after DERIVED token at line %d of %s -- ignoring", line_no, name); continue; } if (i == PAPI_EVENTS_IN_DERIVED_EVENT) { t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); } // if something was provided following the list of events to be used by the operation, process it if ( t!= NULL && strlen(t) > 0 ) { do { // save the field name char *fptr = papi_strdup(t); // get the value to be used with this field t = trim_note(strtok_r(NULL, ",", &tok_save_ptr)); if ( t== NULL || strlen(t) == 0 ) { papi_free(fptr); break; } // Handle optional short descriptions, long descriptions and notes if (strcasecmp(fptr, "SDESC") == 0) { results[res_idx].short_descr = papi_strdup(t); } if (strcasecmp(fptr, "LDESC") == 0) { results[res_idx].long_descr = papi_strdup(t); } if (strcasecmp(fptr, "NOTE") == 0) { results[res_idx].note = papi_strdup(t); } SUBDBG( "Found %s (%s) on line %d\n", fptr, t, line_no); papi_free (fptr); // look for another field name t = trim_string(strtok_r(NULL, ",", &tok_save_ptr)); if ( t== NULL || strlen(t) == 0 ) { break; } } while (t != NULL); } (*event_count)++; continue; } PAPIERROR("Unrecognized token %s at line %d of %s -- ignoring", t, line_no, name); } if (event_file) { fclose(event_file); } SUBDBG("EXIT: Done processing derived event file.\n"); return PAPI_OK; }
/*---IT USES LIST FOR KEEPING TOKENS---*/ int add_var(char exp[],int ret) { int i=0; float eval_ans; //to store answer of evaluation expression char eval[20]; char var_name[MAXNAME]; //VARIABLE NAME SHOULD NOT EXCEED MAXNAME. List infix_list; //INFIX TOKENS List postfix_list; //POSTFIX TOKENS // HEADER ASSUMED FOR LIST MANIPULATION infix_list = malloc(sizeof(struct node)); postfix_list = malloc(sizeof(struct node)); if(infix_list == NULL || postfix_list == NULL) //ERROR CHECK FOR MALLOC { printf("\nmemory not enough\n"); exit(1000); } infix_list->Next = NULL; postfix_list->Next = NULL; if(check_assign(exp)==1) //if '=' is there { /*store variable name*/ i=0; int sp_val=0; //to account for end of var_name and only spaces left char *tmp; //to account for the knowledge of var_name end except for leftover spaces while(*exp==' ' || *exp=='\t') exp++; while(*exp!='=') { /*to remove all the spaces once the variable name ends i do this*/ { tmp = exp; while(*tmp!='=') { if(*tmp!=' ') break; tmp++; } if(*tmp == '=') { sp_val=1; while(*exp!='=') exp++; } } if(sp_val != 1) { if(i==29) /*the var_nmae is exceeding limit,TRUNCATE*/ { while(*exp!='=') exp++; break; } else { var_name[i] = *exp; exp++; i++; }//else }//if(sp_val) }//while exp++; var_name[i]='\0'; if(check_evaluate(exp)==1) { /*carry out evaluation*/ Createlist(exp,infix_list); infix_to_postfix(infix_list,postfix_list); eval_ans = evaluate(postfix_list); add_lexicon(var_name,0,eval_ans,NULL); }//check_evaluate else { /*string value,just add it.*/ add_lexicon(var_name,0,1,exp); } }//check_assign else { /*no variable to assign*/ if(check_evaluate(exp)==1) { Createlist(exp,infix_list); infix_to_postfix(infix_list,postfix_list); eval_ans = evaluate(postfix_list); /*ret = 0,implies that return ans on terminal, else it implies return the answer to program*/ if(ret == 0) { printf("%f\n",eval_ans); } else { return eval_ans; } } }//else return ret; }
/** This Compares the given expression value to the the value current log , Whether it's conforming that expression or not. */ boolean isValideLog(char *expression) { int i=0,j,k=0,key=1; j=0; char compchar='\0'; char newstr[N],keystr[N],valuestr[N]; while(expression[i]!='\0') { if(expression[i]==' ') { i++; continue; } else if(expression[i]=='='||expression[i]=='>'||expression[i]=='<') { compchar=expression[i]; keystr[j]='\0'; key=0; j=0; } else if(expression[i]=='&'||expression[i]=='|'||expression[i]=='!'||expression[i]=='('||expression[i]==')' ||expression[i]=='!') { if(expression[i]=='('||expression[i]=='!') { newstr[k++]=expression[i]; } else if((i-1)>=0&&(expression[i]!=')'||expression[i]==')')&&expression[i-1]==')') { newstr[k++]=expression[i]; } else{ valuestr[j]='\0'; Tries *node; node=find(&root,keystr); if(node==NULL) { newstr[k++]='0'; } else if(!strcmp(keystr,s[1])) { int t=0; switch(compchar) { case '=': if(atoi(node->value)==atoi(valuestr)) t=1; break; case '<':if(atoi(node->value)<atoi(valuestr)) t=1; break; case '>': if(atoi(node->value)>atoi(valuestr)) t=1; } if(t==1) newstr[k++]='1'; else newstr[k++]='0'; } else if(!strcmp(keystr,s[2])) { int t=0; switch(compchar) { case '=': if(atoi(node->value)==atoi(valuestr)) t=1; break; case '<':if(atoi(node->value)<atoi(valuestr)) t=1; break; case '>': if(atoi(node->value)>atoi(valuestr)) t=1; } if(t==1) newstr[k++]='1'; else newstr[k++]='0'; } else if(strcmp(node->value,valuestr)) { newstr[k++]='0'; } else { newstr[k++]='1' ; } newstr[k++]=expression[i]; keystr[0]=valuestr[0]='\0'; key=1; j=0; } } else { if(key==1) { keystr[j++]=expression[i]; } else { valuestr[j++]=expression[i]; } } i++; } if(keystr[0]!='\0'&&valuestr[0]!='\0') { valuestr[j]='\0'; Tries *node; node=find(&root,keystr); if(node==NULL) newstr[k++]='0'; else if(!strcmp(keystr,s[1])) { int t=0; if(node){ switch(compchar) { case '=': if(atoi(node->value)==atoi(valuestr)) t=1; break; case '<':if(atoi(node->value)<atoi(valuestr)) t=1; break; case '>': if(atoi(node->value)>atoi(valuestr)) t=1; } } if(t==1) newstr[k++]='1'; else newstr[k++]='0'; } else if(!strcmp(keystr,s[2])) { int t=0; if(node){ switch(compchar) { case '=': if(atoi(node->value)==atoi(valuestr)) t=1; break; case '<':if(atoi(node->value)<atoi(valuestr)) t=1; break; case '>': if(atoi(node->value)>atoi(valuestr)) t=1; } } if(t==1) newstr[k++]='1'; else newstr[k++]='0'; } else if(strcmp(node->value,valuestr)) { newstr[k++]='0'; } else { newstr[k++]='1' ; } newstr[k++]=expression[i]; keystr[0]=valuestr[0]='\0'; key=1; j=0; } newstr[k]='\0'; char postfix[N]; infix_to_postfix(newstr,postfix); if(eval_postfix(postfix)) { return 1; } else return 0; }