Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions BinaryNumbers/BinaryNumbers.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BinaryNumbers", "BinaryNumbers\BinaryNumbers.vcxproj", "{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Debug|x64.ActiveCfg = Debug|x64
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Debug|x64.Build.0 = Debug|x64
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Debug|x86.ActiveCfg = Debug|Win32
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Debug|x86.Build.0 = Debug|Win32
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Release|x64.ActiveCfg = Release|x64
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Release|x64.Build.0 = Release|x64
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Release|x86.ActiveCfg = Release|Win32
{5C800CDD-E117-44CE-B92D-4BB73C1CE4E6}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0862E17A-C16E-473A-A416-F533D44A449A}
EndGlobalSection
EndGlobal
147 changes: 147 additions & 0 deletions BinaryNumbers/BinaryNumbers/BinaryNumbers.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{5c800cdd-e117-44ce-b92d-4bb73c1ce4e6}</ProjectGuid>
<RootNamespace>BinaryNumbers</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>false</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
22 changes: 22 additions & 0 deletions BinaryNumbers/BinaryNumbers/BinaryNumbers.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
</Project>
168 changes: 168 additions & 0 deletions BinaryNumbers/BinaryNumbers/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <stdbool.h>

int scanOne();
void print(int number);

int binaryToDecimal(bool *binaryNumber)
{
int multiplier = 1;
int number = 0;
for (int i = 11; i >= 0; --i) {
number += multiplier * binaryNumber[i];
multiplier = multiplier << 1;
}

return binaryNumber[0] ? number - 4096 : number;
}
int sizeBinary(int number) {
int size = 0;
while (number) {
number = number >> 1;
++size;
}
return size;
}

void translateDecimalToBinary(bool *binaryNumber, int size, int number) {
for (int i = 11; i >= 12 - size; --i) {
binaryNumber[i] = (number & 1);
number = number >> 1;
}
}

void printArrayBinary(bool *binaryNumber, int size, int maxPow2) {
for (int i = maxPow2 - size; i < maxPow2; ++i) {
printf("%d", binaryNumber[i]);
}
printf("\n");
}

void sumToBinary(bool *binaryNumber1, bool *binaryNumber2, bool binaryNumber3[], int size1, int size2) {
bool pastSum = false;
int i = 11;
int size3 = 0;

while (i >= 0) {
if (binaryNumber1[i] && binaryNumber2[i] && !pastSum || !binaryNumber1[i] && binaryNumber2[i] && pastSum || binaryNumber1[i] && !binaryNumber2[i] && pastSum) {
binaryNumber3[i] = false;
pastSum = true;
} else if (binaryNumber1[i] && binaryNumber2[i] && pastSum) {
binaryNumber3[i] = binaryNumber1[i] + binaryNumber2[i] + pastSum;
pastSum = true;
} else {
binaryNumber3[i] = binaryNumber1[i] + binaryNumber2[i] + pastSum;
pastSum = false;
}
if (binaryNumber3[i]) {
size3 = 12 - i;
}
--i;
}

if (binaryNumber1[0] && binaryNumber2[0]) {
binaryNumber3[0] = true;
size3 = 12;
}
}

int toChangeNegativeNumbers(int number) {
return number < 0 ? 4096 + number : number;
}

bool testStandart(void) {
int size1 = sizeBinary(12);
int size2 = sizeBinary(13);
bool* binaryNumber1 = (bool*)calloc(12, sizeof(int));
bool* binaryNumber2 = (bool*)calloc(12, sizeof(int));
translateDecimalToBinary(binaryNumber1, size1, 12);
translateDecimalToBinary(binaryNumber2, size2, 13);
bool* binaryNumber3 = (bool*)calloc(12, sizeof(int));
sumToBinary(binaryNumber1, binaryNumber2, binaryNumber3, size1, size2);
free(binaryNumber1);
free(binaryNumber2);
int result = binaryToDecimal(binaryNumber3);
free(binaryNumber3);
return result == 25;
}

bool testNotStandart() {
int number1 = toChangeNegativeNumbers(1001);
int number2 = toChangeNegativeNumbers(-1002);
int size1 = sizeBinary(number1);
int size2 = sizeBinary(number2);
bool* binaryNumber1 = (bool*)calloc(12, sizeof(int));
bool* binaryNumber2 = (bool*)calloc(12, sizeof(int));
translateDecimalToBinary(binaryNumber1, size1, number1);
translateDecimalToBinary(binaryNumber2, size2, number2);
bool* binaryNumber3 = (bool*)calloc(12, sizeof(int));
sumToBinary(binaryNumber1, binaryNumber2, binaryNumber3, size1, size2);
free(binaryNumber1);
free(binaryNumber2);
int result = binaryToDecimal(binaryNumber3);
free(binaryNumber3);
return result == -1;
}

int main() {
setlocale(LC_ALL, "RUS");

if (!testStandart() || !testNotStandart()) {
printf("%s", "����� �� ��������\n");
return 0;
} else {
printf("%s", "����� ������� ��������\n");
}

printf("%s", "������� ��� �����, ������� ����� ����� ������������� � �������� �������������.����� � �� ����� ������ ���� ������ 2048 �� ������\n");
int number1 = scanOne();
while (abs(number1) >= 2048) {
printf("%s\n", "��������� ����� ��������� �����������! ������� ������");
number1 = scanOne();
}
int number2 = scanOne();
while (abs(number2) >= 2048 || abs(number1 + number2) >= 2048) {
printf("%s\n", "��������� ����� ��������� �����������! ������� ������");
number1 = scanOne();
number2 = scanOne();
}
number1 = toChangeNegativeNumbers(number1);
number2 = toChangeNegativeNumbers(number2);
int size1 = sizeBinary(number1);
int size2 = sizeBinary(number2);
bool *binaryNumber1 = (bool*)calloc(12, sizeof(int));
bool *binaryNumber2 = (bool*)calloc(12, sizeof(int));
translateDecimalToBinary(binaryNumber1, size1, number1);
translateDecimalToBinary(binaryNumber2, size2, number2);
printArrayBinary(binaryNumber1, size1, 12);
printArrayBinary(binaryNumber2, size2, 12);
int size3 = 12;
bool *binaryNumber3 = (bool*)calloc(12, sizeof(int));
sumToBinary(binaryNumber1, binaryNumber2,binaryNumber3, size1, size2);
printArrayBinary(binaryNumber3, size3, 12);
printf("%d", binaryToDecimal(binaryNumber3));
free(binaryNumber1);
free(binaryNumber2);
free(binaryNumber3);
}

void print(int number) {
printf("%d\n", number);
}

int scanOne() {
int number = 0;
int checkScanf = scanf("%d", &number);

while (checkScanf != 1) {
while (getchar() != '\n') {
}

printf("%s", "������... ��������� ������������ ����� \n");
checkScanf = scanf("%d", &number);
}

return number;
}