Created
April 11, 2025 19:19
-
-
Save VKONSTANTINIUS/93bfc5daaa975bd238956940a3ea1d1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <windows.h> | |
#include <cstdlib> | |
#include <ctime> | |
#include <algorithm> | |
using namespace std; | |
//1. Написати функцію Line, яку можна буде викликати так : Line(20, '@', 12, true); | |
//І при цьому горизонтально буде намальована лінія, що складається з 20 «собачок» червоного кольору. | |
//Якщо передати в останньому параметрі false, то лінія стане вертикальною. | |
void Line(int lenth, string symb, int color, bool way) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(h, color); | |
for (int i = 1; i <= lenth; i++) { | |
if (way == true) { | |
cout << symb << " "; | |
} | |
else { | |
cout << symb << "\n"; | |
} | |
} | |
cout << "\n"; | |
} | |
//2. Написати функцію Rectangle, яка виводить на екран прямокутник. | |
//Функція приймає такі параметри : ширина, висота, символ рамки, символ заливки, колір рамки, колір заливки, координати верхнього лівого кута по X та Y. | |
//У функції мають бути параметри за замовчуванням. | |
void Rectangle( | |
int width = 5, int height = 5, | |
string border = "0", string inn = "*", | |
int border_color = 13, int inn_color = 12, | |
int x = 5, int y = 22) { | |
for (int i = 0; i < height; ++i) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < width; ++j) { | |
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) { | |
SetConsoleTextAttribute(h, border_color); | |
cout << border << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, inn_color); | |
cout << inn << " "; | |
} | |
} | |
} | |
cout << "\n"; | |
} | |
//3. Написати функцію, яка повертає куб переданого числа. | |
int Cube(int num) { | |
return num * num * num; | |
} | |
//4. Написати функцію, яка перевіряє, чи є передане число простим. | |
//Число називається простим, якщо воно ділиться без залишку тільки на себе і на одиницю. | |
void simple_num(int num) { | |
int count = 0; | |
if (num <= 1) cout << "Ваше число - " << num << " - не просте\n"; | |
else { | |
for (int i = 2; i <= num; i++) | |
if (num % i == 0) count += 1; | |
if (count > 1 and num != 1) cout << "Ваше число - " << num << " - не просте\n"; | |
else cout << "Ваше число - " << num << " - просте\n"; | |
} | |
} | |
//5. Написати функцію, яка отримує як параметри два цілі числа і повертає суму чисел із діапазону між ними. | |
int segment_sum(int a, int b) { | |
int sum = 0; | |
for (int i = a; i <= b; ++i) | |
sum += i; | |
return sum; | |
} | |
//6. Написати функцію, яка приймає дві дати(тобто функція приймає шість параметрів) і обчислює різницю в днях між цими датами. | |
//Для вирішення цієї задачі також потрібно написати функцію, яка визначає, чи є рік високосним. | |
bool leap_year(int year) { | |
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; | |
else return false; | |
} | |
int days_beetween(int day1, int month1, int year1, | |
int day2, int month2, int year2) { | |
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; | |
int days_start = day1; | |
int days_finish = day2; | |
for (int i1 = 0; i1 < (month1-1); i1++) { | |
days_start += days[i1]; | |
} | |
if (month1 > 2 && leap_year(year1)) { | |
days_start += 1; | |
} | |
for (int i2 = 0; i2 < year1; i2++) { | |
if (leap_year(i2)) { | |
days_start += 366; | |
} | |
else { | |
days_start += 365; | |
} | |
} | |
for (int j1 = 0; j1 < (month2-1); j1++) { | |
days_finish += days[j1]; | |
} | |
if (month2 > 2 && leap_year(year2)) { | |
days_finish += 1; | |
} | |
for (int j2 = 0; j2 < year2; j2++) { | |
if (leap_year(j2)) { | |
days_finish += 366; | |
} | |
else { | |
days_finish += 365; | |
} | |
} | |
return (days_finish - days_start); | |
} | |
//7. Функція малює гарний кольоровий гральний кубик. | |
// Як параметри передаються випадкові колір, координати і значення кубика. | |
void roll_dice() { | |
cout << "\n"; | |
srand(time(0)); | |
int choise; | |
int x = rand() % 61; | |
int y = rand() % 16; | |
for (int t = 1; t < 44; t++) { | |
choise = rand() % 6 + 1; | |
} | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
if (GetAsyncKeyState(VK_ESCAPE)) | |
{ | |
system("cls"); | |
cout << "ESCAPE!\n"; | |
exit(0); | |
} | |
for (int i = 0; i < 30; ++i) { | |
for (int j = 0; j < 80; ++j) { | |
SetConsoleTextAttribute(h, 0); | |
cout << " "; | |
} | |
} | |
switch (choise) { | |
case 1: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || (i == 4 && j == 4)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
case 2: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || | |
(i == 2 && j == 6) || (i == 6 && j == 2)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
case 3: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || | |
(i == 4 && j == 4) || (i == 2 && j == 6) || (i == 6 && j == 2)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
case 4: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || | |
(i == 2 && j == 6) || (i == 6 && j == 2) || | |
(i == 2 && j == 2) || (i == 6 && j == 6)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
case 5: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || | |
(i == 2 && j == 6) || (i == 6 && j == 2) || | |
(i == 2 && j == 2) || (i == 6 && j == 6) || (i == 4 && j == 4)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
case 6: | |
for (int i = 0; i < 9; ++i) { | |
COORD position; | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
for (int j = 0; j < 9; ++j) { | |
if (i == 0 || i == 8 || j == 0 || j == 8 || | |
(i == 2 && j == 6) || (i == 6 && j == 2) || | |
(i == 2 && j == 2) || (i == 6 && j == 6) || | |
(i == 4 && j == 2) || (i == 4 && j == 6)) { | |
SetConsoleTextAttribute(h, 7 * 16); | |
cout << " "; | |
} | |
else { | |
SetConsoleTextAttribute(h, choise * 16); | |
cout << " "; | |
} | |
} | |
cout << "\n"; | |
} | |
cout << "\n"; | |
break; | |
} | |
cout << "\nPress Esc to Exit\n"; | |
system("pause"); | |
roll_dice(); | |
} | |
int main() | |
{ | |
SetConsoleOutputCP(1251); | |
cout << "Hello World Again!\n\n"; | |
Line(20, " ", 1 * 16, true); | |
Line(20, " ", 2 * 16, true); | |
Line(20, " ", 3 * 16, true); | |
Line(20, " ", 4 * 16, true); | |
Line(20, " ", 5 * 16, true); | |
Line(20, " ", 6 * 16, true); | |
Line(20, " ", 7 * 16, true); | |
Line(20, " ", 8 * 16, true); | |
Line(20, " ", 9 * 16, true); | |
Line(20, " ", 10 * 16, true); | |
Line(20, " ", 11 * 16, true); | |
Line(20, " ", 12 * 16, true); | |
Line(20, " ", 13 * 16, true); | |
Line(20, "@", 2, false); | |
system("pause"); | |
Rectangle(9, 9, " ", " ", 16, 32, 41, 5); | |
Rectangle(); | |
system("pause"); | |
cout << "\n\n\tЗАВДАННЯ 3\n\n"; | |
int num; | |
cout << "Введіть число "; | |
cin >> num; | |
cout << "\nКуб " << num << " дорівнює: " << Cube(num) << endl; | |
system("pause"); | |
cout << "\n\n\tЗАВДАННЯ 4\n\n"; | |
simple_num(1); | |
simple_num(3); | |
simple_num(4); | |
system("pause"); | |
cout << "\n\n\tЗАВДАННЯ 5\n\n"; | |
cout << "Cума в діапазоні від 2 до 4 дорівнює " << segment_sum(2, 4); | |
system("pause"); | |
cout << "\n\n\tЗАВДАННЯ 6\n\n"; | |
cout <<"\n Кількість днів між датами 01.01.2020-01.03.2020 - " << days_beetween(1, 1, 2020, 1, 3, 2020); | |
cout << "\n Кількість днів між датами 01.01.2025-01.01.2026 - " << days_beetween(1, 1, 2025, 1, 1, 2026); | |
system("pause"); | |
roll_dice(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment