5
Struct
- #include <iostream>
- using namespace std;
- typedef struct _complex
- {
- double r, i;
- } Complex;
- Complex mult( Complex a, Complex b )
- {
- Complex p;
- p.r = a.r * b.r - a.i * b.i;
- p.i = a.i * b.r + a.r * b.i;
- return p;
- }
- void print( Complex c )
- {
- cout << "(r=" << c.r << " i=" << c.i << ")";
- }
- int main() {
- Complex x, y;
- x.r = 2.2;
- x.i = -1.3;
- y.r = 4.1;
- y.i = 1;
- Complex c = mult( x, y );
- cout << "c=";
- print( c );
- cout << endl;
- }
$ g++ -g -o struct struct.cpp
$ ./struct
c=(r=10.32 i=-3.13)
Comments