wheelsmanx

WCC 272 MP1 source.cpp

Mar 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <regex>
  5. #include <algorithm>
  6. #include "Header.h"
  7.  
  8. using namespace std;
  9.  
  10. class Complex {
  11. friend ostream& operator<<(ostream& out, const Complex& theComplex);
  12. friend istream& operator>>(istream& in, Complex& theComplex);
  13. friend Complex operator+(const Complex& lhs, const Complex& rhs);
  14. friend Complex operator-(const Complex& lhs, const Complex& rhs);
  15. friend Complex operator*(const Complex& lhs, const Complex& rhs);
  16. friend Complex operator/(const Complex& lhs, const Complex& rhs);
  17. public:
  18. Complex(double re = 0.0, double im = 0.0);
  19. double getReal(void) const; // returntor real part
  20. double getImaginary(void) const; // return imaginary part
  21. void setReal(double re); // sets the real part
  22. void setImaginary(double im); // sets the imaginary part
  23. void convertStringToComplex(const string& complexString);
  24. private:
  25. double real;
  26. double imag;
  27. };
  28.  
  29. Complex::Complex(double re, double im) {
  30. }
  31. double Complex::getReal(void) const {
  32. double returnObject = this->real;
  33. return returnObject;
  34. }
  35. double Complex::getImaginary(void) const {
  36. double returnObject = this->imag;
  37. return returnObject;
  38. }
  39. void Complex::setReal(double re) {
  40. this->real = re;
  41. }
  42. void Complex::setImaginary(double im) {
  43. this->imag = im;
  44. }
  45. void Complex::convertStringToComplex(const string& complexString) {
  46. string testString = complexString;
  47. vector<double> test;
  48. smatch matches;
  49. regex regexString("((\\d*)[.](\\d*))");
  50. size_t sz;
  51.  
  52.  
  53. while (regex_search(testString, matches, regexString)) {
  54. test.push_back(stod(matches[0], &sz));
  55. matches.empty();
  56. testString = matches.suffix().str();
  57. }
  58. this->real = test[0];
  59. this->imag = test[1];
  60. }
  61.  
  62. ostream& operator<<(ostream& out, const Complex& theComplex) {
  63. out << theComplex.real << " + " << theComplex.imag << "i" << endl;
  64. return out;
  65. }
  66. istream& operator>>(istream& in, Complex& theComplex) {
  67. in >> theComplex.real >> theComplex.real;
  68. return in;
  69. }
  70. Complex operator+(const Complex& lhs, const Complex& rhs) {
  71. Complex returnObject;
  72. returnObject.real = lhs.real + rhs.real;
  73. returnObject.imag = lhs.imag + rhs.imag;
  74. return returnObject;
  75. }
  76. Complex operator*(const Complex& lhs, const Complex& rhs) {
  77. Complex returnObject;
  78. returnObject.real = lhs.real * rhs.real;
  79. returnObject.imag = lhs.imag * rhs.imag;
  80. return returnObject;
  81. }
  82. Complex operator-(const Complex& lhs, const Complex& rhs) {
  83. Complex returnObject;
  84. returnObject.real = lhs.real - rhs.real;
  85. returnObject.imag = lhs.real - rhs.real;
  86. return returnObject;
  87. }
  88.  
  89. Complex operator/(const Complex& lhs, const Complex& rhs) {
  90. Complex returnObject;
  91. returnObject.real = lhs.real / rhs.real;
  92. returnObject.imag = lhs.imag / rhs.imag;
  93. return returnObject;
  94. }
  95. // this here is the BST SEARCH ARRAY
  96. pair<int, int> search(int arr[], int sizeOfArr, int searchNum) {
  97. pair<int, int> returnObject;
  98. // first returns the index where it was found
  99. returnObject.first = 0;
  100. // second is to return the number of cycles
  101. returnObject.second = 0;
  102. // initialize the variables
  103. int first, last, middle;
  104. first = 0;
  105. last = sizeOfArr;
  106. middle = (first + last) / 2;
  107. while (first <= last) {
  108. // if the search number is greater than the middle number then make this the new starting point
  109. if (arr[middle] < searchNum) {
  110. first = middle + 1;
  111. }
  112. // else if we found it then make the returnobject.first return as the index of it
  113. else if (arr[middle] == searchNum) {
  114. returnObject.first = middle;
  115. break;
  116. }
  117. // else make the middle the new last
  118. else {
  119. last = middle - 1;
  120. }
  121. // calculate the middle
  122. middle = (first + last) / 2;
  123. // add to the cycle count
  124. returnObject.second++;
  125. //cout << first << " " << middle << " " << last << endl;
  126. }
  127. // if first is greater than last then return -1
  128. if (first > last) {
  129. returnObject.first = -1;
  130. }
  131. return returnObject;
  132. }
  133.  
  134. pair<Complex, Complex> ask() {
  135. string userInput1;
  136. string userInput2;
  137. cout << "in the syntax of 2.5 + 2.4i" << endl;
  138. cout << "please enter first complex number: " << endl;
  139. cin >> userInput1;
  140. cout << "please enter second complex number: " << endl;
  141. cin >> userInput2;
  142. Complex firstNumber;
  143. Complex secondNumber;
  144. firstNumber.convertStringToComplex(userInput1);
  145. secondNumber.convertStringToComplex(userInput2);
  146. pair<Complex, Complex> returnObject;
  147. returnObject.first = firstNumber;
  148. returnObject.second = secondNumber;
  149. return returnObject;
  150. }
  151.  
  152. void printAddComplex(Complex userInput, Complex userInput1) {
  153. cout << "(" << userInput.getReal() << " + " << userInput.getImaginary() << "i" << ")";
  154. cout << "+";
  155. cout << "(" << userInput1.getReal() << " + " << userInput1.getImaginary() << "i" << ")";
  156. cout << "=";
  157. cout << userInput + userInput1 << endl;
  158. }
  159.  
  160. void main() {
  161. //part 1
  162. pair<Complex, Complex> myPair;
  163. myPair = ask();
  164. printAddComplex(myPair.first,myPair.second);
  165.  
  166. const int ARRSIZE = 50000;
  167. const int RANDOMVALUES = 100000, RANDOMLIMIT = 200000;
  168. int table[ARRSIZE];
  169. int sumBinSearchSuccess = 0, sumBinSearchFail = 0, success = 0;
  170.  
  171. for (int i = 0; i < sizeof(table) / sizeof(int); i++) {
  172. table[i] = rand() % (RANDOMLIMIT - 1) + 1;
  173. }
  174. sort(table, table + ARRSIZE);
  175. for (auto e : table) {
  176. //cout << e << endl;
  177. }
  178. for (int i = 0; i < RANDOMVALUES; i++) {
  179. pair<int, int> mypair;
  180. mypair = search(table, sizeof(table) / sizeof(int), rand() % (RANDOMLIMIT-1) + 1);
  181. //cout << "Break" << endl;
  182. //cout << "index: " << mypair.first << endl << "Cycle Count: " << mypair.second << endl;
  183. if (mypair.first != -1) {
  184. success++;
  185. sumBinSearchSuccess += mypair.second;
  186. }
  187. else {
  188. sumBinSearchFail += mypair.second;
  189. }
  190. }
  191. //cout << sumBinSearchSuccess << " " << sumBinSearchFail << " " << success << endl;
  192. cout << "Emperical Average Case: " << sumBinSearchSuccess / success << endl;
  193. cout << "Emperical Worst Case: " << sumBinSearchFail / (RANDOMVALUES - success) << endl;
  194. cout << "Theoretical bound for worst case: " << 2.0 * (1.0 + int(log(static_cast<double>(ARRSIZE)) / log(2.0))) << endl;
  195. system("pause");
  196. }
Advertisement
Add Comment
Please, Sign In to add comment