#include<iostream>
//
#include "Header.h"
//
#include"funchion.cpp"
#define _CRE_SECURE_NO_WARINGS
#include<cstdlib>
#include<string>
#include<fstream>
//
#include"funchion.cpp"
void show_menu() {
std::cout << "[1] Add note.\n[2] show all notes.\n[3] find note.\n[4] remove one note.\n[5] remove all notes.\n[6] exit\n"; }
void add_note(const std::string& new_note) {
std::cout << new_note; std::fstream file("notes.txt", std::ios::app);
if (!
file.is_open()) {
std::cout << "[-] file is not opened!";
exit(EXIT_FAILURE); }
file << new_note << "\n";
file.close();
std::cout << " " << std::endl; }
void see_all_notes() {
std::ifstream file("notes.txt");
if (!
file.is_open()) {
std::cout << "no notes\n"; }
std::string one_note;
for (int i = 1; !file.eof(); i++) {
getline(file, one_note);
if (one_note.empty()) continue;
std::cout << '[' << i << ']' << one_note << std::endl; }
file.close();
}
std::string* find_one_note(const std::string& note_to_find, int& n_count) {
std::ifstream file("notes.txt");
if (!
file.is_open()) { return nullptr; }
//std::string one_note;
std::string one_note;
std::string* found_notes = nullptr;
std::string* found_notes_tmp = nullptr;
int cot = 0;
while (!file.eof())
{ getline(file, one_note);
if (one_note.find(note_to_find) != -1) {
cot++;
found_notes_tmp = new std::string[cot];
for (int i = 0; i < cot - 1; i++) {
found_notes_tmp[i] = found_notes[i];
found_notes_tmp[cot - 1] = one_note;
delete[] found_notes;
found_notes = found_notes_tmp;
//std::cout << found_notes; } } }
file.close();
n_count = cot;
return found_notes; }
std::string* all_notes(int& count) {
std::string* all_notes = nullptr;
std::string* all_notes_ptr = nullptr;
std::ifstream file("notes.txt");
if (!
file.is_open()) {
std::cout << "File is not opened!";
exit(EXIT_FAILURE); }
std::string one_note;
int cont;
cont = 0;
while (!file.eof()) {
getline(file, one_note);
count++;
all_notes_ptr = new std::string[count];
for (int i = 0; i < count - 1; i++) {
all_notes_ptr[i] = all_notes[i]; } }
all_notes_ptr[count - 1] = one_note;
delete[] all_notes;
all_notes = all_notes_ptr;
all_notes_ptr = nullptr;
file.close();
return all_notes; }
void remove_all_notes() { std::fstream file("notes.txt",std::ios::out | std::ios::trunc);
if (!
file.is_open()) { std::cout << "[-] file is not opened" << std::endl;
exit(EXIT_FAILURE); }
else { } file.close(); }
int main() { setlocale(LC_ALL, "rus");
using std::cout;
using std::cin;
using std::endl;
using std::string;
//show_menu();
cout << "Your choice: ";
while (true) { string new_note;
show_menu();
cout << " твой выбор - ";
int choice = 0;
cin >> choice;
if (choice == 1) { system("cls");
cout << "add_note()\n";
getline(cin, new_note);
cout << "note to add:";
getline(cin, new_note);
cin >> new_note;
const string n;
n == new_note;
add_note(new_note);
system("pause"); }
else if (choice == 2) { system("cls");
cout << "see_all_notes()\n";
see_all_notes(); system("pause");
}
else if (choice == 3) { system("cls");
string note_to_find;
getline(cin,note_to_find);
cout << "find note()\n";
getline(cin, note_to_find);
int n_found = 0;
string* found_notes = find_one_note(note_to_find, n_found);
if (found_notes) { for (int i = 0; i < n_found; i++) { cout << found_notes[i] << std::endl; }
delete[] found_notes; }
else { cout << "no such note \n"; } }
else if (choice == 4) { system("cls");
cout << "remove_one note()\n";
int n_count = 1;
string* all_notes_arr = all_notes(n_count);
//int n_count; all_notes(n_count);
for (int i = 0; i < 1; i++) { cout << "[" << i + 1 << "]" << all_notes_arr[i] << endl;
//delete[] all_notes_arr; }
delete[] all_notes_arr;
system("pause"); }
else if (choice == 5)
{ system("cls"); cout << "remove_all_notes()\n";
remove_all_notes();
system("pause"); }
else { break; } } }