题,要求实现:1、输入一个复数2、求它的平方根3、Show出结果。例如输入:3 -4输出(先输出实部>0的)(2-1i)(-2+1i)提示

后缀代码:
//StudybarCommentBegin
int main()
{
Cmycomplex  z1(3,4),z2(-1),z3;
double  x,y;
cin>>x>>y;
z1.Set(x,y);
z3=z1.sqrt();
z3.Show();
cout<<"\n";
z3=z3*z2;
z3.Show();
}
//StudybarCommentEnd

 sqrt在std空间中使用要加入前缀std::才能在空间中识别

 #include<iostream>
#include<cmath>
using namespace std;
class Cmycomplex{
    private:
        double x,y;
    public:
        Cmycomplex(double a,double b){
            x=a;y=b;
        }
        Cmycomplex(double a){
            x=a;y=0;
        }
        Cmycomplex(){
            x=0;y=0;
        }
        void Set(double a,double b){
            x=a;y=b;
        }
        Cmycomplex operator* (Cmycomplex &z){
            Cmycomplex t;
            t.x=x*z.x-y*z.y;
            t.y=x*z.y+y*z.x;
            return t;
        }
        
        Cmycomplex sqrt(){
            Cmycomplex t;
            t.x=std::sqrt((x + std::sqrt(x*x+y*y)) / 2);;
            t.y=(y >= 0) ? std::sqrt((-x + std::sqrt(x * x + y * y)) / 2) : -std::sqrt((-x + std::sqrt(x * x + y * y)) / 2);
            return t;
        }
        void Show(){
            if(y<0){
                cout<<"("<<x<<y<<"i"<<")";
            }
            else
                cout<<"("<<x<<"+"<<y<<"i"<<")";
        }
};
//StudybarCommentBegin
int main()
{
Cmycomplex  z1(3,4),z2(-1),z3;
double  x,y;
cin>>x>>y;
z1.Set(x,y);
z3=z1.sqrt();
z3.Show();
cout<<"\n";
z3=z3*z2;
z3.Show();
}
//StudybarCommentEnd

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值