diff --git a/KR.userprefs b/KR.userprefs new file mode 100644 index 0000000..8602d50 --- /dev/null +++ b/KR.userprefs @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/KR/KR1.csproj b/KR/KR1.csproj new file mode 100644 index 0000000..c5cc3b8 --- /dev/null +++ b/KR/KR1.csproj @@ -0,0 +1,40 @@ + + + + Debug + x86 + {4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC} + Exe + KR1 + KR + v4.6.1 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + + \ No newline at end of file diff --git a/KR/Program.cs b/KR/Program.cs new file mode 100644 index 0000000..554e116 --- /dev/null +++ b/KR/Program.cs @@ -0,0 +1,104 @@ +/* +ФИО: Костюченко Илья Игоревич +Группа: БПИ 171-1 +Дата: 17.10.17 +Вариант: KRmod1.1 +*/ + +using System; +using static Iko.Console; +using static System.Console; + +/* +# Alternative solution 1 + +Instead of printing to the console and retrning a bool, ProcessNumber could return +an array of numbers, conforming to the passed predecate. + +# Alternative solution 2 + +ProcessNumber could take just the number as a parameter and +return an array of tuples (, ) + +# Alternative solution 3 + +The number of `0` and `1` can also be counted by repeatedly dividing the given number by 2. +*/ + +class MainClass { + /// + /// Counts the number of charecters that are equal to the specified character in the binary string represenattion of the given number. + /// + /// the number of characters + /// the number to be processed + /// the character represenation of the bit ti be counted + static int CountBinary(int number, char bit) { + int outp = 0; + foreach (char character in Convert.ToString(number, 2)) { + if (character == bit) { + outp += 1; + } + } + return outp; + } + + /// + /// Counts the number of `1` in the binary represenation of the number + /// + /// The number of `1` in the binary represenation of the number + /// The number to be processed + static int CountBinaryOnes(int number) { + return CountBinary(number, '1'); + } + + /// + /// Counts the number of `0` in the binary represenation of the number + /// + /// The number of `0` in the binary represenation of the number + /// The number to be processed + static int CountBinaryZeros(int number) { + return CountBinary(number, '0'); + } + + /// + /// Prompts the user for a number in the range (0;10000) and returns the number. + /// If the number does not conform to the requirements, the user will be prompted again. + /// + /// The number put in by the user + /// A string propmpting the user for input. + /// A string telling the user, he didn't input correct data + static int ReadNumber(string message, string errorMessage) { + return GetInt(message, x => x > 0 && x < 10000, errorMessage); + } + + /// + /// Outputs all numbers from the range (0; number], conforming to the passed function. + /// + /// true, if number any number was output to the console, false otherwise. + /// The upper-bound of the range to be tested + /// The function used to test numbers + static bool ProcessNumber(int number, Func validate) { + bool didSucceed = false; + for (int i = 1; i <= number; i++) { + if (validate(CountBinaryZeros(i), CountBinaryOnes(i))) { + didSucceed = true; + WriteLine(i); + } + } + return didSucceed; + } + + public static void Main(string[] args) { + Repeat(() => { + int number = ReadNumber($"Please enter a positive number smaller than 10000{Environment.NewLine}N = " + , "That is not a valid positive number smaller than 10000"); + if (ProcessNumber(number, (a, b) => a == b)) { + return; + } + if (ProcessNumber(number, (a, b) => a + 1 == b)) { + return; + } + WriteLine("That number does not yield any results..."); + }); + } +} diff --git a/KR/Properties/AssemblyInfo.cs b/KR/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a4f1e5e --- /dev/null +++ b/KR/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("KR")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/KR/bin/Debug/KR.exe b/KR/bin/Debug/KR.exe new file mode 100644 index 0000000..155ffce Binary files /dev/null and b/KR/bin/Debug/KR.exe differ diff --git a/KR/bin/Debug/KR.pdb b/KR/bin/Debug/KR.pdb new file mode 100644 index 0000000..dcee07a Binary files /dev/null and b/KR/bin/Debug/KR.pdb differ diff --git a/KR/iko.cs b/KR/iko.cs new file mode 100644 index 0000000..cd7754e --- /dev/null +++ b/KR/iko.cs @@ -0,0 +1,64 @@ +using System; +using static System.Console; + +namespace Iko { + static class Console { + public static double GetDouble(string text, Func validate, string falseText) { + Write(text); + double res; + while (true) { + if (double.TryParse(ReadLine(), out res) && validate(res)) { + return res; + } else { + WriteLine(falseText); + Write(text); + } + } + } + + public static double GetDouble(string text, string falseText) { + return GetDouble(text, _ => { return true; }, falseText); + } + + public static float GetFloat(string text, Func validate, string falseText) { + Write(text); + float res; + while (true) { + if (float.TryParse(ReadLine(), out res) && validate(res)) { + return res; + } else { + WriteLine(falseText); + Write(text); + } + } + } + + public static float GetFloat(string text, string falseText) { + return GetFloat(text, _ => { return true; }, falseText); + } + + public static int GetInt(string text, Func validate, string falseText) { + Write(text); + int res; + while (true) { + if (int.TryParse(ReadLine(), out res) && validate(res)) { + return res; + } else { + WriteLine(falseText); + Write(text); + } + } + } + + public static int GetInt(string text, string falseText) { + return GetInt(text, _ => { return true; }, falseText); + } + + public static void Repeat(Action f) { + do { + f(); + Write("Press Enter to repeat program. Press any other key to quit."); + } while (ReadKey().Key == ConsoleKey.Enter); + } + } +} \ No newline at end of file diff --git a/KR/obj/x86/Debug/KR.csproj.CoreCompileInputs.cache b/KR/obj/x86/Debug/KR.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..97b1581 --- /dev/null +++ b/KR/obj/x86/Debug/KR.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +62543f0b6aa88a2a527b88cae3a0aa492fa84e34 diff --git a/KR/obj/x86/Debug/KR.csproj.FileListAbsolute.txt b/KR/obj/x86/Debug/KR.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..f1ea705 --- /dev/null +++ b/KR/obj/x86/Debug/KR.csproj.FileListAbsolute.txt @@ -0,0 +1,5 @@ +/Users/ilya/Developer/HSE/KR/KR/bin/Debug/KR.exe +/Users/ilya/Developer/HSE/KR/KR/bin/Debug/KR.pdb +/Users/ilya/Developer/HSE/KR/KR/obj/x86/Debug/KR.csproj.CoreCompileInputs.cache +/Users/ilya/Developer/HSE/KR/KR/obj/x86/Debug/KR.exe +/Users/ilya/Developer/HSE/KR/KR/obj/x86/Debug/KR.pdb diff --git a/KR/obj/x86/Debug/KR.exe b/KR/obj/x86/Debug/KR.exe new file mode 100644 index 0000000..155ffce Binary files /dev/null and b/KR/obj/x86/Debug/KR.exe differ diff --git a/KR/obj/x86/Debug/KR.pdb b/KR/obj/x86/Debug/KR.pdb new file mode 100644 index 0000000..dcee07a Binary files /dev/null and b/KR/obj/x86/Debug/KR.pdb differ diff --git a/KR/obj/x86/Debug/KR1.csproj.CoreCompileInputs.cache b/KR/obj/x86/Debug/KR1.csproj.CoreCompileInputs.cache new file mode 100644 index 0000000..ec12475 --- /dev/null +++ b/KR/obj/x86/Debug/KR1.csproj.CoreCompileInputs.cache @@ -0,0 +1 @@ +8578ba116c5805ead7d11590607b3474c8cd2ea8 diff --git a/KR/obj/x86/Debug/KR1.csproj.FileListAbsolute.txt b/KR/obj/x86/Debug/KR1.csproj.FileListAbsolute.txt new file mode 100644 index 0000000..2ac17af --- /dev/null +++ b/KR/obj/x86/Debug/KR1.csproj.FileListAbsolute.txt @@ -0,0 +1,5 @@ +/Users/ilya/Developer/HSE/KR1/KR/bin/Debug/KR.exe +/Users/ilya/Developer/HSE/KR1/KR/bin/Debug/KR.pdb +/Users/ilya/Developer/HSE/KR1/KR/obj/x86/Debug/KR1.csproj.CoreCompileInputs.cache +/Users/ilya/Developer/HSE/KR1/KR/obj/x86/Debug/KR.exe +/Users/ilya/Developer/HSE/KR1/KR/obj/x86/Debug/KR.pdb diff --git a/KR1.sln b/KR1.sln new file mode 100644 index 0000000..01ce25a --- /dev/null +++ b/KR1.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KR1", "KR\KR1.csproj", "{4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC}.Debug|x86.ActiveCfg = Debug|x86 + {4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC}.Debug|x86.Build.0 = Debug|x86 + {4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC}.Release|x86.ActiveCfg = Release|x86 + {4C6DB024-2B7E-4F8B-A9EE-8D25899E11AC}.Release|x86.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/KR1.userprefs b/KR1.userprefs new file mode 100644 index 0000000..bf6d67c --- /dev/null +++ b/KR1.userprefs @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ef14a8e --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# КР1 \ No newline at end of file