1. 程式人生 > >引入類型struct(結構),提高代碼的邏輯性和可讀性

引入類型struct(結構),提高代碼的邏輯性和可讀性

復數 and 可讀性 complex tchar add ace pac int

創建一個新類型(struct, 即 結構)SComplex 來表示復數,提高了代碼的邏輯性和可讀性,代碼更加的簡潔。

#include "stdafx.h"
#include "iostream"
using namespace std;

struct SComplex
{
    double dReal;
    double dImg;
};

SComplex ComplexAdd(SComplex c1,SComplex c2)
{
    SComplex c;
    c.dReal = c1.dReal + c2.dReal;
    c.dImg = c1.dImg + c2.dImg;
    
return c; } double Rand(double dMin, double dMax) { double dVal = (double)rand() / RAND_MAX; return dMin + dVal * (dMax - dMin); } int _tmain(int argc, _TCHAR* argv[]) { SComplex c1; c1.dReal = Rand(-10, 10); c1.dImg = Rand(-10, 10); SComplex c2; c2.dReal = Rand(-10, 10
); c2.dImg = Rand(-10, 10); SComplex c = ComplexAdd(c1, c2); cout << c.dReal << "+" << c.dImg << endl; getchar(); return 0; }

引入類型struct(結構),提高代碼的邏輯性和可讀性