Untitled
unknown
c_cpp
2 years ago
1.8 kB
5
Indexable
//Напиши программу, которая заполняет вектор объектами класса Rectangle. Стороны каждого прямоугольника вводит пользователь. Сделай проверку ввода. // Отсортируй вектор с помощью функции sort (algorithm) по возрастанию площади. #include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <ctime> #include <windows.h> #include <algorithm> using namespace std; class MyRectangle { private: int a, b; public: int GetA() const { return a; } int GetB() const { return b; } void SetA( const int a ) { if ( a > 0 ) this->a = a; } void SetB( const int b ){ if ( b > 0 ) this->a = a; } //конструктор 2 в 1 MyRectangle(const int a = 1, const int b = 1): a(a), b(b) { } //вычисление площади int Area() const { return a * b; } }; void FillVec( vector <MyRectangle> &vec, const int count ); void PrintVec( const vector <MyRectangle> &vec ); int main() { SetConsoleCP( 1251 ); SetConsoleOutputCP( 1251 ); srand( time( 0 )); vector<MyRectangle> pVec; FillVec( pVec); PrintVec( pVec ); sort( pVec.begin(), pVec.end(), Ares ); PrintVec( pVec ); return 0; } void FillVec( vector <MyRectangle> &vec ){ for (int i = 0; i < count; ++i) { MyPoint p( rand() % 101 - 50, rand() % 101 - 50 ); vec.push_back( p ); } } void PrintVec( const vector <MyRectangle> &vec){ for( const MyPoint& p : vec ) cout << p << " "; }
Editor is loading...