Hej,
Co tu dużo pisać. Chciałem się zapytać czy takie narzędzie (podobne zastosowanie jak metody z klasy Math
) jest dobrze napisane pod różnymi względami.
Starałem się nadawać nazwy, który mówią jak najwięcej, zresztą sami zobaczcie jak to wyszło.
Jedyny problem jaki miałem to jak zrobić, żeby jakość logicznie zwracać liczbę, a nie string
.
Drugi problem, o który chciałem się zapytać to czy ten kod zalicza się do czytelnego, a jak nie to co byście zmienili?
Decimal
to dobry typ zmiennej w tym zastosowaniu?
Mam nadzieję, że żadnych błędów nie ma.
static class AdvancedMathOperations
{
private static int MIN_EXPONENTIAL_NOTATION_VALUE = 1;
private static int MAX_EXPONENTIAL_NOTATION_VALUE = 10;
/// <summary>
/// Return Exponential Notation in STRING!
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string MakeExponentialNotation(decimal number)
{
if(number >= MIN_EXPONENTIAL_NOTATION_VALUE && number <= MAX_EXPONENTIAL_NOTATION_VALUE)
{
return number.ToString();
}
if(number > MAX_EXPONENTIAL_NOTATION_VALUE)
{
return MakeExponentialNotationWhenNumberIsToBig(number, true);
}
else if(number < -MAX_EXPONENTIAL_NOTATION_VALUE)
{
return MakeExponentialNotationWhenNumberIsToBig(number, false);
}
else if(number < MIN_EXPONENTIAL_NOTATION_VALUE && number > 0)
{
return MakeExponentialNotationWhenNumberIsToSmall(number, true);
}
else if(number > -MIN_EXPONENTIAL_NOTATION_VALUE && number < 0)
{
return MakeExponentialNotationWhenNumberIsToSmall(number, false);
}
return number.ToString();
}
static string MakeExponentialNotationWhenNumberIsToBig(decimal number, bool isPositive)
{
int iterationCount = 0;
if (isPositive)
{
while (number > MAX_EXPONENTIAL_NOTATION_VALUE)
{
number /= MAX_EXPONENTIAL_NOTATION_VALUE;
iterationCount++;
}
return number + " * " + MAX_EXPONENTIAL_NOTATION_VALUE.ToString() + "^" + iterationCount;
}
else
{
while (number < -MAX_EXPONENTIAL_NOTATION_VALUE)
{
number /= MAX_EXPONENTIAL_NOTATION_VALUE;
iterationCount++;
}
return number + " * " + MAX_EXPONENTIAL_NOTATION_VALUE.ToString() + "^" + iterationCount;
}
}
static string MakeExponentialNotationWhenNumberIsToSmall(decimal number, bool isPositive)
{
int iterationCount = 0;
if(isPositive)
{
while(number < MIN_EXPONENTIAL_NOTATION_VALUE)
{
number *= MAX_EXPONENTIAL_NOTATION_VALUE;
iterationCount--;
}
return number + " * " + MAX_EXPONENTIAL_NOTATION_VALUE.ToString() + "^" + iterationCount;
}
else
{
while(number > -MIN_EXPONENTIAL_NOTATION_VALUE)
{
number *= MAX_EXPONENTIAL_NOTATION_VALUE;
iterationCount--;
}
return number + " * " + MAX_EXPONENTIAL_NOTATION_VALUE.ToString() + "^" + iterationCount;
}
}
}
Wielkie dzięki, jakość kodu to chyba jedyna rzecz, którą praktycznie nie da się wyczytać prosto z internetu.