鱼C论坛

 找回密码
 立即注册
查看: 3789|回复: 12

[已解决]c++题 复数的运算(*) 求大神 太难了

[复制链接]
发表于 2021-6-20 20:04:04 | 显示全部楼层 |阅读模式
30鱼币
本帖最后由 寂寞知己 于 2021-6-20 20:05 编辑

求大神给出代码
ME5UK2)LRPE7AQ)}A[)5Z34.png
KE$GY87RG%UXD5WA8U08SEX.png
A`52U%J{@L~T8WQL4LL8/.png
求大神给出代码
最佳答案
2021-6-20 20:04:05
  1. #include <iostream>
  2. #include <cctype>

  3. class complex_t {
  4. public:
  5.     complex_t(double real, double imag = 0): real(real), imag(imag) {}
  6.     complex_t(): complex_t(0, 0) {}
  7.     friend std::istream &operator>>(std::istream &is, complex_t &rhs) {
  8.         is >>rhs.real >> rhs.imag;
  9.         if(is.get() != 'i') {
  10.             is.unget();
  11.             std::cerr << "invalid input" << std::endl;
  12.             return is;
  13.         }
  14.         if(!std::isspace(is.get())) {
  15.             is.unget();
  16.             std::cerr << "invalid input" << std::endl;
  17.             return is;
  18.         }
  19.         return is;
  20.     }
  21.     friend std::ostream &operator<<(std::ostream &os, const complex_t &rhs) {
  22.         const auto flags = os.flags();
  23.         os << std::noshowpos << rhs.real << std::showpos << rhs.imag << "i";
  24.         os.flags(flags);
  25.         return os;
  26.     }
  27.     friend const complex_t operator+(const complex_t &lhs, const complex_t &rhs) {
  28.         return complex_t(lhs.real + rhs.real, lhs.imag + rhs.imag);
  29.     }
  30. private:
  31.     double real, imag;
  32. };

  33. int main() {
  34.     {
  35.         complex_t a;
  36.         std::cout << a << std::endl;
  37.     }
  38.     {
  39.         complex_t a(3);
  40.         std::cout << a << std::endl;
  41.     }
  42.     {
  43.         complex_t a(3, 4);
  44.         std::cout << a << std::endl;
  45.     }
  46.     {
  47.         complex_t a(3, 4), b(2, -7);
  48.         std::cout << a << ' ' << b << std::endl;
  49.     }
  50.     {
  51.         complex_t a(3, 4), b(2, -7);
  52.         complex_t c = a + b;
  53.         std::cout << c << std::endl;
  54.     }
  55.     {
  56.         complex_t a(3, 4), b(2, -7), c(-1, 8);
  57.         complex_t d = a + b + c;
  58.         std::cout << d << std::endl;
  59.     }

  60.     {
  61.         complex_t a;
  62.         std::cin >> a;
  63.         std::cout << a << std::endl;
  64.     }
  65.     {
  66.         complex_t a, b;
  67.         std::cin >> a >> b;
  68.         std::cout << a << ' ' << b << std::endl;
  69.     }
  70.     return 0;
  71. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-20 20:04:05 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <cctype>

  3. class complex_t {
  4. public:
  5.     complex_t(double real, double imag = 0): real(real), imag(imag) {}
  6.     complex_t(): complex_t(0, 0) {}
  7.     friend std::istream &operator>>(std::istream &is, complex_t &rhs) {
  8.         is >>rhs.real >> rhs.imag;
  9.         if(is.get() != 'i') {
  10.             is.unget();
  11.             std::cerr << "invalid input" << std::endl;
  12.             return is;
  13.         }
  14.         if(!std::isspace(is.get())) {
  15.             is.unget();
  16.             std::cerr << "invalid input" << std::endl;
  17.             return is;
  18.         }
  19.         return is;
  20.     }
  21.     friend std::ostream &operator<<(std::ostream &os, const complex_t &rhs) {
  22.         const auto flags = os.flags();
  23.         os << std::noshowpos << rhs.real << std::showpos << rhs.imag << "i";
  24.         os.flags(flags);
  25.         return os;
  26.     }
  27.     friend const complex_t operator+(const complex_t &lhs, const complex_t &rhs) {
  28.         return complex_t(lhs.real + rhs.real, lhs.imag + rhs.imag);
  29.     }
  30. private:
  31.     double real, imag;
  32. };

  33. int main() {
  34.     {
  35.         complex_t a;
  36.         std::cout << a << std::endl;
  37.     }
  38.     {
  39.         complex_t a(3);
  40.         std::cout << a << std::endl;
  41.     }
  42.     {
  43.         complex_t a(3, 4);
  44.         std::cout << a << std::endl;
  45.     }
  46.     {
  47.         complex_t a(3, 4), b(2, -7);
  48.         std::cout << a << ' ' << b << std::endl;
  49.     }
  50.     {
  51.         complex_t a(3, 4), b(2, -7);
  52.         complex_t c = a + b;
  53.         std::cout << c << std::endl;
  54.     }
  55.     {
  56.         complex_t a(3, 4), b(2, -7), c(-1, 8);
  57.         complex_t d = a + b + c;
  58.         std::cout << d << std::endl;
  59.     }

  60.     {
  61.         complex_t a;
  62.         std::cin >> a;
  63.         std::cout << a << std::endl;
  64.     }
  65.     {
  66.         complex_t a, b;
  67.         std::cin >> a >> b;
  68.         std::cout << a << ' ' << b << std::endl;
  69.     }
  70.     return 0;
  71. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-20 20:11:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-21 07:41:26 | 显示全部楼层

a.cpp: In function ‘void Test0()’:
a.cpp:87:3: error: ‘COMPLEX’ was not declared in this scope
   COMPLEX a, b(rp1), c(rp2, ip2);
   ^~~~~~~
a.cpp:88:11: error: ‘a’ was not declared in this scope
   cout << a;
           ^
a.cpp:90:11: error: ‘b’ was not declared in this scope
   cout << b;
           ^
a.cpp:92:11: error: ‘c’ was not declared in this scope
   cout << c;
           ^
a.cpp: In function ‘void Test1()’:
a.cpp:102:3: error: ‘COMPLEX’ was not declared in this scope
   COMPLEX a(rp1, ip1), b(rp2, ip2), c(rp3, ip3);
   ^~~~~~~
a.cpp:103:11: error: ‘a’ was not declared in this scope
   cout << a << ' ' << b << ' ' << c << endl;
           ^
a.cpp:103:23: error: ‘b’ was not declared in this scope
   cout << a << ' ' << b << ' ' << c << endl;
                       ^
a.cpp:103:35: error: ‘c’ was not declared in this scope
   cout << a << ' ' << b << ' ' << c << endl;
                                   ^
a.cpp: In function ‘void Test2()’:
a.cpp:109:2: error: ‘COMPLEX’ was not declared in this scope
  COMPLEX a, b, c;
  ^~~~~~~
a.cpp:110:16: error: ‘a’ was not declared in this scope
  while (cin >> a >> b >> c)
                ^
a.cpp:110:21: error: ‘b’ was not declared in this scope
  while (cin >> a >> b >> c)
                     ^
a.cpp:110:26: error: ‘c’ was not declared in this scope
  while (cin >> a >> b >> c)
                          ^
a.cpp: In function ‘void Test3()’:
a.cpp:118:2: error: ‘COMPLEX’ was not declared in this scope
  COMPLEX a, b, c, d, s;
  ^~~~~~~
a.cpp:119:16: error: ‘a’ was not declared in this scope
  while (cin >> a >> b >> c >> d)
                ^
a.cpp:119:21: error: ‘b’ was not declared in this scope
  while (cin >> a >> b >> c >> d)
                     ^
a.cpp:119:26: error: ‘c’ was not declared in this scope
  while (cin >> a >> b >> c >> d)
                          ^
a.cpp:119:31: error: ‘d’ was not declared in this scope
  while (cin >> a >> b >> c >> d)
                               ^
a.cpp:121:3: error: ‘s’ was not declared in this scope
   s = a + b + c + d;
   ^
a.cpp: In function ‘void Test4()’:
a.cpp:129:2: error: ‘COMPLEX’ was not declared in this scope
  COMPLEX a(2.5), b(3.6, 4.9), x, y, z;
  ^~~~~~~
a.cpp:130:10: error: ‘a’ was not declared in this scope
  cout << a << ' ' << b << ' ' << x << ' ' << y << ' ' << z << endl;
          ^
a.cpp:130:22: error: ‘b’ was not declared in this scope
  cout << a << ' ' << b << ' ' << x << ' ' << y << ' ' << z << endl;
                      ^
a.cpp:130:34: error: ‘x’ was not declared in this scope
  cout << a << ' ' << b << ' ' << x << ' ' << y << ' ' << z << endl;
                                  ^
a.cpp:130:46: error: ‘y’ was not declared in this scope
  cout << a << ' ' << b << ' ' << x << ' ' << y << ' ' << z << endl;
                                              ^
a.cpp:130:58: error: ‘z’ was not declared in this scope
  cout << a << ' ' << b << ' ' << x << ' ' << y << ' ' << z << endl;
                                                          ^
a.cpp: In function ‘int main()’:
a.cpp:140:5: error: redefinition of ‘int main()’
int main()
     ^~~~
a.cpp:40:5: note: ‘int main()’ previously defined here
int main() {
     ^~~~

这是怎么回事
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 07:52:37 | 显示全部楼层
寂寞知己 发表于 2021-6-21 07:41
a.cpp: In function ‘void Test0()’:
a.cpp:87:3: error: ‘COMPLEX’ was not declared in this scop ...

我看不到你的代码呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-21 08:06:49 | 显示全部楼层
人造人 发表于 2021-6-21 07:52
我看不到你的代码呀

用你的代码显示的错误
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:08:59 | 显示全部楼层
寂寞知己 发表于 2021-6-21 08:06
用你的代码显示的错误

这个报错不是我的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:10:45 | 显示全部楼层
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-21 08:16:56 | 显示全部楼层

1@UOQW4EB`Q)8662)`5RRGX.png
确实是
不知道哪错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:21:33 | 显示全部楼层
题目让使用 COMPLEX 这个名字,我用的是 complex_t,你把名字改一下
还有main函数中的内容
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:28:48 | 显示全部楼层
你也看一看我写的代码呀,我不是完全按照题目要求写的
你要根据自己的需求进行修改的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-21 08:37:40 | 显示全部楼层
人造人 发表于 2021-6-21 08:28
你也看一看我写的代码呀,我不是完全按照题目要求写的
你要根据自己的需求进行修改的

a.cpp: In function ‘int main()’:
a.cpp:140:5: error: redefinition of ‘int main()’
int main()
     ^~~~
a.cpp:40:5: note: ‘int main()’ previously defined here
int main() {
     ^~~~

这是什么意思
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:43:28 | 显示全部楼层
寂寞知己 发表于 2021-6-21 08:37
a.cpp: In function ‘int main()’:
a.cpp:140:5: error: redefinition of ‘int main()’
int main( ...

有多个main函数?
没用过你刷题的那个软件,不知道应不应该写main函数,如何写main函数,不清楚你刷题的那个软件对代码有什么要求
这个代码在我这边测试没有问题
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 15:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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