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

Main() OutMark()

Process()

ReadMark1 версия 1

ReadMark версия 2

  1. Программа

1.1

#include <iostream>

#include <fstream>

using namespace std;

const unsigned n = 200;

unsigned i;

struct StrM{char A[n + 1];char Mark;};

int ReadMark1(ifstream& f, ofstream& out, ofstream& R, StrM& a1, int& flag)

{int koll = 0;char a2, s;

f >> a1.Mark;

if (f.eof()) {

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

return 0;

flag = 1;}

f >> a2;

if (f.eof()) {

cout << "Недостаточно данных (после внутреннего маркера ничего нет).";

R << "Недостаточно данных (после внутреннего маркера ничего нет).";

R << a1.Mark;

return 0;

flag = 1;}

else {i = 0;

f.unsetf(ios::skipws);

a1.A[i] = a1.Mark;

R << "Строка до внешнего маркера:" << "\n";

out << "Строка до внешнего маркера:" << "\n";

while (1) {

f >> s;

koll++;

if (s == a2) {

if (koll == 1) {

R << "Символ конца файла (внешний маркер)" << "\n";}break;}

if (f.eof()) {

R << "Пустая строка" << "\n";

break;}

if (s == '\n') {

//break;}

a1.A[i] = s;

out << a1.A[i];

R << a1.A[i];

i++;

if (i >= n) break;}

a1.A[i] = ' ';

a1.A[i + 1] = a1.Mark;}}

void outMark(ofstream& out, ofstream& R, StrM& a1, StrM& V){;

out << "\n\n" << "Строка до внутреннего маркера: " << "\n";

int y = 0;

while (a1.A[y] != a1.Mark) {

out << a1.A[y];

R << a1.A[y];

V.A[y] = a1.A[y];

//cout << a1.A[y];

y++;}

V.A[y] = a1.Mark;}

void Process(ofstream& out, ofstream& F, StrM& a1, StrM& V){

if (a1.A[0] == a1.Mark) {out << "\n" << "Символ конца строки(внутренний маркер)";}

int B[n][3] = { 0 };

int q = 0;

int colvo = 0;

for (q; a1.A[q] != a1.Mark; q++) {

if (a1.A[q] != ' '){int st = q;

int e = q;

for (e + 1; a1.A[e] != ' '; e++) {;}

int end = e - 1;

q = e;

B[colvo][0] = st;

B[colvo][1] = end;

B[colvo][2] = end - st + 1;

colvo++;}}

out << "Количество слов: " << colvo << "\n\n";

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

int dlina1 = B[i][2];

for (int j = i + 1; j < colvo; j++) {

int dlina2 = B[j][2];

if (dlina1 == dlina2) {

out << "\n" << "Длины слов : " << dlina1 << " " << dlina2 << " __ Сравним буквы : " << "\n";

int flag = 0;

int sym1 = B[i][0];

int sym2 = B[j][0];

for (int t = 0; t < dlina1; t++) {

out << a1.A[sym1+t] << " " << a1.A[sym2+t] << "\n";

if (a1.A[sym1+t] != a1.A[sym2+t]) {

flag = 1;

break;}}

if (flag == 0) {

out << "\n"<<"ОДИНАКОВЫЕ СЛОВА с индексами " << i << " " << j << "\n\n";

for (int d = 0; d < dlina1; d++) {

V.A[sym1 + d] = ' ';

V.A[sym2 + d] = ' ';}

int y = 0;

while (V.A[y] != a1.Mark) {

F << V.A[y];

//out << V.A[y];

y++;}

F << "\n";}}}}}

void Rez(ofstream& R, StrM& a1, StrM& V){

R << "\n\n" << "Результат: удалены лишние пробелы, символы новых строк и слова, встречающиеся более 1 раза\n" << "\n";

int p = 0;

while (V.A[p] != a1.Mark) {

while (((V.A[p] == ' ') && (V.A[p + 1] == ' ')) || ((p == 0) && (V.A[p] == ' '))) {

int t = p;

for (t; V.A[t] !=a1.Mark; t++) {

V.A[t] = V.A[t + 1];}}

R << V.A[p];

p++;}}

int main(){

setlocale(LC_ALL, "Russian");

ifstream f("in.txt", ios::in);

if (!f.is_open())

{cout << "Ошибка открытия исходного файла" << "\n";}

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

if (!out.is_open())

{cout << "Ошибка открытия файла записи" << "\n";}

ofstream F("process.txt", ios::out);

if (!F.is_open())

{cout << "Ошибка открытия файла записи" << "\n";}

ofstream R("result.txt", ios::out);

if (!R.is_open())

{cout << "Ошибка открытия файла записи" << "\n";}

StrM a1;

StrM V;

int flag = 0;

ReadMark1(f, out, R,a1, flag);

out << "\n\n";

if (flag == 0) {

outMark(out, R, a1, V);

out << "\n";

out << "\n";

Process(out, F, a1, V);

out << "\n";

Rez(R, a1, V);}

f.close();

out.close();

F.close();

R.close();

return 0;}

1.2

#include <iostream>

#include <fstream>

using namespace std;

const unsigned n = 300;

unsigned i;

struct StrM

{char A[n + 1];char Mark;int Int;};

int ReadMark1(ifstream& f, ofstream& out, ofstream& R, StrM& a1, int& flag){

char s;

int kkk = 0;

f >> a1.Mark;

if (f.eof()) {

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

return 0;

flag = 1;}

f >> a1.Int;

if (a1.Int <= 0) { a1.Int = 0;

kkk = 1;}

if (a1.Int > n) {a1.Int = n;

kkk = 1;}

if (a1.Int > 0){ kkk = 1; }

if (f.eof()) {

if (kkk == 0) {

cout << "Недостаточно данных (после внутреннего маркера ничего нет).\n";}

else {

cout << "После числа нет символов. \n";}

out << "текст состоит из пустой строки";

return 0;

flag = 1;}

if (kkk == 1){

i = 0;

int koll = 0;

f.unsetf(ios::skipws);

a1.A[i] = a1.Mark;

//cout << "Строка до внешнего маркера:" << "\n";

R << "Строка с заданной длинной:" << "\n";

while (1) {

f >> s;

koll++;

if (f.eof()) break;

a1.A[i] = s;

R << a1.A[i];i++;

if (i > n) break;

if (koll == a1.Int) break;}

a1.A[i] = ' ';

a1.A[i+1] = a1.Mark;

//cout << "\nмаркер: " << a1.Mark << "\n";}}

void outMark(ofstream& out, ofstream& R, StrM& a1, StrM& V)

{

R << "\n\n" << "Строка до внутреннего маркера: " << "\n";

//cout << "\n\n" << "Строка до внутреннего маркера: " << "\n";

int y = 0;

while (a1.A[y] != a1.Mark) {

out << a1.A[y];

R << a1.A[y];

V.A[y] = a1.A[y];y++;}

V.A[y] = a1.Mark;}

void Process(ofstream& out, ofstream& F, StrM& a1, StrM& V)

{if (a1.A[0] == a1.Mark) {

out << "\n" << "Символ конца строки(внутренний маркер)";}

int B[n][3] = { 0 };

int q = 0;

int colvo = 0;

for (q; a1.A[q] != a1.Mark; q++) {

if (a1.A[q] != ' '){

int st = q;

int e = q;

for (e + 1; a1.A[e] != ' '; e++) {;}

int end = e - 1;q = e;

B[colvo][0] = st;

B[colvo][1] = end;

B[colvo][2] = end - st + 1;

colvo++; }}

out << "Количество слов: " << colvo << "\n\n";

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

int dlina1 = B[i][2];

for (int j = i + 1; j < colvo; j++) {

int dlina2 = B[j][2];

if (dlina1 == dlina2) {

out << "\n" << "Длины слов : " << dlina1 << " " << dlina2 << " __ Сравним буквы : " << "\n";

int flag = 0;

int sym1 = B[i][0];

int sym2 = B[j][0];

for (int t = 0; t < dlina1; t++) {

out << a1.A[sym1 + t] << " " << a1.A[sym2 + t] << "\n";

if (a1.A[sym1 + t] != a1.A[sym2 + t]) {

flag = 1;

break; }}

if (flag == 0) {

out << "\n" << "ОДИНАКОВЫЕ СЛОВА с индексами " << i << " " << j << "\n\n";

for (int d = 0; d < dlina1; d++) {

V.A[sym1 + d] = ' ';

V.A[sym2 + d] = ' ';}

int y = 0;

while (V.A[y] != a1.Mark) {

F << V.A[y];

//out << V.A[y];

y++;}

F << "\n";}}}}}

void Rez(ofstream& R, StrM& a1, StrM& V){

R << "\n\n" << "Результат: удалены лишние пробелы, символы новых строк и слова, встречающиеся более 1 раза" << "\n";

int p = 0;

while (V.A[p] != a1.Mark) {

while (((V.A[p] == ' ') && (V.A[p + 1] == ' ')) || ((p == 0) && (V.A[p] == ' '))) {

int t = p;

for (t; V.A[t] != a1.Mark; t++) {

V.A[t] = V.A[t + 1]; }}

//R << V.A[p];

p++;}

V.A[p] = a1.Mark;

int u = 0;

while (V.A[u] != a1.Mark) {

if (V.A[u] != '\n') {

R << V.A[u];}

u++;}}

int main(){

setlocale(LC_ALL, "Russian");

ifstream f("in2.txt", ios::in);

if (!f.is_open()){

cout << "Ошибка открытия исходного файла" << "\n";}

ofstream out("out2.txt", ios::out);

if (!out.is_open()){

cout << "Ошибка открытия файла записи" << "\n";}

ofstream F("process2.txt", ios::out);

if (!F.is_open()){

cout << "Ошибка открытия файла записи" << "\n";}

ofstream R("result2.txt", ios::out);

if (!R.is_open()){

cout << "Ошибка открытия файла записи" << "\n";}

StrM a1;

StrM V;

int flag = 0;

ReadMark1(f, out, R, a1, flag);

out << "\n\n";

if (flag == 0) {

outMark(out, R, a1, V);

out << "\n";

out << "\n";

Process(out, F, a1, V);

out << "\n";

Rez(R, a1, V);}

f.close();

out.close();

F.close();

R.close();

return 0;}

2.1

#include <iostream>

#include <fstream>

using namespace std;

const unsigned N = 200;

class Str {

private:

char A1[N];

unsigned L;

char p;

char V[N];

int koll;

public:

void set_symb(ifstream& f) {

f >> p;}

char get_symb() {

return p;}

char set_Read1(ifstream& f) {

set_symb(f);

char M = get_symb(); // считался маркер

koll = 0;

if (f.eof()) {

cout << "файл пуст";

return 0;

}else {

int i = 0;

char a1;

f.unsetf(ios::skipws);

while (1) {

set_symb(f); //считался символ

a1 = get_symb();

if (f.eof()) {

//cout << "после маркера ничего нет" << "\n";break;}

if (a1 == '\n') {

//cout << "конец строки" << "\n";

break;}

if (a1 == M){

cout << "маркер" << "\n";

break;}

A1[i] = a1;

i++;

if (i == N) break;

koll++; //длина строки обозначена}

cout << "длина строки " << koll;

A1[koll] = ' ';

koll++;

}}int get_Read1() {

L = koll; //длина строки передана

return L;}

void OutM1(ofstream& out){

cout << "\n\n" << "Строка до внутреннего маркера: " << "\n\n";

int y = 0;

for (y; y < L; y++) {

cout << A1[y];

V[y] = A1[y];}}

void Process(ofstream& out){

int B[N][3] = { 0 };

int q = 0;

int colvo = 0;

for (q; q < L; q++) {if (A1[q] != ' '){

int st = q;

int e = q;

for (e + 1; A1[e] != ' '; e++) {;}

int end = e - 1;

q = e;

B[colvo][0] = st;

B[colvo][1] = end;

B[colvo][2] = end - st + 1;

colvo++;}}

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

int dlina1 = B[i][2];

for (int j = i + 1; j < colvo; j++) {

int dlina2 = B[j][2];

if (dlina1 == dlina2) {

cout << "\n" << "Длины слов : " << dlina1 << " " << dlina2 << " __ Сравним буквы : " << "\n";

int flag1 = 0;

int sym1 = B[i][0];

int sym2 = B[j][0];

for (int t = 0; t < dlina1; t++) {

out << A1[sym1 + t] << " " << A1[sym2 + t] << "\n";

if (A1[sym1 + t] != A1[sym2 + t]){

flag1 = 1;

break;}}

if (flag1 == 0) {

cout << "\n" << "ОДИНАКОВЫЕ СЛОВА с индексами " << i << " " << j << "\n\n";

for (int d = 0; d < dlina1; d++){

V[sym1 + d] = ' ';

V[sym2 + d] = ' ';}

int y = 0;

while (y != L) {

cout << V[y];

y++;}

cout << "\n";}}}}

V[L] = ' ';

V[L + 1] = '\n';}

void Rez(ofstream& out){

cout << "\n\n" << "Результат: удалены лишние пробелы и слова, встречающиеся более 1 раза\n" << "\n";

int p = 0;

while (V[p] != '\n') {

while (((V[p] == '\n') && (V[p + 1] == ' ')) || ((p == 0) && (V[p] == ' '))) {

int t = p;

for (V[t]; V[t]!='\n'; t++) {

V[t] = V[t + 1];}}

if (V[p] != 'm') {

cout << V[p];}

p++;}}};

void main() {

setlocale(LC_ALL, "Russian");

ifstream f("inp.txt", ios::in);

if (!f.is_open()){

cout << "Ошибка открытия исходного файла" << "\n";}

ofstream out("outp.txt", ios::out);

if (!out.is_open()){

cout << "Ошибка открытия файла записи" << "\n";}

Str K;

K.set_Read1(f);

K.get_Read1();

K.OutM1(out);

cout << "\n\n";

K.Process(out);

K.Rez(out);

cout << "\n\n";

f.close();

out.close();}

2.2

#include <iostream>

#include <fstream>

using namespace std;

const unsigned N = 200;

class Str {

private:

char A1[N];

unsigned L;

int pp;

char p;

char V[N];

int koll;

public:

void set_colvo(ifstream& f) {

f >> pp;}

int get_colvo() {

return pp;}

void set_symb(ifstream& f) {

f >> p;}

char get_symb() {

return p;}

char set_Read1(ifstream& f) {

set_colvo(f);

int Int = get_colvo();

int kkk = 0;

koll = 0;

if (f.eof()) {

if (kkk == 0) {

cout << "Файл пуст.\n";

return 0;}}

else {

if (Int <= 0) { Int = 0; kkk = 1; }

if (Int > N) { Int = N; kkk = 1; }

if (Int > 0) { kkk = 1; }

cout << Int;}

if (f.eof()) {

if (kkk == 1) {

cout << Int;

cout << "\nПосле числа ничего нет";

return 0;}}

else {

koll = 0;

int i = 0;

char a1;

f.unsetf(ios::skipws);

while (1) {

set_symb(f); //считался символ

a1 = get_symb();

if (f.eof()) {

break;}

if (a1 == '\n') {

break;}

A1[i] = a1;

i++;

if (i >= N) break;

koll++; //длина строки обозначена}

cout << "\n";

cout << koll;

if (Int < koll) {

koll = Int; }

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

A1[koll] = ' ';

koll++;}}

int get_Read1() {

L = koll; //длина строки передана

return L;}

void OutM1(ofstream& out){

cout << "\n\n" << "Строка до внутреннего маркера: " << "\n\n";

int y = 0;

for (y; y < L; y++) {

cout << A1[y];

V[y] = A1[y];}}

void Process(ofstream& out){

int B[N][3] = { 0 };

int q = 0;

int colvo = 0;

for (q; q < L; q++) {

if (A1[q] != ' '){

int st = q;

int e = q;

for (e + 1; A1[e] != ' '; e++) {;}

int end = e - 1;

q = e;

B[colvo][0] = st;

B[colvo][1] = end;

B[colvo][2] = end - st + 1;

colvo++;}}

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

int dlina1 = B[i][2];

for (int j = i + 1; j < colvo; j++) {

int dlina2 = B[j][2];

if (dlina1 == dlina2) {

cout << "\n" << "Длины слов : " << dlina1 << " " << dlina2 << " __ Сравним буквы : " << "\n";

int flag1 = 0;

int sym1 = B[i][0];

int sym2 = B[j][0];

for (int t = 0; t < dlina1; t++) {

out << A1[sym1 + t] << " " << A1[sym2 + t] << "\n";

if (A1[sym1 + t] != A1[sym2 + t]){

flag1 = 1;

break;}}

if (flag1 == 0) {

cout << "\n" << "ОДИНАКОВЫЕ СЛОВА с индексами " << i << " " << j << "\n\n";

for (int d = 0; d < dlina1; d++){

V[sym1 + d] = ' ';

V[sym2 + d] = ' ';}

int y = 0;

while (y != L) {

cout << V[y];

y++;}

cout << "\n";}}}}

V[L] = ' ';

V[L + 1] = '\n';}

void Rez(ofstream& out){

cout << "\n\n" << "Результат: удалены лишние пробелы и слова, встречающиеся более 1 раза\n" << "\n";

int p = 0;

while (V[p] != '\n') {

while (((V[p] == '\n') && (V[p + 1] == ' ')) || ((p == 0) && (V[p] == ' '))) {

int t = p;

for (V[t]; V[t] != '\n'; t++) {

V[t] = V[t + 1];}}

if (V[p] != 'm') {

cout << V[p];}

p++;}}};

void main() {

setlocale(LC_ALL, "Russian");

ifstream f("inp2.txt", ios::in);

if (!f.is_open())

{cout << "Ошибка открытия исходного файла" << "\n";}

ofstream out("outp2.txt", ios::out);

if (!out.is_open())

{cout << "Ошибка открытия файла записи" << "\n";}

Str K;

K.set_Read1(f);

K.get_Read1();

K.OutM1(out);

cout << "\n\n";

K.Process(out);

K.Rez(out);

cout << "\n\n";

f.close();

out.close();}