-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Most frequent element in array(read info from file) #6
base: master
Are you sure you want to change the base?
Changes from all commits
8e6192b
97b6422
8b06a47
ad0d494
84ad3d7
dfd3ae6
d6d91a6
e09bad0
284065d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 15 | ||
VisualStudioVersion = 15.0.27703.1 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Most frequent element in array(read info from file)", "Most frequent element in array(read info from file)\Most frequent element in array(read info from file).vcxproj", "{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}" | ||
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 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Debug|x64.ActiveCfg = Debug|x64 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Debug|x64.Build.0 = Debug|x64 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Debug|x86.ActiveCfg = Debug|Win32 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Debug|x86.Build.0 = Debug|Win32 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Release|x64.ActiveCfg = Release|x64 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Release|x64.Build.0 = Release|x64 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Release|x86.ActiveCfg = Release|Win32 | ||
{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}.Release|x86.Build.0 = Release|Win32 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {3D2987D6-6376-40EA-A17C-5ED5EC189DE0} | ||
EndGlobalSection | ||
EndGlobal |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="15.0" 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>15.0</VCProjectVersion> | ||
<ProjectGuid>{9FA3E77A-2B2E-4C03-B364-5D5A0170B65E}</ProjectGuid> | ||
<RootNamespace>Mostfrequentelementinarrayreadinfofromfile</RootNamespace> | ||
<WindowsTargetPlatformVersion>10.0.17134.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>v141</PlatformToolset> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<CharacterSet>MultiByte</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v141</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>MultiByte</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 /> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<SDLCheck>true</SDLCheck> | ||
<ConformanceMode>true</ConformanceMode> | ||
</ClCompile> | ||
<Link> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="main.cpp" /> | ||
<ClCompile Include="quickSort.cpp" /> | ||
<ClCompile Include="mostFrequentElement.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="mostFrequentElement.h" /> | ||
<ClInclude Include="quickSort.h" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="Файлы заголовков"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;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.cpp"> | ||
<Filter>Исходные файлы</Filter> | ||
</ClCompile> | ||
<ClCompile Include="quickSort.cpp"> | ||
<Filter>Исходные файлы</Filter> | ||
</ClCompile> | ||
<ClCompile Include="mostFrequentElement.cpp"> | ||
<Filter>Исходные файлы</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClInclude Include="quickSort.h"> | ||
<Filter>Файлы заголовков</Filter> | ||
</ClInclude> | ||
<ClInclude Include="mostFrequentElement.h"> | ||
<Filter>Файлы заголовков</Filter> | ||
</ClInclude> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
4 7 10 -5 13 0 | ||
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 | ||
-7 0 -813 0 36 -56 -6 4 67 -1 0 4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "mostFrequentElement.h" | ||
#define _CRT_SECURE_NO_WARNINGS | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <iostream> | ||
|
||
// ���������� ������ 3 �� �������� ������� ���, ����� ���������� ���� � ��������� ������ | ||
// � ������ ������� ������ �� �����. | ||
|
||
void mostFrequentElementFromFile(FILE* file); | ||
int parser(int* numbers, char const * string); | ||
void tests(); // � ����� � ����� ��������� ������ � ������� �����, �� ����� �� ���� ��������� | ||
|
||
int main() | ||
{ | ||
FILE* file = fopen("arrays.txt", "r"); | ||
mostFrequentElementFromFile(file); | ||
fclose(file); | ||
|
||
tests(); | ||
|
||
system("pause"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нельзя :) |
||
return 0; | ||
} | ||
|
||
void mostFrequentElementFromFile(FILE* file) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Было бы архитектурно лучше, если бы она сама открывала файл и возвращала прочитанные значения, которые потом в main-е передавались бы в mostFrequentElement. Это бы упрочтило переиспользуемость. |
||
{ | ||
if (file == nullptr) | ||
{ | ||
perror("ERROR OPENING FILE\n"); | ||
} | ||
else | ||
{ | ||
int const size = 100; | ||
char* string = new char[size]; | ||
while (!feof(file)) | ||
{ | ||
fgets(string, size, file); | ||
int* numbers = new int[size]; | ||
int sizeOfArray = parser(numbers, string); | ||
for (int i = 0; i < sizeOfArray; ++i) | ||
{ | ||
printf("%i ", numbers[i]); | ||
} | ||
printf("\n"); | ||
printf("Most frequent element in array is %i", mostFrequentElement(sizeOfArray, numbers)); | ||
printf("\n"); | ||
delete[] numbers; | ||
} | ||
delete[] string; | ||
} | ||
} | ||
|
||
int parser(int* numbers, char const * string) | ||
{ | ||
int const sizeOfTemp = 100; | ||
char* temp = new char[sizeOfTemp]; | ||
int size = 0; | ||
int iterator = 0; | ||
while (string[iterator] != '\0') | ||
{ | ||
int j = 0; | ||
bool flag = false; | ||
while (string[iterator] != ' ' && string[iterator] != '\n' && string[iterator] != '\0') | ||
{ | ||
temp[j] = string[iterator]; | ||
++j; | ||
++iterator; | ||
flag = true; | ||
} | ||
if (flag) | ||
{ | ||
temp[j] = '\0'; | ||
numbers[size] = atoi(temp); | ||
++size; | ||
} | ||
else | ||
{ | ||
++iterator; | ||
} | ||
} | ||
delete[] temp; | ||
return size; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. С этим бы scanf лучше справился :) Тут, например, табуляции в файле всё ломают |
||
|
||
void tests() | ||
{ | ||
printf("TESTS\n"); | ||
FILE* file = fopen("tests.txt", "r"); | ||
mostFrequentElementFromFile(file); | ||
fclose(file); | ||
printf("Test 1 passed if result is 0\n"); | ||
printf("Test 2 passed if result is 1 or 2 or 3 or 4 or 5\n"); | ||
printf("Test 3 passed if result is -34\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тесты должны быть автоматическими |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "mostFrequentElement.h" | ||
#include "quickSort.h" | ||
|
||
int mostFrequentElement(int const size, int* array) | ||
{ | ||
int* arrayCopy = new int[size]; | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
arrayCopy[i] = array[i]; | ||
} | ||
quickSort(size, arrayCopy); | ||
int mostFrequent = 0; | ||
int count = 0; | ||
int temporary = array[0]; | ||
int countTemporary = 0; | ||
for (int i = 0; i < size; ++i) | ||
{ | ||
if (arrayCopy[i] == temporary) | ||
{ | ||
countTemporary++; | ||
} | ||
else | ||
{ | ||
if (countTemporary >= count) | ||
{ | ||
count = countTemporary; | ||
mostFrequent = temporary; | ||
temporary = arrayCopy[i]; | ||
countTemporary = 1; | ||
} | ||
if (countTemporary < count) | ||
{ | ||
temporary = arrayCopy[i]; | ||
countTemporary = 1; | ||
} | ||
} | ||
} | ||
if (arrayCopy[size - 1] == temporary) | ||
{ | ||
countTemporary++; | ||
if (countTemporary > count) | ||
{ | ||
count = countTemporary; | ||
mostFrequent = temporary; | ||
} | ||
} | ||
delete[] arrayCopy; | ||
return mostFrequent; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
int mostFrequentElement(int const size, int* array); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо либо iostream, либо stdio, это две библиотеки, делающие по сути одно и то же.