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 18 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
164 changes: 164 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include "Phonebook.h"

bool compareStr(char const * str1, char const * str2);

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

Phonebook::Phonebook()
{
int const size = 100;
base = new Subscriber[size];

Choose a reason for hiding this comment

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

Тут, мне кажется, можно было просто new Subscriber[100];, всё равно size нигде больше не используется

numberOfNotes = 0;
readInfoFromFile();
}

void 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");
}

Choose a reason for hiding this comment

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

И return? :)

else
{
char* data[sizeData] = {};
int linesRead = 0;
while (!feof(file))
{
char* buffer = new char[sizeBuffer];
const int readBytes = fscanf(file, "%s", buffer);
if (readBytes < 0)
{
break;
}
data[linesRead] = buffer;
++linesRead;
}
if (linesRead > 0)
{
numberOfNotes = linesRead / 2;
for (int i = 0; i < linesRead; ++i)
{
Subscriber* s = new Subscriber;
s->setName(data[i]);
s->setNumber(data[i + 1]);
(*this)[i / 2] = *s;
++i;
if (i == linesRead - 1)
{
delete s;
}
}
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);
}

Phonebook::Phonebook(Phonebook const & p) {} // ����������� �����������

void Phonebook::operator=(Phonebook const & p) {} // �������� ������������

Choose a reason for hiding this comment

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

Нее, это делается в .h-нике:

	Phonebook(Phonebook const & p) = delete; // конструктор копирования
	void operator=(Phonebook const & p) = delete; // оператор присваивания


Phonebook::~Phonebook()
{
delete[] base;
numberOfNotes = 0;

Choose a reason for hiding this comment

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

Менять значения полей в деструкторе уже поздно

}

// 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();
}
}

bool compareStr(char const * str1, char const * str2)
{
if (strlen(str1) != strlen(str2))
{
return false;
}
int const length = (int)strlen(str1);
for (int i = 0; i < length; ++i)
{
if (str1[i] != str2[i])
{
return false;
}
}
return true;
}

Choose a reason for hiding this comment

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

Тоже, есть strcmp, которая и так всё делает.


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

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

void Phonebook::saveToFile() const // 5
{
FILE* file = fopen("phonebook.txt", "w");
if (!file)
{
printf("FILE WAS NOT FOUND\n!");
}

Choose a reason for hiding this comment

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

И тут тоже бы return, а то там ещё ниже fclose, ну и вообще, лишний else

else
{
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);
}
42 changes: 42 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//�������� ��������� - ���������� ����������.��� ������ ����� ������� ����� � ������ ���������,
//� ������������� ������ ������������ ��������� �������� :
//0 - �����
//1 - �������� ������(��� � �������)
//2 - ����������� ��� ��������� ������
//3 - ����� ������� �� �����
//4 - ����� ��� �� ��������
//5 - ��������� ������� ������ � ����
//��� ������� ��������� ������ ������ ������ �� �����, ���� ����� ��� - �������� � ������ ���� �������.
//������ ���� ��������� ������ �������.

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "Subscriber.h"
#include <stdio.h>
#include <string.h>

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
void saveToFile() const; // 5
private:
void readInfoFromFile(); // ��������������� ����� � ������������
Phonebook(Phonebook const & p); // ����������� �����������
void operator=(Phonebook const & p); // �������� ������������

Subscriber* base;
int numberOfNotes;
};
129 changes: 129 additions & 0 deletions hw4/Phonebook/Phonebook/Phonebook.vcxproj
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>{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>
</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" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Phonebook.h" />
<ClInclude Include="Subscriber.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading