鱼C论坛

 找回密码
 立即注册
查看: 445|回复: 1

[已解决]裁判测试程序

[复制链接]
发表于 2023-12-19 22:54:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
已知一个名为Complex的复数类,这个类包含:
(1)私有成员:实部、虚部,且均为int 型
(2)公有的带默认形参值的构造函数、复制构造函数
(3)公有成员函数Display,其作用为显示复数
要求:
(1)实现满足上述属性和行为的Complex类定义;
(2)设计函数AddComplex,函数AddComplex功能为实现两个复数相加,要求该函数的形参为复数类的常引用;
(3)保证如下主函数能正确运行。
裁判测试程序样例:

/* 请在这里填写答案 */

int main(){
    int x,y;
    cin>>x>>y;
        Complex  c0(x,y);
        Complex  c1(c0);
        cout<<"c1 is: ";
        c1.Display();
        cin>>x>>y;
        Complex c2(x,y);
        cout<<"c2 is: ";
        c2.Display();
        Complex c3;
        c3 = AddComplex(c1,c2);
        cout<<"c3 is: ";
        c3.Display();   
        return 0;
}



输入样例:
在这里给出一组输入。例如:

2 -3
3 4
输出样例:
在这里给出相应的输出。例如:

c1 is: 2-3i
c2 is: 3+4i
c3 is: 5+1i

这题代码怎么写
最佳答案
2023-12-19 22:54:21
  1. #include <iostream>
  2. using namespace std;
  3. class Complex {
  4. private:
  5.     int real;
  6.     int imag;
  7. public:
  8.     Complex(int r = 0, int i = 0) : real(r), imag(i) {}
  9.     Complex(const Complex& c) : real(c.real), imag(c.imag) {}
  10.    
  11.     void Display() {
  12.         cout << real << (imag >= 0 ? "+" : "-") << abs(imag) << "i" << endl;
  13.     }
  14.    
  15.     friend Complex AddComplex(const Complex& c1, const Complex& c2);
  16. };
  17. Complex AddComplex(const Complex& c1, const Complex& c2) {
  18.     int r = c1.real + c2.real;
  19.     int i = c1.imag + c2.imag;
  20.     return Complex(r, i);
  21. }
  22. int main() {
  23.     int x, y;
  24.     cin >> x >> y;
  25.     Complex c0(x, y);
  26.     Complex c1(c0);
  27.     c0.Display();
  28.    
  29.     cin >> x >> y;
  30.     Complex c2(x, y);
  31.     c2.Display();
  32.    
  33.     Complex c3 = AddComplex(c0, c2);
  34.     c3.Display();
  35.    
  36.     return 0;
  37. }
复制代码

代码解析:
- 首先定义了一个Complex类,包含私有成员real(实部)和imag(虚部),默认值均为0。公有成员包括:带有默认形参值的构造函数、复制构造函数和Display函数。
- 接下来定义了一个友元函数AddComplex,用来实现两个复数相加。该函数传入两个复数类的常引用,返回一个新的Complex对象。
- 在主函数中,先读取两个整数作为第一个复数的实部和虚部,并创建一个对应的Complex对象c0。然后用c0初始化另一个Complex对象c1,并调用Display函数显示c0。
- 再次读取两个整数作为第二个复数的实部和虚部,并创建一个对应的Complex对象c2。然后调用Display函数显示c2。
- 最后,调用AddComplex函数将c0和c2相加,得到一个新的Complex对象c3,然后调用Display函数显示c3。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-19 22:54:21 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. using namespace std;
  3. class Complex {
  4. private:
  5.     int real;
  6.     int imag;
  7. public:
  8.     Complex(int r = 0, int i = 0) : real(r), imag(i) {}
  9.     Complex(const Complex& c) : real(c.real), imag(c.imag) {}
  10.    
  11.     void Display() {
  12.         cout << real << (imag >= 0 ? "+" : "-") << abs(imag) << "i" << endl;
  13.     }
  14.    
  15.     friend Complex AddComplex(const Complex& c1, const Complex& c2);
  16. };
  17. Complex AddComplex(const Complex& c1, const Complex& c2) {
  18.     int r = c1.real + c2.real;
  19.     int i = c1.imag + c2.imag;
  20.     return Complex(r, i);
  21. }
  22. int main() {
  23.     int x, y;
  24.     cin >> x >> y;
  25.     Complex c0(x, y);
  26.     Complex c1(c0);
  27.     c0.Display();
  28.    
  29.     cin >> x >> y;
  30.     Complex c2(x, y);
  31.     c2.Display();
  32.    
  33.     Complex c3 = AddComplex(c0, c2);
  34.     c3.Display();
  35.    
  36.     return 0;
  37. }
复制代码

代码解析:
- 首先定义了一个Complex类,包含私有成员real(实部)和imag(虚部),默认值均为0。公有成员包括:带有默认形参值的构造函数、复制构造函数和Display函数。
- 接下来定义了一个友元函数AddComplex,用来实现两个复数相加。该函数传入两个复数类的常引用,返回一个新的Complex对象。
- 在主函数中,先读取两个整数作为第一个复数的实部和虚部,并创建一个对应的Complex对象c0。然后用c0初始化另一个Complex对象c1,并调用Display函数显示c0。
- 再次读取两个整数作为第二个复数的实部和虚部,并创建一个对应的Complex对象c2。然后调用Display函数显示c2。
- 最后,调用AddComplex函数将c0和c2相加,得到一个新的Complex对象c3,然后调用Display函数显示c3。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 19:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表