Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions lab9/t1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdlib.h>
#include <ctype.h>

// 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);
}
44 changes: 44 additions & 0 deletions lab9/t14a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>

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;
}
46 changes: 46 additions & 0 deletions lab9/t14b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>

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;
}
46 changes: 46 additions & 0 deletions lab9/t2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdlib.h>
#include <ctype.h>

// 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");
}
46 changes: 46 additions & 0 deletions lab9/t3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

// 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);
}
69 changes: 69 additions & 0 deletions lab9/t4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>

// 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
}
Loading