A friend wrote an algorithm that converts the input integer or floating-point number to the scientific notation expression. After writing the algorithm, he asked me to help him see if there were any bugs or other issues that were not fully considered. I have been scared by the fact that I have not carefully read it-I have written nearly three hundred lines of code. I didn't say anything about him. I just went back to my computer and wrote a try.
Requirement: enter a number, which is represented by a scientific notation. there must be three valid numbers, and the power part must also be three digits. If the power is insufficient, the value of the power is zero.
The Code is as follows:
Public abstract class ScienceCount
{
Public static string KXJSF (double num)
{
Double bef = System. Math. Abs (num );
Int aft = 0;
While (bef> = 10 | (bef <1 & bef! = 0 ))
{
If (bef> = 10)
{
Bef = bef/10;
Aft ++;
}
Else
{
Bef = bef * 10;
Aft --;
}
}
Return string. Concat (num> = 0? "": "-", ReturnBef (bef), "E", ReturnAft (aft ));
}
/// <Summary>
/// Processing valid numbers
/// </Summary>
/// <Param name = "bef"> valid number </param>
/// <Returns> three valid numbers. If the number is insufficient, the value is set to zero. </returns>
Public static string ReturnBef (double bef)
{
If (bef. ToString ()! = Null)
{
Char [] arr = bef. ToString (). ToCharArray ();
Switch (arr. Length)
{
Case 1:
Case 2: return string. Concat (arr [0], ".", "00"); break;
Case 3: return string. Concat (arr [0] + "." + arr [2] + "0"); break;
Default: return string. Concat (arr [0] + "." + arr [2] + arr [3]); break;
}
}
Else
Return "000 ";
}
/// <Summary>
/// Power Processing
/// </Summary>
/// <Param name = "aft"> power </param>
/// <Returns> three-digit power. If the value is insufficient, the value is set to zero. </returns>
Public static string ReturnAft (int aft)
{
If (aft. ToString ()! = Null)
{
String end;
Char [] arr = System. Math. Abs (aft). ToString (). ToCharArray ();
Switch (arr. Length)
{
Case 1: end = "00" + arr [0]; break;
Case 2: end = "0" + arr [0] + arr [1]; break;
Default: end = System. Math. Abs (aft). ToString (); break;
}
Return string. Concat (aft> = 0? "+": "-", End );
}
Else
Return "+ 000 ";
}
}
Call code:
Static void Main ()
{
String num = "0 ";
While (num! = "#")
{
Num = Console. ReadLine ();
If (! String. IsNullOrEmpty (num. Trim ()))
Console. WriteLine (ScienceCount. KXJSF (double. Parse (num )));
}
}