编写函数:分数的乘法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct fraction
{
int numerator; // 分子
int denominator; // 分母
int symbol; // 符号,分子为负时取值为-1,为正时取值为1,其它取值无意义
};
int scan_frac(struct fraction *f, struct fraction *g)//按题目格式输入两个分数,存入f和g所指的内存中。
{
int a,b,x,y;
scanf("%d/%d*%d/%d",&a,&b,&x,&y);
f->numerator=a;
f->denominator=b;
g->numerator=x;
g->denominator=y;
if(a>0)
{
f->symbol=1;
}
else
{
f->symbol=-1;
}
if(x>0)
{
g->symbol=1;
}
else
{
g->symbol=-1;
}
}
struct fraction multiply_frac(struct fraction f, struct fraction g)//返回分数f和g的乘积
{
struct fraction result;
int a,b,t,m,n;
a=f.numerator*g.numerator;
b=f.denominator*g.denominator;
m=a;
n=b;
if(a<b)
{
t=a;
a=b;
b=t;
}
while(a%b!=0)
{
t=b;
b=a%b;
a=t;
}
a=m/b;
b=n/b;
result.numerator=a;
result.denominator=b;
result.symbol=f.symbol*g.symbol;
return result;
}
int main()
{
struct fraction f1, f2, product;
while(scan_frac(&f1, &f2) != EOF)
{
product = multiply_frac(f1, f2);
if(product.numerator == 0)
{
printf("0\n");
continue;
}
if(product.symbol == -1)
printf("-");
printf("%d/%d\n", product.numerator, product.denominator);
}
return 0;
}
为什么我的程序一运行就要崩(主函数由题目给出,不能改动)