diff --git a/BWT/BWT/Program.cs b/BWT/BWT/Program.cs deleted file mode 100644 index 11af52d..0000000 --- a/BWT/BWT/Program.cs +++ /dev/null @@ -1,208 +0,0 @@ -namespace Sort; - -using System; -using System.Text; - -enum WhichStringIsBigger -{ - First, - Second, - Same, - Error -} -class Program -{ - public const int NumberOfCharacters = 65536; - - // A method of comparing two rows using indexes indicating their beginning - public static WhichStringIsBigger CompareStrings(string stringToBWT, int positionFirst, int positionSecond) - { - int cyclicalFirstStringPosition = positionFirst; - int cyclicalSecondStringPosition = positionSecond; - int comparedSymbolsCount = 0; - while (comparedSymbolsCount < stringToBWT.Length) - { - if (stringToBWT[cyclicalFirstStringPosition % stringToBWT.Length] > stringToBWT[cyclicalSecondStringPosition % stringToBWT.Length]) - { - return WhichStringIsBigger.First; - } - else if (stringToBWT[cyclicalFirstStringPosition % stringToBWT.Length] < stringToBWT[cyclicalSecondStringPosition % stringToBWT.Length]) - { - return WhichStringIsBigger.Second; - } - - ++cyclicalFirstStringPosition; - ++cyclicalSecondStringPosition; - ++comparedSymbolsCount; - } - return WhichStringIsBigger.Same; - } - - // Sorting by inserts - public static void InsertSort(string stringToBWT, int[] arrayPositions, int startArray, int endArray) - { - for (int i = startArray + 1; i <= endArray; ++i) - { - int j = i; - while (j >= startArray + 1 && CompareStrings(stringToBWT, (arrayPositions[j - 1] + 1) % arrayPositions.Length, - (arrayPositions[j] + 1) % arrayPositions.Length) == WhichStringIsBigger.First) - { - (arrayPositions[j - 1], arrayPositions[j]) = (arrayPositions[j], arrayPositions[j - 1]); - - --j; - } - } - } - - // Finding a reference element - public static int Partition(string stringToBWT, int[] arrayPositions, int startArray, int endArray) - { - int pivot = arrayPositions[endArray]; - int i = startArray; - - for (int j = startArray; j < endArray; ++j) - { - WhichStringIsBigger result = CompareStrings(stringToBWT, (arrayPositions[j] + 1) % arrayPositions.Length, (pivot + 1) % arrayPositions.Length); - if (result == WhichStringIsBigger.Second || result == WhichStringIsBigger.Same) - { - (arrayPositions[j], arrayPositions[i]) = (arrayPositions[i], arrayPositions[j]); - - ++i; - } - } - (arrayPositions[i], arrayPositions[endArray]) = (arrayPositions[endArray], arrayPositions[i]); - return i; - } - - // Quick sort, which sorts an array of indexes by comparing rows obtained by cyclic permutations - public static void QSort(string stringToBWT, int[] arrayPositions, int startArray, int endArray) - { - if (endArray - startArray + 1 <= 10) - { - InsertSort(stringToBWT, arrayPositions,startArray, endArray); - return; - } - if (startArray < endArray) - { - int positionElement = Partition(stringToBWT, arrayPositions, startArray, endArray); - QSort(stringToBWT, arrayPositions, startArray, positionElement - 1); - QSort(stringToBWT, arrayPositions, positionElement + 1, endArray); - } - } - - // Shell for running quick sort - public static void QuickSort(string stringToBWT, int[] arrayPositions) - { - QSort(stringToBWT, arrayPositions, 0, arrayPositions.Length - 1); - } - - // String compression by the Burrows-Wheeler algorithm - public static (string result, int firstPosition) BwtConvert(string stringToBWT) - { - var arrayPositions = new int[stringToBWT.Length]; - for (int i = 0; i < stringToBWT.Length; ++i) - { - arrayPositions[i] = i; - } - - QuickSort(stringToBWT, arrayPositions); - - var stringAfterBWT = new StringBuilder(stringToBWT); - for (int i = 0; i < stringToBWT.Length; ++i) - { - stringAfterBWT[i] = stringToBWT[arrayPositions[i]]; - } - - int firstPosition = 0; - for (int i = 0; i < stringToBWT.Length; ++i) - { - if (arrayPositions[i] == stringToBWT.Length - 1) - { - firstPosition = i; - return (stringAfterBWT.ToString(), firstPosition); - } - } - return (stringAfterBWT.ToString(), firstPosition); - } - - // The function receives a string after the Burrows-Wheeler algorithm as input, returns a string before the Burrows-Wheeler algorithm - public static string BWTReverseСonvert(string stringAfterBWT, int firstPosition) - { - var arraySymbols = new int[NumberOfCharacters]; - var arrayPreCalculationTable = new int[stringAfterBWT.Length]; - - for (int i = 0; i < stringAfterBWT.Length; ++i) - { - ++arraySymbols[stringAfterBWT[i]]; - } - - int summary = 0; - for (int i = 0; i < NumberOfCharacters; i++) - { - summary = summary + arraySymbols[i]; - arraySymbols[i] = summary - arraySymbols[i]; - } - - for (int i = 0; i < stringAfterBWT.Length; ++i) - { - int j = i - 1; - while (j >= 0) - { - if (stringAfterBWT[i] == stringAfterBWT[j]) - { - arrayPreCalculationTable[i]++; - } - --j; - } - } - var stringBeforeBWT = new StringBuilder(stringAfterBWT); - int positionForNewChar = stringBeforeBWT.Length - 1; - stringBeforeBWT[positionForNewChar] = stringAfterBWT[firstPosition]; - --positionForNewChar; - int sum = arrayPreCalculationTable[firstPosition] + arraySymbols[stringAfterBWT[firstPosition]]; - while (positionForNewChar >= 0) - { - stringBeforeBWT[positionForNewChar] = stringAfterBWT[sum]; - sum = arrayPreCalculationTable[sum] + arraySymbols[stringAfterBWT[sum]]; - --positionForNewChar; - } - return stringBeforeBWT.ToString(); - } - - // Checking compression and unzipping by the Burrows-Wheeler algorithm - public static bool TestBWT() - { - string stringToTest = "ABACABA"; - (var stringAfterBWT, var firstPosition) = BwtConvert(stringToTest); - if (stringAfterBWT != "BCABAAA") - { - return false; - } - return BWTReverseСonvert(stringAfterBWT, firstPosition) == "ABACABA"; - } - - public static void Main(string[] args) - { - if (TestBWT()) - { - Console.WriteLine("All tests correct"); - } - else - { - Console.WriteLine("Some problems with tests..."); - return; - } - Console.WriteLine("Input string"); - var stringToBWT = Console.ReadLine(); - if (stringToBWT == null) - { - Console.WriteLine("You input null string or your input is not correct"); - return; - } - (var returnedStringFromBWT, var firstPosition) = BwtConvert(stringToBWT); - Console.WriteLine("String after BWT"); - Console.WriteLine(returnedStringFromBWT); - var stringBeforeBWT = BWTReverseСonvert(returnedStringFromBWT, firstPosition); - Console.WriteLine(stringBeforeBWT); - } -} \ No newline at end of file diff --git a/Bor/Bor.sln b/Bor/Bor.sln new file mode 100644 index 0000000..e022dee --- /dev/null +++ b/Bor/Bor.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bor", "Bor\Bor.csproj", "{5CEA5896-9CC3-4F02-90BF-DC6F85F2A3A1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BorTest", "BorTest\BorTest.csproj", "{0CD6D566-31A1-495A-B839-D6ABB8B650D6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5CEA5896-9CC3-4F02-90BF-DC6F85F2A3A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CEA5896-9CC3-4F02-90BF-DC6F85F2A3A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CEA5896-9CC3-4F02-90BF-DC6F85F2A3A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CEA5896-9CC3-4F02-90BF-DC6F85F2A3A1}.Release|Any CPU.Build.0 = Release|Any CPU + {0CD6D566-31A1-495A-B839-D6ABB8B650D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CD6D566-31A1-495A-B839-D6ABB8B650D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CD6D566-31A1-495A-B839-D6ABB8B650D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CD6D566-31A1-495A-B839-D6ABB8B650D6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {54281C5E-DB00-4ACD-A071-1FB58D538B0E} + EndGlobalSection +EndGlobal diff --git a/Bor/Bor/Bor.cs b/Bor/Bor/Bor.cs new file mode 100644 index 0000000..e25b87d --- /dev/null +++ b/Bor/Bor/Bor.cs @@ -0,0 +1,169 @@ +namespace Bor; + +// A container for storing strings, in the form of a suspended tree +public class Bor +{ + private const int alphabetSize = 65536; + + private BorElement root = new BorElement(); + + // Adding an element + public bool Add(string element) + { + if (root == null) + { + throw new InvalidOperationException(); + } + if (element == null && !root.IsTerminal) + { + root.HowManyStringInDictionary++; + root.IsTerminal = true; + return true; + } + if (element != null) + { + root.HowManyStringInDictionary++; + } + if (element == null) + { + return false; + } + var walker = root; + int i = 0; + while (i < element.Length) + { + int number = (int)element[i]; + if (!walker.Next.ContainsKey(number)) + { + walker.Next.Add(number, new BorElement()); + ++walker.SizeDictionary; + } + ++walker.Next[number].HowManyStringInDictionary; + walker = walker.Next[number]; + i++; + } + return walker.IsTerminal == false ? walker.IsTerminal = true && true : false; + } + + private bool RemoveHelp(BorElement walker, string element, int position, ref bool isDeleted) + { + if (position == element.Length) + { + if (walker.IsTerminal) + { + walker.IsTerminal = false; + return true; + } + return false; + } + + if (walker.Next.ContainsKey(element[position])) + { + bool isCorrect = RemoveHelp(walker.Next[element[position]], element, position + 1, ref isDeleted); + if (!isCorrect) + { + return false; + } + if (walker.Next[element[position]].HowManyStringInDictionary == 1) + { + walker.Next.Remove(element[position]); + isDeleted = true; + return true; + } + if (isDeleted == true) + { + --walker.Next[element[position]].SizeDictionary; + isDeleted = false; + } + --walker.Next[element[position]].HowManyStringInDictionary; + } + else + { + return false; + } + return true; + } + + // Deleting an element in the tree + public bool Remove(string element) + { + if (root == null) + { + throw new InvalidOperationException(); + } + if (element == null) + { + root.IsTerminal = false; + return true; + } + var walker = root; + bool flag = false; + if (RemoveHelp(walker, element, 0, ref flag)) + { + --root.HowManyStringInDictionary; + return true; + } + return false; + } + + // Counts the number of rows with the same prefix + public int HowManyStartsWithPrefix(String prefix) + { + if (root == null) + { + if (prefix == null) + { + return 1; + } + return 0; + } + var walker = root; + int i = 0; + while(i < prefix.Length) + { + if (!walker.Next.ContainsKey(prefix[i])) + { + return 0; + } + walker = walker.Next[prefix[i]]; + ++i; + } + return walker.HowManyStringInDictionary; + } + + // Checks for the presence of a string + public bool Contains(string element) + { + if (root == null) + { + return element == null; + } + var walker = root; + int i = 0; + while(i < element.Length) + { + if (!walker.Next.ContainsKey(element[i])) + { + return false; + } + walker = walker.Next[element[i]]; + ++i; + } + return true; + } + + private class BorElement + { + public Dictionary Next { get; set; } + public bool IsTerminal { get; set; } + + public int SizeDictionary { get; set; } + + public int HowManyStringInDictionary { get; set; } + + public BorElement() + { + Next = new Dictionary(); + } + } +} \ No newline at end of file diff --git a/Bor/Bor/Program.cs b/Bor/Bor/Program.cs new file mode 100644 index 0000000..3d6b713 --- /dev/null +++ b/Bor/Bor/Program.cs @@ -0,0 +1 @@ +Console.WriteLine("Get started with Bor, a class created specifically for easy handling of strings!"); \ No newline at end of file diff --git a/BWT/BWT.sln b/stackCalculator/stackCalculator.sln similarity index 58% rename from BWT/BWT.sln rename to stackCalculator/stackCalculator.sln index db64427..bbded00 100644 --- a/BWT/BWT.sln +++ b/stackCalculator/stackCalculator.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BWT", "BWT\BWT.csproj", "{97F74F11-2F27-4B42-A5B1-8D88573DFB01}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackCalculator", "stackCalculator\StackCalculator.csproj", "{89A2482C-4A20-41A4-9A42-1623A4FA88C3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Debug|Any CPU.Build.0 = Debug|Any CPU - {97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Release|Any CPU.ActiveCfg = Release|Any CPU - {97F74F11-2F27-4B42-A5B1-8D88573DFB01}.Release|Any CPU.Build.0 = Release|Any CPU + {89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89A2482C-4A20-41A4-9A42-1623A4FA88C3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {65A6BC48-3287-4942-BACC-A033A3982A41} + SolutionGuid = {E6D50142-7CF4-466F-9F0D-9262A98C0B2C} EndGlobalSection EndGlobal diff --git a/stackCalculator/stackCalculator/InterfaceForStack.cs b/stackCalculator/stackCalculator/InterfaceForStack.cs new file mode 100644 index 0000000..2e4e88c --- /dev/null +++ b/stackCalculator/stackCalculator/InterfaceForStack.cs @@ -0,0 +1,16 @@ +namespace StackCalculator; + + interface IOperationsWithStack + { + // Add element to stack + void AddElement(double value); + + // Remove element in stack and return deleted item + (bool, double) RemoveElement(); + + // Print all elements + void PrintTheElements(); + + // Checking that the stack is empty + bool IsEmpty(); +} \ No newline at end of file diff --git a/stackCalculator/stackCalculator/Program.cs b/stackCalculator/stackCalculator/Program.cs new file mode 100644 index 0000000..8b925d5 --- /dev/null +++ b/stackCalculator/stackCalculator/Program.cs @@ -0,0 +1,33 @@ +namespace StackCalculator; + +using System; +class Program +{ + public static void Main(string[] args) + { + Test test = new Test(); + if (test.TestForProgram()) + { + Console.WriteLine("All tests correct"); + } + else + { + Console.WriteLine("Problems..."); + return; + } + Console.WriteLine("Enter an example in the postfix form"); + var stringWithExpression = Console.ReadLine(); + StackCalculator calculator = new(); + if (stringWithExpression == null) + { + return; + } + (var isCorrectWork, var result) = calculator.ConvertToAResponse(stringWithExpression); + if (!isCorrectWork) + { + Console.WriteLine("Problems with expression or you tried to divide by zero"); + return; + } + Console.WriteLine(result); + } +} \ No newline at end of file diff --git a/stackCalculator/stackCalculator/Stack.cs b/stackCalculator/stackCalculator/Stack.cs new file mode 100644 index 0000000..45016c6 --- /dev/null +++ b/stackCalculator/stackCalculator/Stack.cs @@ -0,0 +1,4 @@ +namespace StackCalculator; + +//Standart stack +public class Stack : StackList{} \ No newline at end of file diff --git a/stackCalculator/stackCalculator/StackCalculator.cs b/stackCalculator/stackCalculator/StackCalculator.cs new file mode 100644 index 0000000..8dc99d7 --- /dev/null +++ b/stackCalculator/stackCalculator/StackCalculator.cs @@ -0,0 +1,71 @@ +namespace StackCalculator; + +public class StackCalculator +{ + private double delta = 0.0000000000001; + + // Receives the input string in which the expression is written in postfix form, finds the result + public (bool, double) ConvertToAResponse(string stringWithExpression) + { + Stack stackExpression = new(); + int i = 0; + string[] expressionArray = stringWithExpression.Split(' '); + while (i < expressionArray.Length) + { + var isCorrectNumber = Int32.TryParse(expressionArray[i], out var number); + if (isCorrectNumber) + { + stackExpression.AddElement(number); + } + else + { + if (expressionArray[i].Length != 1) + { + return (false, 0); + } + double numberAfter = 0; + (var isCorrect, var firstNumber) = stackExpression.RemoveElement(); + + if (!isCorrect) + { + return (false, 0); + } + + (isCorrect, var secondNumber) = stackExpression.RemoveElement(); + + if (!isCorrect) + { + return (false, 0); + } + + switch (expressionArray[i][0]) + { + case '*': + numberAfter = firstNumber * secondNumber; + break; + case '+': + numberAfter = firstNumber + secondNumber; + break; + case '-': + numberAfter = secondNumber - firstNumber; + break; + case '/': + if (Math.Abs(firstNumber) < delta) + { + return (false, 0); + } + numberAfter = secondNumber / firstNumber; + break; + } + stackExpression.AddElement(numberAfter); + } + ++i; + } + (var isCorrectExpression, var result) = stackExpression.RemoveElement(); + if (!isCorrectExpression) + { + return (false, 0); + } + return stackExpression.IsEmpty() ? (true, result) : (false, 0); + } +} diff --git a/stackCalculator/stackCalculator/StackWithList.cs b/stackCalculator/stackCalculator/StackWithList.cs new file mode 100644 index 0000000..9d09748 --- /dev/null +++ b/stackCalculator/stackCalculator/StackWithList.cs @@ -0,0 +1,58 @@ +namespace StackCalculator; + +// Stack implemented on list +public class StackList : IOperationsWithStack +{ + private StackElement headStack; + + public void AddElement(double value) + { + var item = new StackElement(value); + if (headStack == null) + { + headStack = item; + } + else + { + StackElement copy = headStack; + headStack = item; + headStack.next = copy; + } + } + + public (bool, double) RemoveElement() + { + if (headStack == null) + { + return (false, 0); + } + double item = headStack.valueStack; + StackElement copy = headStack.next; + headStack = copy; + return (true, item); + } + + public void PrintTheElements() + { + StackElement walker = headStack; + while (walker != null) + { + Console.WriteLine(walker.valueStack); + walker = walker.next; + } + } + + public bool IsEmpty() => headStack == null; + + private class StackElement + { + public StackElement(double value) + { + valueStack = value; + next = null; + } + + public double valueStack { get; set; } + public StackElement next { get; set; } + } +} \ No newline at end of file diff --git a/stackCalculator/stackCalculator/Tests.cs b/stackCalculator/stackCalculator/Tests.cs new file mode 100644 index 0000000..47d7fc7 --- /dev/null +++ b/stackCalculator/stackCalculator/Tests.cs @@ -0,0 +1,17 @@ +namespace StackCalculator; + +public class Test +{ + // Tests the program + public bool TestForProgram() + { + StackCalculator calculator = new StackCalculator(); + (var isCorrectWork, var result) = calculator.ConvertToAResponse("123 23 +"); + if (!isCorrectWork || result != 146) + { + return false; + } + (isCorrectWork, result) = calculator.ConvertToAResponse("123 23"); + return !isCorrectWork; + } +} diff --git a/BWT/BWT/BWT.csproj b/stackCalculator/stackCalculator/stackCalculator.csproj similarity index 100% rename from BWT/BWT/BWT.csproj rename to stackCalculator/stackCalculator/stackCalculator.csproj