works:programmer:cpp:tablicaumnozenija

Программка тест таблицы умножения для детей 1-2 класса

Для деток написал программку, что-бы им было интереснее учить таблицу умножения. Потом в программку добавилось ещё деление а потом и сложение с вычитанием.

Файл main.cpp

#pragma hdrstop
#pragma argsused
#include <tchar.h>
#include <stdio.h>
#include <math.h>
#include <windows.h>
 
#include<cstdlib>
#include<ctime>
 
#define LVL_SLOZENIE	1
#define LVL_VI4ITANIE	2
#define LVL_SLOZENIEB	4
#define LVL_VI4ITANIEB	8
#define LVL_UMNOZENIE	16
#define LVL_DELENIJE	32
#define LVL_ALL		    63
 
int ans_good = 0, ans_bad = 0;
int times[65535] = {0};
 
void calculate_avg_and_total(int &total_time, int &avg_time);
int SolveForX(int y, int z);
int SolveForY(int x, int z);
int sloznostj(void);
void cyr(const wchar_t* str); // Вывод текста на русском.
 
 
int select_operation(int qlevel, int &a, int &b); // Бывираем случайную операцию из сложности
 
 
int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE _stdout = GetStdHandle(STD_OUTPUT_HANDLE);
 
	time_t time_a, time_b;
	int total_time;
	int avg_time;
	int qlevel = sloznostj();
	int alevel;
	int a, b;
	char otvet[256];
	int iotvet;
 
 
	std::srand((unsigned int) time (NULL)); //activates the rand generator
 
	while ( true ) {
 
		// Вопрос белым цветом
		SetConsoleTextAttribute(_stdout, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
 
		// Исходя из сложности выбераем вопрос	
		alevel = select_operation(qlevel, a, b);
		// Ждём ввода ответа и засекаем время за которе ответили
		time_a = time(NULL);
		gets(otvet);
		time_b = time(NULL);
		iotvet = std::atoi(otvet);
 
		// Записывем время на ответ
		times[ans_good+ans_bad] = time_b - time_a;
 
		// Исходя из вопроса высчитаем ответ
		switch ( alevel )
		{
			case LVL_SLOZENIE  :
			case LVL_SLOZENIEB :
				if ( a + b == iotvet ) { // a+b == N
					ans_good++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
					cyr(L" правельно");
				} else {
					ans_bad++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
					cyr(L" неправельно");
					SetConsoleTextAttribute(_stdout, FOREGROUND_INTENSITY);					
					cyr(L" (ответ "); printf("%d)", a + b);
				}
			break;
			case LVL_VI4ITANIE :
			case LVL_VI4ITANIEB :
				if ( a == iotvet ) { // a+b == N
					ans_good++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
					cyr(L" правельно");
				} else {
					ans_bad++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
					cyr(L" неправельно");
					SetConsoleTextAttribute(_stdout, FOREGROUND_INTENSITY);					
					cyr(L" (ответ "); printf("%d)", a);
				}
			break;
			case LVL_UMNOZENIE :
				if ( a * b == iotvet ) { // a*b == N
					ans_good++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
					cyr(L" правельно");
				} else {
					ans_bad++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
					cyr(L" неправельно");
					SetConsoleTextAttribute(_stdout, FOREGROUND_INTENSITY);					
					cyr(L" (ответ "); printf("%d)", a * b);
				}
			break;
			case LVL_DELENIJE :
				if ( a == iotvet ) { // a*N == b
					ans_good++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
					cyr(L" правельно");
				} else {
					ans_bad++;
					SetConsoleTextAttribute(_stdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
					cyr(L" неправельно");
					SetConsoleTextAttribute(_stdout, FOREGROUND_INTENSITY);					
					cyr(L" (ответ "); printf("%d)", a);
				}
			break;
		}
 
		SetConsoleTextAttribute(_stdout, FOREGROUND_INTENSITY);
		printf(" (%d", time_b - time_a); cyr(L" секунд)\n");
 
		if (((ans_good+ans_bad) % 10 == 0 ) || ( ans_good+ans_bad == 65535 )) {
			calculate_avg_and_total(total_time, avg_time);
			cyr(L"Правельных : "); printf("%d. ", ans_good);
			cyr(L"Неправельных : "); printf("%d.\n", ans_bad);
			cyr(L"Процент праельных : "); printf("%d%%.\n", SolveForY(ans_good, ans_good+ans_bad));
			cyr(L"Времени ушло : "); printf("%d", total_time); cyr(L" секунд. "); 
			cyr(L"На один ответ : "); printf("%d", avg_time); cyr(L" секунды.\n\n"); 
		}
 
		if ( ans_good+ans_bad == 65535 ) {
			cyr(L"Всё! Конец! Нажмите ENTER для выхода\n\n");
			break;
		}
	}
	getchar();
	return 0;
}
 
void calculate_avg_and_total(int &total_time, int &avg_time)
{
	int count = ans_good+ans_bad;
	total_time = 0;
 
	for ( int i=0 ; i<count; i++) {
		total_time += times[i];
	}
 
	avg_time = total_time / count;
}
 
 
// This function solves for x in the equation "x is y% of z".
int SolveForX(int y, int z)
{
	return floor(z * (y * 0.01));
}
 
// This function solves for y in the equation "x is y% of z".
int SolveForY(int x, int z)
{
	if (z == 0) return 0;
	return floor((x * 100.0) / z);
}
 
 
void cyr(const wchar_t* str)
{
	int slen = wcslen(str);
	char* buffer = (char*) calloc(slen+1, sizeof(char));
	if (TRUE == CharToOemBuffW(str, buffer, slen+1)) {
		printf(buffer);
	}
	free(buffer);
}
 
int sloznostj(void)
{
	char cotvet[1024];
	int iotvet;
 
	do {
		cyr(L"Cложность вопросов :\n");
		cyr(L" 1  = Сложение 1-10\n");
		cyr(L" 2  = Вычитание 1-10\n");
		cyr(L" 4  = Сложение 10-100\n");
		cyr(L" 8  = Вычитание 10-100\n");
		cyr(L" 16  = Умножение\n");
		cyr(L" 32  = Деление\n");
		cyr(L" 63 = Всё сразу\n");
		cyr(L"Можно группировать, например 4+8 = сложение и вычитание от 10 до 100\n");
		cyr(L"Выберите сложность : "); gets(cotvet);
 
		iotvet = std::atoi(cotvet);
 
		if ((iotvet > 0) && (iotvet <= LVL_ALL)) {
			break;
		} else {
			cyr(L"Выберите число от 1 до ");
			printf("%d\n", LVL_ALL);
		}
	} while ( true );
	return iotvet;
}
 
 
int select_operation(int qlevel, int &a, int &b)
{
	int chance;
	while ( true ) {
		if (((qlevel & LVL_DELENIJE) == LVL_DELENIJE) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%9+1;
			b = std::rand()%9+1;
			printf(" %d / %d = ", a*b, b);
			return LVL_DELENIJE;
		}
		if (((qlevel & LVL_UMNOZENIE) == LVL_UMNOZENIE) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%9+1;
			b = std::rand()%9+1;
			printf("%d * %d = ", a, b);
			return LVL_UMNOZENIE;
		}
		if (((qlevel & LVL_VI4ITANIEB) == LVL_VI4ITANIEB) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%99+1;
			b = std::rand()%99+1;
			printf(" %d - %d = ", a+b, b);
			return LVL_VI4ITANIEB;
		}
		if (((qlevel & LVL_SLOZENIEB) == LVL_SLOZENIEB) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%89+10;
			b = std::rand()%89+10;
			printf(" %d + %d = ", a, b);
			return LVL_SLOZENIEB;
		}
		if (((qlevel & LVL_VI4ITANIE) == LVL_VI4ITANIE) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%9+1;
			b = std::rand()%9+1;
			printf(" %d - %d = ", a+b, b);
			return LVL_VI4ITANIE;
		}
		if (((qlevel & LVL_SLOZENIE) == LVL_SLOZENIE) && ( std::rand()%10 == 5 ))
		{
			a = std::rand()%9+1;
			b = std::rand()%9+1;
			printf(" %d + %d = ", a, b);
			return LVL_SLOZENIE;
		}
	}
}

Проект для C++ Builder XE5 : tablicaumnoz.zip

works/programmer/cpp/tablicaumnozenija.txt · Last modified: 2018/12/15 17:18 (external edit)