# Representation and calculation of fraction (c + +)

Keywords: C++

Before, it was always a simple idea to convert fractions into decimals for calculation. In fact, the corresponding structure can be used to separate the numerator and denominator for storage, which can have a wonderful effect;

Score storage:

``````struct Fraction{
int up;
int down;
};``````

Where up is the numerator and down is the denominator;

For scores, there are several basic rules:
1. Sign on molecule;
2. When the fraction is 0, the numerator is 0 and the denominator is 1;
3. The numerator denominator must be the simplest, that is, there is no common divisor other than 1;

When four operations are performed on fractions, they are also simplified based on these three properties;

``````Fraction reduction(Fraction result){
if(result.down<0){
result.up=-result.up;
result.down=-result.down;
}
if(result.up==0){
result.down=1;
}else{
int d=gcd(abs(result.up),abs(result.down));
result.up/=d;
result.down/=d;
}
return result;
}``````

It is worth noting that when calculating the greatest common divisor, we must pay attention to the fact that the molecule may be negative, so we should deal with the absolute value in advance;

Four operations of fractions:
On the basis of the above simplified function, we can carry out corresponding four operations according to the rule, and the four operations strictly follow the general rule of calculation;

``````Fraction add(Fraction f1,Fraction f2){
Fraction result;
result.up=f1.up*f2.down+f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result);
}``````

2. Subtraction:

``````Fraction minu(Fraction f1,Fraction f2){
Fraction result;
result.up=f1.up*f2.down-f1.down*f2.up;
result.down=f1.down*f2.down;
return reduction(result);
}``````

3. Multiplication

``````Fraction multi(Fraction f1,Fraction f2){
Fraction result;
result.up=f1.up*f2.up;
result.down=f2.down*f2.down;
return reduction(result);
}``````

4. Division operation:

``````Fraction divide(Fraction f1,Fraction f2){
Fraction result;
result.up=f1.up*f2.down;
result.down=f2.up*f2.down;
return reduction(result);
}``````

It is worth noting that the reciprocal calculation of division is adopted here;

Output form of score:
For a normal form of score, there are often three forms:
1. Integer: only the numerator is output at this time (since the numerator denominator is simplified in the program code, if there is an integer denominator, it must be 1);
2. True score: output in a/b format;
3. False score: at this time, it should be output according to the format with score, and the symbol is in front;

The code is as follows:

``````void showResult(Fraction r){
r=reduction(r);
if(r.down==1)
printf("%lld",r.up);
else if(abs(r.up)>r.down){
printf("%d %d/%d",r.up/r.down,abs(r.up)%r.down,r.down);
}else{
printf("%d/%d",r.up,r.down);
}
}``````

It is worth noting that when calculating with fraction, when calculating the true fraction remainder, we must pay attention to the absolute value of abs;

Posted by reagent on Sun, 01 Dec 2019 17:24:41 -0800