Advertisement
KySoto

lab8c

Oct 16th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. /*DIRECTIONS Take the notes for the Complex structure and write regular c-type functions that add, subtract,
  2. and multiply complex numbers. Use the following function prototypes.
  3.  
  4. Complex addComplex(Complex x, Complex y);
  5. Complex subtractComplex(Complex x, Complex y);
  6. Complex multiplyComplex(Complex x, Complex y);
  7.  
  8. If time allows, write a function that divides complex numbers.
  9. mathew myers
  10. Template blank Source document*/
  11.  
  12. #include<iostream>
  13. #include<string>
  14. #include<sstream>
  15. #include<cmath>
  16. //globals
  17. using namespace std;
  18. struct Complex{
  19.  
  20.     // Data members that define a complex number a + bi
  21.     double a,b;
  22.    
  23.     // function within a data structure = METHOD
  24.     // functions specific to this type of object.
  25.     void printComplex(void);
  26.  
  27. };
  28. Complex addComplex(Complex x, Complex y);
  29. Complex subtractComplex(Complex x, Complex y);
  30. Complex multiplyComplex(Complex x, Complex y);
  31. Complex divideComplex(Complex x, Complex y);
  32. bool runAgain(void);
  33.  
  34.  
  35. void main()
  36. {
  37. //Declarations
  38.     do{
  39. //inputs
  40.  
  41. //outputs
  42.     }while( runAgain() );
  43.  
  44. }
  45.  
  46. void Complex::printComplex(void){
  47.  
  48.     if( b >= 0 )
  49.         cout << a << " + " << b << "i" << endl;
  50.     else
  51.         cout << a << " - " << -1*b << "i"<< endl;
  52.  
  53. }
  54. Complex addComplex(Complex x, Complex y)
  55. {
  56.     Complex z;
  57.     z.a = x.a + y.a;
  58.     z.b = x.b + y.b;
  59.     return z;
  60. }
  61. Complex subtractComplex(Complex x, Complex y)
  62. {
  63.     Complex z;
  64.     z.a = x.a - y.a;
  65.     z.b = x.b - y.b;
  66.     return z;
  67. }
  68. Complex multiplyComplex(Complex x, Complex y)
  69. {
  70.     Complex z;
  71.     z.a = ((x.a*y.a)-(x.b*y.b));
  72.     z.b = ((x.a*y.b)+(x.b*y.a));
  73.     return z;
  74. }
  75. Complex divideComplex(Complex x, Complex y)
  76. {
  77.     Complex z;
  78.     Complex temp1;
  79.     Complex temp2;
  80.         temp1 = y;
  81.         temp1.b = temp1.b * -1;
  82.         temp2 = multiplyComplex(y,temp1);
  83.         temp1 = multiplyComplex(x,temp1);
  84.         z.a = temp1.a / temp2.a;
  85.         z.b = temp1.b / temp2.a;
  86.     return z;
  87. }
  88. bool runAgain(void){
  89.     char userResponse;
  90.  
  91.     cout << "\nWould you like to run again (y or n): ";
  92.     cin >> userResponse;
  93.  
  94.     if(userResponse == 'y')
  95.         return(true);
  96.  
  97.     return(false);
  98. }
  99. /* lazy copy stuff
  100. if()
  101.     {
  102.     }
  103. else if()
  104.     {
  105.     }
  106. else
  107.     {
  108.     }
  109.  
  110. for( int i = 0; i < x;i++)
  111.     {
  112.     }
  113. switch()
  114.     {
  115.         case :
  116.         break;
  117.         case :
  118.         break;
  119.         default:
  120.     }
  121. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement