第九周。

This commit is contained in:
unlockable
2023-04-18 14:52:58 +08:00
parent 30ae2dbc8c
commit f7dffc8e2c
4 changed files with 114 additions and 35 deletions

35
OOP/09/Exercise01.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <iomanip>
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 <<std::setfill('+') << std::setw(60) << "" << std::endl;
std::cout << "\n";
std::cout << setMiddle(60, " ", "Main menu") << "\n";
std::cout << setMiddle(60, " ", "1. Import data") << "\n";
std::cout << setMiddle(60, " ", "2. Search data") << "\n";
std::cout << setMiddle(60, " ", "3. Save data ") << "\n";
std::cout << setMiddle(60, " ", "4. Quit ") << "\n";
std::cout << setMiddle(60, " ", "Please Select:") << std::endl;
return 0;
}

20
OOP/09/Exercise02.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include <iostream>
#include <iomanip>
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;
}

16
OOP/09/Optional01.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include <iostream>
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;
}

View File

@@ -1,52 +1,60 @@
#include <iostream> #include <iostream>
class Complex { class A {
protected: protected:
int real, im; int a;
public: public:
Complex(); A(int _a) : a(_a) {
Complex(int real, int im); }
Complex(Complex &other);
void operator=(Complex &other);
Complex& operator+(Complex &other);
void display() { void display() {
std::cout << this->real << "+" << this->im << "i" << std::endl; std::cout << a << std::endl;
} }
}; };
Complex::Complex() { class B : virtual public A {
this->real = 0; protected:
this->im = 0; int b;
}
Complex::Complex(int newReal, int newIm) { public:
this->real = newReal; B(int _a, int _b) : A(_a+1), b(_b) {
this->im = newIm; }
} void display() {
A::display();
std::cout << b << std::endl;
}
};
Complex::Complex(Complex &other) { class C : virtual public A {
this->real = other.real; protected:
this->im = other.im; int c;
}
void Complex::operator=(Complex &other) { public:
this->real = other.real; C(int _a, int _c) : A(_a + 2), c(_c) {
this->im = other.im; }
} void display() {
A::display();
std::cout << c << std::endl;
}
};
Complex& Complex::operator+(Complex &other) { class D : public B, public C {
Complex tmp(this->real + other.real, this->im + other.im); protected:
return tmp; 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() { int main() {
Complex i1(1,2); D objD(1, 2, 3, 4);
Complex i2; objD.display();
int a[3][5] = {0};
int (*ptr)[5] = (int(*)[5]) a[3];
i2 = i1 + i1;
i2.display();
return 0; return 0;
} }