Avatar of dreaminbinary
dreaminbinary
 asked on

Complex math class assignment

My assignment is

Implement a complex number object having multiply,divide add and subtract operations via the definitions :

(a+b)+(x+y)=(a+x)+(b+y) add

(a+b)-(x+y)=(a-x)+(b-y) sub

(a+b)*(x+y)=(a*x+b*y)+(a*y+b*x) mult

(a+b)/(x+y)=((a+b)*(x-y)) / (xsquared+ ysquared) divide

Here is what I did

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

class Operand{
   protected:
      float a,b,x,y;
   public:
      Operand(void){a=0,b=0,x=0,y=0;}
      Operand(float n1, float n2,float n3,float n4){a=n1,b=n2,x=n3,y=n4;}
      void setNum1(const float n1){a=n1;}
      void setNum2(const float n2){b=n2;}
      void setNum3(const float n3){x=n3;}
      void setNum4(const float n4){y=n4;}
      const float getNum1(void) {return this->a;} // read state services
      const float getNum2(void) {return this->b;} // read state services
      const float getNum3(void) {return this->x;} // read state services  
      const float getNum4(void) {return this->y;} // read state services

};

class ComplexOperation: public Operand{
   private:
     float result;
   public:
      ComplexOperation(void) {result=0;}
      void add(void)  {result = (a+x)+(b+y);}
      void sub(void)  {result = (a-x)+(b-y);}
      void div(void)  {result = ( (a+b)*(x-y)  / (x+y)+(x+y) );}
      void mul(void)  {result = ( (a*x + b*y) + (a*y+b*x));}
      float showResult() {return result;}          
 };



int main (void)
{
  srand(time(0)); // Initialize random number generator.

  float r(((rand() % 10) + 1));
  float r2(((rand() % 10) + 1));
  float r3(((rand() % 10) + 1));
  float r4(((rand() % 10) + 1));

  ComplexOperation math1;
 
  math1.setNum1(r);
  math1.setNum2(r2);
  math1.setNum3(r3);
  math1.setNum4(r4);

  math1.add();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") + ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.sub();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") - ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.mul();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") * ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";
  math1.div();
  cout<<"\n ("<<math1.getNum1()<<"+"<<math1.getNum2()<<") / ("<<math1.getNum3()<<"+"<<math1.getNum4()<<")="<<math1.showResult()<<"\n";

  return 0;
}


1) I know the divide operations doesn't work
2) Is this code right and effecient? ( im taking an intro to OOP class)
3) I need help with a copy constructor
4) is this code reusable ( whole reason for OOP right ?)


C++

Avatar of undefined
Last Comment
itsmeandnobodyelse

8/22/2022 - Mon
SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dreaminbinary

ASKER
(1) What is wrong with my code for the divide function ?
void div(void)  {result = ( (a+b)*(x-y)  / (x+y)+(x+y) );}

Also my mult function doesnt work with negative numbers.
jkr

Um, speaking of that, none of the operations make sense at all:

      void add(void)  {result = (a+x)+(b+y);}
      void sub(void)  {result = (a-x)+(b-y);}
      void div(void)  {result = ( (a+b)*(x-y)  / (x+y)+(x+y) );}
      void mul(void)  {result = ( (a*x + b*y) + (a*y+b*x));}

You are assigning a complex number to a float variable. Shouldn't you have a complex result also?

See http://en.wikipedia.org/wiki/Complex_number
ASKER CERTIFIED SOLUTION
itsmeandnobodyelse

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck