Created
April 3, 2025 17:45
-
-
Save Helen460/db5c5e8b3cc2ffe733c7dab7862f005e to your computer and use it in GitHub Desktop.
6DZ
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
// 1. Написати функцію Line, яку можна буде викликати так : Line(20, '@', 12, true); І при цьому горизонтально буде намальована лінія, що складається з 20 «собачок» червоного кольору.Якщо передати в останньому параметрі false, то лінія стане вертикальною. | |
#include <iostream> | |
#include <windows.h> | |
using namespace std; | |
void Line(int length, char symbol, int color, bool isHorizontal) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(h, color); | |
if (isHorizontal) { | |
for (int i = 0; i < length; i++) { | |
cout << symbol; | |
} | |
} | |
else { | |
for (int i = 0; i < length; i++) { | |
cout << symbol << "\n"; | |
} | |
} | |
cout << "\n"; | |
} | |
int main() { | |
Line(20, '@', 12, true); | |
Line(12, '@', 12, false); | |
} | |
// 2. Написати функцію Rectangle, яка виводить на екран прямокутник.Функція приймає такі параметри : ширина, висота, символ рамки, символ заливки, колір рамки, колір заливки, координати верхнього лівого кута по X та Y.У функції мають бути параметри за замовчуванням. | |
#include <iostream> | |
#include <windows.h> | |
using namespace std; | |
void Rectangle(int width = 10, int height = 5, char border = '#', char fill = ' ', int borderColor = 12, int fillColor = 7, int x = 0, int y = 0) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
COORD position; | |
SetConsoleTextAttribute(h, borderColor); | |
for (int i = 0; i < width; i++) { | |
position.X = x + i; | |
position.Y = y; | |
SetConsoleCursorPosition(h, position); | |
cout << border; | |
} | |
for (int i = 1; i < height - 1; i++) { | |
position.X = x; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
cout << border; | |
position.X = x + width - 1; | |
SetConsoleCursorPosition(h, position); | |
cout << border; | |
} | |
for (int i = 0; i < width; i++) { | |
position.X = x + i; | |
position.Y = y + height - 1; | |
SetConsoleCursorPosition(h, position); | |
cout << border; | |
} | |
SetConsoleTextAttribute(h, fillColor); | |
for (int i = 1; i < height - 1; i++) { | |
for (int j = 1; j < width - 1; j++) { | |
position.X = x + j; | |
position.Y = y + i; | |
SetConsoleCursorPosition(h, position); | |
cout << fill; | |
} | |
} | |
cout << "\n"; | |
} | |
int main() { | |
Rectangle(20, 10, '#', '.', 12, 7, 5, 5); | |
} | |
// 3. Написати функцію, яка повертає куб переданого числа. | |
#include <iostream> | |
using namespace std; | |
int cube(int num) { | |
return num * num * num; | |
} | |
int main() { | |
int number = 5; | |
cout << "Cube of " << number << " - " << cube(number) << "\n"; | |
} | |
// 4. Написати функцію, яка перевіряє, чи є передане число простим.Число називається простим, якщо воно ділиться без залишку тільки на себе і на одиницю. | |
#include <iostream> | |
using namespace std; | |
bool isPrime(int num) { | |
if (num <= 1) return false; | |
for (int i = 2; i * i <= num; i++) { | |
if (num % i == 0) return false; | |
} | |
return true; | |
} | |
int main() { | |
int num = 17; | |
if (isPrime(num)) { | |
cout << num << " Prime!\n"; | |
} | |
else { | |
cout << num << " Not prime!\n"; | |
} | |
} | |
// 5. Написати функцію, яка отримує як параметри два цілі числа і повертає суму чисел із діапазону між ними. | |
#include <iostream> | |
using namespace std; | |
int sum_between(int a, int b) { | |
int sum = 0; | |
if (a > b) { | |
int temp = a; | |
a = b; | |
b = temp; | |
} | |
for (int i = a; i <= b; i++) { | |
sum += i; | |
} | |
return sum; | |
} | |
int main() { | |
cout << "Sum between 3 and 6: " << sum_between(3, 6) << "\n"; | |
} | |
// 6. Написати функцію, яка приймає дві дати(тобто функція приймає шість параметрів) і обчислює різницю в днях між цими датами.Для вирішення цієї задачі також потрібно написати функцію, яка визначає, чи є рік високосним. | |
#include <iostream> | |
using namespace std; | |
bool is_leap_year(int year) { | |
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); | |
} | |
int days_in_month(int month, int year) { | |
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { | |
return 31; | |
} | |
else if (month == 4 || month == 6 || month == 9 || month == 11) { | |
return 30; | |
} | |
else if (month == 2) { | |
return is_leap_year(year) ? 29 : 28; | |
} | |
return 0; | |
} | |
int days_since_epoch(int day, int month, int year) { | |
int total_days = 0; | |
for (int y = 1; y < year; ++y) { | |
total_days += is_leap_year(y) ? 366 : 365; | |
} | |
for (int m = 1; m < month; ++m) { | |
total_days += days_in_month(m, year); | |
} | |
total_days += day; | |
return total_days; | |
} | |
int days_between(int d1, int m1, int y1, int d2, int m2, int y2) { | |
int days1 = days_since_epoch(d1, m1, y1); | |
int days2 = days_since_epoch(d2, m2, y2); | |
return abs(days2 - days1); | |
} | |
int main() { | |
cout << "Days between 01/01/2000 and 01/01/2021: " << days_between(1, 1, 2000, 1, 1, 2021) << " days\n"; | |
} | |
// 7. Функція малює гарний кольоровий гральний кубик.Як параметри передаються випадкові колір, координати і значення кубика. | |
#include <iostream> | |
#include <windows.h> | |
using namespace std; | |
enum class Color : unsigned short { BLACK, DARKBLUE, DARKGREEN, TURQUOISE, DARKRED, PURPLE, DARKYELLOW, GREY, DARKGREY, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE }; | |
void draw_dice(int x, int y, int value, Color color) { | |
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(h, (WORD)color); | |
COORD position; | |
position.X = x; | |
position.Y = y; | |
SetConsoleCursorPosition(h, position); | |
string dice_faces[] = { | |
" ----- \n| |\n| o |\n| |\n ----- ", | |
" ----- \n| o |\n| |\n| o |\n ----- ", | |
" ----- \n| o |\n| o |\n| o |\n ----- ", | |
" ----- \n| o o |\n| |\n| o o |\n ----- ", | |
" ----- \n| o o |\n| o |\n| o o |\n ----- ", | |
" ----- \n| o o |\n| o o |\n| o o |\n ----- " | |
}; | |
cout << dice_faces[value - 1] << "\n"; | |
} | |
int main() { | |
int value = 3; | |
draw_dice(10, 5, value, Color::YELLOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment