精华内容
下载资源
问答
  • 复数运算符重载

    2018-03-26 09:09:51
    复数运算符重载,主要用于复数的运算,是控制台程序,不懂可以留言!
  • 在学习了运算符重载后,知道了运算符重载的本质是一个特殊的成员或友元函数,重载后原意义没有失去,而是定义了相对类的一个新运算符。下面来做个运算符重载的练习: 先在复数类中声明好函数原型与复数私有变量 ...

    在学习了运算符重载后,知道了运算符重载的本质是一个特殊的成员或友元函数,重载后原意义没有失去,而是定义了相对类的一个新运算符。下面来做个运算符重载的练习:
    在这里插入图片描述
    先在复数类中声明好函数原型与复数私有变量

    class Complex {
    public:
    	Complex(int a=0, int b=0) { n = a, d = b; }
    	Complex operator =(Complex &);
    	Complex operator ++();
    	Complex operator ++(int);
    	Complex operator --();
    	Complex operator --(int);
    	friend Complex operator +(const Complex &, const Complex &);
    	friend Complex operator -(const Complex &, const Complex &);
    	friend Complex operator *(const Complex &, const Complex &);
    	friend Complex operator /(const Complex &, const Complex &);
    	friend ostream & operator<<(ostream &, Complex &);
    	friend istream & operator>>(istream & , Complex &);
    	int getN() {
    		return n;
    	}
    	int getD() {
    		return d; 
    	}
    private:
    	int n;
    	int d;
    };
    

    再在对应cpp写好函数定义:

    Complex Complex::operator =(Complex & a) {
    	n = a.n; d = a.d;
    	return *this;
    }
    Complex operator +(const Complex & a, const Complex & b) {
    	int n, d;
    	n = a.n + b.n;
    	d = a.d + b.d;
    	return Complex(n, d);
    }
    Complex operator -(const Complex & a, const Complex & b) {
    	return Complex(a.n - b.n, a.d - b.d);
    }
    Complex operator *(const Complex & a, const Complex & b) {	
    	return Complex((a.n * b.n) - (a. d * b.d), (a.n * b.d) + (a. d * b.n));
    }
    Complex operator /(const Complex & a, const Complex & b) {
    	return Complex(((a.n * b.n) + (a.d * b.d)) / (b.n*b.n + b.d*b.d), (a.d * b.n - a.n*b.d) / (b.n*b.n + b.d*b.d));
    }
    ostream & operator<<(ostream & output, Complex & a) {
    	output << a.getN() << "+" << a.getD() << "i" << endl;
    	return output;
    }
    istream & operator>>(istream & input, Complex & a) {
    	int n, d;
    	input >> n;	input >> d;
    	a = Complex(n, d);
    	return input;
    }
    Complex Complex::operator ++() {
    	n++; d++;
    	return *this;
    }
    Complex Complex::operator ++(int) {
    	Complex temp;
    	temp.n = n++;
    	temp.d = d++;
    	return temp;
    }
    Complex Complex::operator --() {
    	n--; d--;
    	return *this;
    }
    Complex Complex::operator --(int) {
    	Complex temp;
    	temp.n = n--;
    	temp.d = d--;
    	return temp;
    }
    

    写段测试代码:

    void main(){
    	Complex a(1, 2);
    	cin >> a;
    	a++;
    	cout << a;
    	++a;
    	cout << a;
    	a = Complex(1);
    	cout << a;
    }
    

    在这里插入图片描述

    达成了练习的目的,目标达成!

    展开全文
  • 复数运算符重载(C++)

    2021-03-13 22:18:22
    这是用C++实现的对与复数相关的运算符重载 首先在头文件(Complex.h)中创建复数类,加入成员属性,声明成员方法: #pragma once #include<iostream> #include<cmath> using namespace std; class ...

    这是用C++实现的对与复数相关的运算符重载

    首先在头文件(Complex.h)中创建复数类,加入成员属性,声明成员方法:

    #pragma once
    #include<iostream>
    #include<cmath>
    using namespace std;
    class Complex {
    	friend ostream& operator<<(ostream& cout, const Complex& complex);
    private:
    	double real, image;
    public:
    	Complex(double real = 0.0, double image = 0.0);
    	Complex operator+(const Complex& complex);
    	Complex operator-(const Complex& complex);
    	Complex operator*(const Complex& complex);
    	Complex operator/(const Complex& complex);
    	Complex operator+(double n);
    	Complex operator-(double n);
    	Complex operator*(double n);
    	Complex operator/(double n);
    	friend Complex operator+(double n, const Complex& c);
    	friend Complex operator-(double n, const Complex& c);
    	friend Complex operator*(double n, const Complex& c);
    	friend Complex operator/(double n, const Complex& c);
    };
    

    然后在源文件(Complex.cpp)中实现成员方法:

    #include "Complex.h"
    
    Complex::Complex(double real, double image){
    	this->real = real;
    	this->image = image;
    }
    
    Complex Complex::operator+(const Complex& complex)
    {
    	Complex temp(this->real + complex.real, this->image + complex.image);
    	return temp;
    }
    
    Complex Complex::operator-(const Complex& complex){
    	Complex temp(this->real - complex.real, this->image - complex.image);
    	return temp;
    }
    
    Complex Complex::operator*(const Complex& complex){
    	double a = this->real, b = this->image, c = complex.real, d = complex.image;
    	Complex temp(a * c - b * d, b * c + a * d);
    	return temp;
    }
    
    Complex Complex::operator/(const Complex& complex){
    	double a = this->real, b = this->image, c = complex.real, d = complex.image;
    	Complex temp;
    	if (c == 0 && d == 0)
    		cout << "除数不能为零!";
    	else {
    		temp.real = (a * c + b * d) / (c * c + d * d);
    		temp.image = (b * c + a * d) / (c * c, d * d);
    	}
    	return temp;
    }
    
    Complex Complex::operator+(double n){
    	Complex temp(this->real + n, this->image);
    	return temp;
    }
    
    Complex Complex::operator-(double n){
    	Complex temp(real - n, image);
    	return temp;
    }
    
    Complex Complex::operator*(double n){
    	Complex temp(real * n, image * n);
    	return temp;
    }
    
    Complex Complex::operator/(double n){
    	Complex temp;
    	if (n == 0)
    		cout << "除数不能为零!";
    	else {
    		temp.real = real / n;
    		temp.image = image / n;
    	}
    	return temp;
    }
    
    Complex operator+(double n, const Complex& c){
    	Complex temp(c.real + n, c.image);
    	return temp;
    }
    
    Complex operator-(double n, const Complex& c){
    	Complex temp(c.real - n, c.image);
    	return temp;
    }
    
    Complex operator*(double n, const Complex& c){
    	Complex temp(c.real * n, c.image * n);
    	return temp;
    }
    
    Complex operator/(double n, const Complex& c){
    	Complex temp;
    	if (c.real == 0 && c.image == 0)
    		cout << "除数不能为零!";
    	else {
    		temp.real = (c.real * n) / (c.real * c.real + c.image * c.image);
    		temp.image = (c.image * n) / (c.real * c.real + c.image * c.image);
    	}
    	return temp;
    }
    
    

    值得注意的是,在创建double型对复数运算符重载时,由于传参不同,因此需要创建为全局函数,且在头文件中声明的时候也应该声明成类的友元函数。

    另附上求模函数和<<操作符重载(也需要先在类中声明):

    double Complex::mod(){
    	return sqrt(this->real * this->real + this->image * this->image);
    }
    
    ostream& operator<<(ostream& cout, const Complex& complex){
    	if (complex.image != 0) {
    		cout << complex.real << " + " << complex.image << "i";
    	}
    	else {
    		cout << complex.real;
    	}
    	return cout;
    }
    

    测试代码

    #include"Complex.h"
    
    int main() {
    	Complex c1(3, 4);
    	Complex c2(6, 6);
    	cout << "c1 = " << c1 << '\t' << "c2 = " << c2 << endl;
    	cout << "c1 + c2 = " << c1 + c2 << endl;
    	cout << "c1 - c2 = " << c1 - c2 << endl;
    	cout << "c1 * c2 = " << c1 * c2 << endl;
    	cout << "c1 / c2 = " << c1 / c2 << endl;
    	cout << "c1的模为:" << c1.mod() << endl;
    	cout << "c1 + 10 = " << c1 + 10 << endl;
    	cout << "c1 - 10 = " << c1 - 10 << endl;
    	cout << "c1 * 10 = " << c1 * 10 << endl;
    	cout << "c1 / 10 = " << c1 / 10 << endl;
    	cout << "10 + c2 = " << 10 + c2 << endl;
    	cout << "10 - c2 = " << 10 - c2 << endl;
    	cout << "10 * c2 = " << 10 * c2 << endl;
    	cout << "10 / c2 = " << 10 / c2 << endl;
    	
    	return 0;
    }
    

    在这里插入图片描述

    都看到这了,不妨给博主点个小赞吧,谢谢啦~

    展开全文
  • C++复数运算符重载(+与)

    千次阅读 2014-06-03 17:31:08
    Problem A: C++复数运算符重载(+与 [Submit][Status][Web Board] Description 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的两个运算量可以都是类...

    Problem A: C++复数运算符重载(+与<<)


    [ Submit][ Status][ Web Board]

    Description

    定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。
    (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。
    (2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。
    编写程序,分别求两个复数之和、整数和复数之和,并且输出。



    Input

    一个复数的实部和虚部,另一个复数的实部和虚部 
    一个复数的实部和虚部,一个实数 
    一个实数,一个复数的实部和虚部

    Output

    两个复数之和、复数和实数之和,实数和复数之和。

    Sample Input

    3 4 5 -103 4 55 3 4

    Sample Output

    c1+c2=(8.00-6.00i)c1+d=(8.00+4.00i)d+c1=(8.00+4.00i)

    HINT

    #include <iostream>
    #include <iomanip>
    using namespace std;
     
    class Complex
    {
    public:
        Complex():real(0),imag(0) {}
        Complex(double r,double i):real(r),imag(i) {}
        Complex operator+(Complex &);
        Complex operator+(double &);
        friend Complex operator+(double&,Complex &);
        friend ostream& operator << (ostream& output, const Complex& c);
    private:
        double real;
        double imag;
    };
     
    Complex Complex::operator+(Complex &p)
    {
        return Complex(real+p.real,imag+p.imag);
     
    }
    Complex Complex::operator+(double &p)
    {
        return Complex(real+p,imag);
     
    }
    Complex operator+(double&p,Complex &p1)
    {
        return Complex(p+p1.real,p1.imag);
     
     
    }
    ostream& operator << (ostream& output, const Complex& c)
    {
        if(c.imag>0)
         
        output<<setiosflags(ios::fixed)<<setprecision(2)<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
        else
                output<<setiosflags(ios::fixed)<<setprecision(2)<<"("<<c.real<<c.imag<<"i)"<<endl;
     
     
        return output;
     
    }
     
    int main()
    {
        //测试复数加复数
        double real,imag;
        cin>>real>>imag;
        Complex c1(real,imag);
        cin>>real>>imag;
        Complex c2(real,imag);
        Complex c3=c1+c2;
        cout<<"c1+c2=";
        cout<<c3;
     
        //测试复数加实数
        double d;
        cin>>real>>imag;
        cin>>d;
        c3=Complex(real,imag)+d;
        cout<<"c1+d=";
        cout<<c3;
     
        //测试实数加复数
        cin>>d;
        cin>>real>>imag;
        c1=Complex(real,imag);
        c3=d+c1;
        cout<<"d+c1=";
        cout<<c3;
     
        return 0;
    }



    展开全文
  • 2535: C++复数运算符重载(+与<<) 时间限制:1 Sec内存限制:128 MB 提交:867解决:532 题目描述 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的...

    2535: C++复数运算符重载(+与<<)

    时间限制: 1 Sec   内存限制: 128 MB
    提交: 867   解决: 532

    题目描述

    定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。
    (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。
    (2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。
    编写程序,分别求两个复数之和、整数和复数之和,并且输出。

    请在下面的程序段基础上完成设计:
    #include <iostream>
    #include <iomanip>
    using namespace std;

    class Complex
    {
    public:
    Complex():real(0),imag(0) {}
    Complex(double r,double i):real(r),imag(i) {}
    Complex operator+(Complex &);
    Complex operator+(double &);
    friend Complex operator+(double&,Complex &);
    friend ostream& operator << (ostream& output, const Complex& c);
    private:
    double real;
    double imag;
    };

    //将程序需要的其他成份写在下面,只提交begin到end部分的代码
    //******************** begin ********************


    //********************* end ********************
    int main()
    {
    //测试复数加复数
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    cout<<c3;

    //测试复数加实数
    double d;
    cin>>real>>imag;
    cin>>d;
    c3=Complex(real,imag)+d;
    cout<<"c1+d=";
    cout<<c3;

    //测试实数加复数
    cin>>d;
    cin>>real>>imag;
    c1=Complex(real,imag);
    c3=d+c1;
    cout<<"d+c1=";
    cout<<c3;

    return 0;
    }

    输入

    一个复数的实部和虚部,另一个复数的实部和虚部 
    一个复数的实部和虚部,一个实数 
    一个实数,一个复数的实部和虚部

    输出

    两个复数之和、复数和实数之和,实数和复数之和。

    样例输入

    3 4 5 -10
    3 4 5
    5 3 4

    样例输出

    c1+c2=(8.00-6.00i)
    c1+d=(8.00+4.00i)
    d+c1=(8.00+4.00i)

    提示

    只提交自己定义的函数部分,控制输出小数点后两位,用 setiosflags(ios::fixed);和setprecision(2);控制



    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <iostream>
    #include <iomanip>
    using namespace std;
    class Complex
    {
    public:
        Complex():real(0),imag(0) {}
        Complex(double r,double i):real(r),imag(i) {}
        Complex operator+(Complex &);
        Complex operator+(double &);
        friend Complex operator+(double&,Complex &);
        friend ostream& operator << (ostream& output, const Complex& c);
    private:
        double real;
        double imag;
    };
    Complex Complex::operator+(double &x)
    {
        Complex c;
        c.real=x+real;
        c.imag=imag;
        return c;
    }
    Complex Complex::operator+(Complex &c1)
    {
        Complex c;
        c.real=real+c1.real;
        c.imag=imag+c1.imag;
        return c;
    }
    ostream& operator << (ostream& output, const Complex& c)
    {
        output<<"("<<setiosflags(ios::fixed)<<setprecision(2)<<c.real;
        output<<setiosflags(ios::fixed)<<setprecision(2);
        if(c.imag<0)cout<<c.imag<<"i)"<<endl;
        else cout<<"+"<<c.imag<<"i)"<<endl;
        return output;
    }
    Complex operator+(double &x,Complex &c1)
    {
        return Complex(x+c1.real,c1.imag);
    }
    int main()
    {
        //测试复数加复数
        double real,imag;
        cin>>real>>imag;
        Complex c1(real,imag);
        cin>>real>>imag;
        Complex c2(real,imag);
        Complex c3=c1+c2;
        cout<<"c1+c2=";
        cout<<c3;
     
        //测试复数加实数
        double d;
        cin>>real>>imag;
        cin>>d;
        c3=Complex(real,imag)+d;
        cout<<"c1+d=";
        cout<<c3;
     
        //测试实数加复数
        cin>>d;
        cin>>real>>imag;
        c1=Complex(real,imag);
        c3=d+c1;
        cout<<"d+c1=";
        cout<<c3;
     
        return 0;
    }

    转载于:https://www.cnblogs.com/im0qianqian/p/5989488.html

    展开全文
  • cout 复数复数:" ; cout ; c3.display(); c3 = c1 - c2; cout ; c3.display(); c3 = c1 * c2; cout *c2="; c3.display(); c3 = c1 / c2; cout ; c3.display(); cout ; cout 复数与实数:" ; cout ; cout ; c1....
  • Problem J: C++复数运算符重载(+与 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 755 Solved: 466 [Submit][Status][Web Board] Description 定义一个复数类Complex,重载运算符“+”,使之能用于...
  • c1,c2,c3,c4都是复数 已经可以使实现 c3=c1+c2; c3=c1*c2; 但c3=c1+(c1*c2);就会出现 [img=https://img-bbs.csdn.net/upload/202005/25/1590383133_465513.png][/img] 请问这是为什么呢
  • *文件名称: C++复数运算符重载(+与) *作者:刘中林 *完成日期:2014 年 6 月 12 日 *版本号:v1.0 *对任务及求解方法的描述部分: *输入描述:定义一个复数类Complex,重载运算符“+”,使之
  • 复数运算符重载(3)

    2016-05-23 19:32:02
      ... ...*问题描述:定义一个定义完整的类(是可以当作独立的产品发布,成为众多项目中的“基础工程”)。这样的类在(2)的基础上,扩展+、-、*、/运算符的功能,使之能与double型... 学会了复数运算符重载
  • OJ2535复数运算符重载

    2014-06-11 18:51:00
    //测试复数复数 double real,imag; cin>>real>>imag; Complex c1(real,imag); cin>>real>>imag; Complex c2(real,imag); Complex c3=c1+c2; cout; cout; //测试复数加实数 double d; cin>>real>>imag; cin>>d; c3...
  • /* ... * All rights reserved. * 作 者: 霍雨佳 * 完成日期:2014 年5月20日 * 版 本 号:v1.0 ...* 问题分析:定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 *(1
  • //#include //using namespace std; #include class Complex { private: double real; double img; public: Complex(double lp=0.0,double rp=0.0);...最近写的关于C++ 复数运算符重载的,特分享一下
  • 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数...
  • 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为...
  • //测试复数复数 double real,imag; cin>>real>>imag; Complex c1(real,imag); cin>>real>>imag; Complex c2(real,imag); Complex c3=c1+c2; cout; cout; //测试复数加实数 double d; cin>>real>>imag; ...
  • 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为...
  • 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为...
  • //测试复数复数 double real,imag; cin>>real>>imag; Complex c1(real,imag); cin>>real>>imag; Complex c2(real,imag); Complex c3=c1+c2; cout; //测试复数加实数 double d; cin>>real>>imag; cin>>...
  • //测试复数复数 double real,imag; cin>>real>>imag; Complex c1(real,imag); cin>>real>>imag; Complex c2(real,imag); Complex c3=c1+c2; cout; cout; //测试复数加实数 double d; cin>>...
  • 定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。 (1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为...
  • * 问题描述: 通过运算符重载,计算复数 * 程序输出: 计算后的复数 * 程序头部的注释结束 */ #include using namespace std; class Complex { public: Complex(){real=0;imag=0;} Complex(double ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 7,629
精华内容 3,051
关键字:

复数运算符的重载