第九周。
This commit is contained in:
35
OOP/09/Exercise01.cpp
Normal file
35
OOP/09/Exercise01.cpp
Normal 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
20
OOP/09/Exercise02.cpp
Normal 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
16
OOP/09/Optional01.cpp
Normal 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;
|
||||
}
|
||||
78
OOP/test.cpp
78
OOP/test.cpp
@@ -1,52 +1,60 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user