Работы по программированию

This commit is contained in:
IgorVolochay
2026-07-28 09:56:54 +03:00
parent b85155ff95
commit 8602b7e845
73 changed files with 4090 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
/*
15.Вычислить сумму двух обыкновенных дробей. Ответ дать в виде обыкновенной и в виде десятичной дробей. Числители и знаменатели относятся к целому типу данных.
*/
#include <stdio.h>
#include <math.h>
typedef struct Fraction {
int up;
int down;
} fraction;
int GCD(int a, int b) {
int result = (a < b) ? a : b;
while (result > 0)
{
if (a % result == 0 && b % result == 0)
{
break;
}
result--;
}
return result;
}
int LCM(int a, int b) {
return a / GCD(a, b) * b;
}
void printSumFraction(fraction fraction_1, fraction fraction_2) {
int a = fraction_1.up;
int b = fraction_1.down;
int c = fraction_2.up;
int d = fraction_2.down;
/*
a c
- + -
b d
*/
int lcm = LCM(b, d);
int answer_up = a * (lcm / b) + c * (lcm / d);
printf("%2d %2d %2d * %2d + %2d %2d\n", a, c, a, lcm / b, c*lcm / d, a * (lcm / b) + c * (lcm / d));
printf("-- + -- = ------------ = -- = %.3lf\n", (double)answer_up/lcm);
printf("%2d %2d %2d %2d\n", b, d, lcm, lcm);
}
int main()
{
fraction first;
fraction second;
printf("Enter a b c and d: ");
scanf("%d %d %d %d", &first.up, &first.down, &second.up, &second.down);
printSumFraction(first, second);
return 0;
}