Sunday, January 1, 2012

複數的加減乘除的C++類別與程式

// Complex.cpp 

#include "StdAfx.h"
#include "Complex.h"
#include <iostream>

using namespace std;

Complex::Complex(double r, double i)
{
    real = r;
    imaginary = i;
}

Complex::~Complex(void)
{
}

void Complex::Add(Complex &c)
{

    real += c.real;
    imaginary += c.imaginary;

}


void Complex::Subtract(Complex &c)
{

    real -= c.real;
    imaginary -= c.imaginary;

}


void Complex::Multiply(Complex &c)
{
    real = real * c.real - imaginary * c.imaginary;
    imaginary = real * c.imaginary + imaginary * c.real;
}

void Complex::Divide(Complex &c)
{

    if ( c.real != 0 || c.imaginary != 0)
    {
    
        double fenmu = c.real * c.real + c.imaginary * c.imaginary;
        real = real * c.real + imaginary * c.imaginary;
        real /= fenmu;

        imaginary = imaginary * c.real - real * c.imaginary;
        imaginary / fenmu;
    }
    else
    {
        cout << "Cannot divide a number by zero.\n";
    }

}


void Complex::OutputValue()
{
    if (real == 0 && imaginary == 0)
        cout << "0\n";
    else if (real == 0)
    {
        if (imaginary > 0)
            cout << imaginary << "*i\n";
        else
            cout << -imaginary << "*i\n";
    }
    else if (imaginary == 0)
        cout << real << "\n";
    else
    {
        if (imaginary > 0)
            cout << real << " + " << imaginary << "*i\n";

        else
            cout << real << " - " << -imaginary << "*i\n";
    }
}
//Complex.h

#pragma once

class Complex
{
public:
    Complex(double = 0, double = 0);
    ~Complex(void);

    void Add(Complex &);
    void Subtract(Complex &);
    void Multiply(Complex &);
    void Divide(Complex &);

    void OutputValue();


private:
    float real;
    float imaginary;

};

// main.cpp : 
//

#include "stdafx.h"
#include <stdlib.h>
#include "Complex.h"
#include <iostream>
#include <ctype.h>
#define size 12

using namespace std;


bool IsDouble(char a[])
{
    int i;
    int pointsNum = 0;

    if ( !isdigit(a[0])  && a[0] != '-' && a[0] != '.')
        return false;

    if (a[0] == '.')
        pointsNum = 1;

    for (i = 1; i < size, a[i] != '\0' ; i ++ )
    {
        if ( !isdigit(a[i]) && a[i] != '.')
            return false;
        else if (a[i] == '.')
            pointsNum ++;
    }

    if (pointsNum <= 1 && i < 12)
        return true;

}


int _tmain(int argc, _TCHAR* argv[])
{
    char rr[size], ii[size];
    double r, i;

    cout << "Enter complex number c1.\n";
    cout << "real part: ";
    cin >> rr;

    while (!IsDouble(rr))
    {
        cout << "Sorry, please input a real number";
        cin >> rr;
    }
    
    r = atof(rr);
    
    cout << "imaginary part: ";
    cin >> ii;

    while (!IsDouble(ii))
    {    
        cout << "Sorry, please input a real number";
        cin >> ii;
    }
    
    i = atof(ii);

    Complex c1(r, i);

    //////////

    cout << "Enter complex number c2.\n";
    cout << "real part: ";
    cin >> rr;

    while (!IsDouble(rr))
    {
        cout << "Sorry, please input a real number";

        cin >> rr;
    }

    r = atof(rr);

    cout << "imaginary part: ";
    cin >> ii;

    while (!IsDouble(ii))
    {    
        cout << "Sorry, please input a real number";
        cin >> ii;
    }
    
    
    i = atof(ii);

    Complex c2(r, i);

    ///////

    Complex tempc1, tempc2;
    tempc1 = c1;
    tempc2 = c2;

    cout << "\nThe value c1 + c2 is :";
    c1.Add(c2);
    c1.OutputValue();

    /////////

    c1 = tempc1;
    c2 = tempc2;

    cout << "\nThe value c1 - c2 is :";
    c1.Subtract(c2);
    c1.OutputValue();

    /////////

    c1 = tempc1;
    c2 = tempc2;

    cout << "\nThe value c1 * c2 is :";
    c1.Multiply(c2);
    c1.OutputValue();

    /////////

    c1 = tempc1;
    c2 = tempc2;

    cout << "\nThe value c1 / c2 is :";
    c1.Divide(c2);
    c1.OutputValue();

    /////////

    c1 = tempc1;
    c2 = tempc2;

    system("pause");
    return 0;
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. 另外stdafx.h是

    #pragma once
    #include "targetver.h"
    #include
    #include

    stdafx.cpp是
    #include "stdafx.h"

    targetver.h是

    #pragma once

    // 下列巨集會定義最低平台需求。最低平台需求是指各種版本的Windows、Internet Explorer 中,具備執行應用程式所需功能的最早版本。巨集的作用,是在指定或更新版本的平台上啟用所有可用的功能。

    // 如果您有必須優先選取的平台,請修改下列定義。
    參考 MSDN 取得不同平台對應值的最新資訊。

    #ifndef _WIN32_WINNT // 指定最低平台需求為 Windows Vista。
    #define _WIN32_WINNT 0x0600 // 將它變更為針對 Windows 其他版本的適當值。
    #endif

    ReplyDelete