Другие языки программирования и технологии

Программа для работы со строками C++

День добрый! Сразу скажу, я дилетант в программировании. Мне поставили задачу:
1) Создать объект "Клиент";
2) Задать свойства "Фамилия", "Имя", "Отчество";
3) Создать метод, который выделяет инициалы;
4) Организовать поиск по шаблону (к примеру, клиентов с фамилией на букву "А").
Юра Фартуков
Юра Фартуков
3 911
#include "stdio.h" //Заменить " " на < >.
#include "string.h" //Заменить " " на < >.
#include "stdlib.h" //Заменить " " на < >.

class cl_clients
{
struct st_client
{
int client_id;//Номер клиента
char* last_name;//Фамилия
char* first_name;//Имя
char* middle_name;//Отчество

st_client()
{
client_id = 0;
last_name = NULL;
first_name = NULL;
middle_name = NULL;
}

void edit_client_information(int _client_id, char* _last_name, char* _first_name, char* _middle_name)
{
last_name = new char[strlen(_last_name)];
first_name = new char[strlen(_first_name)];
middle_name = new char[strlen(_middle_name)];

client_id = _client_id;
strcpy_s(last_name, strlen(_last_name) * sizeof(char*), _last_name);
strcpy_s(first_name, strlen(_first_name) * sizeof(char*), _first_name);
strcpy_s(middle_name, strlen(_middle_name) * sizeof(char*), _middle_name);
}

~st_client()
{
delete[] last_name;
delete[] first_name;
delete[] middle_name;
}

void initials(char& f, char& m)
{
f = *first_name;//И.
m = *middle_name;//О.
}

void print()
{
char f, m;
printf("\n\nclient_id:%d", client_id);
printf("\nlast_name:%s", last_name);
printf("\nfirst_name:%s", first_name);
printf("\nmiddle_name:%s", middle_name);
initials(f, m);
printf("\nintitals:%c. %c.", f, m);
}
};

int number_of_clients;
st_client* clients;

int _find(char* substring, char* string)
{
int i,j;
int length_of_string = strlen(string);
int length_of_substring = strlen(substring);
for (i = 0; i <= length_of_string - length_of_substring; i++)
{
if (*substring == string[i])
{
for (j = 0; j < length_of_substring; j++)
if (substring[j] != string[i + j])
break;
if (length_of_substring == j)
return i;
}
}
return -1;
}

public:
cl_clients()
{
number_of_clients = 0;
clients = NULL;
}

~cl_clients()
{
free(clients);
}

void add_client(int client_id)
{
number_of_clients++;
clients = (st_client*)realloc(clients, sizeof(st_client) * number_of_clients);
(clients + number_of_clients - 1)->client_id = client_id;
}

void edit_client_information(int client_id, char* last_name, char* first_name, char* middle_name)
{
for (int i = 0; i < number_of_clients; i++)
if ((clients + i)->client_id == client_id)
(clients + i)->edit_client_information(client_id, last_name, first_name, middle_name);
}

void find_by_id(int client_id)
{
for (int i = 0; i < number_of_clients; i++)
if ((clients + i)->client_id == client_id)
(clients + i)->print();
}

void find_by_last_name(char* last_name)
{
for (int i = 0; i < number_of_clients; i++)
if (_find(last_name, (clients + i)->last_name) >= 0)
(clients + i)->print();
}

void find_by_first_name(char* first_name)
{
for (int i = 0; i < number_of_clients; i++)
if (_find(first_name, (clients + i)->first_name) >= 0)
(clients + i)->print();

}

void find_by_middle_name(char* middle_name)
{
for (int i = 0; i < number_of_clients; i++)
if (_find(middle_name, (clients + i)->last_name) >= 0)
(clients + i)->print();
}
};

int main()
{
cl_clients* clients = new cl_clients;
system("cls");
clients->add_client(1);
clients->edit_client_information(1, (char*)"Fedorov", (char*)"Vasilyi", (char*)"Petrovich");
clients->add_client(2);
clients->edit_client_information(2, (char*)"Petrov", (char*)"Andrey", (char*)"Vladimirovich");
clients->find_by_first_name((char*)"A");
delete clients;
printf("\nPress Enter key for continue...");
getchar();
return 0;
}
СГ
Сергей Ганиман
11 953
Лучший ответ