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;
    }
    


    感悟:还行吧!!

    !。!

  • 相关阅读:
    基础最短路(模板 bellman_ford)
    UVA-12304 Race(递推)
    How do you add?(递推)
    Coconuts, Revisited(递推+枚举+模拟)
    UVA-10726 Coco Monkey(递推)
    UVA-10995 Educational Journey
    UVA-10339 Watching Watches
    【React】377- 实现 React 中的状态自动保存
    【JS】376- Axios 使用指南
    【Nodejs】375- 如何加快 Node.js 应用的启动速度
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7082170.html
Copyright © 2011-2022 走看看