Добавил:
надеюсь это добро кому-то поможет Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2 сем лаба 2 Структуры и классы. Обработка текста..docx
Скачиваний:
0
Добавлен:
08.07.2025
Размер:
431.21 Кб
Скачать

Блок-схемы. Алгоритм решения.

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

ReadBlocks.cpp

#include "types.h"

void inputStroka(ifstream& f, StrP& dl, int nach, int& end)

{f.seekg(nach, ios_base::beg);

int j = 0; char s;

while (!f.eof()) {f >> s;

if (f.eof()) {break; }

if (s == '\n') {break; }

j++;if (f.tellg() >= end) {break; }}dl.Len = j;}

void inputPolosa(ifstream& f, StrT& T, int nach, int& end) {

int i = 0;

while (i < vi) {

inputStroka(f, T.P[i], nach, end); nach = nach + T.P[i].Len + 2;

i++;if (nach >= end) {T.flag = 2; break;}}

T.Hei = i;}

int PolosaKon(ifstream& f, StrT& T, int nach, int& end) {

inputPolosa(f, T, nach, end); T.kon = f.tellg();

if (T.kon == -1) {T.kon = end; }

else { T.kon = T.kon - 2; }return (T.kon);}

int MaxLen(StrT T) {T.Max = 0; for (int i = 0; i < T.Hei; i++) {

T.Max = (T.Max < T.P[i].Len) ? T.P[i].Len : T.Max; }return T.Max;}

void inputStr(ifstream& f, StrP& dl, StrL& str1, int n, int num, int end) {int k = n + dl.Len - (sh * num); f.seekg(n, ios_base::beg);

char sy;int koll = 0;

while (koll < sh) {f >> sy;

if (f.eof()) { f.clear(); }

if (n < k) {str1.S[koll] = sy; n++;koll++;}

if (n >= k) { break; }

if (f.tellg() >= end) {break;}}str1.lenn = koll;}

void InputBlock(ifstream& f, StrT& T, int v, int num, int& end) {

int e = (vi < T.Hei) ? vi : T.Hei;int n = v;

for (int i = 0; i < e; i++) {inputStr(f, T.P[i], T.T[i], n, num, end);n = n + T.P[i].Len + 2;}}

ReadBlocks.h

#pragma once

void inputStroka(ifstream& f, StrP& dl, int nach, int& end);

void inputPolosa(ifstream& f, StrT& T, int nach, int& end);

int PolosaKon(ifstream& f, StrT& T, int nach, int& end);

int MaxLen(StrT T);

void inputStr(ifstream& f, StrP& dl, StrL& str1, int n, int num, int end);

void InputBlock(ifstream& f, StrT& T, int v, int num, int& end);

OutB.cpp

#include "types.h"

#include "readBlocks.h"

void OutStr(StrL& str1, int& dlW, ofstream& out1, int&numStr, ofstream& out) {

int lastInStr = str1.lenn - 1;int dlWW = 0;

for (int p = 0; p < str1.lenn; p++) {

dlW++; cout << str1.S[p];

out << str1.S[p];out1 << str1.S[p];}

int beg = 0;

for (int p = str1.lenn - 1; p >= 0; p--) {

if ((str1.S[p] == ' ') && (str1.S[lastInStr] != ' ')) {beg = p + 1;break; }}

int begProb = 0;

for (int p = 0; p < str1.lenn; p++) {

if (str1.S[p] == ' ') {begProb = p;

if (begProb != 0) {}break; }}

if (str1.S[lastInStr] != ' ') {

cout << "\n\t\tнезаконченное слово: ";

if (dlWW != 0) {

for (int p = beg; p < str1.lenn; p++) {cout << str1.S[p];}}}

if (str1.S[0] != ' ') {

cout << "\n\t\tконец незаконченного слова: ";

for (int p = 0; p < begProb; p++) {cout << str1.S[p];}}}

void OutBlock(ifstream& f, StrT& T, ofstream& out1, ofstream& out, int&numBlock) {

int ee = (vi < T.Hei) ? vi : T.Hei; int dlW = 0;

int flagg = 1; int numStr = 0;

for (int i = 0; i < ee; i++) {

cout << "строка " << 1 + i << "| ";

out << "строка " << 1 + i << "| ";

out1 << numBlock <<" " << 1 + i << " ";

numStr++;

OutStr(T.T[i], dlW, out1, numStr, out);cout << "\n"; out << "\n";

out1 << "\n";}}

void File(ifstream& f, StrT& T, int& end, ofstream& out1, ofstream& out)

{int Start[vi]; int End[vi];T.flag = 1;int bloks;int i = 0, j = 0;

f.seekg(0, ios_base::beg);int N = 0, V = 0;int Kon = 0;

f.unsetf(ios::skipws);

while (T.flag != 2) {

cout << "\n ПОЛОСА " << i + 1 << "\n";out << "\n ПОЛОСА " << i + 1 << "\n";int numBlock = i + 1; int Kon = PolosaKon(f, T, N, end);

cout << "\nT.flag" << T.flag << "\n";

bloks = (MaxLen(T) + (sh - 1)) / sh;

for (j; j < bloks; j++) {V = N + j * sh; int flag = 1;

cout << "\n БЛОК " << j + 1 << "\n\n";

out << "\n БЛОК " << j + 1 << "\n\n";

InputBlock(f, T, V, j, end); OutBlock(f, T, out1, out, numBlock);}

i++;j = 0; N = Kon + 2; bloks = 0;

for (int l = 0; l < vi; l++) {

Start[l] = -5; End[l] = -5;} out << "\n";}}

OutB.h

#pragma once

void OutStr(StrL& str1, int& dlW, ofstream& out1, int& numStr, ofstream&out);

void OutBlock(ifstream& f, StrT& T, ofstream& out1, ofstream&out, int&numBlock);

void File(ifstream& f, StrT& T, int& end, ofstream& out1, ofstream&out);

Types.h

#pragma once

#include <iostream>

#include <fstream>

using namespace std;

const unsigned sh = 30, vi = 2;

struct StrP { int Len; int nach; };

struct StrL { char S[sh]; int lenn; };

struct StrT { StrP P[vi]; StrL T[vi]; int Hei; int Max; int kon; int flag; };

Process.h

#pragma once #include <iostream> #include <fstream>

using namespace std;

void Prob(ifstream& outt1, ofstream& outt2);

void OrderFile(fstream& ord,ifstream& outt);

void Words(ifstream& ordd, ofstream& ordd2, ofstream& ordd3, ofstream& out);

void Compare(ifstream& v, ifstream& v2, ofstream& out);

Process.cpp

#include "types.h"

#include "Process.h"

void Prob(ifstream& outt1, ofstream& outt2) {char s; int nP; int nS;

outt1.unsetf(ios::skipws);

while (!outt1.eof()) {outt1 >> nP >> s >> nS >> s;

outt1 >> s;

if (s != '\n') {outt2 << nP << " " << nS << " ";

while (s != '\n') {

if (s != '\n') {outt2 << s;}outt1 >> s;

}outt2 << "\n";}}}

void OrderFile(fstream& ord, ifstream& outt) {

char s; int count = 0;

int nP; int nS; int pol; int flag = 0; int kol = 1; int num = 1; int nP1 = 1; int nS1 = 1; outt.unsetf(ios::skipws);

while (!outt.eof()) {

outt >> nP >> s >> nS >> s; outt >> s;

while (s != '\n') {outt >> s; pol = nP; }}

while (nP1 <= pol) {while (nS1 <= vi) {

outt.clear(); outt.seekg(0);

while (!outt.eof()) {outt >> nP >> s >> nS >> s; outt >> s; if (nP1 == nP && nS1 == nS) {ord << num<<" ";}

while (s != '\n') {if (nP1 == nP && nS1 == nS) {ord << s;

cout << s; flag = 1; }

if (nP1 != nP || nS1 != nS) { flag = 0; }outt >> s; }

if (flag == 1) {ord << "\n";cout << "\n";}}

nS1++;num++;}nP1++;nS1 = 1; }}

void Words(ifstream& ordd, ofstream& ordd2, ofstream& ordd3, ofstream& out) {

int str; int kk = 1000000000; int vs = 1; char s; int pos = 0; char* arrW = new char[kk]; ordd.unsetf(ios::skipws);

ordd2.unsetf(ios::skipws); ordd3.unsetf(ios::skipws);

ordd.clear(); ordd.seekg(0);

while (!ordd.eof()) {ordd >> str; ordd >> s;ordd >> s;

if (str != vs) {cout << vs<< " ";ordd2 << vs << " "; ordd3 << vs << " ";for (int i = 0; i < pos; i++) {cout << arrW[i];

ordd2 << arrW[i]; ordd3 << arrW[i]; }

cout << " \n";ordd2 << " \n";ordd3 << " \n";pos = 0; }

while (s != '\n') {if (s != ' ') {arrW[pos] = s; pos++;}

if (s == ' ') {cout << str<< " ";

ordd2 << str << " ";ordd3 << str << " ";

for (int i = 0; i < pos; i++) {cout << arrW[i]; ordd2 << arrW[i]; ordd3 << arrW[i]; }cout << " \n";ordd2 << " \n";

ordd3 << " \n";pos = 0; }ordd >> s; }vs = str;

if (str == 0) { break; };}

delete[]arrW;}

void Compare(ifstream& v, ifstream& v2, ofstream& out) {

int str, str2; int kk = 1000000000; int kollEnd = 100000000000;

char s, s1; int pos = 0; int pos2 = 0;

char* arrW = new char[kk]; char* arrW2 = new char[kk];

char* arrRes = new char[kk];

v.unsetf(ios::skipws); v2.unsetf(ios::skipws);

v.clear(); v.seekg(0);

cout << "\n\nобработка : сравнение \n";

while (!v.eof()) {v >> str >> s; v >> s;

while (s != '\n') {arrW[pos++] = s; v >> s; }

if (s == '\n') {v2.clear(); v2.seekg(0);

while (!v2.eof()) {v2 >> str2 >> s1; v2 >> s1;

while (s1 != '\n') {arrW2[pos2++] = s1; v2 >> s1; }

if (s1 == '\n') {if (str != str2) {if (pos == pos2) { int flag = 1; cout << "Слова совпали по длине\n"; for (int i = 0; i < pos; i++) { if (arrW[i] != arrW2[i]) {flag = 0; }} if (flag == 1) {cout << "Слова совпадают\n";for (int i = 0; i < pos; i++) {cout << arrW[i]; }

if (kollEnd > pos) {kollEnd = pos;

for (int i = 0; i < pos; i++) {arrRes[i] = arrW[i]; }}cout << "\n";}pos2 = 0; }

else { pos2 = 0; }}pos2 = 0; }}pos = 0; }}

if (kollEnd != 100000000000) {

out << "\n\nСАМОЕ КОРОТКОЕ СЛОВО, ВСТРЕЧАЮЩЕЕСЯ В РАЗНЫХ СТРОКАХ:\n";

for (int i = 0; i < kollEnd; i++) {out << arrRes[i]; }}

else {out << "\nСовпадающих слов не найдено!";}

delete[]arrW; delete[]arrW2; delete[]arrRes;}

Main.cpp

#include "types.h" #include "readBlocks.h" #include "OutB.h"

#include "Process.h"#include "main.h"

void main() {

setlocale(LC_ALL, "Russian");

ifstream f("in.txt", ios::in);if (!f.is_open()) { cout << "Ошибка открытия исходного файла" << "\n"; }

ofstream out1("help1.txt", ios::out);if (!out1.is_open()) { cout << "Ошибка открытия файла записи" << "\n"; }

ofstream out("outMain.txt", ios::out);if (!out.is_open()) { cout << "Ошибка открытия файла записи" << "\n"; }

f.seekg(0, ios_base::end); int end = f.tellg();

cout << "Размер файла (в байтах): " << end;

f.seekg(0, ios_base::beg);

StrL str1; StrP dl; StrT T; cout << "\n";

File(f, T, end, out1, out);

f.seekg(0, ios_base::end);

cout << "Размер файла (в байтах): " << end;

f.seekg(0, ios_base::beg); cout << "\n";out1.close();

f.close();

ifstream outt1("help1.txt", ios::in);

ofstream outt2("forOrder.txt", ios::out);

if (!outt1.is_open()) { cout << "Ошибка открытия файла записи с блоками" << "\n"; }

if (!outt2.is_open()) { cout << "Ошибка открытия файла записи с блоками" << "\n"; }

Prob(outt1, outt2);outt1.close();outt2.close();

ifstream outt("forOrder.txt", ios::in);

fstream ord("Order.txt", ios::out);

if (!outt.is_open()) { cout << "Ошибка открытия файла записи с блоками" << "\n"; }

if (!ord.is_open()) { cout << "Ошибка открытия файла ord " << "\n"; }

OrderFile(ord, outt);

ord.close();outt.close();

ifstream ordd("Order.txt", ios::out);

ofstream ordd2("Vords.txt", ios::out);

ofstream ordd3("Vords2.txt", ios::out);

if (!ordd.is_open()) { cout << "Ошибка открытия файла ordd " << "\n"; }

if (!ordd2.is_open()) { cout << "Ошибка открытия файла ordd2 " << "\n"; }

if (!ordd3.is_open()) { cout << "Ошибка открытия файла ordd3 " << "\n"; }

Words(ordd,ordd2,ordd3, out);ordd.close();ordd2.close();

ordd3.close();

ifstream v("Vords.txt", ios::in);

ifstream v2("Vords2.txt", ios::in);

if (!v.is_open()) {cout << "Ошибка открытия файла v " << "\n";}

if (!v2.is_open()) { cout << "Ошибка открытия файла v2 " << "\n"; }

Compare(v, v2, out);v.close();v2.close();out.close();}