/* * Programmieren fuer Physiker, SS 2010 * * ein Beispiel fuer struct und Funktionen * hier: ein paar Operationen mit komplexen Zahlen */ #include using namespace std ; struct comp { double re ; double im ; } ; comp add (comp a, comp b) { comp c ; c.re = a.re + b.re ; c.im = a.im + b.im ; return c ; } comp mult (comp a, comp b) { comp c ; c.re = a.re*b.re - a.im*b.im ; c.im = a.re*b.im + a.im*b.re ; return c ; } void print (const comp a) { // a.re += 1 ; // geht nicht mit "const" flag cout << "(Re,Im)=" << a.re << " " << a.im << endl ; } int main() { comp r = { 0.0, 1.0} ; comp s ; s = r ; // ok, elementweises kopieren fuer struct s.re = 1.0 ; // ok, Elementzugriff s.im = 1.0 ; // if (s==r) cout << "Identical\n" ; // "==" nicht definiert // cout << s ; // geht nicht // cout weiss nicht, wie es den struct ausgeben soll. print( add(s, mult( r,s))) ; }