-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonebook.cpp
More file actions
152 lines (128 loc) · 4.24 KB
/
Copy pathphonebook.cpp
File metadata and controls
152 lines (128 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "phonebook.h"
#include <iostream>
#include <string>
#include <fstream>
#include "memtrace.h"
//konstruktorok
Phonebook::Phonebook(){
this->size = 0;
this->contacts = new Contact[size];
}
Phonebook::Phonebook(size_t s){
this->size = s;
this->contacts = new Contact[size];
}
//masolo konstruktor
Phonebook::Phonebook(const Phonebook& p) {
this->size = p.getSize();
delete[] this->contacts;
this->contacts = new Contact[p.getSize()];
for (size_t i = 0; i < size; ++i) {
this->contacts[i] = p.contacts[i];
}
}
//egyenloseg operator
//@param rhs: ezzel teszi egyenlove a phonebookot
Phonebook Phonebook::operator=(const Phonebook& rhs) {
this->size = rhs.getSize();
delete[] this->contacts;
this->contacts = new Contact[rhs.getSize()];
for (size_t i = 0; i < size; ++i) {
this->contacts[i] = rhs[i];
}
return *this;
}
//indexelo operator, visszaadja a megfelelo elemet Contact tipuskent
//@param idx: a kivalasztando elem indexe
Contact Phonebook::operator[](size_t idx) const {
return this->contacts[idx];
}
//editorok: egy elem megfelelo adatat valtoztatjak meg
//@param n/s: erre valtoztatja meg az adatot
//@param idx: a valtoztatando elem indexe a tombben
void Phonebook::editName(const Name& n, size_t idx) {
this->contacts[idx].setName(n);
}
void Phonebook::editPNum(const std::string& s, size_t idx) {
this->contacts[idx].setPNum(s);
}
void Phonebook::editWNum(const std::string& s, size_t idx) {
this->contacts[idx].setWNum(s);
}
void Phonebook::editAddr(const std::string& s, size_t idx) {
this->contacts[idx].setAddr(s);
}
//size getter
size_t Phonebook::getSize() const {
return this->size;
}
//a tomb vegere, a tombot dinamikusan novelve beszur egy elemet
//@param c: a beszurando Contact elem
void Phonebook::add(Contact c) {
Contact* temp = new Contact[this->size + 1];
for (size_t i = 0; i < size; ++i) {
temp[i] = contacts[i];
}
temp[size] = c;
size++;
delete[] contacts;
contacts = temp;
}
//beolvas egy fajlt, es az abban levo elemeket a telefonkonyv vegehez hozzafuzi
//@param file_name: a beolvasando fajl neve
void Phonebook::file_read(const std::string& file_name) {
std::ifstream file(file_name);
if(!file.is_open())
throw "Nem lehetett megnyitni a fajlt";
std::string line;
for(size_t i=0; getline(file, line); i++)
{
std::string substrings[7] = {"", "", "", "", "", "", ""};
for (int j = 0; j < 7; ++j) {
substrings[j]=(line.substr(0,line.find(";")));
line = line.substr(line.find(";")+1, line.size());
}
Name temp(substrings[0], substrings[1], substrings[2], substrings[3]);
Contact toadd(temp, substrings[4], substrings[5], substrings[6]);
add(toadd);
}
file.close();
};
//kiirja egy fajlba a telefonkonyv elemeit
//@param file_name: ebbe a fjalba irja
void Phonebook::file_write(const std::string& file_name) {
std::ofstream file(file_name);
if(!file.is_open())
throw "Nem lehetett megnyitni a fajlt";
for (size_t i = 0; i < this->size; ++i) {
file << this->contacts[i] << std::endl;
}
file.close();
}
//kilistazza a telefonkonyv osszes elemet
void Phonebook::list_all() {
for (size_t i = 0; i < this->size; ++i) {
std::cout << i << " " << this->contacts[i] << std::endl;
}
}
//kilistazza a telefonkonyv azon elemeit, amiben egy keresett szoveg megtalalhato
//@param s: a keresett string
void Phonebook::list_searched(const std::string& s) {
for (size_t i = 0; i < this->size; ++i) {
if(this->contacts[i].is_in(s))
std::cout << i << " " << this->contacts[i] << std::endl;
}
}
//kitorol egy elemet a telefonkonyvbol, es csokkenti annak meretet
//@param idx: a kitorlendo elem indexe
void Phonebook::remove(size_t idx) {
if(idx < 0 || idx >= this->size) throw "Hibas index!";
for (size_t i = idx; i < this->size-1; ++i) {
this->contacts[i] = this->contacts[i+1];
}
this->size = size-1;
}
//destruktor: felszabaditja a dinamikus tombot
Phonebook::~Phonebook() {
delete[] contacts;
}