The subject asks to write a procedure, calculates 2 rational number's and, difference, product, quotient.
Input Format:
The input gives a rational number in the form of "A1/b1 a2/b2" in a row in two fractions, where the numerator and denominator are all integers within the integer range, and the minus sign may appear only in front of the numerator, and the denominator is not 0.
output Format:
In 4 rows, according to the "rational number 1 operator rational number 2 = result" In the format order Output 2 rational number of the and, difference, product, quotient. Note that each rational number of the output must be the simplest form "K A/b" of the rational number, where k is the integer part, A/b is the simplest fraction, and if it is negative, parentheses are required, and if the division denominator is 0, the "Inf" is output. The title guarantees that the correct output does not exceed integers in the integer range.
Input Sample 1:
2/3 -4/2
Output Example 1:
2/3 + (-2) = (-1 1/3) 2/3-(-2) = 2 2/32/3 * (-2) = (-1 1/3) 2/3/(-2) = ( -1/3)
Input Sample 2:
5/3 0/6
Output Example 2:
1 2/3 + 0 = 1 2/31 2/3-0 = 1 2/31 2/3 * 0 = 2/3/0 = INF
This problem cost me a long time to AC, clearly looks very simple, but actually did a bit, only to find the deep pit. At first I use the process-oriented thinking to do, write to the fourth nested if-else when the code structure is not very good, so with the object-oriented thinking to solve it, the code readability has become better than before, the following is my code:
//1034#include <iostream>#include<stdexcept>#include<string>using namespacestd;Long LongCfact (Long LongALong Longb) { Long LongMin = a > B?b:a; Long Longmax = a < b?b:a; Long LongTMP = max%min; while(TMP) {max=min; Min=tmp; TMP= max%min; } Long Longresult = min >0? Min:-min; returnresult;}structfraction{Long LongMem; Long Longden; Long LongTHD =0; Long LongSim () {if(Mem = =0) {THD=0; return 1; } Else if(Den = =0) { return-1; } Else if(Mem%den = =0) {THD= MEM/den; return 1; } Else{THD= MEM/den; Long LongCF =cfact (Mem, den); if(THD! =0) {mem= ((Mem%den) >0? Mem%den:-(Mem%den))/CF; } Else{mem= MEM/CF; } den= den/CF; if(Den <0) {den= -den; Mem= -Mem; } return 0; } }; Friend Fractionoperator+ (fraction &f1,fraction &F2); Friend Fractionoperator-(Fraction &f1,fraction &F2); Friend Fractionoperator* (Fraction &f1,fraction &F2); Friend Fractionoperator/(Fraction &f1,fraction &F2); stringShow () {stringRes; Long LongTMP =Sim (); if(TMP = =1) if(THD <0) Res="("+ to_string (THD) +")"; //printf ("(%LLD)", THD); Else{res=to_string (THD); //printf ("%lld", THD); } Else if(TMP = =-1) {res="INF"; } Else { if(THD = =0) { if(Mem <0) Res='('+ to_string (MEM) +'/'+ to_string (DEN) +')'; //printf ("(%LLD/%LLD)", Mem, den); ElseRes= To_string (MEM) +'/'+to_string (DEN); //printf ("%lld/%lld", Mem, den); } Else { if(THD <0) //printf ("(%lld%lld/%lld)", THD, Mem, den);res ='('+ to_string (THD) +' '+ to_string (MEM) +'/'+ to_string (DEN) +')'; ElseRes= To_string (THD) +' '+ to_string (MEM) +'/'+to_string (DEN); //printf ("%lld%lld/%lld", Thd,mem,den); } } returnRes; }};fractionoperator+ (fraction &f1, fraction &F2) { Long LongCF =cfact (F1.den, F2.den); Long LongCM = cf* (F1.DEN/CF) * (F2.den/CF); Fraction result; Result.mem= f1.mem* (Cm/f1.den) + f2.mem* (cm/F2.den); Result.den=cm; returnresult;} Fractionoperator-(fraction &f1, fraction &F2) { Long LongCF =cfact (F1.den, F2.den); Long LongCM = cf* (F1.DEN/CF) * (F2.den/CF); Fraction result; Result.mem= f1.mem* (Cm/f1.den)-f2.mem* (CM/F2.den); Result.den=cm;; returnresult;} Fractionoperator* (fraction &f1, fraction &F2) {fraction result; Result.mem= F1.mem *F2.mem; Result.den= F1.den *F2.den; returnresult;} Fractionoperator/(fraction &f1, fraction &F2) {fraction result; Result.mem= F1.mem *F2.den; Result.den= F1.den *F2.mem; returnresult;}intMain () {fraction F1; Fraction F2; Fraction RESULT1,RESULT2,RESULT3,RESULT4; scanf ("%lld/%lld%lld/%lld",&F1.mem,&f1.den,&f2.mem,&F2.den); RESULT1= F1 +F2; RESULT2= F1-F2; RESULT3= F1 *F2; RESULT4= F1/F2; stringF1_str =F1. Show (); stringF2_str =F2. Show (); stringRes1_str =RESULT1. Show (); stringRes2_str =result2. Show (); stringRes3_str =RESULT3. Show (); stringRes4_str =RESULT4. Show (); //F1. Show ();cout << f1_str <<" + "<< F2_str <<" = "<< res1_str<<Endl; cout<< F1_str <<" - "<< F2_str <<" = "<< res2_str<<Endl; cout<< F1_str <<" * "<< F2_str <<" = "<< res3_str<<Endl; cout<< F1_str <<" / "<< F2_str <<" = "<< res4_str<<Endl; /*printf ("+"); F2. Show (); printf ("="); Result1. Show (); printf ("\ n"); F1. Show (); printf ("-"); F2. Show (); printf ("="); Result2. Show (); printf ("\ n"); F1. Show (); printf ("*"); F2. Show (); printf ("="); Result3. Show (); printf ("\ n"); F1. Show (); printf ("/"); F2. Show (); printf ("="); Result4. Show (); printf ("\ n");*/ return 0;}
In the case of a divisor of 0, I thought C + + comes with the <stdexcept> library to provide this exception handling, but it seems that C + + can not handle this situation, to write their own exception class, so I can only find another way to deal with this situation, if it is C # or Java, should be able to handle it very conveniently.
1034. Rational number Arithmetic (20)