Лабораторная работа №4 Вариант 16
.docЛипецкий государственный технический университет
Кафедра автоматизированных систем управления
Лабораторная работа №4
по объектно-ориентированному программированию
Контейнеры стандартной библиотеки
|
Студент |
|
|
|
Филатов А.А. |
|
||||||||
|
|
|
подпись, дата |
|
фамилия, инициалы |
|
||||||||
|
Группа |
|
АС-09-2 |
|
|
|
|
|||||||
|
|
|
|
|
|
|
||||||||
|
Принял |
|
|
|
|
|
||||||||
|
|
|
|
|
Овчинников В.В. |
|
||||||||
|
ученая степень, звание |
|
подпись, дата |
|
фамилия, инициалы |
|
Липецк 2012
1. Задание
Выполнить лабораторные №2 и №3 с использованием контейнеров стандартной библиотеки шаблонов языка C++ (вместо средств работы с динамической памятью).
Таблица 1. Вариант
Вид/ Вариант |
a/x+b |
a*ln(bx)+c |
16 |
+ |
+ |
-
UML-диаграмма классов
Рисунок 1. UML-диаграмма программы №1
Рисунок 1. UML-диаграмма программы №2
3. Исходный текст программы
3.1. Программа №1
#include <stdio.h>
#include <math.h>
#include <vector>
#include <iostream>
using namespace std;
//Ввод с клавиатуры значения типа double
double inputParamFromKeyboard()
{
double temp;
bool success = false;
do
{
cin.clear();
//очистка буферов
_flushall();
cin >> temp;
cout << endl;
if(cin.fail())
cout << "Incorrect input. Please try again: " << endl;
else
success = true;
}
while(!success); //контроль ввода
return temp;
}
class Func
{
public:
virtual double calculate(double x) = 0;
virtual void fillParam() = 0;
virtual void otputType() = 0;
};
class Func1 : public Func
{
public:
double calculate(double x)
{
return a/x+b;
}
void fillParam()
{
cout << "Input a: ";
a = inputParamFromKeyboard();
cout << "Input b: ";
b = inputParamFromKeyboard();
}
void otputType()
{
cout << a << "/x+" << b << endl;
}
private:
double a, b;
};
class Func2 : public Func
{
public:
double calculate(double x)
{
return a*log(b*x)+c;
}
void fillParam()
{
cout << "Input a: ";
a = inputParamFromKeyboard();
cout << "Input b: ";
b = inputParamFromKeyboard();
cout << "Input c: ";
c = inputParamFromKeyboard();
}
void otputType()
{
cout << a << "*log(" << b << "*x)+" << c << endl;
}
private:
double a, b, c;
};
class VectorFunction
{
public:
void fill()
{
int t=0;
bool quit = false, success = false;
do
{
int t;
do
{
cin.clear();
_flushall();
cout << "Choose type of function: a/x+b(1), a*ln(bx)+c(2) or end of adding(0): ";
cin >> t;
cout << endl;
if(cin.fail())
cout << "Incorrect input. Please try again: ";
else
success = true;
}
while(!success);
switch(t)
{
case 1:
vect.push_back(new Func1());
vect.at(vect.size()-1)->fillParam();
break;
case 2:
vect.push_back(new Func2());
vect.at(vect.size()-1)->fillParam();
break;
case 0:
quit = true;
break;
default:
break;
}
}
while(!quit); //контроль ввода
}
void calculateVector(double x)
{
cout << "Values:" << endl;
for(int i=0;i<vect.size();i++)
{
cout << i+1 << ": ";
cout << vect.at(i)->calculate(x);
cout << endl;
}
}
void outpputFuncVector()
{
cout << "Functions:" << endl;
for(int i=0;i<vect.size();i++)
{
cout << i+1 << ": ";
vect.at(i)->otputType();
}
}
~VectorFunction()
{
vect.~vector();
}
private:
vector<Func*> vect;
};
void main()
{
bool exit=false;
double t;
VectorFunction vector;
vector.fill();
while(exit!=true)
{
cout << "What to do: Run(1) or Exit(0)? " << endl;
t = inputParamFromKeyboard();
switch((int) t)
{
case 1:
vector.outpputFuncVector();
cout << endl << "Input X: ";
vector.calculateVector(inputParamFromKeyboard());
break;
case 0:
exit = true;
break;
}
}
}
3.2. Программа №2
#include <stdio.h>
#include <math.h>
#include <vector>
#include <iostream>
using namespace std;
//Ввод с клавиатуры значения типа double
class InputFromKeyboard
{
public:
static double inputDouble()
{
double temp;
bool success = false;
do
{
cin.clear();
//очистка буферов
_flushall();
cin >> temp;
cout << endl;
if(cin.fail())
cout << "Incorrect input. Please try again: " << endl;
else
success = true;
}
while(!success); //контроль ввода
return temp;
}
};
class Func1
{
public:
double calculate(double x)
{
return a/x+b;
}
void fillParam()
{
cout << "Input a: ";
a = InputFromKeyboard::inputDouble();
cout << "Input b: ";
b = InputFromKeyboard::inputDouble();
}
void otputType()
{
cout << a << "/x+" << b << endl;
}
private:
double a, b;
};
class Func2
{
public:
double calculate(double x)
{
return a*log(b*x)+c;
}
void fillParam()
{
cout << "Input a: ";
a = InputFromKeyboard::inputDouble();
cout << "Input b: ";
b = InputFromKeyboard::inputDouble();
cout << "Input c: ";
c = InputFromKeyboard::inputDouble();
}
void otputType()
{
cout << a << "*log(" << b << "*x)+" << c << endl;
}
private:
double a, b, c;
};
template <class T>
class VectorFunction
{
public:
void Fill()
{
int t=0;
bool quit = false, success = false;
do
{
vect.push_back(new T());
vect.at(vect.size()-1)->fillParam();
cout << "Add function(1) or end input(0)?";
t=(int) InputFromKeyboard::inputDouble();
}
while(t!=0); //контроль ввода
}
void calculateVector(double x)
{
cout << "Values:" << endl;
for(int i=0;i<vect.size();i++)
{
cout << i+1 << ": ";
cout << vect[i]->calculate(x);
cout << endl;
}
}
void outpputFuncVector()
{
cout << "Functions:" << endl;
for(int i=0;i<vect.size();i++)
{
cout << i+1 << ": ";
vect[i]->otputType();
}
}
~VectorFunction()
{
vect.~vector();
}
private:
vector<T*> vect;
};
void main()
{
bool exit=false;
double t = 0;
bool quit = false, success = false;
while(t!= 1 && t!=2)
{
cout << "Choose type of function: a/x+b(1), a*ln(bx)+c(2)? ";
t = InputFromKeyboard::inputDouble();
}
switch((int) t)
{
case 1:
{
VectorFunction<Func1> vect;
vect.Fill();
vect.outpputFuncVector();
cout << endl << "Input X: ";
vect.calculateVector(InputFromKeyboard::inputDouble());
}
break;
case 2:
{
VectorFunction<Func2> vect;
vect.Fill();
vect.outpputFuncVector();
cout << endl << "Input X: ";
vect.calculateVector(InputFromKeyboard::inputDouble());
}
break;
case 0:
quit = true;
break;
default:
break;
}
system("pause");
}
4. Контрольный пример
Рисунок 3. Пример работы программы №1
Рисунок 4. Пример работы программы №2
Вывод
При выполнении данной лабораторной работы были получены навыки работы с контейнерами стандартной библиотеки.
Контейнеры позволили не заботиться о выделении памяти и ее освобождении.