Answers to Question 2.2

/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */
001: char ch; 
002: extern char ch;

003: int count = 1;
004: extern int count;

005: char *name = "Bjarne";
006: extern char *name;

007: struct complex { float re, im; };
008: struct complex;
009: complex *x;

010: complex cvar;
011: extern complex cvar;

012: extern complex sqrt(complex);
013: #include <complex.h> 
014: #include <math.h>
015: // determine sqrt of x + yi
016: complex sqrt(complex z)
017: {
018:     double x = real(z);
019:     double y = imag(z);
020: 
021:     // the easy ones: y == 0
022:     if (y == 0.0)
023: 	if (x < 0.0)
024: 	    return complex(0.0, sqrt(-x));
025: 
026: 	else
027: 	    return complex(sqrt(x), 0.0);
028: 
029:     // almost as easy: x == 0
030:     if (x == 0.0)
031: 	if (y < 0.0)
032: 	    {
033: 	    double x = sqrt(-y / 2);
034: 	    return complex(x, -x);
035: 	    }
036: 
037: 	else
038: 	    {
039: 	    double x = sqrt(y / 2);
040: 	    return complex(x, x);
041: 	    }
042: 
043:     // convert to polar and take the root
044:     //
045:     //        2    2  1/2
046:     // r = ( x  + y )
047:     //
048:     // theta = O- = arc tan (y / x)
049:     //
050:     //  1/2    1/2
051:     // z    = r   (cos O-/2 + i sin O-/2)
052:     double root_r = sqrt(sqrt(x * x + y * y));
053:     double half_t = atan2(y, x) / 2.0;
054:     return complex(root_r * cos(half_t),
055: 		   root_r * sin(half_t));
 }

056: extern int error_number;
057: int error_number;

058: typedef complex point;
059: no answer

060: float real(complex *p) { return p->re; }
061: extern float real(complex *p);

062: const double pi = 3.1415926535897932385;
063: extern const double pi = 3.1415926535897932385;

064: struct user;
065: struct user { char *name; int uid, gid; };

Menu of Chapter 2 Answers 
Answer to Question 2.3