主要包括一下几个方面:
1、分数的表示
struct Fraction //结构来存储 分子分母
{
ll up, down;
};
2、分数的化简
Fraction Reducation(Fraction result) //化简操作
{
ll d;
if (result.down < 0) //为负数的时候
{
result.up = -result.up;
result.down = -result.down;
}
if (result.up == 0)
result.down = 1;
else if(result.down==0)
printf("error");
else
{
d = gcd(abs(result.up), abs(result.down)); //化简假分数,通过调用最大公约数
result.up /= d;
result.down /= d;
}
return result;
}
3、分数的加减乘除
Fraction add(Fraction f1, Fraction f2) //加法运算 f1,f2为两个数,通过结构体来调用分子分母
{
Fraction result;
result.up = f1.up*f2.down + f2.up*f1.down;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction jian(Fraction f1, Fraction f2) //减法运算
{
Fraction result;
result.up = f1.up*f2.down - f2.up*f1.down;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction chen(Fraction f1, Fraction f2) //乘法运算
{
Fraction result;
result.up = f1.up*f2.up;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction chu(Fraction f1, Fraction f2) //除法运算
{
Fraction result;
result.up = f1.up*f2.down;
result.down = f1.down*f2.up;
return Reducation(result);
}cpp
4、分数的输出
void showResult(Fraction r)
{
r = Reducation(r);
if (r.down == 1) //分母为1
printf("%lld", r.up);
else if (fabs(r.up) > r.down) //假分数
printf("%lld %lld/%lld", r.up/r.down, abs(r.up)%r.down, r.down);
else //真分数
printf("%lld/%lld", r.up, r.down);
}
5、总的代码
#include <iostream>
#include<string.h>
#include<cstdio>
#include<math.h>
#define _CRT_SECURE_NO_WARNINGS
typedef long long ll; //由于乘法和除法的过程中可能使分子或分母超过int型的表示范围
//因此一般情况,分子分母应当使用long long 型存储
using namespace std;
struct Fraction //结构来存储 分子分母
{
ll up, down;
};
int gcd(ll a, ll b)
{
if (b == 0)
return a;
else
return gcd(b, a%b);
}
Fraction Reducation(Fraction result) //化简操作
{
ll d;
if (result.down < 0) //为负数的时候
{
result.up = -result.up;
result.down = -result.down;
}
if (result.up == 0)
result.down = 1;
else if(result.down==0)
printf("error");
else
{
d = gcd(abs(result.up), abs(result.down)); //化简假分数,通过调用最大公约数
result.up /= d;
result.down /= d;
}
return result;
}
Fraction add(Fraction f1, Fraction f2) //加法运算 f1,f2为两个数,通过结构体来调用分子分母
{
Fraction result;
result.up = f1.up*f2.down + f2.up*f1.down;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction jian(Fraction f1, Fraction f2) //减法运算
{
Fraction result;
result.up = f1.up*f2.down - f2.up*f1.down;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction chen(Fraction f1, Fraction f2) //乘法运算
{
Fraction result;
result.up = f1.up*f2.up;
result.down = f1.down*f2.down;
return Reducation(result);
}
Fraction chu(Fraction f1, Fraction f2) //除法运算
{
Fraction result;
result.up = f1.up*f2.down;
result.down = f1.down*f2.up;
return Reducation(result);
}
void showResult(Fraction r)
{
r = Reducation(r);
if (r.down == 1) //分母为1
printf("%lld", r.up);
else if (fabs(r.up) > r.down) //假分数
printf("%lld %lld/%lld", r.up/r.down, abs(r.up)%r.down, r.down);
else //真分数
printf("%lld/%lld", r.up, r.down);
}
int main()
{
Fraction f1,f2 ;
Fraction result;
char x;
printf("input two fraction:\n");
scanf_s("%lld/%lld ",&f1.up,&f1.down);
scanf_s("%c",&x);
scanf_s("%lld/%lld",&f2.up,&f2.down);
switch (x)
{
case '+':
result = add(f1, f2);
break;
case '-':
result = jian(f1, f2);
break;
case '*':
result = chen(f1, f2);
break;
case '/':
result = chu(f1, f2);
}
showResult(result);
system("pause");
return 0;
}