Untitled
unknown
c_cpp
2 years ago
28 kB
6
Indexable
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <cctype>
#include <algorithm>
using namespace std;
struct contacts {
char name[10];
char number[10];
char fullname[15];
char address[20];
char city[15];
char email[35];
};
int proiz;
std::vector<contacts> carr;
contacts tmp;
int currentPage = 0;
const int contactsPerPage = 15;
char searchStr[20] = "";
std::vector<contacts> originalCarr;
int sortState = 0;
int sortBy = 0;
int sortOrder = 0;
std::string contactToEdit;
void CheckFile();
void SortingF(int NumTableSort, int NumTypeSort);
void TableOut();
void TitlesOut();
void ScreenOut();
void mouseClicks();
void ShowContactFile();
void DrawButtons();
bool InsideButton(int x, int y, int x1, int y1, int x2, int y2);
void AddContact();
void SaveContacts();
void DeleteContact(const std::string& contactName);
std::string GetContactName();
void EditContact(const std::string& contactName);
std::vector<contacts> SearchContacts(const char* searchStr);
void ShowContactDetails(int contactIndex);
void ShowSplashScreen() {
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "");
setbkcolor(BLACK);
cleardevice();
setcolor(WHITE);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(100, 200, "Author: Elzar Shakulov");
outtextxy(100, 250, "Group: EPI-3-22");
outtextxy(100, 300, "Program's name: Phonebook");
delay(1000);
closegraph();
}
int main() {
char *filename1;
filename1 = "contacts.txt";
FILE *file1 = fopen(filename1, "r");
if (file1 == NULL) {
printf("Error opening file\n");
return 1;
}
carr.clear();
contacts c;
while (fscanf(file1, "%s %s %s %s %s %s\n", c.name, c.number, c.fullname, c.address, c.city, c.email) != EOF) {
carr.push_back(c);
}
originalCarr = carr;
fclose(file1);
ShowSplashScreen();
ScreenOut();
ShowContactFile();
mouseClicks();
closegraph();
SaveContacts();
return 0;
}
void SaveContacts() {
FILE* file1 = fopen("contacts.txt", "w");
if (file1 == NULL) {
printf("Error opening file for writing\n");
return;
}
for (int i = 0; i < carr.size(); i++) {
fprintf(file1, "%s %s %s %s %s %s\n", carr[i].name, carr[i].number, carr[i].fullname, carr[i].address, carr[i].city, carr[i].email);
}
fclose(file1);
}
void AddContact() {
cleardevice();
outtextxy(30, 60, "Enter contact name:");
char name[20];
int c, i = 0;
while ((c = getch()) != '\r' && i < 20) {
if (c == '\b' && i > 0) {
i--;
name[i] = '\0';
outtextxy(30, 80, " ");
outtextxy(30, 80, name);
} else if (c >= ' ' && c <= '~' && i < 19) {
i == 0 ? name[i] = toupper(c) : name[i] = c;
name[i + 1] = '\0';
outtextxy(30, 80, name);
i++;
}
}
outtextxy(30, 120, "Enter contact number:");
char number[15];
bool numberExists = false;
do {
numberExists = false;
int i = 0;
while ((c = getch()) != '\r' && i < 15) {
if (c == '\b' && i > 0) {
i--;
number[i] = '\0';
outtextxy(30, 140, " ");
outtextxy(30, 140, number);
} else if (c >= '0' && c <= '9' && i < 14) {
number[i] = c;
number[i + 1] = '\0';
outtextxy(30, 140, number);
i++;
}
}
for (size_t i = 0; i < carr.size(); i++) {
if (strcmp(carr[i].number, number) == 0) {
numberExists = true;
outtextxy(30, 160, "Number already exists. Enter a different number:");
outtextxy(30, 140, " ");
break;
}
}
} while (numberExists);
outtextxy(30, 180, "Enter contact full name:");
char fullname[20];
i = 0;
bool spaceFlag = false;
while ((c = getch()) != '\r' && i < 20) {
if (c == '\b' && i > 0) {
i--;
fullname[i] = '\0';
outtextxy(30, 200, " ");
outtextxy(30, 200, fullname);
if (spaceFlag) {
spaceFlag = false;
}
} else if ((isalpha(c) || isspace(c)) && i < 19) {
if (isspace(c)) {
if (!spaceFlag) {
spaceFlag = true;
fullname[i] = '_';
fullname[i + 1] = '\0';
outtextxy(30, 200, fullname);
i++;
}
} else {
fullname[i] = c;
fullname[i + 1] = '\0';
outtextxy(30, 200, fullname);
i++;
}
}
}
for (int j = 0; j < strlen(fullname); j++) {
if (fullname[j] == ' ') {
fullname[j] = '_';
}
}
outtextxy(30, 240, "Enter contact address:");
char address[30];
i = 0;
spaceFlag = false;
while ((c = getch()) != '\r' && i < 30) {
if (c == '\b' && i > 0) {
i--;
address[i] = '\0';
outtextxy(30, 260, " ");
outtextxy(30, 260, address);
if (spaceFlag) {
spaceFlag = false;
}
} else if ((isalnum(c) || isspace(c)) && i < 29) {
if (isspace(c)) {
if (!spaceFlag) {
spaceFlag = true;
address[i] = '_';
address[i + 1] = '\0';
outtextxy(30, 260, address);
i++;
}
} else {
address[i] = c;
address[i + 1] = '\0';
outtextxy(30, 260, address);
i++;
}
}
}
for (int j = 0; j < strlen(address); j++) {
if (address[j] == ' ') {
address[j] = '_';
}
}
outtextxy(30, 300, "Enter contact city:");
char city[15];
i = 0;
spaceFlag = false;
while ((c = getch()) != '\r' && i < 15) {
if (c == '\b' && i > 0) {
i--;
city[i] = '\0';
outtextxy(30, 320, " ");
outtextxy(30, 320, city);
if (spaceFlag) {
spaceFlag = false;
}
} else if ((isalpha(c) || isspace(c)) && i < 14) {
if (isspace(c)) {
if (!spaceFlag) {
spaceFlag = true;
city[i] = '_';
city[i + 1] = '\0';
outtextxy(30, 320, city);
i++;
}
} else {
city[i] = c;
city[i + 1] = '\0';
outtextxy(30, 320, city);
i++;
}
}
}
for (int j = 0; j < strlen(city); j++) {
if (city[j] == ' ') {
city[j] = '_';
}
}
outtextxy(30, 360, "Enter contact email:");
char email[25];
i = 0;
while ((c = getch()) != '\r' && i < 25) {
if (c == '\b' && i > 0) {
i--;
email[i] = '\0';
outtextxy(30, 380, " ");
outtextxy(30, 380, email);
} else if ((isalnum(c) || c == '.' || c == '@' || isspace(c)) && i < 24) {
email[i] = c;
email[i + 1] = '\0';
outtextxy(30, 380, email);
i++;
}
}
contacts newContact;
strcpy(newContact.name, name);
strcpy(newContact.number, number);
strcpy(newContact.fullname, fullname);
strcpy(newContact.address, address);
strcpy(newContact.city, city);
strcpy(newContact.email, email);
carr.push_back(newContact);
cleardevice();
ShowContactFile();
}
void SortingF(int sortBy, int sortOrder) {
for (int i = 0; i < carr.size() - 1; i++) {
for (int j = 0; j < carr.size() - i - 1; j++) {
bool swapElements = false;
if (sortBy == 0) { // Sort by name
if (sortOrder == 0) { // Ascending order
if (strcmp(carr[j].name, carr[j + 1].name) > 0) {
swapElements = true;
}
} else { // Descending order
if (strcmp(carr[j].name, carr[j + 1].name) < 0) {
swapElements = true;
}
}
} else { // Sort by number
if (sortOrder == 0) {
if (strcmp(carr[j].number, carr[j + 1].number) > 0) {
swapElements = true;
}
} else {
if (strcmp(carr[j].number, carr[j + 1].number) < 0) {
swapElements = true;
}
}
}
if (swapElements) {
contacts temp = carr[j];
carr[j] = carr[j + 1];
carr[j + 1] = temp;
}
}
}
}
std::vector<contacts> SearchContacts(const char* searchStr) {
std::string lowerSearchStr(searchStr);
std::transform(lowerSearchStr.begin(), lowerSearchStr.end(), lowerSearchStr.begin(), ::tolower);
if (lowerSearchStr == "") {
return carr;
}
std::vector<contacts> result;
for (int i = 0; i < carr.size(); i++) {
std::string nameLower = carr[i].name;
std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower);
if (nameLower.find(lowerSearchStr) != std::string::npos) {
result.push_back(carr[i]);
}
}
return result;
}
void ShowContactFile() {
cleardevice();
TableOut();
TitlesOut();
DrawButtons();
char searchLabel[40] = "Showing results for: ";
strcat(searchLabel, searchStr);
outtextxy(350, 40, searchLabel);
carr = SearchContacts(searchStr);
SortingF(sortBy, sortOrder);
int startIndex = currentPage * contactsPerPage;
int endIndex = std::min(startIndex + contactsPerPage, (int)carr.size());
for (int y = 53, i = startIndex; i < endIndex; i++, y += 26) {
outtextxy(30, y, carr[i].name);
outtextxy(180, y, carr[i].number);
}
int totalPages = (carr.size() + contactsPerPage - 1) / contactsPerPage;
char pageStr[20];
sprintf(pageStr, "Page %d/%d", currentPage + 1, totalPages);
outtextxy(140, 450, pageStr);
outtextxy(350, 60, "Enter search string:");
}
void DeleteContact(const std::string& contactNumber) {
bool contactDeleted = false;
for (int i = 0; i < carr.size(); i++) {
if (carr[i].number == contactNumber) {
carr.erase(carr.begin() + i);
contactDeleted = true;
break;
}
}
if (contactDeleted) {
cleardevice();
ScreenOut();
ShowContactFile();
char deleteMsg[40];
sprintf(deleteMsg, "Deleted contact: %s", contactNumber.c_str());
outtextxy(450, 170, deleteMsg);
} else {
outtextxy(450, 170, "Contact not found!");
Sleep(3000);
cleardevice();
ScreenOut();
ShowContactFile();
}
}
void ShowContactDetails(int contactIndex) {
cleardevice();
// Display the contact details
outtextxy(30, 60, "Contact Details");
outtextxy(30, 80, "Name: ");
outtextxy(180, 80, carr[contactIndex].name);
outtextxy(30, 100, "Number: ");
outtextxy(180, 100, carr[contactIndex].number);
outtextxy(30, 120, "Full Name: ");
outtextxy(180, 120, carr[contactIndex].fullname);
outtextxy(30, 140, "Address: ");
outtextxy(180, 140, carr[contactIndex].address);
outtextxy(30, 160, "City: ");
outtextxy(180, 160, carr[contactIndex].city);
outtextxy(30, 180, "Email: ");
outtextxy(180, 180, carr[contactIndex].email);
// Wait for a key press to return to the contact list
outtextxy(30, 220, "Press any key to return to the contact list");
getch();
// Clear the screen and show the contact list again
cleardevice();
ShowContactFile();
}
string GetContactName() {
string contactName;
cleardevice();
outtextxy(30, 60, "Enter contact name:");
char name[20];
int c, i = 0;
while ((c = getch()) != '\r' && i < 20) {
if (c == '\b' && i > 0) {
i--;
name[i] = '\0';
outtextxy(30, 80, " ");
outtextxy(30, 80, name);
} else if (c >= ' ' && c <= '~' && i < 19) {
name[i] = c;
name[i+1] = '\0';
outtextxy(30, 80, name);
i++;
}
}
contactName = name;
return contactName;
}
string GetContactNumber() {
string contactNumber;
cleardevice();
outtextxy(30, 60, "Enter contact number:");
char number[20];
int c, i = 0;
while ((c = getch()) != '\r' && i < 20) {
if (c == '\b' && i > 0) {
i--;
number[i] = '\0';
outtextxy(30, 80, " ");
outtextxy(30, 80, number);
} else if (c >= ' ' && c <= '~' && i < 19) {
number[i] = c;
number[i+1] = '\0';
outtextxy(30, 80, number);
i++;
}
}
contactNumber = number;
return contactNumber;
}
void EditContact(const std::string& contactNumber) {
bool contactEdited = false;
std::string lowercaseContactNumber = contactNumber;
transform(lowercaseContactNumber.begin(), lowercaseContactNumber.end(), lowercaseContactNumber.begin(), ::tolower);
for (int i = 0; i < carr.size(); i++) {
std::string lowercaseNumber = carr[i].number;
transform(lowercaseNumber.begin(), lowercaseNumber.end(), lowercaseNumber.begin(), ::tolower);
if (lowercaseNumber == lowercaseContactNumber) {
cleardevice();
// Edit contact name
outtextxy(30, 60, "Enter new contact name (leave empty to keep current):");
char name[20];
int c, j = 0;
while ((c = getch()) != '\r' && j < 20) {
if (c == '\b' && j > 0) {
j--;
name[j] = '\0';
outtextxy(30, 80, " ");
outtextxy(30, 80, name);
} else if (c >= ' ' && c <= '~' && j < 19) {
j == 0 ? name[j] = toupper(c) : name[j] = c;
name[j + 1] = '\0';
outtextxy(30, 80, name);
j++;
}
}
if (j > 0) {
strcpy(carr[i].name, name);
}
// Edit contact number
outtextxy(30, 120, "Enter new contact number (leave empty to keep current):");
char number[15];
j = 0;
while ((c = getch()) != '\r' && j < 15) {
if (c == '\b' && j > 0) {
j--;
number[j] = '\0';
outtextxy(30, 140, " ");
outtextxy(30, 140, number);
} else if (c >= '0' && c <= '9' && j < 14) {
number[j] = c;
number[j + 1] = '\0';
outtextxy(30, 140, number);
j++;
}
}
if (j > 0) {
strcpy(carr[i].number, number);
}
// Edit contact full name
outtextxy(30, 180, "Enter new contact full name (leave empty to keep current):");
char fullname[20];
j = 0;
while ((c = getch()) != '\r' && j < 20) {
if (c == '\b' && j > 0) {
j--;
fullname[j] = '\0';
outtextxy(30, 200, " ");
outtextxy(30, 200, fullname);
} else if (isalpha(c) || isspace(c) && j < 19) {
fullname[j] = c;
fullname[j + 1] = '\0';
outtextxy(30, 200, fullname);
j++;
}
}
if (j > 0) {
strcpy(carr[i].fullname, fullname);
}
// Edit contact email
outtextxy(30, 240, "Enter new contact email (leave empty to keep current):");
char email[30];
j = 0;
while ((c = getch()) != '\r' && j < 30) {
if (c == '\b' && j > 0) {
j--;
email[j] = '\0';
outtextxy(30, 260, " ");
outtextxy(30, 260, email);
} else if ((isalpha(c) || c == '@' || c == '.' || c == '_' || c == '-') && j < 29) {
email[j] = c;
email[j + 1] = '\0';
outtextxy(30, 260, email);
j++;
}
}
if (j > 0) {
strcpy(carr[i].email, email);
}
// Edit contact address
outtextxy(30, 300, "Enter new contact address (leave empty to keep current):");
char address[100];
j = 0;
while ((c = getch()) != '\r' && j < 100) {
if (c == '\b' && j > 0) {
j--;
address[j] = '\0';
outtextxy(30, 320, " ");
outtextxy(30, 320, address);
} else if ((isalpha(c) || c == ' ' || c == ',' || c == '.' || c == '-' || c == '#' || c == '/') && j < 99) {
address[j] = c;
address[j + 1] = '\0';
outtextxy(30, 320, address);
j++;
}
}
if (j > 0) {
strcpy(carr[i].address, address);
}
cleardevice();
ShowContactFile();
contactEdited = true;
break;
}
}
if (!contactEdited) {
outtextxy(450, 170, "Contact not found!");
Sleep(3000);
cleardevice();
ShowContactFile();
}
}
bool InsideButton(int x, int y, int x1, int y1, int x2, int y2) {
return x >= x1 && x <= x2 && y >= y1 && y <= y2;
}
void mouseClicks() {
CheckFile();
DrawButtons();
int x, y;
std::string contactToDelete;
while (1) {
if (kbhit()) {
break;
}
if (ismouseclick(WM_LBUTTONDOWN)) {
getmouseclick(WM_LBUTTONDOWN, x, y);
if (InsideButton(x, y, 10, 450, 100, 470)) {
printf("Back button clicked!\n");
if (currentPage > 0) {
currentPage--;
ShowContactFile();
}
} else if (InsideButton(x, y, 230, 450, 320, 470)) {
printf("Next button clicked!\n");
if ((currentPage + 1) * contactsPerPage < static_cast<size_t>(carr.size())) {
currentPage++;
ShowContactFile();
}
} else if (InsideButton(x, y, 530, 10, 620, 30)) {
printf("Search button clicked!\n");
outtextxy(520, 60, " ");
setfillstyle(1, WHITE);
floodfill (535, 15, WHITE);
setbkcolor(WHITE);
setcolor(0);
outtextxy(540, 12, "Search");
setbkcolor(BLACK);
setcolor(15);
strcpy(searchStr, "");
int c, i = 0;
while ((c = getch()) != '\r' && i < 20) {
if (c == '\b' && i > 0) {
i--;
searchStr[i] = '\0';
} else if (c >= ' ' && c <= '~' && i < 19) {
searchStr[i] = c;
searchStr[i+1] = '\0';
i++;
}
outtextxy(480, 60, " "); // Clear the previous string
outtextxy(480, 60, searchStr); // Draw the updated string
}
outtextxy(480, 60, " "); // Clear the final string after Enter is pressed
cleardevice();
std::vector<contacts> result = SearchContacts(searchStr);
if (result.size() > 0) {
carr = result;
currentPage = 0;
ShowContactFile();
} else {
cleardevice();
outtextxy(150, 150, "No contacts found with the search string");
delay(1500);
strcpy(searchStr, "");
carr = originalCarr;
currentPage = 0;
cleardevice();
ShowContactFile();
}
} else if (InsideButton(x, y, 450, 10, 530, 30)) {
printf("Clear button clicked!\n");
strcpy(searchStr, "");
carr = originalCarr;
currentPage = 0;
cleardevice();
ShowContactFile();
}
// sort
if (sortState == 0 && InsideButton(x, y, 330, 80, 410, 100)) {
printf("Sort button clicked!\n");
sortState = 1;
setfillstyle(1, WHITE);
floodfill (335, 85, WHITE);
setbkcolor(WHITE);
setcolor(0);
outtextxy(350, 82, "Sort");
}
if (sortState == 1) {
if (InsideButton(x, y, 450, 80, 530, 100)) {
printf("Name button clicked!\n");
sortBy = 0;
sortState = 2;
setfillstyle(1, WHITE);
floodfill (455, 85, WHITE);
setbkcolor(WHITE);
setcolor(0);
outtextxy(465, 82, "Name");
} else if (InsideButton(x, y, 540, 80, 620, 100)) {
printf("Number button clicked!\n");
sortBy = 1;
sortState = 2;
setfillstyle(1, WHITE);
floodfill (545, 85, WHITE);
setbkcolor(WHITE);
setcolor(0);
outtextxy(555, 82, "Number");
}
setbkcolor(BLACK);
} else if (sortState == 2) {
if (InsideButton(x, y, 450, 110, 530, 130)) {
printf("Ascending button clicked!\n");
sortOrder = 0;
sortState = 0;
DrawButtons();
} else if (InsideButton(x, y, 540, 110, 620, 130)) {
printf("Descending button clicked!\n");
sortOrder = 1;
sortState = 0;
DrawButtons();
}
}
if (sortState == 0) {
cleardevice();
DrawButtons();
ShowContactFile();
}
if (InsideButton(x, y, 330, 140, 410, 160)) {
printf("Add button clicked!\n");
AddContact();
}
if (InsideButton(x, y, 330, 170, 410, 190)) {
printf("Delete button clicked!\n");
contactToDelete = GetContactName();
if (!contactToDelete.empty()) {
DeleteContact(contactToDelete);
}
}
if (InsideButton(x, y, 330, 200, 410, 220)) {
printf("Edit button clicked!\n");
contactToEdit = GetContactNumber();
if (!contactToEdit.empty()) {
EditContact(contactToEdit);
}
}
// details
if (InsideButton(x, y, 330, 230, 410, 250)) {
printf("Show details button clicked!\n");
std::string contactNumberToEdit = GetContactNumber();
if (!contactNumberToEdit.empty()) {
int contactIndex = -1;
for (int i = 0; i < carr.size(); i++) {
if (carr[i].number == contactNumberToEdit) {
contactIndex = i;
break;
}
}
if (contactIndex != -1) {
ShowContactDetails(contactIndex);
} else {
outtextxy(450, 170, "Contact not found!");
Sleep(3000);
cleardevice();
ShowContactFile();
}
}
}
clearmouseclick(WM_LBUTTONDOWN);
}
Sleep(100);
}
}
// interfaces
void ScreenOut() {
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "");
setfillstyle(1, 0);
bar(0, 0, 640, 480);
TableOut();
TitlesOut();
DrawButtons();
}
void TableOut() {
int i;
setfillstyle(1, 0);
setcolor(15);
setlinestyle(0, 1, 1);
rectangle(10, 10, 320, 440);
line(160, 10, 160, 440);
for (i = 49; i < 440; i = i + 26) {
line(10, i, 320, i);
}
}
void TitlesOut() {
setcolor(15);
outtextxy(30, 20, "NAME");
outtextxy(180, 20, "NUMBER");
}
void DrawButtons() {
/*
setfillstyle(1, WHITE);
floodfill (535, 15, WHITE);
setbkcolor(WHITE);
setcolor(0);
outtextxy(540, 12, "Search");
setbkcolor(BLACK);
setcolor(15);
*/
setcolor(15);
rectangle(10, 450, 100, 470);
outtextxy(35, 452, "Back");
rectangle(230, 450, 320, 470);
outtextxy(255, 452, "Next");
rectangle(530, 10, 620, 30);
outtextxy(540, 12, "Search");
rectangle(450, 10, 530, 30);
outtextxy(465, 12, "Clear");
rectangle(330, 80, 410, 100);
outtextxy(345, 82, "Sort");
rectangle(450, 80, 530, 100);
outtextxy(465, 82, "Name");
rectangle(540, 80, 620, 100);
outtextxy(555, 82, "Number");
rectangle(450, 110, 530, 130);
outtextxy(455, 112, "Ascending");
rectangle(540, 110, 620, 130);
outtextxy(545, 112, "Descending");
rectangle(330, 140, 410, 160);
outtextxy(345, 142, "Add");
rectangle(330, 170, 410, 190);
outtextxy(345, 172, "Delete");
rectangle(330, 200, 410, 220);
outtextxy(345, 202, "Edit");
rectangle(330, 230, 410, 250);
outtextxy(345, 232, "Details");
}
// utilities
void CheckFile(){
FILE *file1 = fopen("contacts.txt", "r");
if (file1 == NULL) printf("error finding a file");
fseek(file1, 0, SEEK_END);
long pos1 = ftell(file1);
proiz = (pos1 != 0);
fclose(file1);
}
Editor is loading...