Skip to content
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

Phonebook #5

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
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 hw4/Phonebook/Phonebook.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 15
VisualStudioVersion = 15.0.27703.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Phonebook", "Phonebook\Phonebook.vcxproj", "{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}"
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
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Debug|x64.ActiveCfg = Debug|x64
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Debug|x64.Build.0 = Debug|x64
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Debug|x86.ActiveCfg = Debug|Win32
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Debug|x86.Build.0 = Debug|Win32
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Release|x64.ActiveCfg = Release|x64
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Release|x64.Build.0 = Release|x64
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Release|x86.ActiveCfg = Release|Win32
{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {027727BB-C558-4EBF-9638-45AA84FA8542}
EndGlobalSection
EndGlobal
136 changes: 136 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#define _CRT_SECURE_NO_WARNINGS
#include "Phonebook.h"
#include "Subscriber.h"
#include <stdio.h>
#include <string.h>

// ������������ - ����������

Phonebook::Phonebook()
{
base = new Subscriber[100];
numberOfNotes = 0;
readInfoFromFile();
}

int Phonebook::readInfoFromFile()
{
FILE* file = fopen("phonebook.txt", "r");
int const sizeData = 100;
int const sizeBuffer = 20;
if (!file)
{
printf("File could not be opened\n");
return -1;
}
char* data[sizeData] = {};
int linesRead = 0;
while (!feof(file))
{
char* buffer = new char[sizeBuffer];
int const readBytes = fscanf(file, "%s", buffer);
if (readBytes < 0)
{
delete[] buffer;
break;

Choose a reason for hiding this comment

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

Так же утечка памяти будет? Ну да, буфер не заполнился, но память-то под него выделена, а тут мы теряем на него указатель.

}
data[linesRead] = buffer;
++linesRead;
}

Choose a reason for hiding this comment

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

И return? :)

if (linesRead > 0)
{
numberOfNotes = linesRead / 2;
for (int i = 0; i < linesRead; ++i)
{
Subscriber s(data[i], data[i + 1]);
(*this)[i / 2] = s;
++i;
}
for (int i = 0; i < linesRead; ++i)
{
delete[] data[i];
}
}

Choose a reason for hiding this comment

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

Мне кажется, что можно вообще все данные в data не считывать, а прямо прочитали две строки, разобрали их в Subscriber, запихали в базу и т.д. Но это просто наблюдение, можно это не править.

fclose(file);
return 0;
}

Phonebook::~Phonebook()
{
delete[] base;
}

// getter - setter

Subscriber Phonebook::operator[](int number) const
{
return base[number];
}

Subscriber & Phonebook::operator[](int number)
{
return base[number];
}

int Phonebook::size() const
{
return numberOfNotes;
}

// ��������� ������

void Phonebook::addNote(Subscriber const & note) // 1
{
(*this)[numberOfNotes] = note;
++numberOfNotes;
}

void Phonebook::printAllNotes() const // 2
{
int const size = this->size();
for (int i = 0; i < size; ++i)
{
(*this)[i].print();
}
}

void Phonebook::findNumberByName(char const * name) const // 3
{
for (int i = 0; i < size(); ++i)
{
if (strcmp((*this)[i].getName(), name))
{
(*this)[i].print();
}
}
}

void Phonebook::findNameByNumber(char const * number) const // 4
{
for (int i = 0; i < size(); ++i)
{
if (strcmp((*this)[i].getNumber(), number))

Choose a reason for hiding this comment

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

strcmp возвращает 0, если строки равны, что соответствует false, если привести его к bool-у. Поэтому неявные преобразования типов --- зло. Этот код находит все записи, кроме той, что нужна :)

{
(*this)[i].print();
}
}
}

int Phonebook::saveToFile() const // 5
{
FILE* file = fopen("phonebook.txt", "w");
if (!file)
{
printf("FILE WAS NOT FOUND\n!");
return -1;
}
for (int i = 0; i < numberOfNotes; ++i)
{
fwrite((*this)[i].getName(), sizeof(char), strlen((*this)[i].getName()), file);
fwrite(" ", sizeof(char), 1, file);
fwrite((*this)[i].getNumber(), sizeof(char), strlen((*this)[i].getNumber()), file);
fwrite("\n", sizeof(char), 1, file);
}
fclose(file);
return 0;
}
40 changes: 40 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//�������� ��������� - ���������� ����������.��� ������ ����� ������� ����� � ������ ���������,
//� ������������� ������ ������������ ��������� �������� :
//0 - �����
//1 - �������� ������(��� � �������)
//2 - ����������� ��� ��������� ������
//3 - ����� ������� �� �����
//4 - ����� ��� �� ��������
//5 - ��������� ������� ������ � ����
//��� ������� ��������� ������ ������ ������ �� �����, ���� ����� ��� - �������� � ������ ���� �������.
//������ ���� ��������� ������ �������.

#pragma once

struct Subscriber;

struct Phonebook
{
// ������������ - ����������
Phonebook();
~Phonebook();

// getter - setter
Subscriber operator[](int number) const;
Subscriber & operator[](int number);
int size() const;

// ��������� ������
void addNote(Subscriber const & note); // 1
void printAllNotes() const; // 2
void findNumberByName(char const * name) const; // 3
void findNameByNumber(char const * number) const; // 4
int saveToFile() const; // 5
private:
int readInfoFromFile(); // ��������������� ����� � ������������
Phonebook(Phonebook const & p) = delete; // ����������� �����������
void operator=(Phonebook const & p) = delete; // �������� ������������

Subscriber* base;
int numberOfNotes;
};
139 changes: 139 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?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>{8F61A54A-0F17-43DA-BBB4-81EEF74C50A1}</ProjectGuid>
<RootNamespace>Phonebook</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>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</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="Phonebook.cpp" />
<ClCompile Include="Subscriber.cpp" />
<ClCompile Include="tests.cpp" />
<ClCompile Include="userInterface.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Phonebook.h" />
<ClInclude Include="Subscriber.h" />
<ClInclude Include="tests.h" />
<ClInclude Include="userInterface.h" />
</ItemGroup>
<ItemGroup>
<Text Include="phonebook.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading