/* Program: complex.cpp Author: Dr. C. S. Tritt Version: 2.0 Created: 10/2/96 Last modified: 9/24/99 This program demonstrates the use of classes and member functions. It uses a complex number class called Complex. Values can be stored in and recalled from Complex objects in rectangular or polar form. This program reads input from a file called "complex.dat" and writes its results to a file called "complex.out." The input file consists of zero or more comment lines that must begin with exclamation marks, followed by one or more data lines. Each data line consists of one of the following a letter "p" followed by a magnitude and an angle (in radians) or a letter "r" followed the real and imaginary parts of a complex number. An example is given below: ! This file contains test data for the complex.cpp program. ! ! Prepared by C. S. Tritt, Ph.D. ! Last updateded 9/24/99 ! ! Format - ! ! p magnitude angle (in radians) ! r xvalue yvalue ! p 100. 0.80 r 50. 50. p 20 4.8 r -100. 25. */ #include // Neeeded for stream I/O #include // Needed for file I/O #include // Needed for I/O manipulators #include // Needed for sqrt and trig functions using namespace std; // Use a typedef to make it easy to change the representation // of real (floating point) numbers. typedef double number; // Declare the complex class. class Complex { public: void rect(number real_part, number imag_part); // Sets value using rectangular form. number real(void); // Returns real part. number imag(void); // Returns imaginary part. void polar(number magnitude, number angle); // Sets value using polar form. number mag(void); // Returns magnitue. number ang(void); // Returns angle (in radians). private: number _real; // Real part. number _imag; // Imaginary part. }; // Declare the skip comments function. int skip_comments(ifstream& input_file, char to_ignore); int main(void) { // This main function reads a series of complex values and // outputs them in both rectangular and complex forms. Complex c; // The complex quantity. number a; // The first value read. number b; // The second value read. int line = 1; // Data line counter. char form; // The format identifier (p = polar, // r = rectangluar). ifstream fin; // Declare the input file stream object. ofstream fout; // Declare the output file stream object. // Open the files, check for errors and skip comments. fin.open("complex.dat", ios::in); if (! fin) { cout << "Can't open input file.\n"; return 1; } if (skip_comments(fin, '!')) { cout << "Error while skipping comments in input file.\n"; return 4; } fout.open("complex.out", ios::out); if (! fout) { cout << "Can't open output file.\n"; return 2; } // Read and output values. while (fin >> form >> a >> b) { if (form == 'p') c.polar(a,b); else if (form == 'r') c.rect(a,b); else { cout << "Can't read line " << line << endl; return 3; } fout << "On line: " << line << endl; fout << "Rectangular form: " << c.real() << " " << c.imag() << endl; fout << "Polar form: " << c.mag() << " " << c.ang() << endl; fout << endl; ++line; } cout << "Done.\n"; return 0; } void Complex::rect(number x, number y) { // This function sets a complex value using rectangular form. _real = x; _imag = y; return; } number Complex::real(void) { // This function returns the real part of a complex value. return _real; } number Complex::imag(void) { // This function returns the imag part of a complex value. return _imag; } void Complex::polar(number mag, number ang) { // This function sets a complex value using polar form (angle must be in // radians). _real = mag * cos(ang); _imag = mag * sin(ang); return; } number Complex::mag(void) { // This function returns the magnitude of a complex value. return sqrt(_real * _real + _imag * _imag); } number Complex::ang(void) { // This function returns the angle (in radians) of a complex value. return atan2(_imag, _real); } int skip_comments(ifstream& file, char mark) { // This function skips the all the lines at the start of the specified // file that begin with the specified character. The file must already // be open when this funciton is called. Error checking not yet // included. const int MAX_CHARS = 100; // This is the maximum characters per line. while (file.peek() == mark) file.ignore(MAX_CHARS, '\n'); return 0; }