I posted this question before and the person who answered me just copy and paste
ID: 3591540 • Letter: I
Question
I posted this question before and the person who answered me just copy and paste the answer of this question that is already on chegg.
So, please do not copy and paste the answer that is already on chegg!!!
The code should ask the user for an input and generate randon numbers every time we run the code.
Write this code in c++
Part A
Create a class complex: In our implementation each complex has an imaginary part and a real part that are integers in range [1 10].
Our goal is to have a bag of complex numbers with size 100.
The problem is: To search for a specific complex number we have to match both real and imaginary parts that is more time consuming than a simple integer, especially when the range of imaginary and real numbers is only 10 numbers. The problem gets worst if the type of object that you are storing in the bag is more difficult to compare.
Suggest a strategy to overcome this problem by linear increase in the memory. Implement your strategy for the complex number.
You must create your own namespaces.
You should have complex.h, complex.cpp, bag.h, bag.cpp and main.cpp files.
Full documentation and precondition and post condition is needed.
Code should be clean and the name of the identifiers should be appropriate to their actual meaning.
main.cpp contains the test of your work. I expect you insert at least 100 numbers into your bag.
Part B
Overload the operator+, operator- for your bag to add or remove the other bag from this bag. Overload them to add or remove a single complex number from your bag.
Overload operator==, operator<<, operator>>.
Override the copy constructor.
You have to use namespace and macroguard.
Explanation / Answer
--------------------- Complex.h -------------------
#ifndef MY_COMPLEX
#define MY_COMPLEX
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
namespace myComplex
{
class Complex
{
int real;
int img;
public:
Complex();
Complex(int, int);
int getReal();
int getImg();
void setReal(int);
void setImg(int);
double getMagnitude();
void print();
bool equals(Complex);
};
}
#endif
-----------------Complex.cpp------------------------
#include "Complex.h"
using namespace myComplex;
Complex::Complex()
{
real = rand()%10 + 1;
img = rand()%10 + 1;
}
Complex::Complex(int real, int img)
{
this->real = real;
this->img = img;
}
int Complex::getReal()
{
return real;
}
int Complex::getImag()
{
return img;
}
void Complex::setReal(int real)
{
this->real = real;
}
void Complex::setImag(int img)
{
this->img = img;
}
double Complex::getMagnitude()
{
return sqrt(real*real + img*img);
}
void Complex::print()
{
cout << real << " + " << img << "i ";
}
boole Complex::equals(Complex &other)
{
return real==other.getReal() && img==other.getImg();
}
----------------------Bag.h--------------------
#ifndef MY_BAG
#define MY_BAG
#include "Complex.cpp"
namespace myBag
{
class Bag
{
// array of type Complex
Complex *arr;
// store the index of the last element in the array
int count;
public:1
Bag();
Bag(int);
void add(int , int);
bool search(Complex);
};
}
#endif
----------------Bag.cpp------------------------
#include "Bag.h"
using namespace myBag;
Bag::Bag()
{
arr = new Complex[10];
count = 0;
}
Bag::Bag(int size)
{
arr = new Complex[size];
count = 0;
}
void Bag::add(int real, int imag)
{
arr[index].setReal(real);
arr[index].setImag(imag);
//cout<<arr[index].print()<<std::endl;
index++;
}
bool Bag::search(Complex ob)
{
int i;
for( i = 0 ; i < index ; i ++)
{
// if element is found
if(arr[i].equals(ob))
return true;
}
// if element is not found
return false;
}
---------------main.cpp------------------
#include "Bag.cpp"
int main()
{
Bag bag(100);
int i;
for(i=0;i<50;i++)
bag.add();
int r,im;
cout << "Search complex number in the bag ";
cout << "Enter real part: ";
cin >> r;
cout << "Enter imaginary part: ";
cin >> im;
Complex search(r, im);
cout<<"Element to be searched : " << search.print() << " ";
if(bag.search(temp))
cout<<"Element is present in the bag ";
else
cout<<"Element is not present in the bag ";
}