Добавил:
надеюсь это добро кому-то поможет Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2 сем лаба 3 сновные операции над линейным списком и их реализация на языке С++ с использованием структур. Удаление элементов списка..docx
Скачиваний:
0
Добавлен:
08.07.2025
Размер:
167.08 Кб
Скачать
  1. Алгоритм работы

Read_file (аналогично для read_elem)

Process

Del

Big_process


  1. Текст программы

TEXT.h

#pragma once

#include "LIST.h"

struct Text

{

ListNode* head = nullptr;

ListNode* headE = nullptr;

//ListNode** head2 = nullptr;

int count;

int chet;

bool read_file(std::string filename, std::ofstream& res, std::string filename2);

bool read_Elem(std::string filename2, std::ofstream& res, ListNode* cur1);

void print2(std::ofstream& res);

void printElem(std::ofstream& res);

//void process_znaki(std::ofstream& res);

void replace(std::ofstream& res);

void del(std::ofstream& res);

void delElem(std::ofstream& res);

void BIG_process(std::string filename, std::ofstream& res, std::string filename2);

ListNode* head3 = nullptr;

int num_stroki;

bool read_file3(std::string filename3, std::ofstream& res);

void print23(std::ofstream& res);

void OutMain3(std::string filename3, std::ofstream& res);

};

STR.h

#pragma once

#include <string>

#include <iostream>

#include <fstream>

#include <iomanip>

static const int N = 100;

static const int M = 100;

struct StrL

{

char M[N];

char A[N];

int Len = 0;

int dl = -1;

bool read_S(std::ifstream& input, std::ofstream& res);

void print1(std::ofstream& res);

bool search1(std::ofstream& res);

int read_El(std::ifstream& f_elem, std::ofstream& res);

void printEl(std::ofstream& res);

char M3[N];

int Len3;

bool read_S3(std::ifstream& input3, std::ofstream& res);

void print13(std::ofstream& res);

};

list.cpp

#include "LIST.h"

using namespace std;

void ListNode::addMemo(ListNode*& cur)

{

if (cur == 0)

cur = new ListNode;

if (cur == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

void ListNode::addNext(ListNode*& cur)

{

cur->next = new ListNode;

if (cur->next == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

void ListNode::elemMemo(ListNode*& curE)

{

if (curE == 0)

curE = new ListNode;

if (curE == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

void ListNode::elemNext(ListNode*& curE)

{

curE->nextE = new ListNode;

if (curE->nextE == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

void ListNode::addMemo3(ListNode*& cur3)

{

if (cur3 == 0)

cur3 = new ListNode;

if (cur3 == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

void ListNode::addNext3(ListNode*& cur3)

{

cur3->next3 = new ListNode;

if (cur3->next3 == nullptr)

{

cout << " ОШИБКА, ПАМЯТЬ КОНЧИЛАСЬ!!! " << endl;

abort();

}

}

LIST.h

#pragma once

#include "STR.h"

struct ListNode

{

StrL line;

ListNode* next = nullptr;

void addMemo(ListNode*& cur);

void addNext(ListNode*& cur);

StrL one_str;

ListNode* nextE = nullptr;

void elemMemo(ListNode*& curE);

void elemNext(ListNode*& curR);

StrL line2;

ListNode* next3 = nullptr;

void addMemo3(ListNode*& cur3);

void addNext3(ListNode*& cur3);

};

str.cpp

ReadandPrintElem.cpp

#include "LIST.h"

using namespace std;

void StrL::print1(std::ofstream& res)

{

int i = 0;

while (i < Len)

{

cout << M[i];

res << M[i];

i++;

}

cout << endl;

res << endl;

}

bool StrL::read_S(std::ifstream& input, ofstream& res)

{

input.unsetf(ios::skipws);

int j = 0;

char s;

while (!input.eof())

{

input >> s;

if (input.eof()) {

break;

}

if (s == '\n') {

break;

}

M[j] = s;

j++;

if (j >= N) {

char Symb;

do

{

input >> Symb;

if (input.eof())

break;

} while (Symb != '\n');

break;

}

Len = j;

}

Len = j;

if (Len == 0) {

cout << "пустая строка, ";

}

cout << "длина-" << Len << "\n";

return true;

}

bool StrL::search1(std::ofstream& res)

{

cout << Len << " длина строки в тексте\n";

cout << dl << " длина одиночной строки\n";

cout << M[0];

int i = 0;

int flag = 0;

while (i < Len)

{

if (Len == dl)

{

for (int k = 0; k < Len; k++) {

if (A[k] == M[k]) {

flag = 1;

}

else { flag = 2; }

}

if (flag == 1) {

cout << "Найдено предложение для удаления!" << endl;

return true;

}

}

i++;

}

return false;

}

#include "STR.h"

using namespace std;

void StrL::printEl(std::ofstream& res)

{

int i = 0;

while (i < dl)

{

cout << A[i];

res << A[i];

i++;

}

cout << endl;

res << endl;

}

int StrL::read_El(std::ifstream& f_elem, ofstream& res)

{

f_elem.seekg(0, ios::beg);

dl = 0;

f_elem.unsetf(ios::skipws);

int e = 0;

char s;

while (!f_elem.eof())

{

f_elem >> s;

if (f_elem.eof()) {

break;

}

if (s == '\n') {

break;

}

A[e] = s;

e++;

if (e >= N) {

char Sym;

do

{

f_elem >> Sym;

if (f_elem.eof())

break;

} while (Sym != '\n');

break;

}

}

dl = e;

if (dl == 0) {

cout << "пустая строка";

}

cout << "длина2=" << dl << "\n";

return true;

}

bool StrL::read_S3(std::ifstream& input3, ofstream& res)

{

input3.unsetf(ios::skipws);

int j = 0;

char s;

int str = 0;

while (1)

{

input3 >> s;

if (input3.eof()) {

break;

}

if (s == '\n') {

str++;

break;

}

M3[j] = s;

j++;

if (j >= N) {

char Symb;

do

{

input3 >> Symb;

if (input3.eof())

break;

} while (Symb != '\n');

break;

}

Len3 = j;

if (Len3 == 0) {

cout << "пустая строка";

}

if (str == 1) break;

}

Len3 = j;

if (Len3 == 0) {

cout << "пустая строка";

}

cout << "длина-" << Len3 << "\n";

return true;

}

void StrL::print13(std::ofstream& res)

{

int i = 0;

while (i < Len3)

{

cout << M3[i];

// res << M3[i];

i++;

}

cout << endl;

// res << endl;

}

text.cpp

main.cpp

#include "TEXT.h"

using namespace std;

void Text::print2(std::ofstream& res)

{ListNode* cur = head;

while (cur != nullptr)

{cur->line.print1(res); cout << " ----> " << endl; cur = cur->next; }}

bool Text::read_file(std::string filename, std::ofstream& res, std::string filename2)

{ifstream input;

input.open(filename, std::ios_base::in);

count = 0;

if (input.eof())

{cout << "ФАЙЛ ПУСТ!" << endl;

res << "ФАЙЛ ПУСТ!" << endl; return 0; }

if (!input.eof())

{head->addMemo(head); }

ListNode* cur = head;

ListNode* prev = nullptr;

while (!input.eof() && count < M)

{if (cur->line.read_S(input, res))

{if (read_Elem(filename2, res, cur))

{cout << "Заданное значение: \n"; }

++count;

cur->addNext(cur); prev = cur; cur = cur->next; }}

{delete cur; cur = nullptr;

if (prev) prev->next = nullptr;

else head = nullptr; }

if (head == nullptr)

{cout << " Файл пуст! " << endl;

res << " Файл пуст! " << endl; }

cout << "\nКоличество строк в in.txt " << count << "\n\n";

return true;}

void Text::print23(std::ofstream& res)

{ListNode* cur3 = head3;

while (cur3 != nullptr)

{cur3->line2.print13(res);

cout << " ----> " << endl; cur3 = cur3->next3; }

cout << " NULL (конец списка)" << endl;}

bool Text::read_file3(std::string filename3, std::ofstream& res)

{ifstream input3;

input3.open(filename3, std::ios_base::in);

count = 0;

if (input3.eof())

{cout << "ФАЙЛ ПУСТ!" << endl; res << "ФАЙЛ ПУСТ!" << endl;

return 0; }

if (!input3.eof())

{head3->addMemo(head3); }

ListNode* cur3 = head3;

while (!input3.eof() && count < M)

{if (cur3->line2.read_S(input3, res))

{if (read_Elem(filename3, res, cur3))

{cout << "Заданное значение: \n";

printElem(res); }

++count; cur3->addNext(cur3);

cur3 = cur3->next; }}

if (head3 == nullptr)

{cout << " Файл пуст! " << endl; res << " Файл пуст! " << endl; }

cout << "\nКоличество строк в in2.txt " << count << "\n\n";

return true;}

void Text::OutMain3(string filename3, ofstream& res)

{

cout << endl << "\n\n Началась обработка файла " << filename3 << endl << endl;

if (!read_file3(filename3, res))

{res << "Oshibka reading" << endl;

cout << "Oshibka reading" << endl; }

else

{cout << endl << " Исходный текст: " << endl;

print23(res);

cout << " Кончилась обработка файла " << filename3 << endl;}}

void Text::del(std::ofstream& res)

{ListNode* cur = head;

if (cur == nullptr)

{cout << " Список пуст " << endl;

res << " Список пуст " << endl; return; }

ListNode* tmp = nullptr;

while (cur != nullptr)

{tmp = cur->next; delete cur; cur = tmp;}

std::cout << " Список был удален. " << endl;

res << " Список был удален. " << endl;}

void Text::replace(std::ofstream& res)

{ListNode* cur = head;

ListNode* cur3 = head3;

if (head == nullptr)

{res << "Список пуст!! " << endl; return; }

ListNode* prev = nullptr;

while (cur != nullptr)

{if (cur->line.search1(res) == true)

{ListNode* tmpp = new ListNode;

if (tmpp != nullptr) {

tmpp->line = cur3->line2;

tmpp->next = cur->next;

cur->next = tmpp; }

ListNode* tmp = cur->next;

if (prev){ prev->next = tmp; }

delete cur;

if (cur == head)

{head = tmp; res << "голова удалена" << endl; }cur = tmp; }

else

{prev = cur; cur = cur->next; } }}

void Text::BIG_process(string filename, ofstream& res, string filename2)

{

cout << endl << " Началась обработка файла " << filename << endl;

if (read_file(filename, res, filename2))

{

cout << "\n\n";

cout << endl << " Исходный текст: " << endl;

res << endl << " Исходный текст: " << endl;

print2(res);

replace(res);

cout << endl << endl;

cout << "RESULT: " << endl << endl;

res << endl << endl;

res << "RESULT: " << endl << endl;

print2(res);

cout << endl;

res << endl;

del(res);

cout << " Кончилась обработка файла " << filename << endl;

res << " Кончилась обработка файла " << filename << endl; }

else {

res << "Oshibka reading" << endl;

cout << "Oshibka reading" << endl; }}

#include "TEXT.h"

using namespace std;

int main(int argc, char* argv[])

{

setlocale(LC_ALL, "rus");

std::string fileRES = "result.txt";

std::ofstream res(fileRES, ios::out | ios::trunc);

Text text;

text.OutMain3("in2.txt", res);

text.BIG_process("in.txt", res, "elem.txt");

return 0;

}

TextElement.cpp

#include "TEXT.h"

using namespace std;

void Text::printElem(std::ofstream& res)

{

ListNode* curE = head;

while (curE != nullptr)

{

curE->line.printEl(res);

// res << " ----> " << endl;

cout << " ----> " << endl;

curE = curE->nextE;

}

// res << endl<< " NULL (конец списка)" << endl;

}

bool Text::read_Elem(std::string filename2, std::ofstream& res, ListNode* cur1)

{

chet = 0;

ifstream f_elem;

f_elem.open(filename2, std::ios_base::in);

if (f_elem.eof())

{

cout << "ФАЙЛ ПУСТ!" << endl;

res << "ФАЙЛ ПУСТ!" << endl;

return 0;

}

if (!f_elem.eof())

{

headE = head;

}

ListNode* curE = cur1;

if (curE->line.read_El(f_elem, res))

{

chet++;

}

{

cout << "\nКоличество строк в elem.txt " << chet << "\n\n";

return true;

}

}

void Text::delElem(std::ofstream& res)

{

ListNode* curE = head;

if (curE == nullptr)

{

cout << " Список пуст " << endl;

res << " Список пуст " << endl;

return;

}

ListNode* tmpE = nullptr;

while (curE != nullptr)

{

tmpE = curE->nextE;

delete curE;

curE = tmpE;

}

std::cout << " Список был удален. " << endl;

res << " Список был удален. " << endl;

}