第五次作业。
This commit is contained in:
104
OOP/05/Exercise01.cpp
Normal file
104
OOP/05/Exercise01.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
|
||||
enum sexType { male, female, unknown };
|
||||
|
||||
class Date {
|
||||
private:
|
||||
int day, month, year;
|
||||
friend std::ostream &operator<<(std::ostream &out, Date date);
|
||||
|
||||
public:
|
||||
Date(int d = 1, int m = 1, int y = 1970);
|
||||
};
|
||||
|
||||
Date::Date(int d, int m, int y) : day(d), month(m), year(y){};
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, Date date) {
|
||||
out << date.month << "/" << date.day << "/" << date.year;
|
||||
return out;
|
||||
}
|
||||
|
||||
class People {
|
||||
private:
|
||||
std::string name;
|
||||
int number;
|
||||
sexType sex;
|
||||
Date birthday;
|
||||
std::string id;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &output, People &ppl);
|
||||
|
||||
friend std::istream &operator>>(std::istream &input, People &ppl);
|
||||
|
||||
public:
|
||||
People(std::string name = "undefined", int number = 0,
|
||||
sexType sex = unknown, int birthdayDay = 1, int birthdayMonth = 1,
|
||||
int birthdayYear = 1970, std::string id = "undefined");
|
||||
};
|
||||
|
||||
People::People(std::string newName, int newNumber, sexType newSex,
|
||||
int birthdayDay, int birthdayMonth, int birthdayYear,
|
||||
std::string newID)
|
||||
: name(newName), number(newNumber), sex(newSex),
|
||||
birthday(birthdayDay, birthdayMonth, birthdayYear), id(newID){};
|
||||
|
||||
std::ostream &operator<<(std::ostream &output, People &ppl) {
|
||||
output << "Name: " << ppl.name << std::endl;
|
||||
output << "Employee number: " << ppl.number << std::endl;
|
||||
output << "Sex: "
|
||||
<< (ppl.sex == male ? "Male"
|
||||
: (ppl.sex == female ? "Female" : "Unknown"))
|
||||
<< std::endl;
|
||||
output << "Birthday: " << ppl.birthday << std::endl;
|
||||
output << "ID: " << ppl.id << std::endl;
|
||||
return output;
|
||||
}
|
||||
|
||||
std::istream &operator>>(std::istream &input, People &ppl) {
|
||||
std::cout << "Name: ";
|
||||
input >> ppl.name;
|
||||
|
||||
std::cout << "Employee No.: ";
|
||||
input >> ppl.number;
|
||||
|
||||
std::string sex;
|
||||
std::cout << "Sex (m/w): ";
|
||||
input >> sex;
|
||||
if (sex == "m") {
|
||||
ppl.sex = male;
|
||||
}
|
||||
else if (sex == "w") {
|
||||
ppl.sex = female;
|
||||
}
|
||||
else {
|
||||
ppl.sex = unknown;
|
||||
}
|
||||
|
||||
int d, m, y;
|
||||
std::cout << "Birthday day: ";
|
||||
input >> d;
|
||||
std::cout << "Birthday month: ";
|
||||
input >> m;
|
||||
std::cout << "Birthday year: ";
|
||||
input >> y;
|
||||
ppl.birthday = Date(d, m, y);
|
||||
|
||||
std::cout << "ID number: ";
|
||||
input >> ppl.id;
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
int main() {
|
||||
People* a = new People[2];
|
||||
for (int i = 0; i < 2; i++) {
|
||||
std::cout << "####" << std::endl << "Update people No. "<< i + 1 << std::endl << "####" << std::endl;
|
||||
std::cin >> a[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
std::cout << a[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
238
OOP/05/Exercise02.cpp
Normal file
238
OOP/05/Exercise02.cpp
Normal file
@@ -0,0 +1,238 @@
|
||||
#include <assert.h>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
class Matrix {
|
||||
private:
|
||||
int rowSize, columnSize;
|
||||
int *content;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &output, Matrix &mat);
|
||||
friend std::istream &operator>>(std::istream &input, Matrix &mat);
|
||||
friend Matrix& operator*(int lambda, Matrix& mat);
|
||||
|
||||
public:
|
||||
Matrix();
|
||||
Matrix(int newRowSize, int newColumSize);
|
||||
Matrix(Matrix &otherMat);
|
||||
~Matrix();
|
||||
|
||||
Matrix &operator*(Matrix &otherMatrix);
|
||||
Matrix &operator*(int lambda);
|
||||
int &operator()(int row, int column);
|
||||
Matrix &operator=(Matrix &otherMatrix);
|
||||
Matrix &operator+(Matrix &otherMatrix);
|
||||
bool operator==(Matrix &otherMatrix);
|
||||
|
||||
int getRowSize();
|
||||
int getColumnSize();
|
||||
|
||||
Matrix &transformation();
|
||||
};
|
||||
|
||||
Matrix::Matrix() {
|
||||
this->rowSize = 0;
|
||||
this->columnSize = 0;
|
||||
this->content = NULL;
|
||||
}
|
||||
|
||||
Matrix::Matrix(int newRowSize, int newColumnSize) {
|
||||
this->rowSize = newRowSize;
|
||||
this->columnSize = newColumnSize;
|
||||
this->content = new int[newRowSize * newColumnSize];
|
||||
for (int i = 0; i < newRowSize * newColumnSize; i++) {
|
||||
this->content[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix::Matrix(Matrix &otherMat) {
|
||||
this->rowSize = otherMat.getRowSize();
|
||||
this->columnSize = otherMat.getColumnSize();
|
||||
this->content = new int[this->rowSize * this->columnSize];
|
||||
for (int r = 0; r < this->rowSize; r++) {
|
||||
for (int c = 0; c < this->columnSize; c++) {
|
||||
(*this)(r, c) = otherMat(r, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Matrix::~Matrix() {
|
||||
delete[] this->content;
|
||||
}
|
||||
|
||||
int &Matrix::operator()(int row, int column) {
|
||||
assert(row >= 0 && row < this->rowSize && column >= 0 &&
|
||||
column < this->columnSize);
|
||||
return this->content[row * this->columnSize + column];
|
||||
}
|
||||
|
||||
Matrix &Matrix::operator*(Matrix &otherMatrix) {
|
||||
if (this->columnSize != otherMatrix.getRowSize()) {
|
||||
std::cout << "The size of these two matrix does not allow "
|
||||
"multiplying. The matrix on the left has "
|
||||
<< this->columnSize
|
||||
<< " columns, whereas the matrix on the right has "
|
||||
<< otherMatrix.getRowSize() << " rows. " << std::endl;
|
||||
return *(new Matrix());
|
||||
}
|
||||
|
||||
int resultRowSize = this->rowSize,
|
||||
resultColumnSize = otherMatrix.getColumnSize();
|
||||
Matrix *resultMat = new Matrix(resultRowSize, resultColumnSize);
|
||||
int tmpSum;
|
||||
for (int resultRow = 0; resultRow < resultRowSize; resultRow++) {
|
||||
for (int resultColumn = 0; resultColumn < resultColumnSize;
|
||||
resultColumn++) {
|
||||
tmpSum = 0;
|
||||
for (int p = 0; p < this->columnSize; p++) {
|
||||
tmpSum += (*this)(resultRow, p) * otherMatrix(p, resultColumn);
|
||||
}
|
||||
(*resultMat)(resultRow, resultColumn) = tmpSum;
|
||||
}
|
||||
}
|
||||
|
||||
return *resultMat;
|
||||
}
|
||||
|
||||
Matrix &Matrix::operator*(int lambda) {
|
||||
return lambda * (*this);
|
||||
}
|
||||
|
||||
Matrix &Matrix::operator=(Matrix &otherMat) {
|
||||
if (NULL != this->content) {
|
||||
delete[] this->content;
|
||||
}
|
||||
this->rowSize = otherMat.getRowSize();
|
||||
this->columnSize = otherMat.getColumnSize();
|
||||
this->content = new int[this->rowSize * this->columnSize];
|
||||
for (int r = 0; r < this->rowSize; r++) {
|
||||
for (int c = 0; c < this->columnSize; c++) {
|
||||
(*this)(r, c) = otherMat(r, c);
|
||||
}
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Matrix &Matrix::operator+(Matrix &otherMat) {
|
||||
if (this->rowSize != otherMat.getRowSize() ||
|
||||
this->columnSize != otherMat.getColumnSize()) {
|
||||
std::cout << "The size of these two does not allow adding. The matrix "
|
||||
"on the left is "
|
||||
<< this->rowSize << "x" << this->columnSize
|
||||
<< " whereas the matrix on the right is "
|
||||
<< otherMat.getRowSize() << "x" << otherMat.getColumnSize()
|
||||
<< "." << std::endl;
|
||||
return *(new Matrix());
|
||||
}
|
||||
|
||||
Matrix* result = new Matrix(*this);
|
||||
|
||||
for (int r = 0; r < this->rowSize; r++) {
|
||||
for (int c = 0; c < this->columnSize; c++) {
|
||||
(*result)(r, c) += otherMat(r, c);
|
||||
}
|
||||
}
|
||||
return (*result);
|
||||
}
|
||||
|
||||
bool Matrix::operator==(Matrix &otherMat) {
|
||||
if (this->rowSize != otherMat.getRowSize() ||
|
||||
this->columnSize != otherMat.getColumnSize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int r = 0; r < this->rowSize; r++) {
|
||||
for (int c = 0; c < this->columnSize; c++) {
|
||||
if ((*this)(r, c) != otherMat(r, c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int Matrix::getRowSize() {
|
||||
return this->rowSize;
|
||||
}
|
||||
|
||||
int Matrix::getColumnSize() {
|
||||
return this->columnSize;
|
||||
}
|
||||
|
||||
Matrix &Matrix::transformation() {
|
||||
Matrix *result = new Matrix(this->columnSize, this->rowSize);
|
||||
for (int r = 0; r < this->rowSize; r++) {
|
||||
for (int c = 0; c < this->columnSize; c++) {
|
||||
(*result)(c, r) = (*this)(r, c);
|
||||
}
|
||||
}
|
||||
return (*result);
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &output, Matrix &mat) {
|
||||
output << "┌";
|
||||
for (int i = 0; i < mat.columnSize * 5 + 1; i++) {
|
||||
output << " ";
|
||||
}
|
||||
output << "┐" << std::endl;
|
||||
for (int r = 0; r < mat.rowSize; r++) {
|
||||
output << "│ ";
|
||||
for (int c = 0; c < mat.columnSize; c++) {
|
||||
output << std::setw(4) << std::setfill(' ') << mat(r, c) << " ";
|
||||
}
|
||||
output << "│" << std::endl;
|
||||
}
|
||||
|
||||
output << "└";
|
||||
for (int i = 0; i < mat.columnSize * 5 + 1; i++) {
|
||||
output << " ";
|
||||
}
|
||||
output << "┘" << std::endl;
|
||||
return output;
|
||||
}
|
||||
|
||||
std::istream &operator>>(std::istream &input, Matrix &mat) {
|
||||
std::cout << "Start input a " << mat.rowSize << "x" << mat.columnSize
|
||||
<< " matrix:" << std::endl;
|
||||
for (int r = 0; r < mat.rowSize; r++) {
|
||||
for (int c = 0; c < mat.columnSize; c++) {
|
||||
std::cout << r << ", " << c << " (original: " << mat(r, c) << "): ";
|
||||
input >> mat(r, c);
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
Matrix& operator*(int lambda, Matrix& mat) {
|
||||
Matrix* result = new Matrix(mat);
|
||||
for (int r = 0; r < mat.getRowSize(); r++) {
|
||||
for (int c = 0; c < mat.getColumnSize(); c++) {
|
||||
(*result)(r, c) *= lambda;
|
||||
}
|
||||
}
|
||||
return (*result);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rowSize, columnSize;
|
||||
std::cout << "Input the size of the matrix: m x n" << std::endl;
|
||||
std::cin >> rowSize >> columnSize;
|
||||
Matrix a(rowSize, columnSize);
|
||||
Matrix b;
|
||||
std::cout << a << b;
|
||||
std::cin >> a;
|
||||
b = a.transformation();
|
||||
Matrix c = a * b;
|
||||
// clang-format off
|
||||
std::cout << "a =\n" << a
|
||||
<< "b =\n" << b
|
||||
<< "a * 3 =\n" << a * 3
|
||||
<< "a * b =\n"<< a * b
|
||||
<< "a * aT =\n" << a * a.transformation()
|
||||
<< "a * a =\n" << a * a
|
||||
<< "a * b == c: " << (a * b == c) << std::endl
|
||||
<< "a == c: " << (a == c)
|
||||
<< std::endl;
|
||||
// clang-format on
|
||||
return 0;
|
||||
}
|
||||
116
OOP/05/Optional01.cpp
Normal file
116
OOP/05/Optional01.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <iostream>
|
||||
|
||||
class Teacher {
|
||||
private:
|
||||
int number;
|
||||
std::string name;
|
||||
std::string sex;
|
||||
int salary;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &output, Teacher &teacher);
|
||||
friend std::istream &operator>>(std::istream &input, Teacher &teacher);
|
||||
|
||||
public:
|
||||
Teacher();
|
||||
int getNumber() {
|
||||
return this->number;
|
||||
}
|
||||
|
||||
std::string getName() {
|
||||
return this->name;
|
||||
}
|
||||
|
||||
std::string getSex() {
|
||||
return this->sex;
|
||||
}
|
||||
|
||||
int getSalary() {
|
||||
return this->salary;
|
||||
}
|
||||
};
|
||||
|
||||
class Student {
|
||||
private:
|
||||
int number;
|
||||
std::string name;
|
||||
std::string sex;
|
||||
int grade;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &output, Student &stu);
|
||||
friend std::istream &operator>>(std::istream &input, Student &stu);
|
||||
|
||||
public:
|
||||
Student();
|
||||
Student(Teacher &teacher);
|
||||
};
|
||||
|
||||
Teacher::Teacher() {
|
||||
this->number = 0;
|
||||
this->name = "undefined";
|
||||
this->sex = "unknown";
|
||||
this->salary = 0;
|
||||
}
|
||||
|
||||
Student::Student() {
|
||||
this->number = 0;
|
||||
this->name = "undefined";
|
||||
this->sex = "unknown";
|
||||
this->grade = 1;
|
||||
}
|
||||
|
||||
Student::Student(Teacher& teacher) {
|
||||
this->number = teacher.getNumber();
|
||||
this->name = teacher.getName();
|
||||
this->sex = teacher.getSex();
|
||||
this->grade = 1;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &output, Teacher &teacher) {
|
||||
output << "Number: " << teacher.number << std::endl
|
||||
<< "Name: " << teacher.name << std::endl
|
||||
<< "Sex: " << teacher.sex << std::endl
|
||||
<< "Salary: " << teacher.salary << std::endl;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::istream &operator>>(std::istream &input, Teacher &teacher) {
|
||||
std::cout << "Number: ";
|
||||
input >> teacher.number;
|
||||
std::cout << "Name: ";
|
||||
input >> teacher.name;
|
||||
std::cout << "Sex: ";
|
||||
input >> teacher.sex;
|
||||
std::cout << "Salary: ";
|
||||
input >> teacher.salary;
|
||||
return input;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &output, Student &stu) {
|
||||
output << "Number: " << stu.number << std::endl
|
||||
<< "Name: " << stu.name << std::endl
|
||||
<< "Sex: " << stu.sex << std::endl
|
||||
<< "Grade: " << stu.grade << std::endl;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
std::istream &operator>>(std::istream &input, Student &stu) {
|
||||
std::cout << "Number: ";
|
||||
input >> stu.number;
|
||||
std::cout << "Name: ";
|
||||
input >> stu.name;
|
||||
std::cout << "Sex: ";
|
||||
input >> stu.sex;
|
||||
std::cout << "Grade: ";
|
||||
input >> stu.grade;
|
||||
return input;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Teacher tea;
|
||||
std::cin >> tea;
|
||||
Student stu = tea;
|
||||
std::cout << tea << std::endl << stu;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user