Lab1Ex2
unknown
c_cpp
4 years ago
2.2 kB
4
Indexable
#include <iostream> using namespace std; template <typename T> class Matrix { T m[5][7]; public: Matrix() {}; void AddValue(int row, int col, T Value) { m[row-1][col-1] = Value; } bool BelongTo(T Value) { for (int i = 0; i < 5; i++) { for (int j = 0; i < 7; i++) { if (m[i][j] == Value) return true; } } return false; } void PrintRow(int row) { for (int i = 0; i < 7; i++) { cout << m[row-1][i] << " "; } cout << endl; } void Print() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 7; j++) { cout << m[i][j] << " "; } cout << endl; } } T MaxValue() { T max = m[0][0]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 7; j++) { if ( m[i][j] > max) { max = m[i][j]; } } } return max; } }; class Date { public: int day, month, year; Date(int d = 1, int m =1, int y= 2000) { day = d; month = m; year = y; } bool operator>(const Date&d) { if (year > d.year) { return true; } if (month > d.month){ return true; } if ( day > d.month){ return true; } return false; } bool operator==(const Date& d) { if (year == d.year && month == d.month && day == d.day) { return true; } return false; } friend ostream& operator<<(ostream& os, const Date& dt); }; ostream& operator<<(ostream& os, const Date& dt) { os << dt.month << '/' << dt.day << '/' << dt.year; return os; } int main() { Matrix<int> m1; m1.AddValue(1,3, 5); cout << m1.BelongTo(2) << endl; m1.PrintRow(1); cout << "#############" << endl; m1.Print(); cout << m1.MaxValue() << endl; cout << "==================" << endl; Matrix<double> m2; m2.AddValue(1, 3, 4.5); cout << m2.BelongTo(2.5) << endl; m2.PrintRow(1); cout << "#############" << endl; m2.Print(); cout << m2.MaxValue() << endl; cout << "==================" << endl; Matrix<Date> m3; Date d1(1, 3, 2021); Date d2(2, 3, 2001); m3.AddValue(1, 3, d1); cout << m3.BelongTo(d2) << endl; m3.PrintRow(1); cout << "#############" << endl; m3.Print(); cout << m3.MaxValue() << endl; }
Editor is loading...