Задание 1.
Создать абстрактный базовый класс с виртуальной
функцией — корни уравнения. Создать производные
классы: класс линейных уравнений и класс квадратных
уравнений. Определить функцию вычисления корней
уравнений.
//Dombrovsky I.V. GROUP 32PPS11
/*
Задание 1.
Создать абстрактный базовый класс с виртуальной
функцией — корни уравнения. Создать производные
классы: класс линейных уравнений и класс квадратных
уравнений. Определить функцию вычисления корней
уравнений.
*/
#include <iostream>
#include <math.h>
using namespace std;
#define TEXT_COLOR_GREEN system("color 0A");
#define STOP_CMD_WINDOWS system("pause");
class RootsEquations // Abstract class
{
public:
RootsEquations(double a, double b, double c) :a(a), b(b), c(c) // CONSTRUCTOR
{
this->d = 0; this->x1 = 0; this->x2 = 0;
}
virtual void rootsEquations() = 0;
protected:
double a, b, c, d;
double x1, x2;
};
class LinearEquation : public RootsEquations { // Class Linear Equation
public:
LinearEquation(double a, double b, double c) : RootsEquations(a, b, c){}
virtual void rootsEquations() override
{
this->x1 = a + b;
cout << "x = " << this->x1 << endl;
}
};
class QuadraticEquation : public RootsEquations { // Class Quadratic Equation
public:
QuadraticEquation(double a, double b, double c) : RootsEquations(a, b, c) {}
virtual void rootsEquations() override
{
this->d = (b * b) - (a * c * 4);
if (d >= 0)
{
this->x1 = (b - sqrt(d)) / (2 * a);
this->x2 = (b + sqrt(d)) / (2 * a);
cout << "x1 = " << this->x1 << endl;
cout << "x2 = " << this->x2 << endl;
}
else
{
cout << "Descriminant less zero!" << endl;
}
}
};
int main() {
TEXT_COLOR_GREEN
RootsEquations * r;
LinearEquation l(2, 1, 5); // 2x - 1 = 5
r = &l;
r->rootsEquations();
QuadraticEquation q(5, 8, 3); // 5x^2 - 8x + 3 = 0
r = &q;
r->rootsEquations();
STOP_CMD_WINDOWS
return 0;
}
Задача2
/*Задание 2.
Создайте абстрактный класс Shape для рисования плоских
фигур. Определите виртуальные методы:
• Show() — вывод на экран информации о фигуре,
• Save() — сохранение фигуры в файл,
• Load() — считывание фигуры из файла.
Определите производные классы:
• Square — квадрат, который характеризуется координатами левого верхнего угла и длиной стороны;
• Rectangle — прямоугольник с заданными координатами верхнего левого угла и размерами;
• Circle — окружность с заданными координатами центра и радиусом;
• Ellipse — эллипс с заданными координатами верхнего угла описанного вокруг него прямоугольника со
сторонами, параллельными осям координат, и размерами этого прямоугольника.
Создайте массив фигур, сохраните фигуры в файл, загрузите в другой массив и отобразите информацию о каждой
из фигур.*/
#include <iostream>
#include <string>
#include <fstream>
#include <iterator>
using namespace std;
#define TEXT_COLOR_GREEN system("color 0A");
#define STOP_CMD_WINDOWS system("pause");
int ValuieFigures(string str); // GET VALUE FIGURES
class Shape { // ABSTRACT CLASS SHAPE
protected:
Shape() :x(0), y(0), a(0), b(0) {}
Shape(double x, double y, double a, double b) :x(x), y(y), a(a), b(b) {}
Shape(double x, double y, double a) :x(x), y(y), a(a) { this->b = 0; }
public:
virtual void Show() = 0;
virtual void Save() = 0;
virtual string Load() = 0;
double x, y, a, b;
};
class Square : public Shape { // FIGURE SQUARE
public:
Square() :Shape() {}
Square(double x, double y, double a) :Shape(x, y, a) {}
virtual void Show() override
{
cout << "Square ==>> " << "x = " << x << " y = " << y << " a = " << a << endl;
}
virtual void Save() override
{
ofstream out("Figure.txt", ios::app);
if (!out.is_open())
{
cerr << "error open file!" << endl;
}
else
{
out << "s" << x << endl;
out << "s" << y << endl;
out << "s" << a << endl;
out << "s-" << endl;
}
out.close();
}
virtual string Load() override
{
string line, str;
ifstream in("Figure.txt");
if (!in.is_open())
{
cerr << "Error open file!" << endl;
}
else
{
while (getline(in, line))
{
if (line.find('s') != string::npos)
{
line.erase(line.find('s'), 1);
str += line;
str += "\n";
}
}
in.close();
}
return str;
}
};
class Rectangle : public Shape { //FIGURE RECTANGLE
public:
Rectangle() :Shape() {}
Rectangle(double x, double y, double a, double b) :Shape(x, y, a, b) {}
virtual void Show() override
{
cout << "Rectangle ==>> " << "x = " << x << " y = " << y << " a = " << a << " b = " << b << endl;
}
virtual void Save() override
{
ofstream out("Figure.txt", ios::app);
if (!out.is_open())
{
cerr << "error open file!" << endl;
}
else
{
out << "r" << x << endl;
out << "r" << y << endl;
out << "r" << a << endl;
out << "r" << b << endl;
out << "r-" << endl;
}
out.close();
}
virtual string Load() override
{
string line, str;
ifstream in("Figure.txt");
if (!in.is_open())
{
cerr << "Error open file!" << endl;
}
else
{
while (getline(in, line))
{
if (line.find('r') != string::npos)
{
line.erase(line.find('r'), 1);
str += line;
str += "\n";
}
}
in.close();
}
return str;
}
};
class Circle : public Shape { // FIGURE CIRCLE
public:
Circle() :Shape() {}
Circle(double x, double y, double a) :Shape(x, y, a) {}
virtual void Show() override
{
cout << "Circle ==>> " << "x = " << x << " y = " << y << " r = " << a << endl;
}
virtual void Save() override
{
ofstream out("Figure.txt", ios::app);
if (!out.is_open())
{
cerr << "error open file!" << endl;
}
else
{
out << "c" << x << endl;
out << "c" << y << endl;
out << "c" << a << endl;
out << "c-" << endl;
}
out.close();
}
virtual string Load() override
{
string line, str;
ifstream in("Figure.txt");
if (!in.is_open())
{
cerr << "Error open file!" << endl;
}
else
{
while (getline(in, line))
{
if (line.find('c') != string::npos)
{
line.erase(line.find('c'), 1);
str += line;
str += "\n";
}
}
in.close();
}
return str;
}
};
class Ellipse : public Shape { // FIGURE ELLIPSE
public:
Ellipse() :Shape() {}
Ellipse(double x, double y, double a, double b) :Shape(x, y, a, b) {}
virtual void Show() override
{
cout << "Ellipse ==>> " << "x = " << x << " y = " << y << " a = " << a << " b = " << b << endl;
}
virtual void Save() override
{
ofstream out("Figure.txt", ios::app);
if (!out.is_open())
{
cerr << "error open file!" << endl;
}
else
{
out << "e" << x << endl;
out << "e" << y << endl;
out << "e" << a << endl;
out << "e" << b << endl;
out << "e-" << endl;
}
out.close();
}
virtual string Load() override
{
string line, str;
ifstream in("Figure.txt");
if (!in.is_open())
{
cerr << "Error open file!" << endl;
}
else
{
while (getline(in, line))
{
if (line.find('e') != string::npos)
{
line.erase(line.find('e'), 1);
str += line;
str += "\n";
}
}
in.close();
}
return str;
}
};
int main() {
TEXT_COLOR_GREEN
double x = 0, y = 0, a = 0, b = 0; // Paramtrs figurs
Shape *** f1 = new Shape **[4]; // The array that will be filled from the file
string str; // parametrs data from file
/*************************************************************/
//CREATE ARRAY AND SEND HIS AT FILE
/*************************************************************/
Square s(52, 33, 2);
Rectangle r(4, 3, 4, 1);
Circle c(2, 5, 1);
Ellipse e(4, 6, 4, 2);
Shape **f = new Shape*[4];
f[0] = &s;
f[1] = &r;
f[2] = &c;
f[3] = &e;
for (size_t i = 0; i < 4; i++) // SHOW DATA
f[i]->Show();
for (size_t i = 0; i < 4; i++) // SAVE AT FILE
f[i]->Save();
for (size_t i = 0; i < 4; i++) // SHOW ALL DATA FROM FILE
cout << f[i]->Load();
/*************************************************************/
//CREATE ARRAY AND GET DATA FROM FILE AND WRITE AT NEW ARRAY
/*************************************************************/
int tmpSIZE = 0, SIZE = 0;
str = f[0]->Load();
tmpSIZE = ValuieFigures(str); // GET SIZE
SIZE += tmpSIZE;
Square * s1 = new Square[tmpSIZE];
string tmpSTR;
int sizeSquare = 0, sizeRectangle = 0, sizeCircle = 0, sizeEllipse = 0;
for (size_t i = 0; i < tmpSIZE; i++) // GET ALL FIGURES ==>> Square IN FILE
{
//-----------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
s1[i].x = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
s1[i].y = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
s1[i].a = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
str = f[0]->Load();
str.erase(0, str.find('-') + 2);
}
//********************************************************************************
sizeSquare = tmpSIZE;
f1[0] = new Shape*[sizeSquare];
for (size_t i = 0; i < sizeSquare; i++)
f1[0][i] = &s1[i];
//********************************************************************************
tmpSIZE = 0, SIZE = 0;
str = f[1]->Load();
tmpSIZE = ValuieFigures(str); // GET SIZE
SIZE += tmpSIZE;
Rectangle * r1 = new Rectangle[tmpSIZE];
for (size_t i = 0; i < tmpSIZE; i++) // GET ALL FIGURES ==>> Rectangle IN FILE
{
//-----------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
r1[i].x = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
r1[i].y = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
r1[i].a = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
r1[i].b = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
str = f[1]->Load();
str.erase(0, str.find('-') + 2);
}
//********************************************************************************
sizeRectangle = tmpSIZE;
f1[1] = new Shape*[sizeRectangle];
for (size_t i = 0; i < sizeRectangle; i++)
f1[1][i] = &r1[i];
//********************************************************************************
tmpSIZE = 0, SIZE = 0;
str = f[2]->Load();
tmpSIZE = ValuieFigures(str); // GET SIZE
SIZE += tmpSIZE;
Circle * c1 = new Circle[tmpSIZE];
for (size_t i = 0; i < tmpSIZE; i++) // GET ALL FIGURES ==>> Circle IN FILE
{
//-----------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
c1[i].x = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
c1[i].y = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
c1[i].a = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
str = f[2]->Load();
str.erase(0, str.find('-') + 2);
}
//********************************************************************************
sizeCircle = tmpSIZE;
f1[2] = new Shape*[sizeCircle];
for (size_t i = 0; i < sizeCircle; i++)
f1[2][i] = &c1[i];
//********************************************************************************
tmpSIZE = 0, SIZE = 0;
str = f[3]->Load();
tmpSIZE = ValuieFigures(str); // GET SIZE
SIZE += tmpSIZE;
Ellipse * e1 = new Ellipse[tmpSIZE];
for (size_t i = 0; i < tmpSIZE; i++) // GET ALL FIGURES ==>> Ellipse IN FILE
{
//-----------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
e1[i].x = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
e1[i].y = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
e1[i].a = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
tmpSTR = str;
tmpSTR.erase(tmpSTR.find('\n'), tmpSTR.size());
e1[i].b = stod(tmpSTR);
tmpSTR = str;
str = tmpSTR.erase(0, tmpSTR.find('\n') + 1);
//------------------------------------------------------
str = f[3]->Load();
str.erase(0, str.find('-') + 2);
}
//********************************************************************************
sizeEllipse = tmpSIZE;
f1[3] = new Shape*[sizeEllipse];
for (size_t i = 0; i < sizeEllipse; i++)
f1[3][i] = &e1[i];
//********************************************************************************
// SHAPES DEMONSTRATION
//********************************************************************************
cout << "SHAPE SHOW Square" << endl;
for (size_t i = 0; i < sizeSquare; i++)
f1[0][i]->Show();
cout << "SHAPE Square END " << endl;
cout << endl;
cout << "SHAPE SHOW Rectangle" << endl;
for (size_t i = 0; i < sizeRectangle; i++)
f1[1][i]->Show();
cout << "SHAPE Rectangle END " << endl;
cout << endl;
cout << "SHAPE SHOW Circle" << endl;
for (size_t i = 0; i < sizeCircle; i++)
f1[2][i]->Show();
cout << "SHAPE Circle END " << endl;
cout << endl;
cout << "SHAPE SHOW Ellipse" << endl;
for (size_t i = 0; i < sizeEllipse; i++)
f1[3][i]->Show();
cout << "SHAPE Ellipse END " << endl;
cout << endl;
delete[] s1;
delete[] r1;
delete[] c1;
delete[] e1;
delete[] f;
for (int i = 0; i < 4; i++)
delete[] f1[i];
delete f1;
STOP_CMD_WINDOWS
return 0;
}
int ValuieFigures(string str) // Get the value of the shapes that repeat
{
int count = 0, value = 0;
while (str[count] != '\0')
{
if (str[count] == '-')
value++;
count++;
}
return value;
}