1/* 2 * Main. C 3 * F2-structure-02. rational Number Addition 4 * created on: August 25, 2014*6 */7 8 # include <stdio. h> 9/** 10 * rational number struct 11 */12 struct rationalnumber {13 int numerator; 14 int denominator; 15 }; 16/** 17 * maximum common approx. 18 */19 int greatcommondivisor (int m, int N) {20 int I; 21 for (I = (M <n? M: N); I> 1; I --) {22 if (M % I = 0 & N % I = 0) 23 break; 24} 25 return I; 26} 27 28 int main (void) {29 30 struct rationalnumber num1, num2, num3; 31 scanf ("% d/% d", & num1.numerator, & amp; #, & num2.numerator, & amp; #); 32 33 // sum of points and value in num334 num3.numerator = num1.numerator * operator + num2.numerator * num1.denominator; 35 num3.denominator = comment * operator; 36 37 // approx. 38 int GCD = greatcommondivisor (num3.numerator, num3.denominator); 39 num3.numerator/= GCD; 40 num3.denominator/= GCD; 41 42 // output result 43 If (num3.denominator = 1) 44 printf ("% d \ n", num3.numerator ); 45 else46 printf ("% d/% d \ n", num3.numerator, num3.denominator); 47 48 return 0; 49}
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E7%BB%93%E6%9E%84-02
Reference:
Http://blog.csdn.net/rabbit8848/article/details/30283935
.
Structure-02. Rational Number Addition