#include <iostream>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <stdexcept>
#include <algorithm>
#include "Array.h"
#include "SortArray.h"
#include "XorArray.h"
using namespace std;
int main()
{
double a[5] = {5, 4, 3, 2, 1};
double b[5] = {5, 7, 8, 2, 9};
double c[5] = {5, 4, 3, 2, 1};
double x[5] = {5, 4, 3, 2, 1};
double y[5] = {5, 7, 8, 2, 9};
double z[5] = {5, 4, 3, 2, 1};
XorArray p1(a,5);
XorArray p2(b,5);
XorArray p3(c,5);
SortArray s1(x,5);
SortArray s2(y,5);
SortArray s3(z,5);
cout << " Sum Xor of 1 and 2 Array" << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << p3 << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << p2 << endl
<< " is" << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << p1 + p2 << endl
<< endl << " Foreach 1 Array sqrt is " << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << p3 << endl;
p3.foreach();
cout << setw(6) << setiosflags(ios::showpoint) << setprecision(3) << p3 << endl << endl << endl;
cout << " Foreach sort is" << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << s1 << endl;
s1.SortArray::foreach();
cout << setw(6) << setiosflags(ios::showpoint) << setprecision(3) << s1 << endl;
s2.SortArray::foreach();
cout << endl << " Sum U of 1 and 2 Array" << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << s1 << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << s2 << endl
<< " is" << endl
<< setw(6) << setiosflags(ios::showpoint) << setprecision(3) << s1 + s2 << endl;
return 0;
}
#ifndef ARRAY
#define ARRAY
#include <iostream>
#include <iomanip>
#include <cstring>
#include <stdexcept>
using namespace std;
class Array
{
public:
virtual Array& operator+ (const Array&) const = 0;
virtual void foreach () const = 0;
};
#endif // ARRAY
#ifndef XORARRAY
#define XORARRAY
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <stdexcept>
#include "Array.h"
using namespace std;
class XorArray : public Array{
private:
double *arr;
size_t sz;
public:
XorArray (double* _arr, size_t _sz): arr(_arr), sz(_sz){};
friend ostream& operator << (ostream& out, const XorArray&c){
for (size_t i = 0; i < c.sz; i++)
out << c.arr[i] << " ";
return out;
}
XorArray& operator+ (const Array &z) const override
{
const XorArray& tmp = dynamic_cast<const XorArray&>(z);
XorArray *result = new XorArray (arr, sz);
for (size_t i = 0; i < sz; i++) {
double d1 = arr[i], d2 = tmp.arr[i];
int z1 = d1, z2 = d2;
arr[i] = (z1 ^ z2) + (d1 - z1) + (d2 - z2);
}
return *result;
}
void foreach() const override{
double *newarr = new double[sz];
for (size_t i = 0; i < sz; i++) {
newarr[i] = arr[i];
}
for (size_t i = 0; i < sz; i++) {
arr[i] = sqrt(newarr[i]);
}
}
};
#endif // XORARRAY
#ifndef SORTARRAY
#define SORTARRAY
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <vector>
#include "Array.h"
using namespace std;
class SortArray : public Array
{
private:
double *arr;
size_t sz;
public:
SortArray (double* _arr, size_t _sz): arr(_arr), sz(_sz) {};
friend ostream& operator << (ostream& out, const SortArray&c)
{
for (size_t i = 0; i < c.sz; i++)
out << c.arr[i] << " ";
return out;
}
SortArray& operator + (const Array &x) const override
{
const SortArray& temp = dynamic_cast<const SortArray&>(x);
for(size_t i = 0; i < sz; i++)
arr[sz] = arr[i];
for(size_t i = 0; i < temp.sz; i++)
{
for (size_t j = 0; j < sz; j++){
if (arr[i] = arr[j])
break;
arr[temp.sz] = arr[sz];
}
}
SortArray *result = new SortArray (temp.arr, temp.sz + sz);
return *result;
}
void foreach()
const
override
{
double temp;
for (size_t i = 0; i < sz - 1; i++)
{
for (size_t j = 0; j < sz - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
};
#endif // SORTARRAY