Explain the utility of each function in this program and their relation.
#include "stdafx.h" #include "stdio.h" struct fraction { int numerator; int denominator; }; struct fraction create(int, int); struct fraction add(struct fraction, struct fraction); struct fraction multiply(struct fraction, struct fraction); int _tmain(int argc, _TCHAR* argv[]) { struct fraction x,y, z; int m, n; printf("Numerator of fraction 1: "); scanf("%d",&m); printf("Denomintor of fraction 1: %d/", m); scanf("%d",&n); x = create(m, n); printf("Numeratorof fraction 2: "); scanf("%d",&m); printf("Denomintorof fraction 2: %d/", m); scanf("%d",&n); y = create(m, n); z = add(x, y); printf("%d/%d + %d/%d =%d/%d\n", x.numerator,x.denominator, y.numerator,y.denominator, z.numerator,z.denominator); z = multiply(x, y); printf("%d/%d × %d/%d =%d/%d\n", x.numerator,x.denominator, y.numerator,y.denominator, z.numerator,z.denominator); return 0; } struct fraction create(int x, int y) { struct fraction f; f.numerator = x; f.denominator = y; return f; } struct fraction add(struct fraction x, struct fraction y) { int m, n, i; m = x.numerator *y.denominator + y.numerator * x.denominator; n = x.denominator *y.denominator; for (i = m; i >1; i--) if (n % i == 0&& m % i == 0) { m /= i; n /= i; } x.numerator = m; x.denominator = n; return x; } struct fraction multiply(struct fraction x, struct fractiony) { int m, n, i; m = x.numerator *y.numerator; n = x.denominator *y.denominator; for (i = m; i >1; i--) if (n % i == 0&& m % i == 0) { m /= i; n /= i; } x.numerator = m; x.denominator = n; return x; }
|