C语言:分数的乘法
答案:4 悬赏:20 手机版
解决时间 2021-11-15 21:30
- 提问者网友:蓝莓格格巫
- 2021-11-14 21:32
C语言:分数的乘法
最佳答案
- 五星知识达人网友:空山清雨
- 2021-11-14 22:41
这是要干嘛,改错?
全部回答
- 1楼网友:逐風
- 2021-11-15 02:25
int scan_frac(struct fraction *f, struct fraction *g){
int a,b,c,d;
if(scanf("%d/%d %d/%d",&a,&b,&c,&d)==EOF)return EOF;
f->symbol=(a<0)?(-1):1;
g->symbol=(c<0)?(-1):1;
f->numerator=a>0?a:(-a);
f->denominator=b;
g->numerator=c>0?c:(-c);
g->denominator=d;return 0;}
struct fraction multiply_frac(struct fraction f, struct fraction g){
struct fraction ans;
int fin;
ans.numerator=f.numerator*g.numerator;
ans.denominator=f.denominator*g.denominator;
ans.symbol=(f.symbol==g.symbol)?1:(-1);
fin=(ans.numerator>ans.denominator)?ans.numerator:ans.denominator;
while(fin>1){
if(ans.numerator%fin==0&&ans.denominator%fin==0){
ans.numerator/=fin;
ans.denominator/=fin;}
fin--;
}
return ans;}
int a,b,c,d;
if(scanf("%d/%d %d/%d",&a,&b,&c,&d)==EOF)return EOF;
f->symbol=(a<0)?(-1):1;
g->symbol=(c<0)?(-1):1;
f->numerator=a>0?a:(-a);
f->denominator=b;
g->numerator=c>0?c:(-c);
g->denominator=d;return 0;}
struct fraction multiply_frac(struct fraction f, struct fraction g){
struct fraction ans;
int fin;
ans.numerator=f.numerator*g.numerator;
ans.denominator=f.denominator*g.denominator;
ans.symbol=(f.symbol==g.symbol)?1:(-1);
fin=(ans.numerator>ans.denominator)?ans.numerator:ans.denominator;
while(fin>1){
if(ans.numerator%fin==0&&ans.denominator%fin==0){
ans.numerator/=fin;
ans.denominator/=fin;}
fin--;
}
return ans;}
- 2楼网友:像个废品
- 2021-11-15 00:57
#include
#include
struct fraction
{
int numerator; // 分子
int denominator; // 分母
int symbol; // 符号,分子为负时取值为-1,为正时取值为1,其它取值无意义
};
int scan_frac(struct fraction *f, struct fraction *g);// 按题目格式输入两个分数,存入f和g所指的内存中。
struct fraction multiply_frac(struct fraction f, struct fraction g); //返回分数f和g的乘积。
int main()
{
struct fraction f1, f2, product;
while(scan_frac(&f1, &f2) != EOF)
{
product = multiply_frac(f1, f2);
if(product.numerator == 0)
{
printf("0 ");
continue;
}
if(product.symbol == -1)
printf("-");
printf("%d/%d ", product.numerator, product.denominator);
}
return 0;
}
//读取
int scan_frac(struct fraction *f, struct fraction *g)
{
char c=0;
int *tmpnum=&f->numerator;
*tmpnum=0;
f->symbol=1;
while((c=getchar())!=' ')
{
if(c<='9'&&c>='0')
*tmpnum=*tmpnum*10+c-'0';
else if(c=='/')
{
tmpnum=&f->denominator;
*tmpnum=0;
}
else if(c=='-')
f->symbol=-1;
}
tmpnum=&g->numerator;
*tmpnum=0;
g->symbol=1;
while((c=getchar())!=' ')
{
if(c<='9'&&c>='0')
*tmpnum=*tmpnum*10+c-'0';
else if(c=='/')
{
tmpnum=&g->denominator;
*tmpnum=0;
}
else if(c=='-')
g->symbol=-1;
}
fflush(stdin);
return c;
}
//乘积
struct fraction multiply_frac(struct fraction f, struct fraction g)
{
int i=0;
int tmp1,tmp2;
struct fraction *result=(struct fraction *)malloc(sizeof( struct fraction));
//约分
tmp1=f.denominator*g.denominator;
tmp2=f.numerator*g.numerator;
i=tmp1>tmp2?tmp2:tmp1;
while(i>=2)
{
if(tmp1%i==0
&&tmp2%i==0)
{
tmp1/=i;
tmp2/=i;
}
i--;
}
result->denominator=tmp1;
result->numerator=tmp2;
result->symbol=f.numerator*g.numerator;
return *result;
}
#include
struct fraction
{
int numerator; // 分子
int denominator; // 分母
int symbol; // 符号,分子为负时取值为-1,为正时取值为1,其它取值无意义
};
int scan_frac(struct fraction *f, struct fraction *g);// 按题目格式输入两个分数,存入f和g所指的内存中。
struct fraction multiply_frac(struct fraction f, struct fraction g); //返回分数f和g的乘积。
int main()
{
struct fraction f1, f2, product;
while(scan_frac(&f1, &f2) != EOF)
{
product = multiply_frac(f1, f2);
if(product.numerator == 0)
{
printf("0 ");
continue;
}
if(product.symbol == -1)
printf("-");
printf("%d/%d ", product.numerator, product.denominator);
}
return 0;
}
//读取
int scan_frac(struct fraction *f, struct fraction *g)
{
char c=0;
int *tmpnum=&f->numerator;
*tmpnum=0;
f->symbol=1;
while((c=getchar())!=' ')
{
if(c<='9'&&c>='0')
*tmpnum=*tmpnum*10+c-'0';
else if(c=='/')
{
tmpnum=&f->denominator;
*tmpnum=0;
}
else if(c=='-')
f->symbol=-1;
}
tmpnum=&g->numerator;
*tmpnum=0;
g->symbol=1;
while((c=getchar())!=' ')
{
if(c<='9'&&c>='0')
*tmpnum=*tmpnum*10+c-'0';
else if(c=='/')
{
tmpnum=&g->denominator;
*tmpnum=0;
}
else if(c=='-')
g->symbol=-1;
}
fflush(stdin);
return c;
}
//乘积
struct fraction multiply_frac(struct fraction f, struct fraction g)
{
int i=0;
int tmp1,tmp2;
struct fraction *result=(struct fraction *)malloc(sizeof( struct fraction));
//约分
tmp1=f.denominator*g.denominator;
tmp2=f.numerator*g.numerator;
i=tmp1>tmp2?tmp2:tmp1;
while(i>=2)
{
if(tmp1%i==0
&&tmp2%i==0)
{
tmp1/=i;
tmp2/=i;
}
i--;
}
result->denominator=tmp1;
result->numerator=tmp2;
result->symbol=f.numerator*g.numerator;
return *result;
}
- 3楼网友:摆渡翁
- 2021-11-15 00:16
关注此问题
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯