工具人来了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_SIZE 1024
#define BOOL int
#define TRUE 1
#define FALSE 0
//分数结构体
typedef struct _Fraction
{
//分子
int numerator;
//分母
int denominator;
} Fraction;
//创建分数对象
Fraction CreateFraction(int n, int d)
{
Fraction result = { n, d };
return result;
}
//最大公约数
int GreatestCommonDivisor(int a, int b)
{
if (a == 1 || b == 1) return 1;
else if (a == b) return a;
int big = a > b ? a : b;
int small = (a + b) - big;
int t = 0;
while (small > 0)
{
t = small;
small = big % small;
big = t;
}
return big;
}
//最小公倍数
int LeastCommonMultiple(int a, int b)
{
int gcd = GreatestCommonDivisor(a, b);
return a * b / gcd;
}
//简化分数
void SimplifyFraction(Fraction* dec)
{
int g = GreatestCommonDivisor(dec->numerator, dec->denominator);
dec->numerator /= g;
dec->denominator /= g;
}
//分数相加
Fraction FractionAdd(Fraction a, Fraction b)
{
int lcm = LeastCommonMultiple(a.denominator, b.denominator);
Fraction result = CreateFraction(a.numerator * (lcm / a.denominator) + b.numerator * (lcm / b.denominator), lcm);
SimplifyFraction(&result);
return result;
}
//打印分数到屏幕
void PrintFraction(Fraction a) { printf_s("当前分数为: %d/%d \n", a.numerator, a.denominator); }
//有限不循环小数转分数
Fraction FiniteDecimalToFraction(char* numStr)
{
int n = 0; //分子
int d = 1; //分母
BOOL isPoint = FALSE; //是否遇到小数点
for (int i = 0; i < strlen(numStr); i++)
{
if (numStr[i] == '.')
{
isPoint = TRUE;
continue;
}
n = n * 10 + (numStr[i] - 0x30);
if (isPoint)
d = d * 10;
}
Fraction result = CreateFraction(n, d);
SimplifyFraction(&result);
return result;
}
//无限循环小数转分数
Fraction InfiniteDecimalToFraction(char* loopStr)
{
int ZeroAfterPoint = 1; //小数点后,循环节开始前0的个数
BOOL isPoint = FALSE; //是否遇到小数点
BOOL isStartLoop = FALSE; //循环节是否开始
int n = 0; //分子
int d = 0; //分母
for (int i = 0; i < strlen(loopStr); i++)
{
if (loopStr[i] == '.')
{
isPoint = TRUE;
continue;
}
if (isPoint && loopStr[i] != '0')
isStartLoop = TRUE;
if (isPoint && !isStartLoop)
ZeroAfterPoint *= 10;
else if (isPoint && isStartLoop)
{
n = n * 10 + (loopStr[i] - 0x30);
d = d * 10 + 9;
}
}
Fraction result = CreateFraction(n, d * ZeroAfterPoint);
return result;
}
//输入
void GetInput()
{
char finite[MAX_SIZE] = { 0 }; //有限部分
char loop[MAX_SIZE] = { 0 }; //循环部分
char str[MAX_SIZE] = { 0 }; //整体
BOOL isLeftBracket = FALSE; //是否遇到左括号
printf_s("请输入一个 有限不循环小数 或 无限循环小数(循环节放在括号内) :\n");
scanf_s("%s%*c", str, MAX_SIZE);
//分离小数的有限部分和循环部分
for (int i = 0; i < MAX_SIZE; i++)
{
if (str[i] == '(')
{
isLeftBracket = TRUE;
continue;
}
else if (str[i] == ')' || str[i] == '\0')
break;
if (!isLeftBracket)
{
finite[i] = str[i];
if(str[i] != '.')
loop[i] = '0';
else
loop[i] = '.';
}
else
{
finite[i - 1] = '\0';
loop[i - 1] = str[i];
}
}
Fraction result;
if (!isLeftBracket) //没有括号,则是有限小数
{
result = FiniteDecimalToFraction(finite);
}
else
{
Fraction finiteFraction = FiniteDecimalToFraction(finite);
Fraction infiniteFraction = InfiniteDecimalToFraction(loop);
result = FractionAdd(finiteFraction, infiniteFraction);
}
PrintFraction(result);
}
int main()
{
GetInput();
return 0;
}
|