Created
May 24, 2019 03:13
-
-
Save alldevic/bc33b4a395046897993b6a97f7128d0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Linq; | |
using System.Text; | |
namespace Practice4 | |
{ | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
try | |
{ | |
var tmp0 = new BinaryString("1010"); | |
var tmp1 = new BinaryString("111"); | |
var sum = BinaryString.Sum(tmp0, tmp1); | |
var composition = BinaryString.Composition(tmp0, tmp1); | |
var difference = BinaryString.Difference(tmp0, tmp1); | |
var quotient = BinaryString.Quotient(tmp0, tmp1); | |
Console.WriteLine($"Число 1: '{tmp0}' : '{tmp0.ToDouble()}'"); | |
Console.WriteLine($"Число 2: '{tmp1}' : '{tmp1.ToDouble()}'"); | |
Console.WriteLine($"Сумма: '{sum}' : '{sum.ToDouble()}'"); | |
Console.WriteLine($"Произведение: '{composition}' : '{composition.ToDouble()}'"); | |
Console.WriteLine($"Рвзность: '{difference}':'{difference.ToDouble()}'"); | |
Console.WriteLine($"Частное: '{quotient}':'{quotient.ToDouble()}'"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
} | |
} | |
/// <summary> | |
/// В качестве разделиттеля целой и вещественной частей - точка | |
/// </summary> | |
public class BinaryString | |
{ | |
private static readonly char Separtor = '.'; | |
private static readonly int MaxDepth = 12; | |
private string _stringValue; | |
private double _doubleValue; | |
public double ToDouble() | |
{ | |
return _doubleValue; | |
} | |
public BinaryString(string stringValue) | |
{ | |
try | |
{ | |
if (IsCorrectString(stringValue)) | |
{ | |
_doubleValue = ToDec(stringValue); | |
_stringValue = stringValue; | |
} | |
else | |
{ | |
throw new ArgumentException("Неверный формат входной строки"); | |
} | |
} | |
catch | |
{ | |
throw; | |
} | |
} | |
public static BinaryString Sum(BinaryString arg1, BinaryString arg2) | |
{ | |
double sum = arg1._doubleValue + arg2._doubleValue; | |
string resString = FromDec(sum); | |
return new BinaryString(resString); | |
} | |
public static BinaryString Difference(BinaryString arg1, BinaryString arg2) | |
{ | |
if (arg1._doubleValue < arg2._doubleValue) | |
{ | |
throw new Exception("Разность"); | |
} | |
double difference = arg1._doubleValue - arg2._doubleValue; | |
string resString = FromDec(difference); | |
return new BinaryString(resString); | |
} | |
public static BinaryString Composition(BinaryString arg1, BinaryString arg2) | |
{ | |
double composition = arg1._doubleValue * arg2._doubleValue; | |
string resString = FromDec(composition); | |
return new BinaryString(resString); | |
} | |
public static BinaryString Quotient(BinaryString arg1, BinaryString arg2) | |
{ | |
if (arg2._doubleValue == 0) | |
{ | |
throw new DivideByZeroException(); | |
} | |
double quotient = arg1._doubleValue / arg2._doubleValue; | |
string resString = FromDec(quotient); | |
return new BinaryString(resString); | |
} | |
public override string ToString() | |
{ | |
return _stringValue; | |
} | |
public static double ToDec(string value) | |
{ | |
var dot = value.IndexOf(Separtor); | |
var fl = dot > 0; | |
var integralResult = int.Parse(value[0].ToString()); | |
if (fl) | |
{ | |
for (var i = 1; i < dot; i++) | |
{ | |
integralResult *= 2; | |
integralResult += int.Parse(value[i].ToString()); | |
} | |
var floatingResult = 0.0d; | |
for (var i = dot + 1; i < value.Length; i++) | |
{ | |
floatingResult += int.Parse(value[i].ToString()) * Math.Pow(2, -i + dot); | |
} | |
return integralResult + floatingResult; | |
} | |
for (var i = 1; i < value.Length; i++) | |
{ | |
integralResult *= 2; | |
integralResult += int.Parse(value[i].ToString()); | |
} | |
return integralResult; | |
} | |
public static bool IsCorrectString(string value) | |
{ | |
var dotCount = 0; | |
foreach (var charItem in value) | |
{ | |
if (charItem == '0' || charItem == '1') | |
{ | |
continue; | |
} | |
if (charItem != '.') | |
{ | |
return false; | |
} | |
if (dotCount > 0) | |
{ | |
return false; | |
} | |
dotCount++; | |
} | |
return true; | |
} | |
public static string FromDec(double value) | |
{ | |
var dep = 0; | |
var integralPart = (int) Math.Truncate(value); | |
var floatingPart = Math.Round(Math.Abs(value) - Math.Abs(integralPart), MaxDepth); | |
var sb = new StringBuilder(); | |
while (integralPart > 1) | |
{ | |
var tmp = integralPart % 2; | |
sb.Append(tmp.ToString(CultureInfo.InvariantCulture)); | |
integralPart /= 2; | |
} | |
sb.Append(integralPart); | |
var integralResult = new string(sb.ToString().Reverse().ToArray()); | |
sb.Clear(); | |
while ((dep < MaxDepth) && floatingPart > Math.Pow(10, -MaxDepth)) | |
{ | |
floatingPart *= 2.0; | |
var tmp = (int) (floatingPart + 5 * Math.Pow(10, -MaxDepth - 1)); | |
if (tmp >= 2) | |
{ | |
tmp = (int) floatingPart; | |
} | |
floatingPart -= tmp; | |
sb.Append(tmp); | |
dep++; | |
} | |
var floatingResult = sb.ToString(); | |
var result = string.IsNullOrEmpty(floatingResult) | |
? integralResult | |
: $"{integralResult}.{floatingResult}"; | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment