예제 #1
0
파일: Eval.cpp 프로젝트: SIGVerse/SIGServer
double Eval::eval(const char *eq_)
{
	assert(eq_);
	assert(strlen(eq_) > 0);

#ifndef WIN32
	char *eq = strdup(eq_);
#else
	char *eq = _strdup(eq_);
#endif
	char *post = infix2postfix(eq, m_dict);
	if (!post) {
		free(eq);
		throw Exception(eq_);
	}

	double v;
	int r = rpnCalc(post, &v);
	if (r != 0) {
		free(eq);
		throw Exception(eq_);
	}
		
	free(eq);
	return v;
}
예제 #2
0
파일: RPNCalc.c 프로젝트: 0x0E01/oss_test1
double Calc(char *exp)
{
	stack tmp;
	tmp=infix2postfix(exp);
	printf("%s\n",stack2exp(tmp));
	return RPNStackCalc(tmp);
}
예제 #3
0
int main(int argc, char *argv[])
{
	//Error if there are not at least two commandline arguments
	if(argc < 2)
		output_usage_and_exit(argv[0]);
	

	//Open specified file, quit if cannot open
	std::ifstream in(argv[1]);
	if(!in){
		std::cerr << "Couldn't open " << argv[1] << std::endl;
		exit (2);

	}

	
	//convert the infix expressions to postfix
	string infix;
	Stack<string> postfix; 
	while(in>>infix){

		//substrings out the infix expression minus the line return and
		//semi-colon.
		string temp = infix.substr(0,infix.length()-2);

		//splits the infix expression on spaces for easier processing. Stores in
		//it in vector.
		std::vector<string> vec = temp.split(' ');

		//function for converting infix expressions to postfix format.
		string result = infix2postfix(vec);

		//places the resulting string from the the conversion on the stack.
	    postfix.push(result);
	}	
	
	//done using the input file
	in.close();
	
	//handles if an output file was specified on commandline
	if(argc==3){
		std::ofstream out(argv[2]);
		if(!out){
			std::cerr << "Couldn't open " << argv[2] <<std::endl;
		}
		while(!postfix.IsEmpty()){
		out<< postfix.pop() << std::endl;}
	}else{
		while(!postfix.IsEmpty()){
        std::cout << postfix.pop() << std::endl;}
		}
	
	//successful
	return 0;
}
예제 #4
0
파일: proj2.c 프로젝트: jaidTw/CSU0013
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));
	}
}
예제 #5
0
int main()
{
    double op2;
    char buf[BUFSIZ];
    int result;
    infix2postfix(buffer);
    puts(buffer);

    while ((result = getop2(buf) != EOF)) {
        switch (result) {
            case '+':
                STACK_push(STACK_pop() + STACK_pop());
                break;

            case '*':
                STACK_push(STACK_pop() * STACK_pop());
                break;

            case '/':
                op2 = STACK_pop();

                if (op2 == 0) {
                    error("zero divisor\n");
                }

                STACK_push(STACK_pop() / op2);
                break;

            case '-':
                op2 = STACK_pop();
                STACK_push(STACK_pop() / op2);
                break;

            case '$':
                STACK_push(sqrt(STACK_pop()));
                break;

            case NUMBER:
                STACK_push(atof(buf));
                break;

            case '\n':
                printf("%f\n", STACK_pop());
                break;

            default:
                error("unrecognize character %c\n", result);
                break;
        }
    }

    return 0;
}
예제 #6
0
// Calculate
bool Expression::calculate(std::string &_return) {
    Term result;

    // Try to do all operations
    if (!tokenize() || !infix2postfix() || !get_result(result)) {
        _return = Errors::get_error_message(m_error.id, m_error.col);
        return false;
    }

    _return = result.value;
    return true;
}
예제 #7
0
int TEST_infix2postfix()
{
    element_type data;

    char *string = "{fdffldfsdfds()}fdfssd[ff...]";
    if (checkBalance(string) == SUCCESS) {
        printf("success, balance!\n");
    }

    char *infix_string = "12 + 2 * 3 + (4*5+6)*1";
    char out_string[100] = "";

    infix2postfix(infix_string, out_string);
    printf("infix string:   %s\n", infix_string);
    printf("postfix string: %s\n", out_string);

    data = calcPostfix(out_string);
    printf("postfix result: %d\n", data);

    return 0;
}
예제 #8
0
static char *infix2postfix_function_args(const char *expr_args,
                                         const size_t esize) {

    char *e;
    char *iexpr, *pexpr;
    char *result = (char *) hefesto_mloc(HEFESTO_MAX_BUFFER_SIZE);
    char *r;
    size_t offset = 1;

    iexpr = get_arg_from_call(expr_args, &offset);

    r = result;
    *r = '(';
    r++;

    while (*iexpr) {
        pexpr = infix2postfix(iexpr, strlen(iexpr), 0);

        for (e = pexpr; *e != 0; e++, r++) *r = *e;

        free(iexpr);

        free(pexpr);

        iexpr = get_arg_from_call(expr_args, &offset);

        if (*iexpr) {
            *r = ',';
            r++;
        }
    }

    free(iexpr);

    *r = 0;

    return result;

}
예제 #9
0
int main(int argc, char *argv[])
{
  
	//Error if less than 2 commandline arguments. Means user did not
	//specify input file.
	if(argc < 2)
		output_usage_and_exit(argv[0]);
	
	std::ifstream in(argv[1]);
	if(!in){
		std::cerr << "Couldn't open: " << argv[1] << std::endl;
		exit(2);
	}
	
	string temp, infix;
	
	if(argc==3){
		std::ofstream out(argv[2]);
		if(!out){
			std::cerr << "Couldn't open: " << argv[2] << std::endl;
			exit(2);
		}
		while(in>>temp){
			
			out << "Infix: " << temp << std::endl;

			infix = temp.substr(0, temp.length()-2);
			
			std::vector<string> vec = infix.split(' ');
			
			string postfix = infix2postfix(vec);
			
			out << "postfix: " << postfix << std::endl;
			std::vector<string> vec2 = postfix.split(' ');
			out << "Assembler:" << std::endl;
			Stack<string> assembler = postfix2assembler(vec2);
			outputassembler(out, assembler);
		}
	}else{
예제 #10
0
char *infix2postfix_args(const char *arg_list, const size_t arg_list_size) {
    char *e = NULL;
    const char *ap = NULL, *ap_end = NULL;
    char *arg_pf = NULL;
    size_t offset = 0;
    size_t real_arg_list_size = 0;
    char *retval = NULL;
    char arg_list_cpy[HEFESTO_MAX_BUFFER_SIZE];

    if (arg_list == NULL) {
        return NULL;
    }

    memset(arg_list_cpy, 0, sizeof(arg_list_cpy));
    strncpy(arg_list_cpy, arg_list, sizeof(arg_list_cpy) - 1);
    real_arg_list_size = strnlen(arg_list_cpy, sizeof(arg_list_cpy));

    ap_end = get_stmt_end(arg_list_cpy, real_arg_list_size);

    ap = &arg_list_cpy[0];

    if (ap_end != NULL) {
        memset((char *)ap_end, 0, real_arg_list_size - (ap_end - ap));
    }

    while (*ap != '(' && *ap != 0) {
        ap++;
    }

    if (*ap == 0) {
        return NULL;
    }

    ap++;

    retval = (char *) hefesto_mloc(arg_list_size * 2);

    memset(retval, 0, arg_list_size * 2);

    *retval = '(';

    e = get_arg_from_call(ap, &offset);

    while (*e) {

        arg_pf = infix2postfix(e, strlen(e), 1);
        strcat(retval, arg_pf);

        free(arg_pf);
        free(e);

        if (arg_list_cpy[offset] == ',') {
            offset++;
        }

        e = get_arg_from_call(arg_list_cpy, &offset);

        if (*e) {
            strcat(retval, ",");
        } else {
            strcat(retval, ")");
        }

    }

    free(e);

    return retval;
}
예제 #11
0
파일: hvm.c 프로젝트: nola-radar/hefesto
hefesto_int_t hvm_forge_project(hefesto_project_ctx *project,
                                hefesto_var_list_ctx **gl_vars,
                                hefesto_func_list_ctx *functions) {

    //hefesto_int_t build_result = 0;
    void *result;
    char *expr_pfix;
    char forge_invocation[HEFESTO_MAX_BUFFER_SIZE] = "";
    hefesto_common_list_ctx *arg;
    hefesto_var_list_ctx *lo_vars_dummy = NULL;
    struct stacked_function_execution_point_ctx *sv_p;
    hefesto_project_ctx *last_project;
    size_t t_sz;

    last_project = HEFESTO_CURRENT_PROJECT;
    HEFESTO_CURRENT_PROJECT = project;

    // executes the prologue

    if (project->prologue && project->prologue->code) {
        hvm_set_current_executed_function(project->prologue);
        result = hvm_exec_function(project->prologue,
                                   &project->prologue->vars,
                                   gl_vars, functions);

        if (result != NULL) free(result);
    }

    if (project->dep_chain_expr != NULL) {
        expr_pfix = infix2postfix(project->dep_chain_expr,
                                  strlen(project->dep_chain_expr), 1);
        result = hvm_str_format(expr_pfix, &lo_vars_dummy, gl_vars, functions);
        free(expr_pfix);
        if (result != NULL) {
            project->dep_chain = get_dep_chain_by_user_dep_string(result);
            //project->dep_chain = NULL;
            if (result != NULL) {
                t_sz = strlen(result);
                project->dep_chain_str = (char *) hefesto_mloc(t_sz + 1);
                memset(project->dep_chain_str, 0, t_sz + 1);
                memcpy(project->dep_chain_str, result, t_sz);
            }
            free(result);
            HEFESTO_CURRENT_DEP_CHAIN = project->dep_chain;
            set_hvm_toolset_src_changes_check_flag(0);
        }
    }

    // executes the forge

    if (HEFESTO_EXIT == 0 && project->toolset && project->toolset->forge) {
        strncpy(forge_invocation, project->toolset->forge->name,
                sizeof(forge_invocation)-1);
        strcat(forge_invocation, "(");
        for (arg = project->args; arg; arg = arg->next) {
            strcat(forge_invocation, (char *)arg->data);
            if (arg->next) {
                strcat(forge_invocation, ", ");
            }
        }
        strcat(forge_invocation, ");");

        HEFESTO_CURRENT_TOOLSET = project->toolset;

        // saves the execution context.
        sv_p = hvm_save_execution_point(project->toolset->forge);
        hvm_init_function_args(forge_invocation, gl_vars, gl_vars, functions,
                               &project->toolset->forge);
        hvm_set_current_executed_function(project->toolset->forge);
        result = hvm_exec_function(project->toolset->forge,
                                   &project->toolset->forge->vars, gl_vars,
                                   functions);

        // restores the execution context saved above.
        hvm_restore_execution_point(&project->toolset->forge, sv_p);

        del_stacked_function_execution_point_ctx(sv_p);

        if (result != NULL) {
            HEFESTO_LAST_FORGE_RESULT = *(hefesto_int_t *)result;
            free(result);
        }

    } else {
        if (project->toolset == NULL) {
            printf(">>>> hefesto PANIC: NULL toolset!!! RUN FOREST!!\n");
        }
        if (project->toolset->forge == NULL) {
            printf(">>>> hefesto PANIC: Hey Beavis Huh we got a NULL "
                   "forge function...\nYeah Yeah Butt-Head, it's not "
                   "good...\nShut-up Beavis, Huh Bughole Huh!\n");
        }
    }

    HEFESTO_EXIT = 0; // because the epilogue needs to be executed anyway,
                      // except when exit_forge() syscall was called.

    // executes the epilogue
    if (project && !HEFESTO_EXIT_FORGE) {
        if (project->epilogue && project->epilogue->code) {
            hvm_set_current_executed_function(project->epilogue);
            result = hvm_exec_function(project->epilogue,
                                       &project->epilogue->vars,
                                       gl_vars, functions);
            if (result != NULL) free(result);
        }
    }

    if (project->dep_chain) {
        del_hefesto_dep_chain_ctx(project->dep_chain);
        project->dep_chain = NULL;
    }

    HEFESTO_CURRENT_PROJECT = last_project;

    return HEFESTO_LAST_FORGE_RESULT;

}