【c】分数类型的定义 c语言分数类型加减乘除的实现

  • 时间:
  • 来源:互联网
  • 文章标签:

思路:
①初始化
②分数的显示
③分数化简
④加减乘除实现
看似麻烦实际上最本质的操作就是 求最大公约数和最小公倍数
代码实现

#include <stdio.h>

int gcd(int a,int b)//求最大公约数
{
	int tmp;
	int r;
	if(a<b)
	{
		tmp=a;
		a=b;
		b=tmp;
	}
	while(b!=0)
	{
		r=a%b;
		a=b;
		b=r;
	}
	return a;
}
int lcm(int a,int b)//求最小公倍数
{
	int Lcm,tmp;
	if(b<a)
	{
		tmp=b;
		b=a;
		a=tmp;
	}
	else if(b==0)
		Lcm=a;
	else if(a==0)
		Lcm=b;
	else
		Lcm=(a*b)/gcd(a,b);
	return Lcm;
}
void SimpFraction(int a,int b)
{

	printf("the fraction in lowest term is %d/%d \n",a/gcd(a,b),b/gcd(a,b));
	
}
void AddFraction(int a,int b,int c, int d)//加法
{

	int Snumerator;
	int Sdenominator;
	int Lcm;
	Lcm=lcm(b,d);
	if((b==d))
	{
		Sdenominator=d;
		Snumerator=a+c;
	}
	else if(Lcm==b||Lcm==d)
	{
		Sdenominator=Lcm;
		if(b<d)
			Snumerator=(a*b)+c;
		if(d<b)
			Snumerator=(c*d)+a;
	}
	else
	{
		Sdenominator=Lcm;
		Snumerator=a*(Lcm/b)+c*(Lcm/d);
	}
	printf("分数相加得:%d/%d \n",Snumerator/gcd(Snumerator,Sdenominator),Sdenominator/gcd(Snumerator,Sdenominator));


}
void SubFraction(int a,int b,int c,int d)//减法
{
	int Snumerator;
	int Sdenominator;
	int Lcm;
	Lcm=lcm(b,d);
	if((b==d))
	{
		Sdenominator=d;
		Snumerator=a-c;
	}
	else if(Lcm==b||Lcm==d)
	{
		Sdenominator=Lcm;
		if(b<d)
			Snumerator=(a*b)-c;
		if(d<b)
			Snumerator=(c*d)-a;
	}
	else
	{
		Sdenominator=Lcm;
		Snumerator=a*(Lcm/b)-c*(Lcm/d);
	}
	printf("分数相减得:%d/%d \n",Snumerator/gcd(Snumerator,Sdenominator),Sdenominator/gcd(Snumerator,Sdenominator));
}
void MulFraction(int a,int b,int c,int d)//乘法
{
	int Snumerator;
	int Sdenominator;
	Sdenominator=b*d;
	Snumerator=a*c;
	if(Sdenominator==0)
		printf("分数无意义 \n");

	else if(Snumerator==0)
		printf("分数相乘得0 \n");
	else
		printf("分数相乘得:%d/%d \n",Snumerator/gcd(Snumerator,Sdenominator),Sdenominator/gcd(Snumerator,Sdenominator));
}
void DivFraction(int a,int b,int c,int d)//除法
{
	int Snumerator;
	int Sdenominator;
	Sdenominator=b*c;
	Snumerator=a*d;
	if(Sdenominator==0)
		printf("分数无意义 \n");

	else if(Snumerator==0)
		printf("分数相除得0 \n");
	else
		printf("分数相除得:%d/%d \n",Snumerator/gcd(Snumerator,Sdenominator),Sdenominator/gcd(Snumerator,Sdenominator));

}



void main()
{
	int a,b,c,d;
	printf("please enter two fractions'numerator and denominator:");
	scanf("%d,%d,%d,%d",&a,&b,&c,&d);
	if(a==0&&b==0&&c==0&&d==0)
		printf("error");
	SimpFraction(a,b);
	SimpFraction(c,d);
	AddFraction(a,b,c,d);
	SubFraction(a,b,c,d);
	MulFraction(a,b,c,d);
	DivFraction(a,b,c,d);
}

测试结果 :(切记 测试输入的时候 一定要输入"," 不然输入","后续加减乘除运算均为计算错误)

please enter two fractions'numerator and denominator:114,114514,514,114514
the fraction in lowest term is 57/57257
the fraction in lowest term is 257/57257
分数相加得:314/57257
分数相减得:200/-57257
分数相乘得:14649/57138577
分数相除得:57/257
Press any key to continue

please enter two fractions'numerator and denominator:615,1722,1107,1923
the fraction in lowest term is 5/14
the fraction in lowest term is 369/641
分数相加得:8371/8974
分数相减得:1961/-8974
分数相乘得:1845/8974
分数相除得:3205/5166
Press any key to continue

please enter two fractions'numerator and denominator:1,0,2,7
the fraction in lowest term is 1/0
the fraction in lowest term is 2/7
分数相加得:2/7
分数相减得:-2/7
分数无意义
分数无意义
Press any key to continue

please enter two fractions'numerator and denominator:0,22,78,45
the fraction in lowest term is 0/1
the fraction in lowest term is 26/15
分数相加得:26/15
分数相减得:-26/15
分数相乘得0
分数相除得0
Press any key to continue

本文链接http://www.taodudu.cc/news/show-1781794.html