Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 2 – More Classes Object Oriented Programming using C++ 1. Create a class cal

ID: 3743975 • Letter: L

Question

Lab 2 – More Classes Object Oriented Programming using C++

1. Create a class called complexNumber that stores a complex number of the form a+bi, where i is 1. a is the real and b is the imaginary part of the number1 . You should be able to get the real and imaginary parts of the number. a and b can be negative, e.g., “3+4i”, “-3+4i”, “3-4i”, and “-3- 4i”.

2. Implement the ability to add, subtract, and multiply two complexNumber objects and save the result in another complexNumber object by overloading operators +, -, and *.

3. Overload the operators >> and << to input and output string of the form a+bi from the complexNumber object respectively.

4. Read a file called complexInput.txt (sample input file attached) containing numbers of the form (a+bi) followed by an operator followed by another complex number of the form (a+ib).

5. Perform the operations on the numbers you read and store the result in a new complexNumber. Output the number in a new output file called complexOutput.txt (sample output file included).

6. [Bonus - optional] Implement error checking on the input and reject/ignore values that are not in the format a+bi. Log this information in the output file. A sample wrong input file called complexInputWrong.txt is attached. What to turn in • A zip file containing the files cmpe126-lab2.cpp, complex.cpp and complex.h files

(NOTE:PLEASE ALSO SHOW HOW TO IMPLEMENT INPUT AND OUTPUT TEXT FILES BELOW TO OBTAIN RESULTS WHEN USED IN THE CODE)

complexInput.txt

(3+4i) * (5-6i)

complexOutput.txt

39+2i

complexInputWrong.txt

(3+4i) - dummy

Explanation / Answer

Screenshot

---------------------------------------------------

Program

Complex.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class complex {
//Member variables
private:
   int real;
   int imaginary;
   char op;
//Member functions
public:
   //Default constructor
   complex();
   //Parameterised constructor
   complex(int real, int imag,char op);
   //Mutators
   void setReal(int real);
   void setImaginary(int imag);
   void setOp(char op);
   //Accessors
   int getReal();
   int getImaginary();
   char getOp();
   //Overload << and >>
   friend ostream &operator << (ostream &output, complex &cmplx);
   friend istream &operator >> (istream &input, complex &cmplx);
   //Overload + , - and *
   complex operator+(const complex& c);
   complex operator-(const complex& c);
   complex operator*(const complex& c);
};

Complex.cpp

#include "Complex.h"
//Default constructor definiton
complex::complex() {
   real = 0;
   imaginary = 0;
   op = '+';
}
//Parameterised constructor definiton
complex::complex(int real, int imag,char op) {
   this->real = real;
   imaginary = imag;
   this->op = op;
}
//Overload <<
ostream& operator<<(ostream & output, complex & cmplx)
{
   output <<cmplx.getReal()<<cmplx.op<<cmplx.getImaginary() <<"i"<< endl;
   return output;
}
//Overload >>
istream& operator>>(istream &input, complex &cmplx) {
   input >> cmplx.real;
   input >> cmplx.imaginary;
   return input;
}
//Mutator definition
void complex::setReal(int real) {
   this->real = real;
}
void complex::setImaginary(int imag) {
   imaginary = imag;
}
void complex::setOp(char op) {
   this->op = op;
}
//Accessor definiton
int complex::getReal() {
   return real;
}
int complex::getImaginary() {
   return imaginary;
}
char complex::getOp() {
   return op;
}
//Overload + operator
complex complex::operator+(const complex& c) {
   complex cmplx;
   cmplx.real = this->real + c.real;
   cmplx.imaginary = this->imaginary + c.imaginary;
   return cmplx;
}
//Overload - operator
complex complex::operator-(const complex& c) {
   complex cmplx;
   cmplx.real = this->real - c.real;
   cmplx.imaginary = this->imaginary - c.imaginary;
   return cmplx;
}
//Overload * operator
complex complex::operator*(const complex& c) {
   complex cmplx;
   cmplx.real = (this->real*c.real) - (this->imaginary*c.imaginary);
   cmplx.imaginary = (this->real*c.imaginary) + (this->imaginary*c.real);
   return cmplx;
}

ComplexTest.cpp

//Header files
#include<fstream>
#include "Complex.h"
//Test method
int main()
{
   //Input file object
   ifstream in;
   //Output file object
   ofstream out;
   //Stringd to read file
   string c4="",c3="",operation="";
   //for identifying symbol
   char op;
   //variables
   int real, imag;
   //I/p O/p file
   in.open("C:/Users/deept/Desktop/complexInput.txt");
   out.open("C:/Users/deept/Desktop/complexOutput.txt");
   //If not open
   if (!in) {
       cout << "File not found!!" << endl;
       exit(0);
   }
   //Otherwise
   else {
       while (!in.eof()) {
           //Read file data
           in >> c4 >> operation >> c3;
           //Dummy check
           if (c4 != ""&& c3 != ""&& operation != "") {
               //First complex number
               complex c;
               //Check the real and imaginary datas sign before setting values
               if (isdigit(c4[1]) && c4[2] == '+') {
                   real = c4[1] - '0';
                   imag = c4[3] - '0';
                   op = c4[2];
                   c.setReal(real);
                   c.setImaginary(imag);
                   c.setOp(op);
               }

               else if (isdigit(c4[1]) && c4[2] == '-') {
                   real = c4[1] - '0';
                   imag = c4[3] - '0';
                   op = c4[2];
                   c.setReal(real);
                   c.setImaginary(-imag);
                   c.setOp(op);
               }
               else if (!isdigit(c4[1]) && c4[3] == '+') {
                   real = c4[2] - '0';
                   imag = c4[4] - '0';
                   op = c4[3];
                   c.setReal(-real);
                   c.setImaginary(imag);
                   c.setOp(op);
               }
               else if (!isdigit(c4[1]) && c4[3] == '-') {
                   real = c4[2] - '0';
                   imag = c4[4] - '0';
                   op = c4[3];
                   c.setReal(-real);
                   c.setImaginary(-imag);
                   c.setOp(op);
               }

               //Second complex number
               complex c1;
               //Check the real and imaginary datas sign before setting values
               if (isdigit(c3[1]) && c3[2] == '+') {
                   real = c3[1] - '0';
                   imag = c3[3] - '0';
                   op = c3[2];
                   c1.setReal(real);
                   c1.setImaginary(imag);
                   c1.setOp(op);
               }

               else if (isdigit(c3[1]) && c3[2] == '-') {
                   real = c3[1] - '0';
                   imag = c3[3] - '0';
                   op = c3[2];
                   c1.setReal(real);
                   c1.setImaginary(-imag);
                   c1.setOp(op);
               }
               else if (!isdigit(c3[1]) && c3[3] == '+') {
                   real = c3[2] - '0';
                   imag = c3[4] - '0';
                   op = c3[3];
                   c1.setReal(-real);
                   c1.setImaginary(imag);
                   c1.setOp(op);
               }
               else if (!isdigit(c3[1]) && c3[3] == '-') {
                   real = c3[2] - '0';
                   imag = c3[4] - '0';
                   op = c3[3];
                   c1.setReal(-real);
                   c1.setImaginary(-imag);
                   c1.setOp(op);
               }
               //Result complex number
               complex c2;
               //Check the operation between complex numbers
               if (operation == "*") {
                   c2 = c * c1;
               }
               else if (operation == "+") {
                   c2 = c + c1;
               }
               else if (operation == "-") {
                   c2 = c - c1;
               }
               //Write result in output file
               if (!out) {
                   cout << "File not found!!" << endl;
                   exit(0);
               }
               else {
                   out << c2;
               }
               //Cross check display on console
               cout << c2;
               //Read strings set default
               c4 = "";
               operation = "";
               c3 = "";

           }
           //If dummy complex write that into file and display console for crosscheck
           else {
               out << "Dummy" << endl;
               cout << "Dummy" << endl;
           }
       }
   }
  
    return 0;
}

-----------------------------------------------------

complexInput.txt

(3+4i) * (5-6i)
(3-4i) + (-5+6i)
(3+4i)

complexOutput.txt

39+2i
-2+2i
Dummy