C++课后作业 23. 模板课堂练习

C++课后作业 23. 模板课堂练习,第1张

文章目录
    • 原题题目
    • 代码实现
    • 提交结果


原题题目


代码实现
#include 
#include 

using namespace std;

class Complex {
 public:
  Complex() : real_(0), imag_(0) {}
  Complex(const double& real, const double& imag)
      : real_(real), imag_(imag) {}

  void input() {
    cin >> real_ >> imag_;
  }

  operator double() { return sqrt(real_ * real_ + imag_ * imag_); }
  Complex operator- (const Complex& other) {
    return Complex(real_ - other.real_, imag_ - other.imag_);
  }

 private:
  double real_;
  double imag_;
};

template <typename T>
double dist(T a, T b) {
  return b - a;
}

int main() {
  int a1, b1;
  double a2, b2;
  Complex a3, b3;
  int type;
  cin >> type;

  if (type == 1) {
    cin >> a1 >> b1;
    cout << dist(a1, b1) << endl;
  }
  else if (type == 2) {
    cin >> a2 >> b2;
    cout << dist(a2, b2) << endl;
  }
  else {
    a3.input();
    b3.input();
    cout << dist(a3, b3) << endl;
  }
  return 0;
}

提交结果

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1295831.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存