• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

CK1NG

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

程序设计实验5

实验任务1

实验任务2

1.

out << left; // 设置对齐方式为左对齐

out << item.rb << endl // 输出 `item.rb`

<< setw(15) << "售价:" << item.sales_price << endl // 输出售价

<< setw(15) << "销售数量:" << item.sales_amount << endl // 输出销售数量

<< setw(15) << "营收:" << item.revenue; // 输出营收

2.

利用sort函数sort(sales_lst.begin(), sales_lst.end(), compare_by_amount);

3.

创建一个私有成员对象Book rb;

实验任务3

 1 #pragma once
 2 #include<string>
 3 using namespace std;
 4 
 5 
 6 class MachinePets {
 7 protected:
 8 string nickname; // 昵称
 9 public:
10 // 带参数的构造函数
11 MachinePets(const string &s) : nickname(s) {}
12 
13 // 纯虚函数,提供宠物叫声的统一接口
14 virtual string talk() const=0;
15 
16 // 获取昵称的方法
17 string get_nickname() const { return nickname; }
18 
19 // 虚析构函数,确保派生类对象正确销毁
20 virtual ~MachinePets() {}
21 };
22 
23 // 宠物猫类
24 class PetCats : public MachinePets {
25 public:
26 // 带参数的构造函数
27 PetCats(const string &s) : MachinePets(s) {}
28 
29 // 实现宠物猫的叫声
30 string talk() const override {
31 return "miao wu~";
32 }
33 };
34 
35 // 宠物狗类
36 class PetDogs : public MachinePets {
37 public:
38 // 注意:这里的构造函数名应该是 PetDogs 而不是 PetCats
39 PetDogs(const string &s) : MachinePets(s) {}
40 
41 // 实现宠物狗的叫声
42 string talk() const override {
43 return "wang wang~";
44 }
45 };
 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<MachinePets *> pets;
 9 
10     pets.push_back(new PetCats("miku"));
11     pets.push_back(new PetDogs("da huang"));
12 
13     for(auto &ptr: pets)
14         cout <<ptr->get_nickname() << " says " << ptr->talk() << endl;
15 }
16 
17 int main() {
18     test();
19 }

实验任务4

 1 #pragma once
 2 
 3 #ifndef FILM_HPP
 4 #define FILM_HPP
 5 
 6 #include<iostream>
 7 #include<string>
 8 #include<iomanip>
 9 
10 using namespace std;
11 
12 class Film {
13 private:
14     string name;
15     string director;
16     string country;
17     int year;
18 public:
19     Film() {}
20     Film(const string& Name, const string& Director, const string& Country, const int& Year)
21         :name(Name), director(Director), country(Country), year(Year) {}
22 
23 
24     friend bool compare_by_year(const Film& f1, const Film& f2);
25     friend ostream& operator<<(ostream& out, const Film& f);
26     friend istream& operator>>(istream& in, Film& f);
27 
28 };
29 
30 istream& operator>>(istream& in, Film& f) {
31     cout << "录入片名:"; cin >> f.name;
32     cout << "录入导演:"; cin >> f.director;
33     cout << "录入制片国家/地区:"; cin >> f.country;
34     cout << "录入上映年份:"; cin >> f.year;
35     return in;
36 }
37 
38 ostream& operator<<(ostream& out, const Film& f) {
39 
40     out << left << setw(12) << f.name << setw(12) << f.director
41         << setw(12) << f.country << setw(12) << f.year;
42     return out;
43 }
44 
45 bool compare_by_year(const Film& f1, const Film& f2) {
46     return f1.year < f2.year;
47 }
48 
49 #endif  //FILM_HPP
50 #pragma once
 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9 
10     int n;
11     cout << "输入电影数目: ";
12     cin >> n;
13 
14     cout << "录入" << n << "部影片信息" << endl;
15     vector<Film> film_lst;
16     for (int i = 0; i < n; ++i) {
17         Film f;
18         cout << string(20, '-') << "第" << i + 1 << "部影片录入" << string(20, '-') << endl;
19         cin >> f;
20         film_lst.push_back(f);
21     }
22 
23     // 按发行年份升序排序
24     sort(film_lst.begin(), film_lst.end(), compare_by_year);
25 
26     cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=') << endl;
27     for (auto& f : film_lst)
28         cout << f << endl;
29 }
30 
31 int main() {
32     test();
33 }

实验任务5

 1 #pragma once
 2 
 3  #ifndef COMPLEX_HPP
 4  #define COMPLEX_HPP
 5 
 6  #include <iostream>
 7 
 8  template < typename T>
 9  class Complex {
10  private:
11          T real;
12          T imag;
13     
14  public:
15          // 默认构造函数
16              Complex(T r = 0, T i = 0) : real(r), imag(i) {}
17     
18              // 拷贝构造函数
19              Complex(const Complex & other) : real(other.real), imag(other.imag) {}
20     
21              // 获取实部
22              T get_real() const { return real; }
23     
24              // 获取虚部
25              T get_imag() const { return imag; }
26     
27              // += 运算符重载
28              Complex & operator+=(const Complex & other) {
29                  real += other.real;
30                  imag += other.imag;
31                  return *this;
32         
33     }
34     
35              // + 运算符重载
36              Complex operator+(const Complex & other) const {
37                  return Complex(real + other.real, imag + other.imag);
38         
39     }
40     
41              // == 运算符重载
42              bool operator==(const Complex & other) const {
43                  return real == other.real && imag == other.imag;
44         
45     }
46     
47              // 流插入运算符重载
48             friend std::ostream & operator<<(std::ostream & os, const Complex & c) {
49                  os << c.real;
50                  if (c.imag >= 0)
51                          os << " + " << c.imag << "i";
52                  else
53                         os << " - " << -c.imag << "i";
54                  return os;
55         
56     }
57     
58             // 流提取运算符重载
59              friend std::istream & operator>>(std::istream & is, Complex & c) {
60                  is >> c.real >> c.imag;
61                  return is;
62         
63     }
64     
65 };
66 
67  #endif // COMPLEX_HPP
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test1() {
10     Complex<int> c1(2, -5), c2(c1);
11 
12     cout << "c1 = " << c1 << endl;
13     cout << "c2 = " << c2 << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15 
16     c1 += c2;
17     cout << "c1 = " << c1 << endl;
18     cout << boolalpha << (c1 == c2) << endl;
19 }
20 
21 void test2() {
22     Complex<double> c1, c2;
23     cout << "Enter c1 and c2: ";
24     cin >> c1 >> c2;
25     cout << "c1 = " << c1 << endl;
26     cout << "c2 = " << c2 << endl;
27 
28     cout << "c1.real = " << c1.get_real() << endl;
29     cout << "c1.imag = " << c1.get_imag() << endl;
30 }
31 
32 int main() {
33     cout << "自定义类模板Complex测试1: " << endl;
34     test1();
35 
36     cout << endl;
37 
38     cout << "自定义类模板Complex测试2: " << endl;
39     test2();
40 }

实验任务6

 

posted on 2024-12-06 16:12  CK1NG  阅读(24)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3