1、复数类输出
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部
2、用Show()现实复数的值。
输出
(2+3i)
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部
//
//2、用Show()现实复数的值。
//
//输出
//
//(2+3i)
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0 , double i = 0):real(r),imag(i){
}
void Show()
{
cout << "(" <<real << "+" << imag <<"i" << ")" <<endl;
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
z1.Show();
}
//StudybarCommentEnd
2、复数类输出实部和虚部
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部
2、用GetReal() GetImaginary()返回实部、虚部,均为double型
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部
//
//2、用GetReal() GetImaginary()返回实部、虚部,均为double型
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0, double i = 0):real(r),imag(i){
}
double GetReal() const
{
return real;
}
double GetImaginary() const
{
return imag;
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
cout<<z1.GetReal()<<"\n";
cout<<z2.GetImaginary()<<"\n";
cout<<z3.GetReal()<<"\n";
}
//StudybarCommentEnd
3、复数类add成员函数
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部
2、实现Add( )功能。
3、Show出结果。
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部
//
//2、实现Add( )功能。
//
//3、Show出结果。
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0 , double i = 0):real(r),imag(i){
}
void Add(const Cmycomplex & other)
{
real += other.real;
imag += other.imag;
}
void Show() const
{
cout <<"(" <<real <<"+" <<imag <<"i)"<<endl;
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
z1.Add(z3);
z1.Show();
}
//StudybarCommentEnd
4、复数类赋值
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现Add( )功能,并实现返回值为复数类。
3、Show出结果。
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现Add( )功能,并实现返回值为复数类。
//
//3、Show出结果。
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0,double i = 0):real(r), imag(i){
}
Cmycomplex Add(const Cmycomplex& other) const
{
return Cmycomplex(real + other.real,imag + other.imag);
}
void Show() const
{
cout <<"(" <<real <<"+" <<imag <<"i)"<<endl;
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
z2=z1.Add(z3);
z2.Show();
}
//StudybarCommentEnd
5、复数类加法运算符重载
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现加法运算符重载功能,并实现返回值为复数类。
3、Show出结果。
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现加法运算符重载功能,并实现返回值为复数类。
//
//3、Show出结果。
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0, double i = 0):real(r),imag(i){
}
Cmycomplex operator+(const Cmycomplex &other) const
{
return Cmycomplex(real + other.real,imag + other.imag);
}
void Show() const
{
cout <<"(" <<real <<"+" <<imag <<"i)" <<endl;
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
z2=z1+z3;
z2.Show();
}
//StudybarCommentEnd
6、带输入的复数类加法运算符重载
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现加法运算符重载功能,并实现返回值为复数类。
3、Show出结果。
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现加法运算符重载功能,并实现返回值为复数类。
//
//3、Show出结果。
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0, double i = 0):real(r), imag(i){
}
Cmycomplex operator+(const Cmycomplex &other) const
{
return Cmycomplex(real + other.real,imag + other.imag);
}
void Set(double r ,double i)
{
real = r;
imag = i;
}
void Show() const
{
cout <<"(" <<real <<"+" <<imag <<"i)";
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(2,3),z2,z3(3);
z2=z1+z3;
z2.Show();
double x,y;
cin>>x>>y;
z2.Set(x,y);
z3=z1+z2;
cout<<"\n";
z3.Show();
}
//StudybarCommentEnd
7、带输入的复数类-加-减-乘运算符重载
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
3、Show出结果。
例如输入
7 8
输出:
(10+12i)
(-4-4i)
(-11+52i)
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
//
//3、Show出结果。
//
//例如输入
//
//7 8
//
//输出:
//
//(10+12i)
//(-4-4i)
//(-11+52i)
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0 ,double i = 0): real(r), imag(i){
}
void Set(double r ,double i)
{
real = r;
imag = i;
}
Cmycomplex operator +(const Cmycomplex & other)const
{
return Cmycomplex(real+other.real, imag+other.imag);
}
Cmycomplex operator -(const Cmycomplex & other)const
{
return Cmycomplex(real-other.real, imag-other.imag);
}
Cmycomplex operator *(const Cmycomplex & other)const
{
return Cmycomplex(real*other.real-imag *other.imag, real*other.imag + imag * other.real);
}
void Show() const
{
if(imag >= 0)
{
cout <<"(" <<real <<"+" <<imag <<"i)";
}
else
{
cout <<"(" <<real <<imag <<"i)";
}
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(3,4),z2(7),z3;
double x,y;
cin>>x>>y;
z2.Set(x,y);
z3=z1+z2;
cout<<"\n";
z3.Show();
z3=z1-z2;
cout<<"\n";
z3.Show();
z3=z1*z2;
cout<<"\n";
z3.Show();
}
//StudybarCommentEnd
8、带输入的复数类-加-减-乘-除运算符重载
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
3、Show出结果。
例如输入
7 8
输出:
(10.00+12.00i)
(-4.00-4.00i)
(-11.00+52.00i)
(0.47+0.04i)
注意:
如果是-4.00±4.00i 结果应该变成 -4.00-4.00i . 即加一个if判断
2,小数的精度
头文件:
#include
#include
using namespace std;
Show()
{ cout <<setiosflags(ios::fixed); 具体显示几位小数的设置
cout<<“(”<<setprecision(2)<<x<<“+”<<setprecision(2)<<y<<“i)”;
}
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
//
//3、Show出结果。
//
//例如输入
//
//7 8
//
//输出:
//
//(10.00+12.00i)
//(-4.00-4.00i)
//(-11.00+52.00i)
//(0.47+0.04i)
//
//
//注意:
//
//如果是-4.00+-4.00i 结果应该变成 -4.00-4.00i . 即加一个if判断
//
//2,小数的精度
//
//头文件:
//#include<iostream>
//#include<iomanip>
//using namespace std;
//
//Show()
//
//{ cout <<setiosflags(ios::fixed); 具体显示几位小数的设置
//cout<<"("<<setprecision(2)<<x<<"+"<<setprecision(2)<<y<<"i)";
//}
#include <iostream>
#include <iomanip>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0, double i = 0): real(r), imag(i){
}
void Set(double r, double i)
{
real = r;
imag = i;
}
Cmycomplex operator+(const Cmycomplex&other)const
{
return Cmycomplex(real +other.real,imag+ other.imag);
}
Cmycomplex operator -(const Cmycomplex & other)const
{
return Cmycomplex(real-other.real, imag-other.imag);
}
Cmycomplex operator *(const Cmycomplex & other)const
{
return Cmycomplex(real*other.real-imag *other.imag, real*other.imag + imag * other.real);
}
Cmycomplex operator /(const Cmycomplex & other) const
{
double denominator = (other.real * other.real + other.imag *other.imag);
return Cmycomplex((real * other.real + imag * other.imag)/denominator, (imag *other.real -real * other.imag) / denominator);
}
void Show() const
{
cout << setiosflags(ios :: fixed) <<setprecision(2);
if(imag >= 0)
{
cout <<"(" <<real <<"+"<<imag <<"i)";
}
else
{
cout <<"(" <<real <<imag <<"i)";
}
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(3,4),z2(7),z3;
double x,y;
cin>>x>>y;
z2.Set(x,y);
z3=z1+z2;
cout<<"\n";
z3.Show();
z3=z1-z2;
cout<<"\n";
z3.Show();
z3=z1*z2;
cout<<"\n";
z3.Show();
z3=z1/z2;
cout<<"\n";
z3.Show();
}
//StudybarCommentEnd
9、带输入的复数类-比较==和!=-运算符重载
如题,要求实现:
1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
3、Show出结果。
例如输入
7 8
7 8
输出:
y
n
//如题,要求实现:
//
//1、复数类含两个参数的构造函数,一个为实部,一个为虚部。
//
//2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。
//
//3、Show出结果。
//
//例如输入
//
//7 8
//
//7 8
//
//输出:
//
//y
//
//n
//
#include <iostream>
#include <iomanip>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(double r = 0, double i = 0): real(r), imag(i){
}
void Set(double r, double i)
{
real = r;
imag = i;
}
Cmycomplex operator+(const Cmycomplex&other)const
{
return Cmycomplex(real +other.real,imag+ other.imag);
}
Cmycomplex operator -(const Cmycomplex & other)const
{
return Cmycomplex(real-other.real, imag-other.imag);
}
Cmycomplex operator *(const Cmycomplex & other)const
{
return Cmycomplex(real*other.real-imag *other.imag, real*other.imag + imag * other.real);
}
bool operator==(const Cmycomplex& other) const
{
return (real == other.real) && (imag == other.imag);
}
bool operator != (const Cmycomplex & other) const
{
return !(*this == other);
}
void Show() const
{
cout << setiosflags(ios :: fixed) <<setprecision(2);
if(imag >= 0)
{
cout <<"(" <<real <<"+"<<imag <<"i)";
}
else
{
cout <<"(" <<real <<imag <<"i)";
}
}
};
//后缀代码:
//StudybarCommentBegin
int main()
{
Cmycomplex z1(3,4),z2(7),z3;
double x,y;
cin>>x>>y;
z1.Set(x,y);
cin>>x>>y;
z2.Set(x,y);
if(z1==z2)
cout<<"y\n";
else
cout<<"n\n";
if(z1!=z2)
cout<<"y\n";
else
cout<<"n\n";
}
//StudybarCommentEnd
10、友元函数计算2+z
如题,要求实现:
1、输入一个复数
2、按后缀那样计算
3、Show出结果。
例如输入:
7 8
输出
(14+8i)
(5+4i)
(7+4i)
//如题,要求实现:
//
//1、输入一个复数
//
//2、按后缀那样计算
//
//3、Show出结果。
//
//例如输入:
//
//7 8
//
//输出
//
//(14+8i)
//(5+4i)
//(7+4i)
#include <iostream>
using namespace std;
class Cmycomplex
{
private:
double real;
double imag;
public:
Cmycomplex(int r = 0,int i = 0):real(r),imag(i){
}
void Set(int r,int i)
{
real = r;
imag = i;
}
Cmycomplex operator+(const Cmycomplex&other)const
{
return Cmycomplex(real +other.real,imag+ other.imag);
}
Cmycomplex operator+(double num) const
{
return Cmycomplex(real + num , imag);
}
friend Cmycomplex operator+(double num,const Cmycomplex& complex)
{
return Cmycomplex(complex.real+num ,complex.imag);
}
void Show() const
{
cout <<"(" <<real;
if(imag >= 0)
{
cout <<"+";
cout <<imag <<"i)";
}
else
{
cout <<imag <<"i)";
}
}
};
//StudybarCommentBegin
int main()
{
Cmycomplex z1(3,4),z2(7),z3,z4(z1);
double x,y;
cin>>x>>y;
z3.Set(x,y);
cout<<endl;
z3=z3+z2;
z3.Show();
z4=2+z4;
cout<<endl;
z4.Show();
z4=z4+2;
cout<<endl;
z4.Show();
}
//StudybarCommentEnd
11、含友元函数运算符重载(含输入输出重载)的复数类
实现一个复数类,要求重载加减乘除及输入输出,输出保留两位小数。
依次输出加减乘除的结果。
例:
输入:
3+4i 7+8i
输出
10.00+12.00i
-4.00-4.00i
-11.00+52.00i
0.47+0.04i
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cmath>
class ComplexNumber {
private:
double real;
double imag;
public:
ComplexNumber(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
// Addition operator
ComplexNumber operator+(const ComplexNumber& other) const {
return ComplexNumber(real + other.real, imag + other.imag);
}
// Subtraction operator
ComplexNumber operator-(const ComplexNumber& other) const {
return ComplexNumber(real - other.real, imag - other.imag);
}
// Multiplication operator
ComplexNumber operator*(const ComplexNumber& other) const {
double r = real * other.real - imag * other.imag;
double i = real * other.imag + imag * other.real;
return ComplexNumber(r, i);
}
// Division operator
ComplexNumber operator/(const ComplexNumber& other) const {
double denominator = other.real * other.real + other.imag * other.imag;
double r = (real * other.real + imag * other.imag) / denominator;
double i = (imag * other.real - real * other.imag) / denominator;
return ComplexNumber(r, i);
}
// Input operator
friend std::istream& operator>>(std::istream& is, ComplexNumber& cn) {
std::string input;
is >> input;
size_t plus_pos = input.find('+');
size_t minus_pos = input.find('-', 1); // Skip first character if it's '-'
size_t i_pos = input.find('i');
if (i_pos == std::string::npos) {
cn.real = std::stod(input);
cn.imag = 0.0;
} else {
if (plus_pos != std::string::npos) {
cn.real = std::stod(input.substr(0, plus_pos));
cn.imag = std::stod(input.substr(plus_pos + 1, i_pos - plus_pos - 1));
} else if (minus_pos != std::string::npos) {
cn.real = std::stod(input.substr(0, minus_pos));
cn.imag = std::stod(input.substr(minus_pos, i_pos - minus_pos));
} else {
if (input[0] == 'i') {
cn.real = 0.0;
cn.imag = 1.0;
} else if (input == "-i") {
cn.real = 0.0;
cn.imag = -1.0;
} else {
cn.real = 0.0;
cn.imag = std::stod(input.substr(0, i_pos));
}
}
}
return is;
}
// Output operator
friend std::ostream& operator<<(std::ostream& os, const ComplexNumber& cn) {
os << std::fixed << std::setprecision(2);
if (cn.imag == 0) {
os << cn.real;
} else {
os << cn.real;
if (cn.imag >= 0) {
os << "+" << cn.imag << "i";
} else {
os << cn.imag << "i";
}
}
return os;
}
};
//StudybarCommentBegin
void CN() { //ComplexNumber
ComplexNumber cn1, cn2;
std::cin >> cn1 >> cn2;
std::cout << (cn1 + cn2) << std::endl;
std::cout << (cn1 - cn2) << std::endl;
std::cout << (cn1 * cn2) << std::endl;
std::cout << (cn1 / cn2) << std::endl;
}
int main(void) {
CN();
return 0;
}
//StudybarCommentEnd