后缀代码: | //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