Title address: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=111??
Description
Write aCprogram to achieve the addition and subtraction of two fractions
input
input contains multiple rows of data
each row of data is a string, and the format is"A/BOC/D".
whichA, B, C, Dis a0-9an integer. ois an operator"+"or"-".
data toEOFEnd
input data guarantees legal
Output
output two fractions for each line of input data.
Note that the results should conform to the writing habit, without unnecessary symbols, molecules, and denominators, and simplify to the simplest fraction
Sample Input
1/8+3/8
1/4-1/2
1/3-1/3
Sample Output
1/2
-1/40?? Code:?
#include <stdio.h>
typedef struct _FRACTION
{
???? Molecular
???? int numerator;
???? Denominator
???? int denominator;
}fraction;
Process the data and print the results
static void Handlerdata (fraction *lhs,fraction *rhs,char symbol);
-Pass
static void Commondenominator (fraction *lhs,fraction *RHS);
Calculates two fractions plus a
static fraction subfraction (fraction *lhs,fraction *RHS);
Calculates two fractions minus
static fraction plusfraction (fraction *lhs,fraction *RHS);
Calculate the greatest common divisor of A and B
static int calcommondivisor (int a,int b);
Calculate the least common multiple of a and B
static int calcommonmultiple (int a,int b);
int main ()
{
???? int x1,y1,x2,y2;
???? char symbol;
????
???? Do
???? {
????????? scanf ("%d/%d%c%d/%d", &x1,&y1,&symbol,&x2,&y2);
????
????????? Fraction LHS = {X1,y1};
????????? Fraction RHS = {X2,y2};
????????? Handlerdata (&lhs,&rhs,symbol);
???? }while (GetChar ()! = EOF);
????
???? return 0;
}
Process the data and print the results
static void Handlerdata (fraction *lhs,fraction *rhs,char symbol)
{
???? Fraction result;
???? Switch (symbol)
???? {
????????? Case ' + ':
?????????????? result = Subfraction (LHS,RHS);
?????????????? Break
????????? Case '-':
?????????????? result = Plusfraction (LHS,RHS);
???? }
????
???? if (result.numerator! = 0)
???? {
????????? if (result.denominator! = 1)
????????? {
?????????????? printf ("%d/%d\n", result.numerator,result.denominator);
????????? }
????????? Else
????????? {
?????????????? printf ("%d\n", result.numerator);
????????? }
???? }
???? Else
???? {
????????? printf ("0\n");
???? }
}
-Pass
static void Commondenominator (fraction *lhs,fraction *RHS)
{
???? int commonnumber = Calcommonmultiple (lhs->denominator,rhs->denominator);
????
???? Numerator multiplied by the denominator-pass multiples
???? Lhs->numerator *= commonnumber/lhs->denominator;
???? Rhs->numerator *= commonnumber/rhs->denominator;
????
????????? Denominator-Pass
???? Lhs->denominator = Commonnumber;
???? Rhs->denominator = Commonnumber;
}
Calculates two fractions plus a
static fraction subfraction (fraction *lhs,fraction *RHS)
{
???? First-Pass
???? Commondenominator (LHS,RHS);
????
???? int tmpnumerator = Lhs->numerator + rhs->numerator;
???? int tmpdenominator = lhs->denominator;
????
???? int tmpdivisor = Calcommondivisor (tmpnumerator,tmpdenominator);
????
???? Fraction result;
???? Result.denominator = Tmpdenominator/tmpdivisor;
???? Result.numerator = Tmpnumerator/tmpdivisor;
????
???? return result;
}
Calculates two fractions minus
static fraction plusfraction (fraction *lhs,fraction *RHS)
{
???? First-Pass
???? Commondenominator (LHS,RHS);
????
???? int tmpnumerator = lhs->numerator-rhs->numerator;
????
???? int tmpdenominator = lhs->denominator;
????
???? int tmpsymbol = 1;
???? if (Tmpnumerator < 0)
???? {
????????? Tmpsymbol =-1;
????????? Tmpnumerator *=-1;
???? }
????
???? int tmpdivisor = Calcommondivisor (tmpnumerator,tmpdenominator);
????
???? Fraction result;
???? Result.denominator = Tmpdenominator/tmpdivisor;
???? Result.numerator = Tmpnumerator/tmpdivisor * TMPSYMBOL;
????
???? return result;
}
Calculate the greatest common divisor of A and B
static int calcommondivisor (int a,int b)
{
??? int maxnum = a>b?a:b;
??? int minnum = a<b?a:b;
???
??? int midresult = 0;
??? while (Minnum! = 0)
??? {
??? ???? Midresult = maxnum% Minnum;
??????? Maxnum = Minnum;
??????? Minnum = Midresult;
??? }
???
??? return maxnum;
}????
Calculate the least common multiple of a and B
static int calcommonmultiple (int a,int b)
{
???? int maxcommondivisor = Calcommondivisor (A, b);
??? return a*b/maxcommondivisor;
} ? ? ? ??? There are: 1. Input data to determine the EOF, in accordance with the code, the only inconvenient is the use of do. While ... The way. 2. The format of the input data, beginning to take the way of reading a line of string, when converting characters to numbers error, can not use Atoi,atoi requirements parameter is char*, direct character variable str[i]-' 0 ' can be. 3. Denominator-pass, is the least common multiple of seeking. And numerator, is the numerator denominator divided by greatest common divisor. In the 4.commonDenominator method, we first get least common multiple, we need to modify the value of the numerator first, and then give the denominator the least common multiple value. 5. When two numbers are added and two numbers are subtracted, the first is the-pass, and then only the numerator is added and subtracted, and the denominator is guaranteed to be constant. 6. When subtracting two numbers, the numerator may be 0 and negative, and if it is 0, the method of seeking greatest common divisor by the author is wrong to return 0 such as code, 0 is the minimum, and the result of Midresult remainder is 0, Then returned 0. This causes the numerator denominator to divide by the least common multiple error, because the denominator is 0 of the division operation is illegal, so least common multiple's solution should not return 0, the code in this topic is judged, but there is no guarantee that the return is 0. 7. Finally reflect on the process of solving,
In fact, you can save-pass steps, when learning mathematics, the reason advanced line-pass, is to calculate molecules, are the smallest number to calculate, but the computer does not care about large numbers or decimals, as long as the guarantee in the effective storage range can be, so can completely omit the-pass process to simplify the calculation. The simplest-pass operation is processed by multiplying the two denominator directly. 8. The requirements for the test data, at least 3 data, a>ba<ba==b?9. Easy to ignore integer results, such as 1/3+2/3 results should be 1 and if not special processing, it is easy to output to 1/ 1 that is, when the denominator is 1, special treatment is needed?
30-language Introduction -30-fractions plus subtraction