5

Struct

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct _complex
  6. {
  7.     double r, i;
  8. } Complex;
  9.  
  10. Complex mult( Complex a, Complex b )
  11. {
  12.     Complex p;
  13.  
  14.     p.r = a.r * b.r - a.i * b.i;
  15.     p.i = a.i * b.r + a.r * b.i;
  16.  
  17.     return p;
  18. }
  19.  
  20. void print( Complex c )
  21. {
  22.     cout << "(r=" << c.r << " i=" << c.i << ")";
  23. }
  24.  
  25. int main() {
  26.     Complex x, y;
  27.  
  28.     x.r = 2.2;
  29.     x.i = -1.3;
  30.     y.r = 4.1;
  31.     y.i = 1;
  32.  
  33.     Complex c = mult( x, y );
  34.  
  35.     cout << "c=";
  36.     print( c );
  37.     cout << endl;
  38. }
$ g++ -g -o struct struct.cpp
$ ./struct
c=(r=10.32 i=-3.13)

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].