#include "stdio.h"
#include "string.h"
#include "math.h"
int divisor(int molecule, int denominator)
{
int temp;
temp = molecule;
if( denominator % molecule == 0)
return molecule;
else
{
while( !((denominator % temp == 0) && ( molecule % temp == 0)) )
{
temp --;
}
return temp;
}
}
int main()
{
char buf;
char *s;
int flag_num, flag_op, int_de, int_mo;
float num = {0,0,0,0}, result;
float *temp;
temp = num;
flag_num = 0;
int_de = 0;
int_mo = 0;
gets( buf );
s = buf;
while( *s )
{
if( ( *s - 48 ) <= 9 && ( *s - 48 ) >= 0 )
{
*temp = (*temp) * 10 + ( *s - 48 );
}
if( *s == '/' || *s == '+' || *s == '-')
{
if(*s == '+')
flag_op = 1;
if(*s == '-')
flag_op = 0;
temp++;
}
s++;
}
if(flag_op == 0)
result = num / num - num / num;
else
result = num / num + num / num;
if(result > 0)
flag_op = 1;
if(result < 0)
flag_op = 0;
if(result == 0)
flag_op = -1;
while( result != (int)result )
{
result = result * 10;
flag_num++;
}
int_de = (int)pow(10,flag_num);
if(flag_op == 1)
printf("%d/%d", (int)result / divisor((int)result,int_de),int_de / divisor((int)result,int_de));
if(flag_op == 0)
printf("-%d/%d", (int)result / divisor((int)result,int_de),abs((int)int_de / divisor((int)result,int_de)));
if(flag_op == -1)
printf("%d",0);
printf("\n");
return 0;
} 不对呢..... 916276623 发表于 2011-6-10 11:24 static/image/common/back.gif
不对呢.....
我知道问题出在哪了,我会更正下,然后发布出来的。
谢谢你的测试
本帖最后由 wangyexin 于 2011-6-21 18:21 编辑
#include<iostream>
using namespace std;
int gcd(int n,int m)
{
while(n!=m)
{
while(n>m)n-=m;
while(n<m)m-=n;
}
return n;
}
int main()
{
int a,b,c,d,k,res,flag;
char ch,o;
while(scanf("%d%c%d%c%d%c%d",&a,&ch,&b,&o,&c,&ch,&d)!=EOF)
{
flag=0;
if(o=='+')
{
res=a*d+c*b;
}
else
{
res=a*d-c*b;
}
if(res==0)
{
cout<<0<<endl;
}
else
{
if(res<0)
{
res=-res;
flag=1;
}
if(flag)printf("-");
k=gcd(b*d,res);
printf("%d/%d",res/k,b*d/k);
}
}
system("pause");
return 0;
}
无回帖,不论坛,这才是人道。 激动人心,无法言表! 强烈支持楼主ing…… 楼主太厉害了,谢谢分享! 无回帖,不论坛,这才是人道。 看帖,回复支持下 看看老帖,支持下 看看,回复支持下 再看看,回帖支持支持 再看看,支持下 楼主加油,鱼C加油!我们都看好你哦! 怡静感谢楼主分享! 多谢楼主! 祝鱼C论坛红红火火!
页:
[1]
2