我是C++新手,这编的程序运行不过呀,用的是VC6.0,紧急求助呀!!
#include<iostream>#include<cmath>usingnamespacestd;constdoublecreal=1,cimaginary=2;cl...
#include<iostream>
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;
class Complex
{
public:
Complex(double real,double imaginary)
{Real=real;
Imaginary=imaginary;}
void add(double Real,double Imaginary);
void sub(double Real,double Imaginary);
void mutiple(double Real,double Imaginary);
void divide(double Real,double Imaginary);
bool equal();
void display();
double ComputeNorm();
Private:
double Real,Imaginary;
};
void Complex::add(double Real,double Imaginary)
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub(double Real,double Imaginary)
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple(double Real,double Imaginary)
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide(double Real,double Imaginary)
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal(double Real,double Imaginary)
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
void Complex::ComputeNorm(double Real,double Imaginary)
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display(double Real,double Imaginary)
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
m.display();
m.add(double Real,double Imaginary);
m.display();
m.sub(double Real,double Imaginary);
m.display();
m.mutiple(double Real,double Imaginary);
m.display();
m.divide(double Real,double Imaginary);
m.display();
m.equal();
m.display();
m.ComputeNorm(double Real,double Imaginary);
m.display();
return 0;
} 展开
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;
class Complex
{
public:
Complex(double real,double imaginary)
{Real=real;
Imaginary=imaginary;}
void add(double Real,double Imaginary);
void sub(double Real,double Imaginary);
void mutiple(double Real,double Imaginary);
void divide(double Real,double Imaginary);
bool equal();
void display();
double ComputeNorm();
Private:
double Real,Imaginary;
};
void Complex::add(double Real,double Imaginary)
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub(double Real,double Imaginary)
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple(double Real,double Imaginary)
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide(double Real,double Imaginary)
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal(double Real,double Imaginary)
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
void Complex::ComputeNorm(double Real,double Imaginary)
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display(double Real,double Imaginary)
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
m.display();
m.add(double Real,double Imaginary);
m.display();
m.sub(double Real,double Imaginary);
m.display();
m.mutiple(double Real,double Imaginary);
m.display();
m.divide(double Real,double Imaginary);
m.display();
m.equal();
m.display();
m.ComputeNorm(double Real,double Imaginary);
m.display();
return 0;
} 展开
展开全部
#include<iostream>
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;
class Complex
{
public:
Complex(double real,double imaginary)
{
Real=real;
Imaginary=imaginary;
}
void add();
void sub();
void mutiple();
void divide();
bool equal();//函数声明要与下面函数实现参数统一
void display();
double ComputeNorm();
private://此处P为小写
double Real,Imaginary;
};
void Complex::add()//由于函数体内没有使用传入的参数,而是使用上面定义的全局变量 creal 和 cimaginary,所以不用传参数
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub()
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple()
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide()
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal()
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
double Complex::ComputeNorm()//此处由于返回了sqrt(m),所以返回值函数返回类型应该为double
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display()
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
double Real=4,Imaginary=5;//需要定义下面对象函数需要使用的变量。
m.display();
m.add();//使用变量时,要使用变量名,不用加上数据类型。
m.display();
m.sub();
m.display();
m.mutiple();
m.display();
m.divide();
m.display();
m.equal();
m.display();
m.ComputeNorm();
m.display();
system("pause");
return 0;
}
已经按照你程序的本来意思改好。只是你这个程序需要注意的地方太多,建议你把基础再扎实一下,然后再继续学习。
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;
class Complex
{
public:
Complex(double real,double imaginary)
{
Real=real;
Imaginary=imaginary;
}
void add();
void sub();
void mutiple();
void divide();
bool equal();//函数声明要与下面函数实现参数统一
void display();
double ComputeNorm();
private://此处P为小写
double Real,Imaginary;
};
void Complex::add()//由于函数体内没有使用传入的参数,而是使用上面定义的全局变量 creal 和 cimaginary,所以不用传参数
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub()
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple()
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide()
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal()
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
double Complex::ComputeNorm()//此处由于返回了sqrt(m),所以返回值函数返回类型应该为double
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display()
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
double Real=4,Imaginary=5;//需要定义下面对象函数需要使用的变量。
m.display();
m.add();//使用变量时,要使用变量名,不用加上数据类型。
m.display();
m.sub();
m.display();
m.mutiple();
m.display();
m.divide();
m.display();
m.equal();
m.display();
m.ComputeNorm();
m.display();
system("pause");
return 0;
}
已经按照你程序的本来意思改好。只是你这个程序需要注意的地方太多,建议你把基础再扎实一下,然后再继续学习。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
错误比较多啊,直接上修改后的代码!
#include<iostream>
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;//这是干嘛用的??
class Complex
{
public:
Complex(double real,double imaginary)
{Real=real; Imaginary=imaginary;}
void add(double creal,double cimaginary);
void sub(double creal,double cimaginary);
void mutiple(double creal,double cimaginary);
void divide(double creal,double cimaginary);
bool equal(double creal,double cimaginary);
void display();
double ComputeNorm();
private:
double Real,Imaginary;
};
void Complex::add(double creal,double cimaginary)
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub(double creal,double cimaginary)
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple(double creal,double cimaginary)
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide(double creal,double cimaginary)
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal(double creal,double cimaginary)
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
double Complex::ComputeNorm()
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display()
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
m.display();
m.add(2,3);
m.display();
m.sub(2,3);
m.display();
m.mutiple(3,4);
m.display();
m.divide(3,4);
m.display();
m.equal(2,3);
m.display();
m.ComputeNorm();
m.display();
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
const double creal=1,cimaginary=2;//这是干嘛用的??
class Complex
{
public:
Complex(double real,double imaginary)
{Real=real; Imaginary=imaginary;}
void add(double creal,double cimaginary);
void sub(double creal,double cimaginary);
void mutiple(double creal,double cimaginary);
void divide(double creal,double cimaginary);
bool equal(double creal,double cimaginary);
void display();
double ComputeNorm();
private:
double Real,Imaginary;
};
void Complex::add(double creal,double cimaginary)
{
Real+=creal;
Imaginary+=cimaginary;
}
void Complex::sub(double creal,double cimaginary)
{
Real=Real-creal;
Imaginary=Imaginary-cimaginary;
}
void Complex::mutiple(double creal,double cimaginary)
{
Real=Real*creal;
Imaginary=Imaginary*cimaginary;
}
void Complex::divide(double creal,double cimaginary)
{
Real=Real/creal;
Imaginary=Imaginary/cimaginary;
}
bool Complex::equal(double creal,double cimaginary)
{
if(Real==creal&&Imaginary==cimaginary)
return true;
else
return false;
}
double Complex::ComputeNorm()
{
double m;
m=Real*Real+Imaginary*Imaginary;
return sqrt(m);
}
void Complex::display()
{
cout<<"对象的实部是:"<<endl;
cout<<Real<<endl;
cout<<"对象的虚部是:"<<endl;
cout<<Imaginary<<endl;
}
int main()
{
Complex m(2,3);
m.display();
m.add(2,3);
m.display();
m.sub(2,3);
m.display();
m.mutiple(3,4);
m.display();
m.divide(3,4);
m.display();
m.equal(2,3);
m.display();
m.ComputeNorm();
m.display();
return 0;
}
追问
不好意思,我想问一下,我那个重点是哪错了,万分感激!!
追答
主要有以下几个地方:
1. Private应该为private,你大写了;
2. main函数里像这样提供实际参数的m.add(double Real,double Imaginary);是无法编译过去的,应该为m.add(4,6)之类的;
3. Complex class定义中add、sub、mutiple、divide的参数中的形参和class定义的成员变量相同,这在人的理解和编译的时候都二义性,如:add(double Real,double Imaginary);应该为add(double xxx, double yyy); xxx、yyy是自己定义形参;
4. void Complex::ComputeNorm应该为double Complex::ComputeNorm;
5. Complex class定义中成员函数声明和下面的函数定义一定要一致,否则编译的时候也是出错;
其它的想不起来了,抱歉。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询