day6: homework

This commit is contained in:
flykhan 2023-08-01 09:07:49 +08:00
parent e5577ed3e5
commit 08a390b0b1
7 changed files with 314 additions and 0 deletions

20
day6/homework/h1.cpp Normal file
View File

@ -0,0 +1,20 @@
// 编写一个模板函数 maxValue接受两个参数并返回其中较大的值。
// 【提示】T maxVal(T &v1, T &v2)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T maxVal(T &v1, T &v2)
{
return v1 > v2 ? v1 : v2;
}
int main()
{
int a = 2, b = 5;
cout << "更大的值是: " << maxVal(a, b) << endl;
return 0;
}

29
day6/homework/h2.cpp Normal file
View File

@ -0,0 +1,29 @@
// 编写一个模板函数 maxValueInArray接受一个数组和数组的大小并返回数组中的最大值。
//【提示】T maxValueInArray(T arr[], int size)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T maxValueInArray(T arr[], int size)
{
T max = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr1[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
char arr2[] = {'a', 'c', 'e', 'G', 'I', 'b', 'd', 'f', 'h', 'j'};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
cout << "arr1 的最大值是: " << maxValueInArray(arr1, size1) << endl;
cout << "arr2 的最大值是: " << maxValueInArray(arr2, size2) << endl;
return 0;
}

40
day6/homework/h3.cpp Normal file
View File

@ -0,0 +1,40 @@
// 创建一个基类 Person其中包含一个纯虚函数 display() ,用于显示人的信息。从 Person 派生出 Student 类和 Teacher 类,分别实现不同类型人的信息显示。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Person
{
public:
virtual void display() = 0;
};
class Student : public Person
{
public:
// 子类的 virtual 函数可以不写 virtual 关键字
virtual void display()
{
cout << "Student" << endl;
}
};
class Teacher : public Person
{
public:
virtual void display()
{
cout << "Teacher" << endl;
}
};
int main()
{
Person *p = new Student();
p->display();
p = new Teacher();
p->display();
return 0;
}

39
day6/homework/h4.cpp Normal file
View File

@ -0,0 +1,39 @@
// 创建一个基类 Animal其中包含一个纯虚函数 move() ,用于描述动物的移动方式。从 Animal 派生出 Bird 类和 Fish 类,分别实现不同类型动物的移动方式。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
public:
virtual void move() = 0;
};
class Bird : public Animal
{
public:
void move()
{
cout << "Bird" << endl;
}
};
class Fish : public Animal
{
public:
void move()
{
cout << "Fish" << endl;
}
};
int main()
{
Animal *p = new Bird;
p->move();
p = new Fish;
p->move();
return 0;
}

39
day6/homework/h5.cpp Normal file
View File

@ -0,0 +1,39 @@
// 创建一个基类 Shape其中包含一个纯虚函数 draw() ,用于绘制形状。从 Shape 派生出 Rectangle 类和 Circle 类,分别实现不同形状的绘制方法
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw()
{
cout << "Rectangle" << endl;
}
};
class Circle : public Shape
{
public:
void draw()
{
cout << "Circle" << endl;
}
};
int main()
{
Shape *p = new Rectangle;
p->draw();
p = new Circle;
p->draw();
return 0;
}

55
day6/homework/h6.cpp Normal file
View File

@ -0,0 +1,55 @@
// 创建一个基类 Shape其中包含一个纯虚函数 perimeter() ,用于计算形状的周长。从 Shape 派生出 Triangle 类和 Square 类,分别实现计算三角形和正方形的周长的函数。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
// 用于计算形状的周长
virtual double perimeter() = 0;
};
class Triangle : public Shape
{
private:
double a, b, c;
public:
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
public:
double perimeter()
{
return a + b + c;
}
};
class Square : public Shape
{
private:
double a;
public:
Square(double a) : a(a) {}
public:
double perimeter()
{
return 4 * a;
}
};
int main()
{
Shape *p = new Triangle(3, 4, 5);
cout << "三角形的周长是: " << p->perimeter() << endl;
p = new Square(5);
cout << "正方形的周长是: " << p->perimeter() << endl;
return 0;
}

92
day6/homework/h7.cpp Normal file
View File

@ -0,0 +1,92 @@
// 继第6题 基于多态方式定义一个全局函数排序函数接收Shape类的数组并按形状的周长进行从大到少排序。
// 【提示】全局函数排序函数 void sort(Shape *arr, int size)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
// 用于计算形状的周长
virtual double perimeter() = 0;
};
class Triangle : public Shape
{
private:
double a, b, c;
public:
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
public:
double perimeter()
{
return a + b + c;
}
};
class Square : public Shape
{
private:
double a;
public:
Square(double a) : a(a) {}
public:
double perimeter()
{
return 4 * a;
}
};
void sort(Shape *arr[], int size) // 这里为什么传指针数组?因为 Shape 是抽象类,不能实例化对象,只能通过指针来操作
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
// 比较形状的周长
if (arr[j]->perimeter() < arr[j + 1]->perimeter())
{
// 交换位置
swap(arr[j], arr[j + 1]);
}
}
}
}
int main()
{
Shape *p = new Triangle(3, 4, 5);
Shape *q = new Square(5);
Shape *r = new Triangle(6, 8, 10);
Shape *s = new Square(10);
Shape *t = new Triangle(5, 12, 13);
Shape *u = new Square(13);
// Shape *arr[6];
// arr[0] = p;
// arr[1] = q;
Shape *arr[] = {p, q, r, s, t, u}; // arr作为一个指针数组存了6个指向 Shape 类对象的指针
int len = sizeof(arr) / sizeof(arr[0]);
cout << "排序前:" << endl;
for (int i = 0; i < len; i++)
{
cout << arr[i]->perimeter() << endl;
}
sort(arr, len);
cout << "排序后:" << endl;
for (int i = 0; i < len; i++)
{
cout << arr[i]->perimeter() << endl;
}
return 0;
}