寂寞知己 发表于 2021-6-20 20:04:04

c++题 复数的运算(*) 求大神 太难了

本帖最后由 寂寞知己 于 2021-6-20 20:05 编辑

求大神给出代码



求大神给出代码

人造人 发表于 2021-6-20 20:04:05

#include <iostream>
#include <cctype>

class complex_t {
public:
    complex_t(double real, double imag = 0): real(real), imag(imag) {}
    complex_t(): complex_t(0, 0) {}
    friend std::istream &operator>>(std::istream &is, complex_t &rhs) {
      is >>rhs.real >> rhs.imag;
      if(is.get() != 'i') {
            is.unget();
            std::cerr << "invalid input" << std::endl;
            return is;
      }
      if(!std::isspace(is.get())) {
            is.unget();
            std::cerr << "invalid input" << std::endl;
            return is;
      }
      return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const complex_t &rhs) {
      const auto flags = os.flags();
      os << std::noshowpos << rhs.real << std::showpos << rhs.imag << "i";
      os.flags(flags);
      return os;
    }
    friend const complex_t operator+(const complex_t &lhs, const complex_t &rhs) {
      return complex_t(lhs.real + rhs.real, lhs.imag + rhs.imag);
    }
private:
    double real, imag;
};

int main() {
    {
      complex_t a;
      std::cout << a << std::endl;
    }
    {
      complex_t a(3);
      std::cout << a << std::endl;
    }
    {
      complex_t a(3, 4);
      std::cout << a << std::endl;
    }
    {
      complex_t a(3, 4), b(2, -7);
      std::cout << a << ' ' << b << std::endl;
    }
    {
      complex_t a(3, 4), b(2, -7);
      complex_t c = a + b;
      std::cout << c << std::endl;
    }
    {
      complex_t a(3, 4), b(2, -7), c(-1, 8);
      complex_t d = a + b + c;
      std::cout << d << std::endl;
    }

    {
      complex_t a;
      std::cin >> a;
      std::cout << a << std::endl;
    }
    {
      complex_t a, b;
      std::cin >> a >> b;
      std::cout << a << ' ' << b << std::endl;
    }
    return 0;
}

寂寞知己 发表于 2021-6-20 20:11:13

{:10_266:}

寂寞知己 发表于 2021-6-21 07:41:26

人造人 发表于 2021-6-20 23:22


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() {
   ^~~~

这是怎么回事

人造人 发表于 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 ...

我看不到你的代码呀

寂寞知己 发表于 2021-6-21 08:06:49

人造人 发表于 2021-6-21 07:52
我看不到你的代码呀

用你的代码显示的错误

人造人 发表于 2021-6-21 08:08:59

寂寞知己 发表于 2021-6-21 08:06
用你的代码显示的错误

这个报错不是我的代码

人造人 发表于 2021-6-21 08:10:45

寂寞知己 发表于 2021-6-21 08:16:56

人造人 发表于 2021-6-21 08:10



确实是
不知道哪错了

人造人 发表于 2021-6-21 08:21:33

题目让使用 COMPLEX 这个名字,我用的是 complex_t,你把名字改一下
还有main函数中的内容

人造人 发表于 2021-6-21 08:28:48

你也看一看我写的代码呀,我不是完全按照题目要求写的
你要根据自己的需求进行修改的

寂寞知己 发表于 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() {
   ^~~~

这是什么意思

人造人 发表于 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]
查看完整版本: c++题 复数的运算(*) 求大神 太难了