zoukankan      html  css  js  c++  java
  • 九周 项目 1

    /*
    * 程序的版权和版本号声明部分:
    * Copyright (c) 2013, 烟台大学计算机学院
    * All rights reserved.
    * 文件名:test.cpp
    * 作    者:赵加响
    * 完毕日期:2014年 4月 22日
    * 输入描写叙述:
    * 程序输出:
    * 问题分析:略
    * 算法设计:略
    */
    #include <iostream>
    using namespace std;
    class Complex
    {
    public:
        Complex(){real=0;imag=0;}
        Complex(double r,double i){real=r; imag=i;}
        Complex operator+(Complex &c2);
        Complex operator-(Complex &c2);
        Complex operator*(Complex &c2);
        Complex operator/(Complex &c2);
        friend ostream&operator<<(ostream&,Complex&);
        friend Complex operator-(Complex &c);//取负
    private:
        double real;
        double imag;
    };
    //以下定义成员函数
    Complex operator-(Complex &c)
    {
        Complex c1;
        c1.imag=0-c.imag;
        c1.real=0-c.real;
        return c1;
    }
    Complex Complex::operator+(Complex &c2)
    {
         Complex c;
         c.real=real+c2.real;
         c.imag=imag+c2.imag;
         return c;
    }
    Complex Complex::operator-(Complex &c2)
    {
         Complex c;
         c.real=real-c2.real;
         c.imag=imag-c2.imag;
         return c;
    }
    Complex Complex::operator*(Complex &c2)
    {
         Complex c;
         c.real=(real*c2.real-imag*c2.imag);
         c.imag=imag*c2.real+real*c2.imag;
         return c;
    }
    Complex Complex::operator/(Complex &c2)
    {
         Complex c;
         c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
         c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
         return c;
    }
    ostream&operator<<(ostream&output,Complex&c)
    {
        if(c.imag>0)
        {
        output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
        }
        else
        output<<"("<<c.real<<"-"<<-c.imag<<"i)"<<endl;
        return output;
    }
    //以下定义用于測试的main()函数
    int main()
    {
        Complex c1(3,4),c2(5,-10),c3;
        cout<<"c1=";
        cout<<c1<<endl;
        cout<<"c2=";
        cout<<c2<<endl;
        c3=c1+c2;
        cout<<"c1+c2=";
        cout<<c3<<endl;
        c3=c1-c2;
        cout<<"c1-c2=";
        cout<<c3<<endl;
        c3=c1*c2;
        cout<<"c1*c2=";
        cout<<c3<<endl;
        c3=c1/c2;
        cout<<"c1/c2=";
        cout<<c3<<endl;
        return 0;
    }
    


    感悟:还行吧!!

    !。!

  • 相关阅读:
    qt解决中文乱码
    二维数组及指针分析
    pyhon Django框架
    java回调(钩子函数)
    java.util.concurrent java并发工具包
    CountDownLatch 计数器
    报表 图形接口查询 (年月周日)
    pg 日期函数
    linux 执行脚本报错 No such file or directory
    python 处理数据常用操作
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7082170.html
Copyright © 2011-2022 走看看