diff --git a/lab9/t1.c b/lab9/t1.c new file mode 100644 index 0000000..f0b3d00 --- /dev/null +++ b/lab9/t1.c @@ -0,0 +1,97 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +// Lab 10 Task 1 + +typedef struct { + uint16_t year; + uint16_t month; + uint16_t day; +} Date; + +typedef struct { + uint8_t r : 3; + uint8_t f : 3; +} ChessSquare; + +typedef struct { + float x, y; +} Point; + +typedef struct { + Point a, b; +} AARect; + +typedef struct { + size_t n; + // array from n+1 element; + double* coefficients; +} Poly; + +void input_date(Date* date) { + scanf("%" SCNu16 "%" SCNu16 "%" SCNu16, &date->day, &date->month, &date->year); +} + +void print_date(const Date* date) { + printf("%" PRIu16 ".%" PRIu16 ".%" PRIu16, date->day, date->month, date->year); +} + +bool input_square(ChessSquare* square) { + char f, r; + if (2 != scanf("%c%c", &f, &r)) + return false; + f = (char)toupper(f); + if (f < 'A' || f > 'H' || r < '1' || r > '7') + return false; + square->f = f - 'A'; + square->r = r - '1'; + return true; +} + +void print_square(const ChessSquare* square) { + printf("%c%c", (char)(square->f + 'A'), (char)(square->r + '1')); +} + +void input_rect(AARect* rect) { + scanf("%f%f%f%f", &rect->a.x, &rect->a.y, &rect->b.x, &rect->b.y); +} + +void print_rect(const AARect* rect) { + printf("(%f, %f) (%f, %f)", rect->a.x, rect->a.y, rect->b.x, rect->b.y); +} + +void free_poly(Poly* poly) { + if (poly->coefficients) { + free(poly->coefficients); + } +} + +void input_poly(Poly* poly) { + free_poly(poly); + size_t n; + scanf("%zu", &n); + poly->n = n; + poly->coefficients = (double*)malloc(sizeof(double) * (n+1)); + for (size_t i = 0; i <= n; i++) scanf("%lf", poly->coefficients+i); +} + +void print_poly(const Poly* poly) { + if (!poly || !poly->coefficients) return; + printf("%zu", poly->n); + for (size_t i = poly->n; i >= 1; i--) + printf("%lf * x^%zu + ", poly->coefficients[i], i); + printf("%lf", poly->coefficients[0]); +} + +int main(void){ + Poly poly = { 0 }; + input_poly(&poly); + poly.coefficients[0] = 42; + print_poly(&poly); + free_poly(&poly); +} \ No newline at end of file diff --git a/lab9/t14a.c b/lab9/t14a.c new file mode 100644 index 0000000..9b6a91f --- /dev/null +++ b/lab9/t14a.c @@ -0,0 +1,44 @@ +#include + +struct Person { + int nameNumber; + char gender; + float height; +}; + +float averageHeightOfWomen(struct Person L[], struct Person P[], int sizeL, int sizeP) { + int womenCount = 0; + float totalHeight = 0; + + for (int i = 0; i < sizeP; ++i) { + if (P[i].gender == 'F') { + for (int j = 0; j < sizeL; ++j) { + if (L[j].nameNumber == P[i].nameNumber) { + totalHeight += L[j].height; + womenCount++; + break; + } + } + } + } + + if (womenCount > 0) { + return totalHeight / womenCount; + } else { + return 0.0; + } +} + +int main() { + struct Person L[] = {{1, 'M', 175.5}, {2, 'F', 160.0}, {3, 'M', 180.0}}; + struct Person P[] = {{2, 'F', 165.5}, {1, 'M', 178.0}}; + + int sizeL = sizeof(L) / sizeof(L[0]); + int sizeP = sizeof(P) / sizeof(P[0]); + + float averageWomenHeight = averageHeightOfWomen(L, P, sizeL, sizeP); + + printf("AFH: %.2f\n", averageWomenHeight); + + return 0; +} diff --git a/lab9/t14b.c b/lab9/t14b.c new file mode 100644 index 0000000..46321c1 --- /dev/null +++ b/lab9/t14b.c @@ -0,0 +1,46 @@ +#include + +struct Person { + int nameNumber; + char gender; + float height; +}; + +int findTallestMan(struct Person L[], struct Person P[], int sizeL, int sizeP) { + int tallestManIndex = -1; + + float maxManHeight = 0; + + for (int i = 0; i < sizeP; ++i) { + if (P[i].gender == 'M') { + for (int j = 0; j < sizeL; ++j) { + if (L[j].nameNumber == P[i].nameNumber) { + if (P[i].height > maxManHeight) { + maxManHeight = P[i].height; + tallestManIndex = j; + } + break; + } + } + } + } + + return tallestManIndex; +} + +int main() { + struct Person L[] = {{1, 'M', 175.5}, {2, 'F', 160.0}, {3, 'M', 180.0}}; + struct Person P[] = {{2, 'F', 165.5}, {1, 'M', 178.0}, {3, 'M', 185.0}}; + + int sizeL = sizeof(L) / sizeof(L[0]); + int sizeP = sizeof(P) / sizeof(P[0]); + int indexTallestMan = findTallestMan(L, P, sizeL, sizeP); + + if (indexTallestMan != -1) { + printf("highest man name: %d\n", L[indexTallestMan].nameNumber); + } else { + printf("no high man.\n"); + } + + return 0; +} diff --git a/lab9/t2.c b/lab9/t2.c new file mode 100644 index 0000000..6c8d1c8 --- /dev/null +++ b/lab9/t2.c @@ -0,0 +1,46 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include +#include +#include + +// Lab 10 Task 2 + +typedef struct { + uint8_t r : 3; + uint8_t f : 3; +} ChessSquare; + +bool input_square(ChessSquare* square) { + char f, r; + if (2 != scanf("%c%c", &f, &r)) + return false; + f = (char)toupper(f); + if (f < 'A' || f > 'H' || r < '1' || r > '7') + return false; + square->f = f - 'A'; + square->r = r - '1'; + return true; +} + +void print_square(const ChessSquare* square) { + printf("%c%c", (char)(square->f + 'A'), (char)(square->r + '1')); +} + +bool can_queen_move(ChessSquare from, ChessSquare to) { + return from.r == to.r || from.f == to.f || + from.r - from.f == to.r - to.f || + from.r + from.f == to.r + to.f; +} + +int main(void) { + ChessSquare a, b; + bool is_corect_input = input_square(&a) && input_square(&b); + if (!is_corect_input) { + printf("incorrect input\n"); + return 0; + } + printf("%s", can_queen_move(a, b) ? "true" : "false"); +} \ No newline at end of file diff --git a/lab9/t3.c b/lab9/t3.c new file mode 100644 index 0000000..eb35f04 --- /dev/null +++ b/lab9/t3.c @@ -0,0 +1,46 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include + +// Lab 10 Task 3 + +typedef struct { + uint16_t year; + uint16_t month; + uint16_t day; +} Date; + +bool is_leap_year(uint16_t y) { + return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; +} + +uint16_t days_in_month(uint16_t m, bool is_leap) { + const uint16_t d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + return d[m] + (uint16_t)(is_leap && m == 1); +} + +Date next_day(Date date) { + date.day += 1; + bool is_leap = is_leap_year(date.year); + uint16_t d = days_in_month(date.month, is_leap); + if (date.day < d) return date; + date.day -= d; + date.month += 1; + if (date.month < 12) return date; + date.month -= 12; + date.year += 1; + return date; +} + +int main(void) { + // today date: 26.10.23 + const Date today = (Date){ + .day = 25, + .month = 9, + .year = 2022, + }; + + const Date tomorrow = next_day(today); + printf("%02hu.%02hu.%04hu", tomorrow.day+1, tomorrow.month+1, tomorrow.year+1); +} \ No newline at end of file diff --git a/lab9/t4.c b/lab9/t4.c new file mode 100644 index 0000000..3cc59fd --- /dev/null +++ b/lab9/t4.c @@ -0,0 +1,69 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include + +// Lab 10 Task 4 + +typedef struct { + int nomirator; + unsigned denomirator; +} Rational; + +inline Rational rat_from_int(int num) { + return (Rational) { num, 1 }; +} + +inline void rat_display(Rational rat) { + printf("%d/%u", rat.nomirator, rat.denomirator); +} + +inline unsigned gcd_impl(unsigned a, unsigned b) { + if (b == 0) return a; + return gcd_impl(b, a%b); +} + +inline Rational rat_simplify(Rational rat) { + unsigned nom = (unsigned)abs(rat.nomirator); + unsigned common = gcd_impl(nom, rat.denomirator); + rat.nomirator /= common; + rat.denomirator /= common; + return rat; +} + +inline Rational rat_add(Rational a, Rational b) { + b.nomirator *= a.denomirator; + a.nomirator *= b.denomirator; + a.denomirator *= b.denomirator; + a.nomirator += b.nomirator; + return rat_simplify(a); +} + +inline Rational rat_sub(Rational a, Rational b) { + b.nomirator *= a.denomirator; + a.nomirator *= b.denomirator; + a.denomirator *= b.denomirator; + a.nomirator -= b.nomirator; + return rat_simplify(a); +} + +inline Rational rat_mul(Rational a, Rational b) { + a.nomirator *= b.nomirator; + a.denomirator *= b.denomirator; + return rat_simplify(a); +} + +inline Rational rat_div(Rational a, Rational b) { + a.denomirator *= b.nomirator; + a.nomirator *= b.denomirator; + return rat_simplify(a); +} + +inline int rat_cmp(Rational a, Rational b) { + return a.nomirator * b.denomirator - b.nomirator * a.denomirator; +} + +int main(void) { + // TODO +} \ No newline at end of file diff --git a/lab9/t5.c b/lab9/t5.c new file mode 100644 index 0000000..d74e10e --- /dev/null +++ b/lab9/t5.c @@ -0,0 +1,73 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +#include + +// Lab 10 Task 5 + +#define MAX_NAME_SIZE 32 + +typedef struct { + const char* name; + double height; +} MountainData; + +MountainData mntn_input() { + char* name = malloc(MAX_NAME_SIZE); + MountainData data = { + .name = name, + }; + printf("name ?= "); + scanf("%s", name); + printf("height ?= "); + scanf("%lf", &data.height); + return data; +} + +void mntn_display(const MountainData* data) { + printf("%s: %lf\n", data->name, data->height); +} + +int mntn_find_heightest(const MountainData* data, int count) { + double h = -1.; + int index = -1; + for (int i = 0; i < count; i++) { + if (data[i].height > h) { + h = data[i].height; + index = i; + } + } + return index; +} + +int mntn_find_by_name(const MountainData* data, int count, const char* name) { + for (int i = 0; i < count; i++) { + if (strcmp(data[i].name, name) == 0) { + return i; + } + } + return -1; +} + +int main(void) { + unsigned n; + scanf("%n", &n); + MountainData* data = (MountainData*)malloc(sizeof(MountainData) * n); + for (unsigned i = 0; i < n; i++) { + data[i] = mntn_input(); + } + + char name_buf[MAX_NAME_SIZE]; + scanf("%s", name_buf); + + int index = mntn_find_by_name(data, n, name_buf); + if (index >= 0) { + printf("h: %lf\n", data[index].height); + } + + for (unsigned i = 0; i < n; i++) { + free((void*)data[i].name); + } + free(data); +} \ No newline at end of file diff --git a/lab9/t6d.c b/lab9/t6d.c new file mode 100644 index 0000000..fc8b977 --- /dev/null +++ b/lab9/t6d.c @@ -0,0 +1,47 @@ +#include +#include + +struct Seminar { + char subject[100]; + char instructor[100]; + int groupNumber; + char dayOfWeek[20]; + int hours; + char classroom[20]; +}; + +void inputSeminar(struct Seminar *seminar) { + printf("subject: "); + scanf("%s", seminar->subject); + + printf("Prof: "); + scanf("%s", seminar->instructor); + + printf("Group: "); + scanf("%d", &(seminar->groupNumber)); + + printf("Day: "); + scanf("%s", seminar->dayOfWeek); + + printf("Hours: "); + scanf("%d", &(seminar->hours)); + + printf("class: "); + scanf("%s", seminar->classroom); +} + +int main() { + struct Seminar mySeminar; + + inputSeminar(&mySeminar); + + printf("\nData:\n"); + printf("subject: %s\n", mySeminar.subject); + printf("Prof: %s\n", mySeminar.instructor); + printf("Group: %d\n", mySeminar.groupNumber); + printf("Day: %s\n", mySeminar.dayOfWeek); + printf("Hours: %d\n", mySeminar.hours); + printf("Class: %s\n", mySeminar.classroom); + + return 0; +}