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 Numbers/Numbers.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 16
VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Numbers", "Numbers\Numbers.vcxproj", "{5BC3DF9F-5706-481E-9A77-5C8008E0645B}"
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
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Debug|x64.ActiveCfg = Debug|x64
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Debug|x64.Build.0 = Debug|x64
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Debug|x86.ActiveCfg = Debug|Win32
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Debug|x86.Build.0 = Debug|Win32
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Release|x64.ActiveCfg = Release|x64
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Release|x64.Build.0 = Release|x64
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Release|x86.ActiveCfg = Release|Win32
{5BC3DF9F-5706-481E-9A77-5C8008E0645B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94186265-65DB-4CDF-94F1-044C60F94747}
EndGlobalSection
EndGlobal
47 changes: 47 additions & 0 deletions Numbers/Numbers/Main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "Numbers.h"
#include "NumbersTest.h"
#include <stdio.h>

int main()
{
if (!numbersTest())
{
printf("Test failed");
return -1;
}
FILE* file = fopen("g.txt", "r");
if (file == NULL)
{
printf("File not found");
return 0;
}
int number = 0;
int numberToCompare = 0;
while (!feof(file))
{
const int readBytes = fscanf(file, "%d", &number);
if (readBytes < 0) {
break;
}
numberToCompare = number;
}
fclose(file);
int data[100] = { 0 };
int counter = readNumbersSmallerSelected(data, numberToCompare, "f.txt");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Никто не обещал, что в файле будет не более 100 чисел. Тем более что в этой задаче читать числа в массив совершенно не нужно, можно было сразу считать, сравнить и записать/не записать.

if (counter == -2)
{
printf("file f.txt not found");
return 0;
}
if (counter == -1)
{
printf("memory allocation error");
return 0;
}
int result = outputOfNumbers(data, counter, "h.txt");
if (result == -2)
{
printf("failed to create a file");
}
printf("the numbers are written to the file in order h.txt");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всю кучу содержательного кода в main по работе с файлами никто не проверяет, потому что тесты делают это сами. Стоило всё это сделать функцией, которая бы принимала на вход просто три имени файла и делала что нужно. В тесте можно было бы открыть выходной файл и проверить, что всё ок.

52 changes: 52 additions & 0 deletions Numbers/Numbers/Numbers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Numbers.h"
#include <stdio.h>
#include <malloc.h>

int readNumbersSmallerSelected(int* data, int numberToCompare, const char* filename)
{
FILE* file = fopen(filename, "r");
if (file == NULL)
{
return -2;
}
int counter = 0;
while (!feof(file))
{
int* buffer = (int*)malloc(sizeof(int)*100);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int* buffer = (int*)malloc(sizeof(int)*100);
int* buffer = (int*)malloc(sizeof(int) * 100);

{
if (buffer == NULL)
{
return -1;
}
}
const int readBytes = fscanf(file, "%d", buffer);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вы читаете одно число, но выделяете на куче массив из сотни чисел для этого

if (readBytes < 0)
{
break;
}
if (*buffer < numberToCompare)
{
data[counter] = *buffer;
counter++;
}
}
fclose(file);
return counter;
}

int outputOfNumbers(int* data, int counter, const char* filename)
{
FILE* file = fopen(filename, "w");
if (file == NULL)
{
printf("File not found");
return -2;
}
int linesRead = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это оказалось не нужно, кажется

for (int i = 0; i < counter; i++)
{
fprintf(file, "%d ", data[i]);
}
fclose(file);
return 0;
}
7 changes: 7 additions & 0 deletions Numbers/Numbers/Numbers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

// Reading numbers smaller than a number(numberToCompare) from a file g.txt and writing these numbers to the data array
int readNumbersSmallerSelected(int* data, int numberToCompare, const char* filename);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как-то не по-английски


// Output of numbers less than a given number to a file h.txt
int outputOfNumbers(int* data, int counter, const char* filename);
153 changes: 153 additions & 0 deletions Numbers/Numbers/Numbers.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?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>{5bc3df9f-5706-481e-9a77-5c8008e0645b}</ProjectGuid>
<RootNamespace>Numbers</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>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</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;_CRT_SECURE_NO_WARNINGS;_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>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(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" />
<ClCompile Include="Numbers.c" />
<ClCompile Include="NumbersTest.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Numbers.h" />
<ClInclude Include="NumbersTest.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,21 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="theMostCommonElement.c">
<ClCompile Include="Numbers.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="qsort.c">
<ClCompile Include="Main.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="qsortTest.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="testTheMostCommonElement.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="main.c">
<ClCompile Include="NumbersTest.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="theMostCommonElement.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="qsort.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="qsortTest.h">
<ClInclude Include="Numbers.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="testTheMostCommonElement.h">
<ClInclude Include="NumbersTest.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
Expand Down
Loading