本帖最后由 LNH_Sniper 于 2011-5-19 16:28 编辑 #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[100];
char *s;
int flag_num, flag_op, int_de, int_mo;
float num[4] = {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[0] / num[1] - num[2] / num[3];
else
result = num[0] / num[1] + num[2] / num[3];
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;
}
|