-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.h
162 lines (129 loc) · 3.22 KB
/
main.h
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
153
154
155
156
157
158
159
160
161
162
//
// Created by sajith-csi on 5/5/2022.
//
#ifndef ADVANCED_TOPICS_MAIN_H
#define ADVANCED_TOPICS_MAIN_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class Contact
{
public:
Contact(const char *name, const char *address, const char *tel);
~Contact();
const char *Name() const { return name; }
const char *Address() const { return address; }
const char *Tel() const { return tel; }
friend std::ostream &operator<<(std::ostream &os, Contact &c);
private:
char *name;
char *address;
char *tel;
};
class ContactDir
{
public:
ContactDir(const int max_size);
~ContactDir();
void Insert(const Contact &);
void Delete(const char *name);
Contact *Find(const char *name);
friend std::ostream &operator<<(std::ostream &, ContactDir &);
private:
int LookUp(const char *name);
Contact **contacts;
int dir_size;
int max_size;
};
Contact::Contact(const char *name, const char *address, const char *tel)
{
Contact::name = new char[strlen(name)] + 1;
Contact::address = new char[strlen(address)] + 1;
Contact::tel = new char[strlen(tel)] + 1;
strcpy(Contact::name, name);
strcpy(Contact::address, address);
strcpy(Contact::tel, tel);
}
Contact::~Contact()
{
delete name;
delete address;
delete tel;
}
std::ostream &operator<<(std::ostream &os, Contact &c)
{
os << "( " << c.name << " , " << c.address << " , " << c.tel << " )\n";
return os;
}
ContactDir::ContactDir(const int mx)
{
typedef Contact *ContactPtr;
dir_size = 0;
max_size = mx;
contacts = new ContactPtr[max_size];
}
ContactDir::~ContactDir()
{
for (int i = 0; i < dir_size; ++i)
{
delete contacts[i];
}
delete[] contacts;
}
void ContactDir::Insert(const Contact &c)
{
if (dir_size < max_size)
{
int idx = LookUp(c.Name());
if (idx > 0 && strcmp(c.Name(), contacts[idx]->Name()) == 0)
{
delete contacts[idx];
}
else
{
for (int i = dir_size; i > idx; --i)
{
contacts[i] = contacts[i - 1];
}
++dir_size;
}
contacts[idx] = new Contact(c.Name(), c.Address(), c.Tel());
}
}
void ContactDir::Delete(const char *name)
{
int idx = LookUp(name);
if (idx < dir_size)
{
delete contacts[idx];
--dir_size;
for (int i = idx; i < dir_size; ++i)
{
contacts[i] = contacts[i + 1];
}
}
}
Contact *ContactDir::Find(const char *name)
{
int idx = LookUp(name);
return (idx < dir_size && strcmp(contacts[idx]->Name(), name) == 0) ? contacts[idx] : nullptr;
}
int ContactDir::LookUp(const char *name)
{
for (int i = 0; i < dir_size; ++i)
{
if (strcmp(contacts[i]->Name(), name) == 0) return i;
}
return dir_size;
}
std::ostream &operator<<(std::ostream &os, ContactDir &c)
{
for (int i = 0; i < c.dir_size; ++i)
{
os << *(c.contacts[i]) << '\n';
}
return os;
}
#endif //ADVANCED_TOPICS_MAIN_H