鱼C论坛

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

以下的代码错在哪

[复制链接]
发表于 2023-12-3 21:09:11 | 显示全部楼层 |阅读模式

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

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

x
#include<iostream>
using namespace std;
class complex
{
        double real, image;
public:
        complex(double r = 0, double i = 0)
        {
                real = r;
                image = i;
        }
        void show()
        {
                if (image > 0)
                {
                        if (image == 1)
                        {
                                cout << real << "+" << "i" << endl;
                        }
                        else
                                cout << real << "+" << image << "i" << endl;
                }
               
                else if (image < 0)
                {
                        if (image == -1)
                        {
                                cout << real << "-" << endl;
                        }
                        else
                                cout << real << "-" << image << "i" << endl;
                }
        }
        friend istream& operator>>(istream& myin, complex& c);
        friend ostream& operator<<(ostream& myout, const complex& c);
       
};
istream& operator>>(istream& myin, complex& c)
{
        cin >> c;
        return myin;
}
ostream& operator<<(ostream&myout, const complex&c)
{
        if (c.image > 0)
        {
                if (c.image == 1)  
                {
                        cout << c.real << "+" << "i" << endl;
                }
                else
                        cout << c.real << "+" << c.image << "i" << endl;
        }

        else if (c.image < 0)
        {
                if (c.image == -1)
                {
                        cout << c.real << "-" << endl;
                }
                else
                        cout << c.real << "-" << c.image << "i" << endl;
        }
        return myout;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-3 06:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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