Advertisement
TheWhiteFang

ECE3206_MidTerm_1_Main_Q2 [80%] - anybody with the answer pm

Jan 27th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. class ComplexNumber
  10. {
  11. private:
  12.     double m_Real;
  13.     double m_Img;
  14. public:
  15.     ComplexNumber(double inReal, double inImg);
  16.     void Print();
  17.     friend class Compute;
  18. };
  19.  
  20. //constructor of class ComplexNumber defined outside of class ComplexNumber
  21. ComplexNumber::ComplexNumber(double inReal = 0.0, double inImg = 0.0)
  22. {
  23.     m_Real = inReal;
  24.     m_Img = inImg;
  25. };
  26.  
  27. //member function of class ComplexNumber defined outside of class ComplexNumber
  28. void ComplexNumber::Print()
  29. {
  30.     if (m_Img > 0){
  31.         cout << m_Real << " " << m_Img << "+" << "i" << endl;
  32.     }
  33.     else
  34.     {
  35.         cout << m_Real << " " << m_Img << "i" << endl;
  36.     }
  37. };
  38.  
  39.  
  40. class Compute
  41. {
  42. public:
  43.     static ComplexNumber Multiply(const ComplexNumber &inA, const ComplexNumber &inB);
  44.     static double Magnitude(const ComplexNumber &in);
  45. };
  46.  
  47. ComplexNumber Compute::Multiply(const ComplexNumber &inA, const ComplexNumber &inB)
  48. {
  49.     double Multiply = ((inA.m_Real*inB.m_Real) - (inA.m_Img*inB.m_Img)) + ((inA.m_Real*inB.m_Img) + (inA.m_Img*inB.m_Real));
  50.     return Multiply;
  51. };
  52.  
  53. double Compute::Magnitude(const ComplexNumber &in)
  54. {
  55.     double Magnitude = sqrt(pow(in.m_Real,2.0)+pow(in.m_Img,2.0));
  56.     return Magnitude;
  57. };
  58.  
  59. int main(){
  60.  
  61.    
  62.     ComplexNumber *v1 = new ComplexNumber(38.5,25.5);
  63.     /*ComplexNumber v1(38.5, 25.5);*/
  64.  
  65.     ComplexNumber *v2 = new ComplexNumber(10.4, -31.6);
  66.     /*ComplexNumber v2(10.4, -31.6);*/
  67.     ComplexNumber v3;
  68.  
  69.     cout << "Complex Num 1: ";
  70.     v1[0].Print();
  71.     cout << "Complex Num 2: ";
  72.     v2[0].Print();
  73.  
  74.     cout << endl;
  75.     cout << "Complex Num1 x Complex Num2: ";
  76.  
  77.    
  78.     /*Compute::Multiply(*v1, *v2);
  79.     ComplexNumber.Print();
  80.     v3.Print();
  81.     cout << endl;
  82.  
  83.     cout << "Magnitude of Complex Num 1: ";
  84.     v1[2] = Compute::Magnitude(v1[0]);
  85.     v1[2].Print();*/
  86.     //cout << comnum1;
  87.     //cout << endl;
  88.  
  89.     //cout << "Magnitude of Complex Num 2: ";
  90.     //double comnum2 = Compute::Magnitude(v2);
  91.     //cout << comnum2;
  92.  
  93.     cout << endl;
  94.  
  95.     //delete[] v1;
  96.     //delete[] v2;
  97.  
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement