我想用结构创建一个函数来简化有理数,
一个成员是分子(int),另一个是分母(int),但程序卡在输入!我知道 scanf() 有点冒险,但我认为它只是几个整数!
#include <stdio.h>
#include <stdlib.h>
typedef struct rational{
int num;
int den;
}rational;
rational makerational(int num,int den);
void printrational(rational r);
int main()
{
int a,b;
printf("\n\n Input integers for fraction:");
scanf(" %d%d",&a,&b);
printrational(makerational(a,b));
}
rational makerational(int a,int b){
int live=1;
rational r;
r.num=a;
r.den=b;
while(live){
if(!(r.num%2 && r.den%2)){
r.num/=2;
r.den/=2;
}else if(!(r.num%3 && r.den%3)){
r.num/=3;
r.den/=3;
}else if(!(r.num%5 && r.den%5)){
r.num/=5;
r.den/=5;
}else if(!(r.num%7 && r.den%7)){
r.num/=7;
r.den/=7;
}else live--;
}
return r;
}
void printrational(rational r){
printf("\nFRACTION -> %d/%d\n",r.num,r.den);
}