// ************************************************************************ // ICS 211 Data Structures 2001 Spring Semester // Project: HW 1 // // File : complex.cpp // Date : 29/03/2001 // Author : Hakan Kul // : Seniz Demir // // Description : Implementation of complex number calculations // // ---------------------------------------------------------------------- // Copyright (C) 2001 Yeditepe University // Information and Computer Science Department // All rights reserved // ------------------------------------------------------------------------ #include #include // complex //------------------------------------------------------------------------ // Implementation Notes: Implementation of Complex Number Class // // -------------------------------------------------------------------- class complex { public: complex(); // Constructor void readComplex(); // Reading the complex number from the user complex add(complex); // Add operation complex sub(complex); // Subtraction complex multiply(complex); // Multiplying complex div(complex); // Division void print(); // Printing the complex number float absoluteValue(); // Absolute value foundation private: float real; // Real part of the number float imag; // Imaginary part of the number float getRealPart(); float getImagPart(); void setRealPart(float); void setImagPart(float); }; // ====================================================================== // // YOU WILL IMPLEMENT THE FOLLOWING MEMBER FUNCTIONS // // ======================================================================= // constructor // ** // * This constructor initializes the complex number // * //------------------------------------------------------------- complex::complex() { } // Following function reads the real and the imaginary parts of the number // from the user void complex::readComplex() { } // Following function adds another complex number given as a parameter to the // number and returns the result complex number. i // // @param num complex number to be added // @return complex number sum of the num and the object itself complex complex::add(complex num) { complex ret; // Result is kept in this number return ret; } // Following function subtracts the complex number given as parameter // and returns the result complex number. // // @param num complex number to be subtracted // @return the result of the subtraction of the num from the object itself complex complex::sub(complex num) { complex ret; return ret; } // Following function multiplies the complex number by the other // complex number given as parameter // and returns the result complex number. // // @param num complex number to be multiplied by the object itself // @return the result of the multiplication complex complex::multiply(complex num) { complex ret; return ret; } // Following function divides the complex number by the other // complex number given as parameter // and returns the result complex number. // // @param num complex number divisor // @return the result of the division complex complex::div(complex num) { complex ret; return ret; } // Following function finds the absolute value of the complex number // and returns the result. // // @return the absolute value of the complex number float complex::absoluteValue() { return 0; } // Following function returns the real part of the complex numbea // // @return the real part of the object float complex::getRealPart() { return 0; } // Following function returns the imaginary part of the complex number // // @return the imaginary part of the object float complex::getImagPart() { return 0; } // Following function sets the reak part of the complex number // // @param the float number to be set to the real part of the object void complex::setRealPart(float num) { } // Following function sets the imaginary part of the complex number // // @param the float number to be set to the imaginary part of the object void complex::setImagPart(float num) { } // Following function prints the complex number void complex::print() { } //------------------------------------------------------------------ // Menu Class // // Menu displayes the options to the user and user chooses one of the // options from the menu such as adding operation // //-------------------------------------------------------------------- class Menu { public: int displayMenu(); }; // Following function displayes a menu to the user. User selects one of the // options from the menu // // @return int choice of the user from the menu int Menu::displayMenu() { int d1 = 1; cout << "\nChoose one of the options from the menu:\n" << endl; cout << "[1]. ADDITION\n"; cout << "[2]. SUBTRACTION\n"; cout << "[3]. MULTIPLICATION\n"; cout << "[4]. DIVISION\n"; cout << "[5]. ABSOLUTE VALUE\n"; cout << "[6]. EXIT\n\n"; // Check for the wrong input: do { if( (d1<1) || (d1>6) ) { cout << "Wrong Choice!!\n"; } cout << "Enter your choice: "; cin >> d1; } while (d1<1 || d1>6); return d1; } void Addition_Function() { complex result; complex n1; complex n2; cout << "\nFor the first number:\n\n"; n1.readComplex(); cout << "\nFor the second number:\n\n"; n2.readComplex(); result = n1.add(n2); cout << "\nThe result is: "; result.print(); } void Subtraction_Function() { complex result; complex n1; complex n2; cout << "\nFor the first number:\n\n"; n1.readComplex(); cout << "\nFor the other number to be subtracted:\n\n"; n2.readComplex(); result = n1.sub(n2); cout << "\nThe result is: "; result.print(); } void Multiply_Function() { complex result; complex n1; complex n2; cout << "\nFor the first number:\n\n"; n1.readComplex(); cout << "\nFor the second number:\n\n"; n2.readComplex(); result = n1.multiply(n2); cout << "\nThe result is: "; result.print(); } void Division_Function() { complex result; complex n1; complex n2; cout << "\nFor the first number:\n\n"; n1.readComplex(); cout << "\nFor the other number to be divided:\n\n"; n2.readComplex(); result = n1.div(n2); cout << "\nThe result is: "; result.print(); } void Absolute_Value_Function() { float absolute; complex n1; n1.readComplex(); absolute = n1.absoluteValue(); cout << "\nThe absoulate value of the complex number is: "; cout << absolute; } //--------------------------------------------------------------------- // // Following function is the main function // //-------------------------------------------------------------------- int main() { complex n1; complex n2; complex result; Menu mainMenu; int option; float absolute; // Display the menu to the user until 'exit' option is selected: do { option = mainMenu.displayMenu(); switch(option) // Do the operation that the user wants to do { case 1: // Addition Addition_Function(); break; case 2: // Subtraction Subtraction_Function(); break; case 3: // Multiplication Multiply_Function(); break; case 4: // Division Division_Function(); break; case 5: // Absolute Value Foundation Absolute_Value_Function(); break; } } while (option != 6); return 0; }