#include<stdio.h>
struct fraction
{
int upper;//分子
int lower;//分母
};
struct fraction GetSum(struct fraction a,struct fraction b)
{
int divider;//分子分母最大公约数
struct fraction result;
if(!(a.lower&&b.lower))//检查分母
{
printf("非法数值!");exit(0);
}
result.lower=a.lower*b.lower;
result.upper=a.upper*b.lower+b.upper*a.lower;
divider=result.lower>result.upper?result.upper:result.lower;
do
{
if(result.upper%divider==0&&result.lower%divider==0)
{
result.lower/=divider;
result.upper/=divider;
return result;//约分后的情况
}
}while(--divider>1);
return result;//不能再约分的情况
}
void main()
{
printf("please enter the fenzi,fenmu of two complex:\n");
struct fraction a,b,c;
scanf("%d %d",&a.upper,&a.lower);
scanf("%d %d",&b.upper,&b.lower);
c=GetSum(a,b);
printf("the result is %d/%d",c.upper,c.lower);
return 0;
}
感谢感谢!自己根据题意完善了一下