diff --git a/day2/homework/h1.cpp b/day2/homework/h1.cpp new file mode 100644 index 0000000..7d41b0a --- /dev/null +++ b/day2/homework/h1.cpp @@ -0,0 +1,13 @@ +// 找错 +#include + +using namespace std; + +int main() +{ + const int n = 10; + const int &p = n; + // p++; // 错误,不能修改常量引用所引用的值 + cout << "n=" << n << endl; + return 0; +} diff --git a/day2/homework/h2.cpp b/day2/homework/h2.cpp new file mode 100644 index 0000000..2e41628 --- /dev/null +++ b/day2/homework/h2.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int func1(int a, int, int b = 20) +{ + return a + b; +} + +int main() +{ + cout << func1(10, 30) << endl; + cout << func1(10, 10, 100) << endl; +} \ No newline at end of file diff --git a/day2/homework/h3.cpp b/day2/homework/h3.cpp new file mode 100644 index 0000000..82c08d1 --- /dev/null +++ b/day2/homework/h3.cpp @@ -0,0 +1,21 @@ +// 1. 2. 创建一个名为 Rectangle 的类,具有成员变量 width 和 height,以及成员函数 double calculateArea() ,用于计算矩形的面积。 +#include + +using namespace std; + +class Rectangle +{ +public: + double calculateArea() + { + return width * height; + } + + int width, height; +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h4.cpp b/day2/homework/h4.cpp new file mode 100644 index 0000000..b94cd93 --- /dev/null +++ b/day2/homework/h4.cpp @@ -0,0 +1,20 @@ +// 1. 2. 创建一个名为 Circle 的类,具有成员变量 radius 和 pi(圆周率),以及成员函数 double calculateArea() ,用于计算圆的面积。 +#include + +using namespace std; + +class Circle +{ +public: + double radius, pi = 3.1415926; + double calculateArea() + { + return pi * radius * radius; + } +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h5.cpp b/day2/homework/h5.cpp new file mode 100644 index 0000000..fa2f5b4 --- /dev/null +++ b/day2/homework/h5.cpp @@ -0,0 +1,24 @@ +// 1. 2. 创建一个名为 BankAccount 的类,具有成员变量 accountNumber 和 balance,以及成员函数 void deposit(double amount) 和 void withdraw(double amount) ,用于存款和取款操作。 +#include + +using namespace std; + +class BankAccount +{ +public: + long accountNumber, balance; + void deposit(double amount) + { + this->balance += amount; + } + void withdraw(double amount) + { + this->balance -= amount; + } +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h6.cpp b/day2/homework/h6.cpp new file mode 100644 index 0000000..3c2896b --- /dev/null +++ b/day2/homework/h6.cpp @@ -0,0 +1,21 @@ +// 创建一个名为 Student 的类,具有成员变量 name 和 grade,以及成员函数 void study(char *course) ,用于表示某班级的某学生正在学习某一门课程。 +#include + +using namespace std; + +class Student +{ +public: + char name[32]; // 学生姓名 + char grade[32]; // 班级名 + void study(char *course) + { + cout << this->grade << "班的" << this->name << "同学正在学习" << course << endl; + } +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h7.cpp b/day2/homework/h7.cpp new file mode 100644 index 0000000..c77af19 --- /dev/null +++ b/day2/homework/h7.cpp @@ -0,0 +1,35 @@ +// 创建一个名为 Car 的类,具有成员变量 brand 和 speed,以及成员函数 void accelerate() 和 void brake() ,用于加速和刹车操作。 +#include +#include + +using namespace std; + +class Car +{ +public: + string brand; + int speed; + void accelerate() + { + this->speed += 1; + cout << "加速 1 码" << endl; + } + void brake() + { + if (this->speed > 0) + { + this->speed -= 1; + cout << "减速中" << endl; + } + else + { + cout << "车已经停下了" << endl; + } + } +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h8.cpp b/day2/homework/h8.cpp new file mode 100644 index 0000000..29a7f68 --- /dev/null +++ b/day2/homework/h8.cpp @@ -0,0 +1,22 @@ +// 1. 2. 创建一个名为 Employee 的类,具有成员变量 name、id 和 salary,以及成员函数 void displayInfo() ,用于显示员工的信息。 +#include + +using namespace std; + +class Employee +{ +public: + string name; + int id; + int salary; + void displayinfo() + { + cout << "员工信息\nid: " << this->id << "\t姓名: " << this->name << "\t薪水: " << this->salary << endl; + } +}; + +int main() +{ + + return 0; +} diff --git a/day2/homework/h9.cpp b/day2/homework/h9.cpp new file mode 100644 index 0000000..32e1329 --- /dev/null +++ b/day2/homework/h9.cpp @@ -0,0 +1,58 @@ +// 创建一个名为 Book 的类,具有成员变量 title、author 和 year,以及成员函数 void displayInfo() ,用于显示图书的信息。 创建Book类的数组,至少存放5本书,并设计全局函数,实现Book对象按year从大到小排序,并显示排序后信息。 +#include + +using namespace std; + +class Book +{ +public: + string title, author; + int year; + // 使用 const 加 & 引用,避免不必要的拷贝 + void init(const string &title, const string &author, int year) + { + this->title = title; + this->author = author; + this->year = year; + } + void displayinfo() + { + cout << "书名: " << this->title << " , 作者: " << this->author << " , 出版年份: " << this->year << endl; + } +}; + +void sortAndShow(Book book[], int n) +{ + int i = 0; + for (; i < n; i++) + { + for (int j = 0; j < n - i - 1; j++) + { + if (book[j].year < book[j + 1].year) + { + Book tmp = book[j]; + book[j] = book[j + 1]; + book[j + 1] = tmp; + } + } + } + + for (i = 0; i < n; i++) + { + book[i].displayinfo(); + } +} + +int main() +{ + Book books[5]; + books[0].init("数学", "张三", 1998); + books[1].init("语文", "李四", 2009); + books[2].init("英语", "王五", 1987); + books[3].init("物理", "赵六", 2008); + books[4].init("化学", "郑七", 1982); + + sortAndShow(books, 5); + + return 0; +}