Exemplo n.º 1
0
int print_all_s(double **u_buf, double min, double max) {
	int i = 0, j = 0, cur_num = 0, color = 255;
//	const double T_SIZE = 1 / TIME;
	const double DX = X_SIZE / X_CELLS;
	const double DT = T_SIZE / T_CELLS;
	int x_max_digits = digits_num(lround(trunc(X_SIZE)));
	int t_max_digits = 0;
	double u_step = (max - min) / 24;

	printf("x");
	if (X_CELLS < 24) 
		t_max_digits = 5 + digits_num(lround(trunc(T_SIZE)));
	for (i = 0; i < x_max_digits + 2 * T_CELLS + 8 + t_max_digits; i++)
		printf(" ");
	printf("u\n");
	
	FILE *file_id = fopen("lab1_out.txt", "w");
	ASSERT(file_id, "fopen error");	

	for (i = X_CELLS; i >= 0; i--) {
		for (j = 0; j < x_max_digits - digits_num(lround(trunc(DX * i))); j++)
			printf(" ");
		printf("%.2lf ", DX * i);
		fprintf(file_id, "%.2lf ", DX * i);
		for (j = 0; j <= T_CELLS; j++) {
			if (lround(max) != lround(min)) color = 255 - lround((u_buf[i][j] - min) * 23 / (max - min));
			printf("\x1b[48;5;%dm  \x1b[0m", color);
			fprintf(file_id, "%.2lf ", u_buf[i][j]);
		}
		cur_num = X_CELLS - i;
		if (cur_num < 24 && (lround(max) != lround(min) || cur_num == 0)) {
			for (j = 0; j < t_max_digits; j++) printf(" ");
				printf(" \x1b[48;5;%dm  \x1b[0m%.2lf", 255 - cur_num, min + cur_num * u_step);
		}
		printf("\n");
		fprintf(file_id, "\n");
	}

	printf(" 0.00");
	for (i = 0; i < 2 * (T_CELLS + 1); i++) printf("=");
	printf("%.2lf t ", T_SIZE);

	if (X_CELLS >= 23) printf("\n");

	for (i = X_CELLS + 1; i < 24; i++) {
		if (i != X_CELLS + 1)
			for (j = 0; j < x_max_digits + 2 * T_CELLS + t_max_digits + 6; j++) printf(" ");
		if (lround(max) != lround(min))
		printf(" \x1b[48;5;%dm  \x1b[0m%.2lf\n", 255 - i, min + i * u_step);
	}
	
	printf("\nAll calculated data is in lab1_out.txt file in format:\n"
	       "x1 u11 u12 u13 ...\n"
	       "x2 u21 u22 u23 ...\n"
	       "...\n");	

	fclose(file_id);
	return 0;
}
Exemplo n.º 2
0
int print_all_p(double **u_buf) {
	int i = 0, j = 0;
//	const double T_SIZE = 1 / TIME;
	const double DX = X_SIZE / X_CELLS;
	const double DT = T_SIZE / T_CELLS;
	
	printf("x");
	int x_max_digits = digits_num(lround(trunc(X_SIZE)));
	for (i = 0; i < x_max_digits + 3; i++)
		printf(" ");
	printf("u(t = %.2lf)\n", T_SIZE);
	for (i = X_CELLS; i >= 0; i--) {
		for (j = 0; j < x_max_digits - digits_num(lround(trunc(DX * i))); j++)
			printf(" ");
		printf("%.2lf %.2lf\n", DX * i, u_buf[i][T_CELLS]);
	}

	return 0;
}
Exemplo n.º 3
0
int main(){
	
	integer player_guess;
	int tries_num, i, errors = 0, result;
	int comp_num[DIGITS_NUM] = {0}, results[2] = {0};

	srand(time(0));
	
		// --- Introduction section ---
	
	printf("Welcmome to \"BULL PGIAA\"!\n"
		   "The computer has chosen a %d digit number and you have to guess it.\n"
		   "Are you smart enough to do it in %d tries?\n", DIGITS_NUM, MAX_TRIES
		   );
	
		// --- Input & Main Program section ---

	random_number(comp_num);	//Randomize a number

    for(tries_num = 1; tries_num <= MAX_TRIES; tries_num++)
	{
		printf("\nTry no.%d\n", tries_num);
		printf("\nEnter your guess(%d digits):", DIGITS_NUM);
		while(true){
			if(scanf("%f", &player_guess) != 1)
			{
				printf(input_error_message);
				return EXIT_FAILURE;
			}
			/*if(errors += (player_guess != (int)player_guess))
				{ printf("You've typed a fractional number! Try again:");errors =0; continue;  }
			*/
			if(errors += (player_guess <= 0))
				{printf("You must type a positive number! Try again:");errors =0; continue;}
			if(errors += (digits_num((int)player_guess) != DIGITS_NUM))
				{printf("You must type a number with %d digits(or you typed the right number of"
						" digits but first digit zero) Try again:", DIGITS_NUM);errors =0; continue;  }
			if(errors += (is_differ((int)player_guess) == FALSE))
				{printf("You've typed a number that's digits aren't different. Try again:");errors =0; continue;  }
			if(errors += (contains_zeros(player_guess) == true) )
				{printf("The numeber you typed contains zeros. Try again:"); errors = 0; continue; }
			if(errors == 0)
				break;
		}
		results[0] = 0;	//Re - initalize
		results[1] = 0;
		compare(comp_num, (int)player_guess, results);
		
		/* This commented code prints the computer's guess
          for(i = DIGITS_NUM-1; i >= 0; --i)
			printf("%d",comp_num[i]);  
		*/
			
		printf("\n");
		
			// --- Output section ---
		
		printf("\n\t you have %dB %dP\n", results[0], results[1]);
		if( results[0] == DIGITS_NUM )
		{
			printf("***************************************************\n"
				   "Congratulations! you have guessed the number right!\n"
				   "***************************************************\n"
				   );
			break;
		}
	}
	if(results[0] != DIGITS_NUM)
		printf("You lose, you aren't as smart as you thought you were\n");

	return EXIT_SUCCESS;
}