From f7dffc8e2c9cd47a58438f28582610105ef3c2ef Mon Sep 17 00:00:00 2001 From: unlockable Date: Tue, 18 Apr 2023 14:52:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E5=91=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/09/Exercise01.cpp | 35 +++++++++++++++++++ OOP/09/Exercise02.cpp | 20 +++++++++++ OOP/09/Optional01.cpp | 16 +++++++++ OOP/test.cpp | 78 ++++++++++++++++++++++++------------------- 4 files changed, 114 insertions(+), 35 deletions(-) create mode 100644 OOP/09/Exercise01.cpp create mode 100644 OOP/09/Exercise02.cpp create mode 100644 OOP/09/Optional01.cpp diff --git a/OOP/09/Exercise01.cpp b/OOP/09/Exercise01.cpp new file mode 100644 index 0000000..bd4cb1b --- /dev/null +++ b/OOP/09/Exercise01.cpp @@ -0,0 +1,35 @@ +#include +#include + +std::string setMiddle(int width, std::string fillChar, std::string originString) { + int len = originString.length(); + if (len >= width) { + return originString; + } + int fill = (width - len) / 2; + std::string result = ""; + for (int i = 0; i < fill; i++) { + result += fillChar; + } + result += originString; + for (int i = result.length(); i < width; i++) { + result += fillChar; + } + return result; +} + +int main() { + std::cout << std::setfill('+') << std::setw(60) << "" << std::endl; + std::cout << "+" << setMiddle(58, " ", "Contoso Company Employee Management System") << "+" << std::endl; + std::cout < +#include + +int main() { + double x = 123.456; + std::cout.width(10); + std::cout.setf(std::ios::dec, std::ios::basefield); + std::cout << x << std::endl; + std::cout.setf(std::ios::left); + std::cout << x << std::endl; + std::cout.width(15); + std::cout.setf(std::ios::right); + std::cout << x << std::endl; + std::cout.setf(std::ios::showpos); + std::cout << x << std::endl; + std::cout << -x << std::endl; + std::cout.setf(std::ios::scientific); + std::cout << x << std::endl; + return 0; +} \ No newline at end of file diff --git a/OOP/09/Optional01.cpp b/OOP/09/Optional01.cpp new file mode 100644 index 0000000..f12e9b0 --- /dev/null +++ b/OOP/09/Optional01.cpp @@ -0,0 +1,16 @@ +#include + +int main() { + std::string str1; + char str2[101], str3[101]; + std::cin >> str1; + std::cin.ignore(100, '\n'); + + std::cin.get(str2, 100, '\n'); + std::cin.ignore(); + + std::cin.getline(str3, 100, '\n'); + + std::cout << "str1:\n" << str1 << "\nstr2:\n" << str2 << "\nstr3:\n" << str3 << std::endl; + return 0; +} \ No newline at end of file diff --git a/OOP/test.cpp b/OOP/test.cpp index fcddd09..555b04a 100644 --- a/OOP/test.cpp +++ b/OOP/test.cpp @@ -1,52 +1,60 @@ #include -class Complex { +class A { protected: - int real, im; + int a; public: - Complex(); - Complex(int real, int im); - Complex(Complex &other); - - void operator=(Complex &other); - Complex& operator+(Complex &other); + A(int _a) : a(_a) { + } void display() { - std::cout << this->real << "+" << this->im << "i" << std::endl; + std::cout << a << std::endl; } }; -Complex::Complex() { - this->real = 0; - this->im = 0; -} +class B : virtual public A { +protected: + int b; -Complex::Complex(int newReal, int newIm) { - this->real = newReal; - this->im = newIm; -} +public: + B(int _a, int _b) : A(_a+1), b(_b) { + } + void display() { + A::display(); + std::cout << b << std::endl; + } +}; -Complex::Complex(Complex &other) { - this->real = other.real; - this->im = other.im; -} +class C : virtual public A { +protected: + int c; -void Complex::operator=(Complex &other) { - this->real = other.real; - this->im = other.im; -} +public: + C(int _a, int _c) : A(_a + 2), c(_c) { + } + void display() { + A::display(); + std::cout << c << std::endl; + } +}; -Complex& Complex::operator+(Complex &other) { - Complex tmp(this->real + other.real, this->im + other.im); - return tmp; -} +class D : public B, public C { +protected: + int d; + +public: + D(int _a, int _b, int _c, int _d) :A(_a), B(_a, _b), C(_a, _c), d(_d){}; + + void display() { + A::display(); + B::display(); + C::display(); + std::cout << d << std::endl; + } +}; int main() { - Complex i1(1,2); - Complex i2; - int a[3][5] = {0}; - int (*ptr)[5] = (int(*)[5]) a[3]; - i2 = i1 + i1; - i2.display(); + D objD(1, 2, 3, 4); + objD.display(); return 0; } \ No newline at end of file