天天看點

分式化簡c語言,化簡分式(對小數有用)

該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓

#include 

#include 

int main()

{

char s[100];

while(cin>>s)

{

long a=0,b=0,i;

int pos=0;

int m=0,n=0;

while(s[pos]!='/')

pos++;

for(i=0;i

{

if(s[i]=='.')

{

m=pos-i-1;

break;

}

}

for(i=pos+1;i

{

if(s[i]=='.')

{

n=strlen(s)-i-1;

break;

}

}

for(i=0;i

if(s[i]!='.')

a=a*10+s[i]-'0';

for(i=pos+1;i

if(s[i]!='.')

b=b*10+s[i]-'0';

a=a*(int)pow(10,n);

b=b*(int)pow(10,m);

if(a==0)

cout<

else if(b==0)

cout<

else

{

int flag=a/b;

if(flag!=0) cout<

a=a%b;

if(a!=0)

{

for(i=2;i<=b;i++)

while(a%i==0 && b%i==0)

{

b=b/i;

a=a/i;

}

if(flag!=0)

cout<

cout<

}

else

cout<

}

}

}