Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- typedef struct {
- char name[80 + 1];
- char phone[12 + 1];
- int group;
- } Contact;
- typedef struct {
- Contact* data = new Contact;
- size_t size = 0;
- size_t capacity = 0;
- } Vector;
- Vector* CreateList();
- void menu(Vector* list);
- void NameCheckup(Vector* list, Contact* c);
- int StrToEnum(char group[20]);
- void CreateContact(Vector* list);
- void DeleteContact(Vector* list);
- void EditName(Vector* list, int index);
- void EditPhoneNumber(Vector* list, int index);
- void EditGroup(Vector* list, int index);
- int FindByName(Vector* list, char* name);
- void EditContact(Vector* list);
- void erase(Vector* v, size_t index);
- void reserve(Vector* v, size_t newCap);
- void PrintByName(Vector* list);
- void PrintByNumber(Vector* list);
- void PrintByGroup(Vector* list);
- void ShowContacts(Vector* list);
- void PrintContact(Vector* list, int index);
- void PrintAllContacts(Vector* list);
- enum Groups {
- NO_GROUP,
- FAMILY,
- FRIENDS,
- COLLEAGUES
- };
- Vector* CreateList() {
- Vector* list = new Vector;
- list->data = 0;
- list->size = 0;
- list->capacity = 0;
- return list;
- }
- void menu(Vector* list){
- cout << "List of available commands :" << endl
- << "1. Create contact" << endl
- << "2. Delete contact" << endl
- << "3. Edit contact" << endl
- << "4. Show contact(s)" << endl
- << "5. Exit"<< endl
- << "Command :" << endl;
- int command;
- cin >> command;
- switch (command) {
- case 1: {
- CreateContact(list);
- break;
- }
- case 2: {
- DeleteContact(list);
- break;
- }
- case 3: {
- EditContact(list);
- break;
- }
- case 4: {
- ShowContacts(list);
- break;
- }
- case 5: {
- return;
- }
- default: {
- cout << " This command does not exist.";
- menu(list);
- }
- }
- }
- void NameCheckup(Vector* list, Contact* c) {
- bool flag = false;
- while (true) {
- cout << "Enter name: ";
- char* buf = new char[81];
- cin.getline(buf, 81);
- for (int i = 0; i < list->size; ++i) {
- if (strcmp(buf, list->data[i].name) == 0)
- {
- cout << "Error! This name cannot be used. Choose another name." << endl;
- flag = true;
- break;
- }
- }
- if (flag == false) {
- strcpy(c->name, buf);
- return;
- }
- else {
- flag = false;
- continue;
- }
- }
- }
- int StrToEnum(char group[20]) {
- if (strcmp(group, "FRIENDS") == 0) {
- return FRIENDS;
- }
- else if (strcmp(group, "FAMILY") == 0) {
- return FAMILY;
- }
- else if (strcmp(group, "COLLEAGUES") == 0) {
- return COLLEAGUES;
- }
- else {
- return NO_GROUP;
- }
- }
- void CreateContact(Vector* list) {
- reserve(list, list->capacity + 1);
- cout << "Contact creation:" << endl;
- Contact* c = new Contact;
- NameCheckup(list, c);
- cout<< "Enter phone number: ";
- cin.getline(c->phone, 12);
- cout << "Enter group: ";
- char group[20];
- cin >> group;
- c->group = StrToEnum(group);
- list->data[list->size + 1] = *c;
- }
- void DeleteContact(Vector* list) {
- cout << "Enter name: ";
- char* del = new char[80];
- cin.getline(del, 80);
- for (int i = 0; i < list->size; i++) {
- if (strcmp(del, list->data[i].name) == 0) {
- erase(list, i);
- return;
- }
- }
- cout << "This contact does not exist" << endl;
- }
- void EditName(Vector* list, int index) {
- char* newName = new char[81];
- cin.getline(newName, 81);
- strcpy(list->data[index].name, newName);
- }
- void EditPhoneNumber(Vector* list, int index) {
- char* newNumber = new char[13];
- cin.getline(newNumber, 13);
- strcpy(list->data[index].phone, newNumber);
- }
- void EditGroup(Vector* list, int index) {
- char newGroup[20];
- cin.getline(newGroup, 20);
- list->data[index].group = StrToEnum(newGroup);
- }
- int FindByName(Vector* list, char* name) {
- for (int i = 0; i < list->size; i++) {
- if (strcmp(name, list->data[i].name) == 0) {
- return i;
- }
- }
- cout << "That contact does not exist" << endl;
- return -1;
- }
- void EditContact(Vector* list) {
- int index;
- while (true) {
- cout << "Enter contact name: ";
- char* buf = new char[80];
- cin.getline(buf, 80);
- int ind = FindByName(list, buf);
- if (ind == -1) {
- cout << "That contact does not exist. Try again:" << endl;
- continue;
- }
- else {
- index = ind;
- break;
- }
- }
- cout << "1. Edit name " << endl
- << "2. Edit phone number" << endl
- << "3. Edit group" << endl
- << "4. Back " << endl;
- int editby;
- cin >> editby;
- switch (editby) {
- case 1:
- EditName(list, index);
- break;
- case 2:
- EditPhoneNumber(list, index);
- break;
- case 3:
- EditGroup(list, index);
- break;
- case 4:
- //возврат к меню
- break;
- default:
- cout << "Error";
- break;
- }
- }
- void erase(Vector* v, size_t index) {
- for (size_t i = index; i < v->size; i++) {
- v->data[i] = v->data[i + 1];
- }
- v->size--;
- reserve(v, v->size);
- }
- void reserve(Vector* v, size_t newCap) {
- if (newCap > v->capacity) {
- Contact* new_data = new Contact [newCap];
- memcpy(new_data, v->data, v->size * sizeof(int));
- delete[] v->data;
- v->data = new_data;
- }
- }
- void PrintByName(Vector* list) {
- cout << "Enter name: ";
- char* name = new char[81];
- cin.getline(name, 81);
- for (int i = 0; i < list->size; ++i) {
- if (strcmp(name, list->data[i].name) == 0) {
- PrintContact(list, i);
- }
- }
- }
- void PrintByNumber(Vector* list) {
- cout << "Enter phone number: ";
- char* number = new char[13];
- cin.getline(number, 13);
- for (int i = 0; i < list->size; ++i) {
- if (strcmp(number, list->data[i].phone) == 0) {
- PrintContact(list, i);
- }
- }
- }
- void PrintByGroup(Vector* list) {
- cout << "Enter group: ";
- char* group = new char[20];
- cin.getline(group, 20);
- int gr = StrToEnum(group);
- for (int i = 0; i < list->size; ++i) {
- if (gr == list->data[i].group) {
- PrintContact(list, i);
- }
- }
- }
- void ShowContacts(Vector* list) {
- cout << "Print contact(s) :" << endl
- << "1. By name" << endl
- << "2. By phone number" << endl
- << "3. By group" << endl
- << "4. All contacts" << endl
- << "5. Back" << endl;
- int comand;
- cin >> comand;
- switch(comand) {
- case 1:
- PrintByName(list);
- break;
- case 2:
- PrintByNumber(list);
- break;
- case 3:
- PrintByGroup(list);
- break;
- case 4:
- PrintAllContacts(list);
- break;
- case 5:
- //возврат к менюшке;
- default:
- cout << "Error!";
- break;
- }
- }
- void PrintContact(Vector* list, int index) {
- cout << "Name: " << list->data[index].name << endl;
- cout << "Number: " << list->data[index].phone << endl;
- cout << "Group: ";
- switch (list->data[index].group) {
- case NO_GROUP: {
- cout << "No group";
- break;
- }
- case FAMILY: {
- cout << "Family";
- break;
- }
- case FRIENDS: {
- cout << "Friends";
- break;
- }
- case COLLEAGUES: {
- cout << "Colleagues";
- break;
- }
- }
- }
- void PrintAllContacts(Vector* list) {
- for (int i = 0; i < list->size; ++i) {
- PrintContact(list, i);
- }
- }
- int main(){
- Vector* PhoneBook = CreateList();
- while (true) {
- menu(PhoneBook);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement