zoukankan      html  css  js  c++  java
  • 使用操作符重载来完成复数的计算程序

    main.cpp

     1 #include <stdio.h>
     2 #include "Complex.h"
     3 int main()
     4 {
     5     complex c1(1,2);
     6     complex c2(3,4);
     7     complex c3 = c2 / c1;
     8     complex c4(0, 0);
     9     (c4 = c3) = c1;
    10     printf("c1.a=%f,c1.b=%f
    ", c1.getA(), c1.getB());
    11     printf("c2.a=%f,c2.b=%f
    ", c2.getA(), c2.getB());
    12     printf("c3.a=%f,c3.b=%f
    ", c3.getA(), c3.getB());
    13     printf("c4.a=%f,c4.b=%f
    ", c4.getA(), c4.getB());
    14     if (c1 == c4)
    15     {
    16         printf("c1和c4相等
    ");
    17     }
    18     if (c1 != c2)
    19     {
    20         printf("c1与c2不相等
    ");
    21     }
    22 
    23     complex c5(0,0);
    24     complex c6 = c5++;
    25     printf("c6.a=%f
    ",c6.getA());
    26     printf("c6.b=%f
    ", c6.getB());
    27     complex c7 = ++c5;
    28     printf("c7.a=%f
    ", c7.getA());
    29     printf("c7.b=%f
    ", c7.getB());
    30     return 0;
    31 }

    Complex.cpp

     1 #include "Complex.h"
     2 #include "math.h"
     3 
     4 double complex::getA()
     5 {
     6     return this->a;
     7 }
     8 double complex::getB()
     9 {
    10     return this->b;
    11 }
    12 double complex::getModulus()
    13 {
    14     return sqrt(a*a + b*b);
    15 }
    16 
    17 complex complex:: operator + (const complex&c)
    18 {
    19     double na = a + c.a;
    20     double nb = b + c.b;
    21     complex ret(na,nb);
    22     return ret;
    23 }
    24 complex complex:: operator - (const complex&c)
    25 {
    26     double na = a - c.a;
    27     double nb = b - c.b;
    28     complex ret(na,nb);
    29     return ret;
    30 }
    31 complex complex:: operator * (const complex&c)
    32 {
    33     double na = a*c.a - b*c.b;
    34     double nb = b*c.a - a*c.b;
    35     complex ret(na,nb);
    36     return ret;
    37 }
    38 complex complex:: operator / (const complex&c)
    39 {
    40     double cm = c.a * c.a + c.b * c.b;
    41     double na = (a*c.a + b*c.b) / cm;
    42     double nb = (b*c.a - a*c.b) / cm;
    43     complex ret(na, nb);
    44     return ret;
    45 
    46 }
    47 bool complex:: operator == (const complex&c)
    48 {
    49     return (a == c.a) && (b == c.b);
    50 }
    51 bool complex:: operator != (const complex&c)
    52 {
    53     return !(*this == c);
    54 }
    55 
    56 complex& complex:: operator = (const complex& c)
    57 {
    58     if (this != &c)
    59     {
    60         a = c.a;
    61         b = c.b;
    62     }
    63     return *this;
    64 }
    65 
    66 complex& complex:: operator ++()
    67 { 
    68     a = a + 1;
    69     b = b + 1;
    70     return *this;
    71 }
    72 complex complex:: operator ++(int)
    73 {
    74     complex ret(a, b);
    75     a = a + 1;
    76     b = b + 1;
    77     return ret;
    78 }

    Complex.h

     1 #ifndef _COMPLEX_H_
     2 #define _COMPLEX_H_
     3 
     4 class complex
     5 {
     6     double a;
     7     double b;
     8 public:
     9     complex(double a,double b) :a(0), b(0)
    10     {
    11         this->a = a;
    12         this->b = b;
    13     }
    14     double getA();
    15     double getB();
    16     double getModulus();
    17     complex operator + (const complex&c);
    18     complex operator - (const complex&c);
    19     complex operator * (const complex&c);
    20     complex operator / (const complex&c);
    21     bool operator == (const complex&c);
    22     bool operator != (const complex&c);
    23     complex& operator = (const complex&c);
    24     complex& operator ++();
    25     complex operator ++(int);
    26 
    27 };
    28 
    29 #endif
    主要记录的是学习听课的笔记
  • 相关阅读:
    oracle的安装与plsql的环境配置
    Working with MSDTC
    soapui-java.lang.Exception Failed to load url
    Oracle 一个owner访问另一个owner的table,不加owner
    Call API relation to TLS 1.2
    Call API HTTP header Authorization: Basic
    VS2008 .csproj cannot be opened.The project type is not supported by this installat
    The changes couldn't be completed.Please reboot your computer and try again.
    Create DB Table View Procedure
    DB Change
  • 原文地址:https://www.cnblogs.com/chengeputongren/p/12237261.html
Copyright © 2011-2022 走看看