紧急求救,如何用建立的复数类求一元二次方程的根?????????

DENGGUOGANG 2006-09-27 07:48:48
复数类如下:
#include<iostream.h>
#include <cmath>
// class
class complex
{
private:

double a,b;
double _copy;


public:
double getreal();
double getimag();
double getZ();
void solution(double a,double b,double c,double p,complex &x1,complex &x2);
complex(int x)
{
a=x;b=0;
}
complex(double av,double bv):a(av),b(bv)
{
}
complex(){}
complex(complex& _copy)
{
a=_copy.a;
b=_copy.b;
}

friend ostream& operator<<(ostream& i,complex& j);
friend istream& operator>>(istream& g,complex& i);


friend complex operator+(complex& x,complex& y);
friend complex operator-(complex& x,complex& y);
friend complex operator*(complex& x,complex& y);
friend complex operator/(complex& x,complex& y);
complex operator=(complex& y);
void output()
{
cout<<"z="<<sqrt((a*a)+(b*b))<<endl;
}
void put()
{
cout<<"("<<a<<"+"<<b<<"i"<<")"<<endl;
}
void gen()
{int sign=1, double g,h;
if(b<0)
sign=-1;
else
sign=1;
g=sqrt((sqrt((a*a)+(b*b))+a)/2);
h=sign*sqrt((sqrt((a*a)+(b*b))-a)/2);
cout<<"the sqrt-root is: "<<g<<"+"<<h<<"i"<<endl;
}
};
ostream& operator<<(ostream& stream ,complex& x)
{
x.put();
return stream;
}

//input
istream& operator>>(istream& stream,complex& x)
{
cout<<"\treal=";
cin>>x.a;
cout<<"\timag=";
cin>>x.b;
return stream;
}
//add
complex operator+(complex& x,complex& y)
{
return complex(x.a+y.a,x.b+y.b);
}
//sub
complex operator-(complex& x,complex& y)
{
return complex(x.a-y.a,x.b-y.b);
}
//mul
complex operator*(complex& x,complex &y)
{
return complex((x.a*y.a)-(x.b*y.b),(x.a*y.b)+(x.b*y.a));
}
//div
complex operator/(complex& x,complex& y)
{
return complex(((x.a*y.a+x.b*y.b))/(y.a*y.a+y.b*y.b),(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b));
}

//assignment
complex complex::operator=(complex& y)
{
this->a=y.a;
this->b=y.b;
return *this;
}
double complex::getreal()
{
return a;
}
double complex::getimag()
{
return b;
}
double complex::getZ()
{
return sqrt(a*a+b*b);
}

void main()
{

//class complex
complex p,q;
cin>>p;
cout<<p<<endl;
p.output();
p.gen();
cin>>q;
cout<<q<<endl;
q.output();
q.gen();


cout<<"copy constructor"<<endl;
complex _copy;
_copy=p;
_copy.put();

complex div,mul,sub,add;
cout<<"add a+b"<<endl;
add=q+p;
add.put();
cout<<"sub a-b"<<endl;
sub=q-p;
sub.put();
cout<<"mul a*b"<<endl;
mul=p*q;
mul.put();
cout<<"div a/b"<<endl;
div=q/p;
div.put();


}
求高手建一个求一元二次方程的函数
...全文
369 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞哥 2006-09-27
  • 打赏
  • 举报
回复
是不是给你的编译器的问题啊,这些都是什么错误啊,乱乱的,或者是库的问题
就不该报错!
sinall 2006-09-27
  • 打赏
  • 举报
回复
我在VC6.0、gcc下测试没有任何问题。

你是不是修改我的程序了?
还有需要注意,建个Win32 Console空工程,然后,新建一个cpp文件,拷贝我的代码。
DENGGUOGANG 2006-09-27
  • 打赏
  • 举报
回复
--------------------Configuration: 111 - Win32 Debug--------------------
Compiling...
111.cpp
d:\c++\msdev98\myprojects\11111\111.cpp(49) : warning C4518: 'double ' : storage-class or type specifier(s) unexpected here; ignored
d:\c++\msdev98\myprojects\11111\111.cpp(49) : warning C4228: nonstandard extension used : qualifiers after comma in declarator list are ignored
d:\c++\msdev98\myprojects\11111\111.cpp(54) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
d:\c++\msdev98\myprojects\11111\111.cpp(55) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
d:\c++\msdev98\myprojects\11111\111.cpp(149) : error C2143: syntax error : missing ';' before '<'
d:\c++\msdev98\myprojects\11111\111.cpp(149) : error C2143: syntax error : missing ';' before '<'
d:\c++\msdev98\myprojects\11111\111.cpp(153) : error C2065: 'x1' : undeclared identifier
d:\c++\msdev98\myprojects\11111\111.cpp(153) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
d:\c++\msdev98\myprojects\11111\111.cpp(154) : error C2065: 'x2' : undeclared identifier
d:\c++\msdev98\myprojects\11111\111.cpp(154) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
d:\c++\msdev98\myprojects\11111\111.cpp(158) : error C2062: type 'double' unexpected
d:\c++\msdev98\myprojects\11111\111.cpp(159) : error C2062: type 'double' unexpected
d:\c++\msdev98\myprojects\11111\111.cpp(161) : error C2228: left of '.real' must have class/struct/union type
d:\c++\msdev98\myprojects\11111\111.cpp(161) : error C2228: left of '.imag' must have class/struct/union type
d:\c++\msdev98\myprojects\11111\111.cpp(162) : error C2228: left of '.real' must have class/struct/union type
d:\c++\msdev98\myprojects\11111\111.cpp(162) : error C2228: left of '.imag' must have class/struc

是这些,刚才的不是你的错
DENGGUOGANG 2006-09-27
  • 打赏
  • 举报
回复
不行呀,大哥
--------------------Configuration: 111 - Win32 Debug--------------------
Compiling...
2222.cpp
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2530: 'x1' : references must be initialized
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2143: syntax error : missing ';' before '&'
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2955: 'complex' : use of class template requires template argument list
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2133: 'complex' : unknown size
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2512: 'complex' : no appropriate default constructor available
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2262: 'complex' : cannot be destroyed
d:\c++\msdev98\myprojects\作业20\2222.cpp(9) : error C2065: 'x2' : undeclared identifier
d:\c++\msdev98\myprojects\作业20\2222.cpp(13) : error C2678: binary '=' : no operator defined which takes a left-hand operand of type 'class std::complex' (or there is no acceptable conversion)
d:\c++\msdev98\myprojects\作业20\2222.cpp(13) : error C2582: 'std::complex' : 'operator =' function is unavailable
d:\c++\msdev98\myprojects\作业20\2222.cpp(18) : error C2064: term does not evaluate to a function
d:\c++\msdev98\myprojects\作业20\2222.cpp(19) : error C2064: term does not evaluate to a function
d:\c++\msdev98\myprojects\作业20\2222.cpp(21) : error C2039: 'real' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
d:\c++\msdev98\myprojects\作业20\2222.cpp(21) : error C2039: 'imag' : is not a member of 'complex'
d:\microsoft visual studio\vc98\include\complex(267) : see declaration of 'complex'
d:\c++\msdev98\myprojects\作业20\2222.cpp(22) : error C2228: left of '.real' must have class/struct/union type
d:\c++\msdev98\myprojects\作业20\2222.cpp(22) : error C2228: left of '.imag' must have class/struct/union type
执行 cl.exe 时出错.

很多错误
sinall 2006-09-27
  • 打赏
  • 举报
回复
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;

int main(void)
{
double a = 1.0, b = 0.0, c = 1.0; // ax^2 + bx + c = 0
complex<double> x1,x2;
double d = b * b - 4.0 * a * c;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
}
else
{
x1 = complex<double>((-b)/(2*a), sqrt(-d)/(2*a));
x2 = complex<double>((-b)/(2*a), -(sqrt(-d)/(2*a)));
}
cout << "x1 = " << x1.real() << " + " << x1.imag() << "i" << endl;
cout << "x2 = " << x2.real() << " + " << x2.imag() << "i" << endl;

return 0;
}
DENGGUOGANG 2006-09-27
  • 打赏
  • 举报
回复
帮人帮到底吧大哥,我今晚写了一晚,现在实在写不下去了,哎.......
谢谢
sinall 2006-09-27
  • 打赏
  • 举报
回复
你看懂了,在写进去吧。有那么费劲吗???
DENGGUOGANG 2006-09-27
  • 打赏
  • 举报
回复
能不能直接加到我前面的程序去???
飞哥 2006-09-27
  • 打赏
  • 举报
回复
C99提供了 complex.h
sinall 2006-09-27
  • 打赏
  • 举报
回复
那不很简单吗?
complex<double> x1,x2;
d = b^2-4ac;
if (d >= 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
}
else
{
x1 = complex<double>((-b)/(2*a), sqrt(-d)/(2*a));
x2 = complex<double>((-b)/(2*a), -(sqrt(-d)/(2*a)));
}
sinall 2006-09-27
  • 打赏
  • 举报
回复

- complex(3C++)

Standard C++ Library
Copyright 1998, Rogue Wave Software, Inc.

NAME
complex

- C++ complex number library

SPECIALIZATIONS
complex <float>
complex <double>
complex <long double>

SYNOPSIS
#include <complex>
template <class T>
class complex;
class complex<float>;
class complex<double>;
class complex<long double>;

DESCRIPTION
complex<T> is a class that supports complex numbers. A com-
plex number has a real part and an imaginary part. The com-
plex class supports equality, comparison and basic arith-
metic operations. In addition, mathematical functions such
as exponents, logarithms, powers, and square roots are also
available.
OOPhaisky 2006-09-27
  • 打赏
  • 举报
回复
太长了...

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧