Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <regex>
- #include <algorithm>
- #include "Header.h"
- using namespace std;
- class Complex {
- friend ostream& operator<<(ostream& out, const Complex& theComplex);
- friend istream& operator>>(istream& in, Complex& theComplex);
- friend Complex operator+(const Complex& lhs, const Complex& rhs);
- friend Complex operator-(const Complex& lhs, const Complex& rhs);
- friend Complex operator*(const Complex& lhs, const Complex& rhs);
- friend Complex operator/(const Complex& lhs, const Complex& rhs);
- public:
- Complex(double re = 0.0, double im = 0.0);
- double getReal(void) const; // returntor real part
- double getImaginary(void) const; // return imaginary part
- void setReal(double re); // sets the real part
- void setImaginary(double im); // sets the imaginary part
- void convertStringToComplex(const string& complexString);
- private:
- double real;
- double imag;
- };
- Complex::Complex(double re, double im) {
- }
- double Complex::getReal(void) const {
- double returnObject = this->real;
- return returnObject;
- }
- double Complex::getImaginary(void) const {
- double returnObject = this->imag;
- return returnObject;
- }
- void Complex::setReal(double re) {
- this->real = re;
- }
- void Complex::setImaginary(double im) {
- this->imag = im;
- }
- void Complex::convertStringToComplex(const string& complexString) {
- string testString = complexString;
- vector<double> test;
- smatch matches;
- regex regexString("((\\d*)[.](\\d*))");
- size_t sz;
- while (regex_search(testString, matches, regexString)) {
- test.push_back(stod(matches[0], &sz));
- matches.empty();
- testString = matches.suffix().str();
- }
- this->real = test[0];
- this->imag = test[1];
- }
- ostream& operator<<(ostream& out, const Complex& theComplex) {
- out << theComplex.real << " + " << theComplex.imag << "i" << endl;
- return out;
- }
- istream& operator>>(istream& in, Complex& theComplex) {
- in >> theComplex.real >> theComplex.real;
- return in;
- }
- Complex operator+(const Complex& lhs, const Complex& rhs) {
- Complex returnObject;
- returnObject.real = lhs.real + rhs.real;
- returnObject.imag = lhs.imag + rhs.imag;
- return returnObject;
- }
- Complex operator*(const Complex& lhs, const Complex& rhs) {
- Complex returnObject;
- returnObject.real = lhs.real * rhs.real;
- returnObject.imag = lhs.imag * rhs.imag;
- return returnObject;
- }
- Complex operator-(const Complex& lhs, const Complex& rhs) {
- Complex returnObject;
- returnObject.real = lhs.real - rhs.real;
- returnObject.imag = lhs.real - rhs.real;
- return returnObject;
- }
- Complex operator/(const Complex& lhs, const Complex& rhs) {
- Complex returnObject;
- returnObject.real = lhs.real / rhs.real;
- returnObject.imag = lhs.imag / rhs.imag;
- return returnObject;
- }
- // this here is the BST SEARCH ARRAY
- pair<int, int> search(int arr[], int sizeOfArr, int searchNum) {
- pair<int, int> returnObject;
- // first returns the index where it was found
- returnObject.first = 0;
- // second is to return the number of cycles
- returnObject.second = 0;
- // initialize the variables
- int first, last, middle;
- first = 0;
- last = sizeOfArr;
- middle = (first + last) / 2;
- while (first <= last) {
- // if the search number is greater than the middle number then make this the new starting point
- if (arr[middle] < searchNum) {
- first = middle + 1;
- }
- // else if we found it then make the returnobject.first return as the index of it
- else if (arr[middle] == searchNum) {
- returnObject.first = middle;
- break;
- }
- // else make the middle the new last
- else {
- last = middle - 1;
- }
- // calculate the middle
- middle = (first + last) / 2;
- // add to the cycle count
- returnObject.second++;
- //cout << first << " " << middle << " " << last << endl;
- }
- // if first is greater than last then return -1
- if (first > last) {
- returnObject.first = -1;
- }
- return returnObject;
- }
- pair<Complex, Complex> ask() {
- string userInput1;
- string userInput2;
- cout << "in the syntax of 2.5 + 2.4i" << endl;
- cout << "please enter first complex number: " << endl;
- cin >> userInput1;
- cout << "please enter second complex number: " << endl;
- cin >> userInput2;
- Complex firstNumber;
- Complex secondNumber;
- firstNumber.convertStringToComplex(userInput1);
- secondNumber.convertStringToComplex(userInput2);
- pair<Complex, Complex> returnObject;
- returnObject.first = firstNumber;
- returnObject.second = secondNumber;
- return returnObject;
- }
- void printAddComplex(Complex userInput, Complex userInput1) {
- cout << "(" << userInput.getReal() << " + " << userInput.getImaginary() << "i" << ")";
- cout << "+";
- cout << "(" << userInput1.getReal() << " + " << userInput1.getImaginary() << "i" << ")";
- cout << "=";
- cout << userInput + userInput1 << endl;
- }
- void main() {
- //part 1
- pair<Complex, Complex> myPair;
- myPair = ask();
- printAddComplex(myPair.first,myPair.second);
- const int ARRSIZE = 50000;
- const int RANDOMVALUES = 100000, RANDOMLIMIT = 200000;
- int table[ARRSIZE];
- int sumBinSearchSuccess = 0, sumBinSearchFail = 0, success = 0;
- for (int i = 0; i < sizeof(table) / sizeof(int); i++) {
- table[i] = rand() % (RANDOMLIMIT - 1) + 1;
- }
- sort(table, table + ARRSIZE);
- for (auto e : table) {
- //cout << e << endl;
- }
- for (int i = 0; i < RANDOMVALUES; i++) {
- pair<int, int> mypair;
- mypair = search(table, sizeof(table) / sizeof(int), rand() % (RANDOMLIMIT-1) + 1);
- //cout << "Break" << endl;
- //cout << "index: " << mypair.first << endl << "Cycle Count: " << mypair.second << endl;
- if (mypair.first != -1) {
- success++;
- sumBinSearchSuccess += mypair.second;
- }
- else {
- sumBinSearchFail += mypair.second;
- }
- }
- //cout << sumBinSearchSuccess << " " << sumBinSearchFail << " " << success << endl;
- cout << "Emperical Average Case: " << sumBinSearchSuccess / success << endl;
- cout << "Emperical Worst Case: " << sumBinSearchFail / (RANDOMVALUES - success) << endl;
- cout << "Theoretical bound for worst case: " << 2.0 * (1.0 + int(log(static_cast<double>(ARRSIZE)) / log(2.0))) << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment