Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <Windows.h>
- const char top[] = "\xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB";
- const char bottom[] = "\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC";
- const char line[] = "\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA";
- struct NOTE
- {
- char name[80];
- char secondName[80];
- char phoneNumber[12];
- int dateOfBirth[3];//day.month.year
- };
- int readNotes(NOTE*, int);
- void showNotes(NOTE*, int);
- void sortNotes(NOTE*, int);
- void showNote(NOTE& note);
- void findPersonIn(NOTE*, int);
- int writeNotesToFile(NOTE*, int, const char[40], FILE*);
- int readNotesFromFile(NOTE*, int, const char[40], FILE*);
- void showHelp();
- int main()
- {
- FILE *file = NULL;
- const int N = 100;
- unsigned n = 0;
- struct NOTE note[N];
- bool exit = false;
- bool dataIsRead = false;
- char control[10] = "";
- const char fileName[40] = "Database.txt";
- showHelp();
- do
- {
- puts("");
- scanf("%s", control);
- if (!strcmp(control, "SHOW"))
- {
- if (dataIsRead)
- showNotes(note, n);
- else
- puts("#There is nothing to show. Database is empty.");
- }
- else if (!strcmp(control, "READ"))
- {
- n = readNotes(note, N);
- dataIsRead = true;
- }
- else if (!strcmp(control, "FIND"))
- {
- if (dataIsRead)
- findPersonIn(note, n);
- else
- puts("#There is nothing to seek. Database is empty.");
- }
- else if (!strcmp(control, "SORT"))
- {
- if (dataIsRead)
- sortNotes(note, n);
- else
- puts("#There is nothing to sort. Database is empty.");
- }
- else if (!strcmp(control, "FWRITE"))
- {
- if (dataIsRead)
- {
- if (writeNotesToFile(note, n, fileName, file))
- puts("File was rewritten succesfully.");
- else
- puts("#Can't rewrite file");
- }
- else
- puts("#There is nothing to write. Database is empty.");
- }
- else if (!strcmp(control, "FREAD"))
- {
- if (n = readNotesFromFile(note, N, fileName, file))
- {
- puts("Data was read from file succesfully");
- dataIsRead = true;
- }
- else
- puts("#Can't read data from file");
- }
- else if (!strcmp(control, "EXIT"))
- {
- exit = true;
- }
- else
- {
- puts("#Unknown kommand. Try again.");
- }
- } while (!exit);
- return 0;
- }
- //Function to input array of notes from the keyboard
- int readNotes(NOTE* note, int N)
- {
- int n = 0;
- printf("How many notes you want to read?: ");
- scanf("%d", &n);
- if (n < 0)
- {
- printf("Really?\n");
- return 0;
- }
- if (n <= N)
- {
- for (int i = 0; i < n; i++)
- {
- printf("Note: %d\n", i + 1);
- printf("\tInput name: ");
- scanf("%s", note[i].name);
- printf("\tInput second name: ");
- scanf("%s", note[i].secondName);
- printf("\tInput phone number: ");
- scanf("%s", note[i].phoneNumber);
- printf("\tInput date of birth (d.m.y): ");
- scanf("%d.%d.%d", ¬e[i].dateOfBirth[0], ¬e[i].dateOfBirth[1], ¬e[i].dateOfBirth[2]);
- }
- }
- else
- {
- printf("It's too much. Maximum size is %d\n", N);
- return 0;
- }
- return n;
- }
- //Function to print the table
- void showNotes(NOTE* note, int N)
- {
- printf("%s\n", top);
- printf("\xBA number \xBA name \xBA second name \xBA number \xBA date of birth \xBA\n");
- for (int i = 0; i < N; i++)
- {
- printf("%s\n", line);
- printf("\xBA%7d \xBA", i + 1);
- printf("%13s \xBA", note[i].name);
- printf("%14s \xBA", note[i].secondName);
- printf("%13s \xBA", note[i].phoneNumber);
- printf(" %02d.%02d.%04d \xBA\n", note[i].dateOfBirth[0], note[i].dateOfBirth[1], note[i].dateOfBirth[2]);
- }
- printf("%s\n", bottom);
- }
- //Function to sort array of notes
- void sortNotes(NOTE* note, int N)
- {
- NOTE t;
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N - 1; j++)
- {
- if (strncmp(note[j].phoneNumber, note[j + 1].phoneNumber, 3) > 0)
- {
- t = note[j];
- note[j] = note[j + 1];
- note[j + 1] = t;
- }
- }
- }
- }
- //Function to print one note
- void showNote(NOTE& note)
- {
- printf("Name: %s\n", note.name);
- printf("Second name: %s\n", note.secondName);
- printf("Phone number: %s\n", note.phoneNumber);
- printf("Date of birth: %02d.%02d.%04d\n", note.dateOfBirth[0], note.dateOfBirth[1], note.dateOfBirth[2]);
- }
- //Function to search persons in array of notes
- void findPersonIn(NOTE* note, int N)
- {
- char secondName[80];
- bool personFound = false;
- printf("\nInput second name: ");
- scanf("%s", secondName);
- puts("");
- for (int i = 0; i < N; i++)
- {
- if (!strcmp(note[i].secondName, secondName))
- {
- showNote(note[i]);
- personFound = true;
- }
- }
- if (!personFound)
- puts("\nWhere is no such person.\n");
- }
- //Function to rewrite the file by array of notes
- int writeNotesToFile(NOTE* note, int N, const char fileName[40], FILE* file)
- {
- if ((file = fopen(fileName, "w")) == NULL)
- {
- perror("#Can't open file to rewrite");
- return 0;
- }
- else
- {
- for (int i = 0; i < N; i++)
- {
- fwrite(note + i, sizeof(struct NOTE), 1, file);
- }
- }
- fclose(file);
- return 1;
- }
- //Function to get array of notes from the file
- int readNotesFromFile(NOTE* note, int N, const char fileName[40], FILE* file)
- {
- int n = 0;
- if ((file = fopen(fileName, "r")) == NULL)
- {
- perror("#Can't open file to read");
- return 0;
- }
- else
- {
- while ( fread(note + n, sizeof(struct NOTE), 1, file) )
- n++;
- }
- fclose(file);
- return n;
- }
- //Function to show hint
- void showHelp()
- {
- puts("Write SHOW to output notes to the screen");
- puts("Write READ to input notes from the keyboard");
- puts("Write FIND to search person in notes");
- puts("Write SORT to sort notes");
- puts("Write FWRITE to rewrite file by notes");
- puts("Write FREAD to get notes from file");
- puts("Write EXIT to close programm");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement