task4_6::solution::solution( const lines& calulator_lines ) { size_t i = 0; expressions exps; for(lines::const_iterator it = calulator_lines.begin(); it != calulator_lines.end(); it++, i++) rpn(*it, i, exps); make_values(exps); }
// requires: exp be a c string, with no // spaces and only valid characters. int rpn(char *exp, int size) { if (size == 1) { return 0; } else { if (check_constraints(exp, size)) { printf("%s\n", exp); printf("Constraints not met\n"); return 1; } int delim_loc = get_delim(exp, size); char delim = exp[delim_loc]; exp[delim_loc] = '\0'; exp[size - 1] = '\0'; char *left, *right; left = exp + 1; right = exp + delim_loc + 1; if (rpn(left, strlen(left)) || rpn(right, strlen(right))) { return 1; } merge(exp, left, right, delim); return 0; } }
int main(int argc, char *argv[]) { char buf[BUFSIZ], *p; _printf("Ctrl-D to quit. > "); while (fgets(buf, BUFSIZ, stdin)) { p = strchr(buf, '\n'); if (p) *p = '\0'; rpn(buf); _printf("Ctrl-D to quit. > "); } _printf("\n"); exit(EXIT_SUCCESS); }
void run_rpn_expression(NAMELIST_TEXT *nltext) { /* expression = NULL; */ log_entry("run_rpn_expression"); /* process the namelist text */ set_namelist_processing_flags(STICKY_NAMELIST_DEFAULTS); set_print_namelist_flags(0); if (processNamelist(&rpn_expression, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (echoNamelists) print_namelist(stdout, &rpn_expression); if (expression) { rpn(expression); if (rpn_check_error()) exitElegant(1); } log_exit("run_rpn_expression"); }
int main (int argc, char **argv) { if (argc == 1) { int exp_num; char exp[512]; if (fgets(exp, sizeof(exp), stdin) && sscanf(exp, "%d", &exp_num) == 1) { for (int i = 0; i < exp_num; i++) { if (fgets(exp, sizeof(exp), stdin)) { exp[strlen(exp) - 1] = '\0'; rpn(exp, strlen(exp)); printf("%s\n", exp); } } } return 0; } else { printf("Testing:\n"); printf("Merge:\n"); char buf[10]; buf[0] = '0'; buf[1] = '1'; buf[2] = '2'; buf[3] = '3'; buf[4] = '4'; buf[5] = '\0'; buf[6] = '6'; buf[7] = '7'; buf[8] = '8'; buf[9] = '\0'; char *left, *right, delim = '+'; left = buf + 1; right = buf + 5 + 1; merge(buf, left, right, delim); printf("%s\n", buf); } }
// @rpn:addDiv => [rpn ne retourne pas le bon resultat pour l entree: \"2 3 + 2 /\"] void addDiv() { char str[10] = "2 3 + 2 /"; CU_ASSERT_EQUAL(rpn(str), 2.5); }
// @rpn:simpleDiv => [rpn ne retourne pas le bon resultat pour l entree: \"5 3 /\"] void simpleDiv() { char str[7] = "15 3 /"; CU_ASSERT_EQUAL(rpn(str), 5); }
// @rpn:simpleMult => [rpn ne retourne pas le bon resultat pour l entree: \"5 3 *\"] void simpleMult() { char str[6] = "5 3 *"; CU_ASSERT_EQUAL(rpn(str), 15); }
// @rpn:simpleDiff => [rpn ne retourne pas le bon resultat pour l entree: \"5 3 -\"] void simpleDiff() { char str[6] = "5 3 -"; CU_ASSERT_EQUAL(rpn(str), 2); }
// @rpn:simpleAdd => [rpn ne retourne pas le bon resultat pour l entree: \"2 3 +\"] void simpleAdd() { char str[6] = "2 3 +"; CU_ASSERT_EQUAL(rpn(str), 5); }
// @rpn:timesAdd => [rpn ne retourne pas le bon resultat pour l entree: \"2 3 * 2 +\"] void timesAdd() { char str[10] = "2 3 * 2 +"; CU_ASSERT_EQUAL(rpn(str), 8); }
long assert_element_links(ELEMENT_LINKS *links, RUN *run, LINE_LIST *beamline, long flags) { long i_link, i_elem, i_item, matrices_changed; long elem_type, data_type, param; double value; ELEMENT_LIST **targ, **sour; char *p_elem; short lengthChanged = 0; log_entry("assert_element_links"); if (!links || links->n_links==0) { log_exit("assert_element_links"); return(0); } if (!links->target_name || !links->item || !links->source_name || !links->equation || !links->n_targets || !links->target_elem || !links->source_elem) { fputs("error: link structure has null pointers (assert_element_links)", stdout); abort(); } matrices_changed = 0; for (i_link=0; i_link<links->n_links; i_link++) { if (!(flags&links->flags[i_link])) continue; targ = links->target_elem[i_link]; sour = links->source_elem[i_link]; elem_type = targ[0]->type; param = links->target_param[i_link]; data_type = entity_description[elem_type].parameter[param].type; #if DEBUG fprintf(stdout, "asserting %ld links of %s.%s to %s\n", links->n_targets[i_link], links->target_name[i_link], links->item[i_link], links->source_name[i_link]); fflush(stdout); fprintf(stdout, "source type is %ld, with %ld parameters\n", sour[0]->type, entity_description[sour[0]->type].n_params); fflush(stdout); #endif for (i_elem=0; i_elem<links->n_targets[i_link]; i_elem++) { #if DEBUG fprintf(stdout, " working on element %ld\n", i_elem); fflush(stdout); #endif p_elem = sour[i_elem]->p_elem; for (i_item=0; i_item<entity_description[sour[0]->type].n_params; i_item++) { char s[1024]; double value0; switch (entity_description[sour[0]->type].parameter[i_item].type) { case IS_DOUBLE: value = *((double*)(p_elem+entity_description[sour[0]->type].parameter[i_item].offset)); rpn_store(value, NULL, rpn_create_mem(entity_description[sour[0]->type].parameter[i_item].name, 0)); value0 = *((double*)(sour[i_elem]->p_elem0+entity_description[sour[0]->type].parameter[i_item].offset)); sprintf(s, "%s0", entity_description[sour[0]->type].parameter[i_item].name); rpn_store(value0, NULL, rpn_create_mem(s, 0)); break; case IS_LONG: value = *((long*)(p_elem+entity_description[sour[0]->type].parameter[i_item].offset)); rpn_store(value, NULL, rpn_create_mem(entity_description[sour[0]->type].parameter[i_item].name, 0)); value0 = *((long*)(sour[i_elem]->p_elem0+entity_description[sour[0]->type].parameter[i_item].offset)); sprintf(s, "%s0", entity_description[sour[0]->type].parameter[i_item].name); rpn_store(value0, NULL, rpn_create_mem(s, 0)); break; default: break; } #if DEBUG fprintf(stdout, " asserting value %e for %s\n", value, entity_description[sour[0]->type].parameter[i_item].name); fprintf(stdout, " asserting value %e for %s0\n", value0, entity_description[sour[0]->type].parameter[i_item].name); fflush(stdout); #endif } p_elem = targ[i_elem]->p_elem; rpn_clear(); /* push original value onto stack */ if (verbosity>1) fprintf(stdout, "prior value is %.15g for %s#%ld.%s at z=%.15gm\n", links->baseline_value[i_link][i_elem], links->target_name[i_link], targ[i_elem]->occurence, links->item[i_link], targ[i_elem]->end_pos); push_num(links->baseline_value[i_link][i_elem]); value = rpn(links->equation[i_link]); if (rpn_check_error()) exitElegant(1); rpn_clear(); if (value>links->maximum[i_link]) value = links->maximum[i_link]; if (value<links->minimum[i_link]) value = links->minimum[i_link]; if (verbosity>0) fprintf(stdout, "asserting value %.15g for %s#%ld.%s at z=%.15gm\n", value, links->target_name[i_link], targ[i_elem]->occurence, links->item[i_link], targ[i_elem]->end_pos); fflush(stdout); if (entity_description[elem_type].flags&HAS_LENGTH && entity_description[elem_type].parameter[param].offset==0) lengthChanged = 1; switch (data_type) { case IS_DOUBLE: *((double*)(p_elem+entity_description[elem_type].parameter[param].offset)) = value; break; case IS_LONG: *((long*)(p_elem+entity_description[elem_type].parameter[param].offset)) = nearestInteger(value); break; case IS_STRING: default: bombElegant("unknown/invalid variable quantity (assert_element_links)", NULL); exitElegant(1); } if (flags&LINK_ELEMENT_DEFINITION) change_defined_parameter_values(&targ[i_elem]->name, ¶m, &targ[i_elem]->type, &value, 1); if ((entity_description[targ[0]->type].parameter[param].flags&PARAM_CHANGES_MATRIX) && targ[i_elem]->matrix) { free_matrices(targ[i_elem]->matrix); tfree(targ[i_elem]->matrix); targ[i_elem]->matrix = NULL; compute_matrix(targ[i_elem], run, NULL); matrices_changed++; } } } #if DEBUG print_line(stdout, beamline); #endif if (lengthChanged) compute_end_positions(beamline); log_exit("assert_element_links"); return(matrices_changed); }
void ParseFormatNotationPostfix(const std::string& notation, FormatTokensVisitor& visitor) { RPNTranslation rpn(visitor); ParseFormatNotation(notation, rpn); rpn.Flush(); }
/* * in - Source file to be interpreted * file_name - Name of the source file, for debugging purposes * sym_table - Global (master) symbol table */ void liten(FILE * in, char * file_name, slist * sym_table, token ** ret) { // *ret = NULL; slist * block_stack = slist_init(); slist * local_table = slist_init(); slist_push(sym_table, local_table); file_desc * desc = malloc(sizeof(file_desc)); desc->file = in; desc->name = file_name; desc->line = 1; int tok; char * temp = malloc(sizeof(char)*MAX_STR); strcpy(temp, "\0"); int type = -1; slist * tok_queue = slist_init(); while (1) { if (in == stdin) { printf(":> "); } else if (tok == EOF) { break; } tok = getc(in); while (1) { if (tok == '\n' || tok == EOF) { desc->line++; tokenize(desc, &temp, &type, tok_queue); type = -1; // slist_print(tok_queue, slist_print_token); // TODO: Check line for syntax errors if (tok_queue->len == 0) break; if (block_stack->len == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else { char * temp_str = ((token *) slist_get(tok_queue, 0)->obj)->obj; block * temp = (block *) slist_get(block_stack, -1)->obj; if (strcmp("end", temp_str) == 0) { rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("if", temp_str) == 0 || strcmp("while", temp_str) == 0) { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); else { block * temp1 = malloc(sizeof(block)); temp1->value = 0; if (strcmp("if", temp_str) == 0) { temp1->type = IF_B; } else if (strcmp("while", temp_str) == 0) { temp1->type = WHILE_B; } slist_push(block_stack, temp1); } } else if (strcmp("elif", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'elif' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else rpn(desc, tok_queue, sym_table, block_stack, ret); } else if (strcmp("else", temp_str) == 0) { if (temp->type != IF_B) lite_error(desc, "'else' exists in 'if' block"); if (temp->value == 1) temp->value = 0; else temp->value = 1; } else { if (temp->value == 1) rpn(desc, tok_queue, sym_table, block_stack, ret); } } tok_queue = slist_init(); } else { token_parse(desc, tok, &temp, &type, tok_queue); } if (in == stdin && tok == '\n') break; tok = getc(in); } } return; }
// @rpn:divMinus => [rpn ne retourne pas le bon resultat pour l entree: \"6 3 / 1 -\"] void divMinus() { char str[10] = "6 3 / 1 -"; CU_ASSERT_EQUAL(rpn(str), 1); }
// @rpn:minusTimes => [rpn ne retourne pas le bon resultat pour l entree: \"2 3 - 2 *\"] void minusTimes() { char str[10] = "2 3 - 2 *"; CU_ASSERT_EQUAL(rpn(str), -2); }
int main(int argc, char **argv) { double x_lo, x_hi, y_lo, y_hi, dx, dy, x, y; double **z_data, z_min, z_max, z_val; long ix, iy, nx, ny; long x_rpn_var, y_rpn_var; char *z_equation = NULL; char *rpn_defns_file; char *rpn_init_command; char *z_udf = "Z.UDF"; char *input, *output, *label[4]; long i_arg; SCANNED_ARG *s_arg; FILE *fp; unsigned long pipeFlags; char pfix[IFPF_BUF_SIZE], *ptr; char * rpn_defns; #if defined(LINUX) struct stat sts; #endif if (argc<2 || argc>(2+N_OPTIONS)) bomb(NULL, USAGE); output = NULL; nx = ny = 0; rpn_defns_file = NULL; rpn_init_command = NULL; pipeFlags = 0; for (ix=0; ix<4; ix++) label[ix] = " "; scanargs(&s_arg, argc, argv); for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { delete_chars(s_arg[i_arg].list[0], "_"); switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case SET_RPN_DEFNS_FILE: if (s_arg[i_arg].n_items!=2 || !(rpn_defns_file=s_arg[i_arg].list[1])) bomb("incorrect -rpndefnsfile syntax", NULL); break; case SET_RPN_INIT_COMMAND: /* if (s_arg[i_arg].n_items!=2 || !(rpn_init_command = s_arg[i_arg].list[1])) bomb("incorrect -rpncommand syntax", NULL); */ if ((s_arg[i_arg].n_items<2) || (s_arg[i_arg].n_items>3)) SDDS_Bomb("incorrect -rpncommand syntax"); if (s_arg[i_arg].n_items==2) { if (!strlen(rpn_init_command=s_arg[i_arg].list[1])) { SDDS_Bomb("incorrect -rpncommand syntax"); } } else if (s_arg[i_arg].n_items==3) { if (strncmp(s_arg[i_arg].list[2], "algebraic", strlen(s_arg[i_arg].list[2]))==0) { ptr = addOuterParentheses(s_arg[i_arg].list[1]); if2pf(pfix, ptr, sizeof pfix); free(ptr); if (!SDDS_CopyString(&rpn_init_command, pfix)) { fprintf(stderr, "error: problem copying argument string\n"); return(1); } } else { SDDS_Bomb("incorrect -rpncommand syntax"); } } break; case SET_Z_EQUATION: /* if (s_arg[i_arg].n_items!=2 || !(z_equation=s_arg[i_arg].list[1])) bomb("incorrect -zequation syntax", NULL); */ if ((s_arg[i_arg].n_items<2) || (s_arg[i_arg].n_items>3)) SDDS_Bomb("incorrect -zequation syntax"); if (s_arg[i_arg].n_items==2) { if (!strlen(z_equation=s_arg[i_arg].list[1])) { SDDS_Bomb("incorrect -zequation syntax"); } } else if (s_arg[i_arg].n_items==3) { if (strncmp(s_arg[i_arg].list[2], "algebraic", strlen(s_arg[i_arg].list[2]))==0) { if2pf(pfix, s_arg[i_arg].list[1], sizeof pfix); if (!SDDS_CopyString(&z_equation, pfix)) { fprintf(stderr, "error: problem copying argument string\n"); return(1); } } else { SDDS_Bomb("incorrect -zequation syntax"); } } break; case SET_X_RANGE: if (s_arg[i_arg].n_items!=4 || 1!=sscanf(s_arg[i_arg].list[1], "%le", &x_lo) || 1!=sscanf(s_arg[i_arg].list[2], "%le", &x_hi) || x_lo>=x_hi || 1!=sscanf(s_arg[i_arg].list[3], "%ld", &nx) || nx<=1) bomb("incorrect -xrange syntax", NULL); break; case SET_Y_RANGE: if (s_arg[i_arg].n_items!=4 || 1!=sscanf(s_arg[i_arg].list[1], "%le", &y_lo) || 1!=sscanf(s_arg[i_arg].list[2], "%le", &y_hi) || y_lo>=y_hi || 1!=sscanf(s_arg[i_arg].list[3], "%ld", &ny) || ny<=1) bomb("incorrect -yrange syntax", NULL); break; case SET_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) bomb("invalid -pipe syntax", NULL); break; default: fprintf(stderr, "error: unknown switch: %s\n", s_arg[i_arg].list[0]); bomb(NULL, NULL); break; } } else { if (output==NULL) output = s_arg[i_arg].list[0]; else bomb("too many filenames (sddscongen)", NULL); } } pipeFlags |= DEFAULT_STDIN; input = NULL; processFilenames("sddscongen", &input, &output, pipeFlags, 0, NULL); if (nx==0) bomb("-xrange must be supplied", NULL); if (ny==0) bomb("-yrange must be supplied", NULL); if (z_equation==NULL) bomb("-zequation must be supplied", NULL); if(!rpn_defns_file) { rpn_defns_file=getenv("RPN_DEFNS"); /*if failed, check where default setting exists for a linux system, G. Shen, Dec 31, 2009 */ if(!rpn_defns_file) { #if defined(LINUX) if (!(stat(rpn_default, &sts) == -1 && errno == ENOENT)) { /* check whether default file exists */ rpn_defns = rpn_default; } #endif } } rpn(rpn_defns_file); /* rpn(rpn_defns_file?rpn_defns_file:getenv("RPN_DEFNS")); */ if (rpn_init_command) rpn(rpn_init_command); x_rpn_var = rpn_create_mem("x", 0); y_rpn_var = rpn_create_mem("y", 0); create_udf(z_udf, z_equation); z_data = (double**)array_2d(sizeof(double), 0, nx-1, 0, ny-1); dx = (x_hi-x_lo)/(nx-1); dy = (y_hi-y_lo)/(ny-1); z_min = FLT_MAX; z_max = -FLT_MAX; for (ix=0,x=x_lo; ix<nx; ix++,x+=dx) { for (iy=0,y=y_lo; iy<ny; iy++,y+=dy) { rpn_store(x, NULL, x_rpn_var); rpn_store(y, NULL, y_rpn_var); if ((z_val = z_data[ix][iy] = rpn(z_udf))>z_max) z_max = z_val; if (z_val<z_min) z_min = z_val; rpn_clear(); } } if (output) fp = fopen_e(output, "w", 0); else fp = stdout; fprintf(fp, "SDDS1\n¶meter name=Variable1Name, type=string, fixed_value=x &end\n"); fprintf(fp, "¶meter name=Variable2Name, type=string, fixed_value=y &end\n"); fprintf(fp, "¶meter name=xInterval, type=double, fixed_value=%e &end\n", dx); fprintf(fp, "¶meter name=xMinimum, type=double, fixed_value=%e &end\n", x_lo); fprintf(fp, "¶meter name=xDimension, type=long, fixed_value=%ld &end\n", nx); fprintf(fp, "¶meter name=yInterval, type=double, fixed_value=%e &end\n", dy); fprintf(fp, "¶meter name=yMinimum, type=double, fixed_value=%e &end\n", y_lo); fprintf(fp, "¶meter name=yDimension, type=long, fixed_value=%ld &end\n", ny); fprintf(fp, "&column name=z, type=double, description=\"%s\" &end\n", z_equation); fprintf(fp, "&data mode=ascii, no_row_counts=1 &end\n"); for (ix=0; ix<nx; ix++) for (iy=0; iy<ny; iy++) fprintf(fp, "%e\n", z_data[ix][iy]); return(0); }