Programming Practice

セ祘Α策琌糶 C 祘Α粂ēΤ瞶计 (rational numbers) 盡  (project, rational_number.dev) 安砞Τ瞶计 p/q 琌ㄏノㄢが借俱计 p q ㄓボ q ぃ箂狦 q 箂砞﹚赣Τ瞶计 0/1  p 0 q 1 Τ瞶计 盡Τ瞶计ざ rational.h 繷郎Τ瞶计龟 rational.c ﹍郎 ㎝莱ノ祘Α rational_main.c ﹍郎 rational.h いΤ瞶计戈篈㎝捌祘Α

typedef struct {

int nume; // numerator (だ)

int deno;  // denominator (だダ)

} rational;

rational plus(rational, rational);

// Rational number subtraction (Τ瞶计搭猭)

rational minus(rational, rational);

// Rational number multiplication (Τ瞶计猭)

rational time(rational, rational);

// Rational number division (Τ瞶计埃猭)

rational divide(rational, rational);

// Convert a pair of integers to a rational number (盢癸俱计锣传ΘΤ瞶计)

rational ii2r(int, int);

// Convert an integer to a rational number (盢俱计锣传ΘΤ瞶计)

rational i2r(int);

// Print a rational number (Τ瞶计)

void printr(rational);

狦Τ瞶计 だ㎝だダぃが借盢だ㎝だダ埃ㄤ程そ计 (叫把σ程そ计 greatest common divisor 捌祘Α gcd.c) 讽Τ瞶计璝赣Τ瞶计タΤ瞶计玥ぃタ璽腹璝赣Τ瞶计璽Τ瞶计玥璽腹璝赣Τ瞶计箂玥 0/1

rational_main.c 祘ΑいΤ瞶计跑计 a, b, c癸–Τ瞶计跑计弄ㄢ俱计 (だ㎝だダ) 锣Θ赣跑计盢  a, b, c 块璸衡の块衡砃Α

1. a + b

2. a - b

3. a * b

4. a / b

5. a * c + b / a

6. a * (c - b) / (b + c * a)

祘Α磅︽絛ㄒ

 Enter the numerator and denomiator of a: 12 14 Rational number a: 6/7 Enter the numerator and denomiator of b: 30 21 Rational number b: 10/7 Enter the numerator and denomiator of c: 6 5 Rational number c: 6/5 a + b = 16/7 a - b = -4/7 a * b = 60/49 a / b = 3/5 a * c + b / a = 283/105 a * (c - b) / (b + c * a) = -24/301 