鱼C论坛

 找回密码
 立即注册
查看: 3431|回复: 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
#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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-20 20:11:13 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> 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() {
     ^~~~

这是怎么回事
想知道小甲鱼最近在做啥?请访问 -> 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 ...

我看不到你的代码呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

用你的代码显示的错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

这个报错不是我的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:10:45 | 显示全部楼层
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

1@UOQW4EB`Q)8662)`5RRGX.png
确实是
不知道哪错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:21:33 | 显示全部楼层
题目让使用 COMPLEX 这个名字,我用的是 complex_t,你把名字改一下
还有main函数中的内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-21 08:28:48 | 显示全部楼层
你也看一看我写的代码呀,我不是完全按照题目要求写的
你要根据自己的需求进行修改的
想知道小甲鱼最近在做啥?请访问 -> 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() {
     ^~~~

这是什么意思
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 14:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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