Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
лабы - 2 сем - Калмычков / лаба 3 - 2 сем ПРОГРАММИРОВАНИЕ.docx
Скачиваний:
0
Добавлен:
09.07.2025
Размер:
230.55 Кб
Скачать
  1. Алгоритм работы

Read_s (аналогично у Read_elem)

Search1

Print1 (аналогично у print_Elem)

Print2 (аналогично у printElem)


addMemo (аналогично у elemMemo)

AddNext (аналогично у elem_next)


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(std::ofstream& res);

void process1(std::ofstream& res);

void del(std::ofstream& res);

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

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);

};

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();}}

#include "LIST.h"

using namespace std;

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);

};

str.cpp

elem.cpp

#include "LIST.h"

using namespace std;

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

{

int i = 0;

if ((Len == 1) && (M[0] == '*')) { cout << "___pusto___"; res << "___pusto___"; }

else {

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 << "пустая строка, ";

M[0] = '*';

Len = 1; }

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

return true;}

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

{

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;

if ((dl == 1) && (A[0] == '*')) { cout << "___pusto___"; res << "___pusto___"; }

else {

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 << "пустая строка";

A[0] = '*';

dl = 1;

}

//cout << "дlllлина-" << dl << "\n";

return true;

}

text.cpp

#include "TEXT.h"

using namespace std;

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

ListNode* cur = head;

while (cur != nullptr) //(cur != nullptr){

cur->line.print1(res);

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

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

cur = cur->next;}

res << endl

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

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

ifstream input;

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

count = 0;

// !! TODO proverka eof i return false !!!!!!

if (input.eof()){

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

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

return 0;}

if (!input.eof()) //! input.eof(){

head->addMemo(head);}

ListNode* cur = head;

ListNode* prev = nullptr;

while (!input.eof() && count < M) // && cur != nullptr ?{

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

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

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

//printElem(res);}

++count;

cur->addNext(cur);

prev = cur;

cur = cur->next;}}

// иначе если не получилось считать у нас хвост в котором ничего нет, надо его удалить, а

// предыдущий элемент сделать новым хвостом (-> next = nullptr){

delete cur;

cur = nullptr;

if (prev)

prev->next = nullptr;

else

head = nullptr; // если предыдущего не было значит только что удалили голову, запишем в голову ноль}

if (head == nullptr){

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

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

return true;}

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::process(std::ofstream &res){

ListNode *cur = head;

if (head == nullptr){

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

// если пустой список сразу возвращаемся

return;}

ListNode *prev = nullptr;

while (cur->next != nullptr){

ListNode* tmp = cur->next;

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

//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)){

//else { cout << "что-то не так"; }

cout << "\n\n";

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

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

print2(res);

process(res);

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

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

printElem(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;}}

text.cpp

main.cpp

#include "TEXT.h"

using namespace std;

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

{

ListNode* curE = head;

while (curE != nullptr) //(cur != 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()) //! input.eof()

{

headE = head;

}

ListNode* curE = cur1;

// ListNode* curE = *(this.head->cur);

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

{

chet++;

}

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

return true;

}

#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.BIG_process("in.txt", res, "elem.txt");

return 0;

}