C++中怎样实现两个复数的相加运算 ?怎样写出这个程序 5
#include<iostream>usingnamespacestd;classplural;{public:plural(doublea,b,c,d)void.sho...
#include <iostream>
using namespace std;
class plural;
{
public:
plural(double a,b,c,d)
void.show()
{cout<<"a"<<"+"<<"b"<<"i/n";}
{cout<<"c"<<"+"<<"d"<<"i/n";}
private
double.a,b,c,d,
};
int main;
plural C.(a,b,c,d)
{a.add(b);}
cout<<"两个复数的和为 "<<endl;
cin>>a>>b>>c>>d>>endl;
C.show();
system ("PAUSE");
return 0;
}
帮忙指出错误 展开
using namespace std;
class plural;
{
public:
plural(double a,b,c,d)
void.show()
{cout<<"a"<<"+"<<"b"<<"i/n";}
{cout<<"c"<<"+"<<"d"<<"i/n";}
private
double.a,b,c,d,
};
int main;
plural C.(a,b,c,d)
{a.add(b);}
cout<<"两个复数的和为 "<<endl;
cin>>a>>b>>c>>d>>endl;
C.show();
system ("PAUSE");
return 0;
}
帮忙指出错误 展开
展开全部
#include <iostream>
using namespace std;
template<typename T>
class Complex
{
T real;
T imag;
public:
//default construction
Complex(){real=0;imag=0;}
//construction with predefined real&imag
Complex(T r, T i){real=r;imag=i;}
//in stream
friend istream& operator>>(istream &is, Complex &n)
{
cout<<"real:";
is>>n.real;
if(!is.fail())
{
cout<<"image:";
is>>n.imag;
}
return is;
}
//out stream
friend ostream& operator<<(ostream &os, const Complex &n)
{
os<<"real:"<<n.real<<" "<<"imag:"<<n.imag;
return os;
}
//operator +, return c1+c2
Complex& operator+(Complex &n)
{
Complex s(real+n.real,imag+n.imag);
return s;
}
};
int main(void) {
Complex<double> n1,n2,sum;
cout<<"**Please type in the first complex (c1):"<<endl;
cin>>n1;
cout<<"**Please type in the second complex (c2):"<<endl;
cin>>n2;
cout<<"**Sum of c1 and c2 is:"<<endl;
sum=n1+n2;
cout<<sum;
return 0;
}
展开全部
#include <cstdio>
#include <cstdlib>
template<typename tp> struct complex{
tp x,i;
complex(void) {x = i = 0;}
complex(tp x) : x(x) {i = 0;}
complex(tp x, tp i) : x(x), i(i) {}
complex add(const complex b){
x += b.x; i += b.i;
}
}c1, c2;
int main(int argc, const char *argv[]){
scanf("%lf+%lfi + %lf+%lfi", &c1.x, &c1.i, &c2.x, &c2.i);
c1.add(c2);
printf("= %lf+%lfi\n", c1.x, c1.i);
system("pause>nul");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询