Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
1 семестр Калмычков / Записка 1 сем..docx
Скачиваний:
71
Добавлен:
08.12.2019
Размер:
630.27 Кб
Скачать

Блок – схемы

Int quotients:

Int points:

Int extra:

Int main:

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

#include "pch.h"

#include <iostream>

#include <string>

#include <fstream>

#include <math.h>

#include <iomanip>

using namespace std;

int quotients(int &SIZE, int count, const unsigned int MaxLines, long double *a, long double *b, long double *c, fstream &protocol)

{

/*Функция считывания коэффицентов

и нахождения прямых*/

setlocale(LC_ALL, "Russian");

fstream A, B, C;

bool check = true;

int MAX = 0;//Макс. количество исп. прямых

int quot_a = 0, quot_b = 0, quot_c = 0;//Коэффиценты

int Max_a = 0, Max_b = 0, Max_c = 0;//Максимальное для каждого из массивов

A.open("A.txt", ios::in);

B.open("B.txt", ios::in);

C.open("C.txt", ios::in);

while ((!A.eof()) && ((SIZE - 1) != MAX))

{

if (check == true)

{

//Первая итерация - нахождение максимума

A >> Max_a;

protocol << "Считано максимальное количество прямых из файла A.txt: " << Max_a << endl;

B >> Max_b;

protocol << "Считано максимальное количество прямых из файла B.txt: " << Max_b << endl;

C >> Max_c;

protocol << "Считано максимальное количество прямых из файла C.txt: " << Max_c << endl;

check = false;

quot_a++;

quot_b++;

quot_c++;

if ((Max_a <= Max_b) && (Max_a <= Max_c))

MAX = Max_a;

if ((Max_b <= Max_a) && (Max_b <= Max_c))

MAX = Max_b;

if ((Max_c <= Max_b) && (Max_c <= Max_a))

MAX = Max_c;

protocol << "Итоговое количество используемых прямых: " << MAX << endl;

if (MAX <= 1)

{

cout << "Недостаточно информации для решения задания" << endl;

protocol << "Недостаточно информации для решения задания" << endl;

exit(0);

}

if (MAX >= MaxLines)

{

MAX = MaxLines;

}

}

else

{

A >> *(a + quot_a);

B >> *(b + quot_b);

C >> *(c + quot_c);

if ((*(a + quot_a) == 0) || *(b + quot_b) == 0)

{

cout << "Прямой " << count << " не существует" << endl;

protocol << "Прямой " << count << ") не существует (координаты при x и(или) y равны 0)" << endl;

count++;

}

else

{

cout << "Коэффиценты для " << count << " прямой: " << *(a + quot_a) << " " << *(b + quot_b) << " " << *(c + quot_c) << endl;

protocol << "Считана прямая " << count << ") с коэфицентами " << *(a + quot_a) << " " << *(b + quot_b) << " " << *(c + quot_c) << endl;

count++;

}

quot_a++;

quot_b++;

quot_c++;

}

SIZE++;

}

cout << endl;

A.close();

B.close();

C.close();

return 0;

}

int points(int &SIZE, int &count, long double *a, long double *b, long double *c, fstream &protocol)

{

/* Функция нахождения

точек пересечения */

fstream points;

points.open("points.txt", ios::out);

int aSize = 1, bSize = 1, cSize = 1, a1Size = 1, b1Size = 1, c1Size = 1;//Элементы массива

float a1, b1, c1, a2, b2, c2;//Временные переменные для выполнения действий

float x1, x2, x3, y1, y3;//Временные переменные для выполнения действий

float opr1, opr2;//Переменные - определители найденные методом Гаусса

protocol << "Выполняется поиск точек пересечения прямых методом Крамера: " << endl;

for (aSize = 1, bSize = 1, cSize = 1; aSize <= SIZE-1 && bSize <= SIZE-1 && cSize <= SIZE-1; aSize++, bSize++, cSize++)

{

a1 = a[aSize];

b1 = b[bSize];

c1 = c[cSize];

if ((a1 != 0) && (b1 != 0))

{

a1Size = aSize + 1;

b1Size = bSize + 1;

c1Size = cSize + 1;

for (a1Size, b1Size, c1Size; a1Size <= SIZE - 1 && b1Size <= SIZE - 1 && c1Size <= SIZE - 1; a1Size++, b1Size++, c1Size++)

{

a2 = a[a1Size];

b2 = b[b1Size];

c2 = c[c1Size];

if ((a2 != 0) && (b2 != 0))

{

x1 = (c1 * b2) - (c2 * b1);

x2 = (a1 * b2) - (a2 * b1);

if (x2 == 0)

{

opr1 = (a1 * c2) - (a2 * c1);

opr2 = (b1 * c2) - (b2 * c1);

protocol << "Посчитаны определители для прямых " << aSize << " и " << a1Size << endl;

if (opr1 == 0 && opr2 == 0)

{

cout << "Прямые " << aSize << " и " << a1Size << " совпадают" << endl;

protocol << "Прямые " << aSize << ") и " << a1Size << ") совпадают" << endl;

}

else

{

cout << "Прямые " << aSize << " и " << a1Size << " параллельны" << endl;

protocol << "Прямые " << aSize << ") и " << a1Size << ") параллельны" << endl;

}

}

else

{

x3 = (x1 / x2) * (-1);

y1 = (a1 * c2) - (a2 * c1);

y3 = (y1 / x2) * (-1);

cout << count << ") Точка пересечения прямых " << aSize << " и " << a1Size << ": (" << x3 << " ; " << y3 << ")" << endl;

protocol << "Найдена точка пересечения прямых " << aSize << " и " << a1Size << " : (" << x3 << " ; " << y3 << ") " << "под номером " << count << endl;

points << x3 << " " << y3 << endl;

count++;

}

}

}

}

}

protocol << "Точки пересечения прямых выведены в файл points.txt" << endl;

cout << endl;

points.close();

return 0;

}

int extra(int &count, int &count_points, fstream &protocol)

{

protocol << "Выполняется поиск одинаковых точек пересечения" << endl;

fstream points, extra_points;

points.open("points.txt", ios::in);

extra_points.open("extra points.txt", ios::out);

bool check = true;

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

points.clear();

int count_space = 0;//Счетчик пробелов(считывание матрицы)

char symbol;

while (!points.eof())

{

points.get(symbol);

if (symbol == ' ')

count_space++;

if (symbol == '\n')

break;

}

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

points.clear();

int rows = count - 1;//Строки

int colls = count_space + 1;//Столбцы

int tmp_count1 = 0, tmp_count2 = 0, tmp_count3 = 0, tmp_count4 = 0;//Временные счетчики

double **mass_points;//Массив точек пересечения

mass_points = new double *[rows];

for (int i = 0; i < rows; i++)

mass_points[i] = new double[colls];

for (int i = 0; i < rows; i++)

for (int j = 0; j < colls; j++)

points >> mass_points[i][j];

protocol << "Считан массив mass_points из файла points.txt" << endl;

protocol << "Количество точек в нем " << rows << endl;

cout << mass_points[0][0] << " " << mass_points[0][1] << endl;

extra_points << mass_points[0][0] << " " << mass_points[0][1] << endl;

for (int i = 1; i < rows; i++)

{

for (int minus = 1; minus <= i; minus++)

{

for (int j = 0; j < colls; j++)

{

if (mass_points[i][j] == mass_points[i - minus][j])

{

tmp_count1++;

if (tmp_count1 == 2)

{

cout << "Точка " << i + 1 << ") и " << i - minus + 1 << ") совпадают" << endl;

protocol << "Точка " << i + 1 << ") и " << i - minus + 1 << ") совпадают" << endl;

tmp_count2++;

}

}

}

tmp_count1 = 0;

}

if (tmp_count2 < 1)

{

cout << mass_points[i][0] << " " << mass_points[i][1] << endl;

protocol << "Найдена уникальная точка " << mass_points[i][0] << " " << mass_points[i][1] << endl;

extra_points << mass_points[i][0] << " " << mass_points[i][1] << endl;

count_points++;

}

tmp_count2 = 0;

}

protocol << "Выполняется поиск количества прямых, принадлежащих каждой из точек " << endl;

for (int i = 0; i < rows; i++)

{

for (int m = 0; m < rows; m++)

{

for (int j = 0; j < colls; j++)

{

if (mass_points[i][j] == mass_points[m][j])

{

tmp_count3++;

if (tmp_count3 == 2)

{

tmp_count4++;

}

}

}

tmp_count3 = 0;

}

cout << "Точке " << i+1 << " принадлежит " << tmp_count4 << " прямым" << endl;

protocol << "Точке " << i + 1 << " принадлежит " << tmp_count4 << " прямым" << endl;

tmp_count4 = 0;

}

points.close();

extra_points.close();

cout << "Количество разных точек: " << count_points << endl;

protocol << "Количество разных точек: " << count_points << endl;

return 0;

}

int main()

{

fstream protocol;

protocol.open("protocol.txt", ios::out);

int count = 1;

int SIZE = 0;

const unsigned int MaxLines = 100;

int count_points = 1;

long double a[MaxLines], b[MaxLines], c[MaxLines];

quotients(SIZE, count, MaxLines, a, b, c, protocol);

points(SIZE, count, a, b, c, protocol);

extra(count, count_points, protocol);

protocol << "Программа успешно завершила свою работу" << endl;

protocol.close();

}

Соседние файлы в папке 1 семестр Калмычков