Ejemplo n.º 1
0
int main(int argc, char** args) {
	FILE* instruction_file = fopen(args[1], "r");
	char** instructions = NULL;
	int lines = get_instructions(instruction_file, &instructions);
	fclose(instruction_file);

	var** input_variables = NULL;
	int num_input_variables = get_input_variables(instructions, lines, &input_variables);

	var** temp_variables = NULL;
	int num_temp_variables = get_temporary_variables(instructions, lines, &temp_variables);

	var** output_variables = NULL;
	int num_output_variables = get_output_variables(instructions, lines, &output_variables);

	FILE* value_file = fopen(args[2], "r");
	char* string = (char *) malloc(256 * sizeof(char *));
	while (fscanf(value_file, "%[^\n]\n", string) == 1) {
		int i;
		for (i = 0; i < num_input_variables; i++) {
			input_variables[i] -> value = string[i*2] - '0';
		}
		run_program(instructions, lines, 
			input_variables, num_input_variables, 
			temp_variables, num_temp_variables,
			output_variables, num_output_variables
		);
	}
	return 0;
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
    char line_buffer[1024];

    /* get input variables */
    if (fgets(line_buffer, 1024, stdin) == NULL) {
        fprintf(stderr, "fgets: crash\n");
        exit(-1);
    }
    
    get_input_variables(line_buffer);

    /* get circuit spec */ 
    if (fgets(line_buffer, 1024, stdin) == NULL) {
        fprintf(stderr, "fgets: crash\n");
        exit(-1);
    }
    
    get_circuit(line_buffer);
    replace_labels(circuit);

    /* evaluate until eof */
    while (fgets(line_buffer, 1024, stdin) != NULL) {
        set_inputs(line_buffer);
        final_val = evaluate(circuit);
    }

    printf("Result: %d\n", final_val);

    free_element(circuit);
    exit(0);
}