Make your own free website on Tripod.com
« July 2018 »
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
You are not logged in. Log in
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Midterm
Saturday, 29 March 2008
Midterm

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#ifndef _COMPLEX0_H_
#define _COMPLEX0_H_
#include<iostream>
using namespace std;

class Complex{
      private:
              double real, imaginary;
      public:
              Complex();
              Complex(double, double);
              Complex operator+(const Complex &) const;
              Complex operator-(const Complex &) const;
              Complex operator*(const Complex &) const;
              Complex operator*(const double &) const;
              friend Complex operator~(const Complex &);
              friend Complex operator+(const double&, const Complex &);
              friend Complex operator*(const double&, const Complex &);
              friend ostream & operator << (ostream &, const Complex &);
              friend bool operator >> (istream &, Complex &);
};

Complex::Complex(){
      real = 0;
      imaginary = 0;
}

Complex::Complex(double r, double i){
      real=r;
      imaginary=i;
}

Complex Complex::operator+(const Complex &x) const{
      return Complex (real + x.real ,imaginary + x.imaginary);
}

Complex operator+(const double &x, const Complex &y){
       return Complex (x + y.real, y.imaginary);
}

Complex Complex::operator-(const Complex &x) const{
       return Complex (real - x.real, imaginary - x.imaginary);
}

Complex Complex::operator*(const Complex &x) const{
       return Complex (real * x.real - imaginary * x.imaginary, real * x.imaginary + imaginary * x.real );
}

Complex Complex::operator*(const double & x) const{
      return Complex (x * real, x * imaginary);
}

Complex operator~(const Complex & x) {
       return Complex (x.real ,-x.imaginary);
}

Complex operator*(const double & x, const Complex &y) {
       return Complex (x * y.real, x * y.imaginary);
}

ostream & operator << (ostream & os, const Complex & x) {
       os << "("<< x.real <<", " << x.imaginary << "i)";
       return os;
}
bool operator >> (istream & is, Complex & x) {
       cout<<"Real part: ";
       cin>> x.real;
       if (cin.fail()) {
            return false;
       }
       else {
            cout<<"Imaginary part: ";
            cin>> x.imaginary;
            return true; 
       }
#endif
}

int main()
{
    Complex a(3.0, 4.0);   // initialize to (3,4i)
    Complex c;
    cout << "Enter a complex number (q to quit):\n";
    while (cin >> c) //  **********20%
    {
        cout << "c is " << c << '\n'; //  **********10%
        cout << "complex conjugate is " << ~c << '\n'; //  **********10%
        cout << "a is " << a << '\n'; //  **********10%
        cout << "a + c is " << a + c << '\n'; //  **********10%
        cout << "2.0 + c is " << 2.0 +c << '\n'; //  **********10% global
        cout << "a - c is " << a - c << '\n'; //  **********10%
        cout << "a * c is " << a * c << '\n'; //  **********10%
        cout << "2 .0* c is " << 2.0 * c << '\n'; //  **********10% global
        cout << "Enter a complex number (q to quit):\n";
    }
    cout << "Done!\n";
    return 0;
}


Posted by veronicak5678 at 1:34 AM EDT

Newer | Latest | Older