This repository has been archived on 2026-07-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
college-work/Задания по C и C++/c/16.c
T

63 lines
1.5 KiB
C
Executable File

/*
16.Вычислить разность двух обыкновенных дробей. Ответ дать в виде обыкновенной и в виде десятичной дробей. Числители и знаменатели относятся к целому типу данных.*/
#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;
}