Help with my code

Hi guys I've been working on this for a while now and I cannot find any help on this

There is 3 different parts to the code and I will mark where i need to fix.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Complx.h
#include <iostream>
#include <fstream>
using namespace std;
 
class complx
{
      double real,
             imag;
public:
      complx( double real = 0., double imag = 0.); // constructor
      complx operator+(complx);       // operator+()
 
      complx operator+(double);       // operator+()with double //needs fixed
      complx operator- (complx);       // operator-()           //needs fixed
      complx operator* (complx);       // operator*()           //needs fixed
 
      bool operator== (complx);   // operator==()               //needs fixed
 
 
       //Sets private data members.
     void Set(double new_real, double new_imaginary) {
     real = new_real;
     imag = new_imaginary;
     }
 
     //Returns the real part of the complex number.
          double Real() {
          return real;
     }
 
     //Returns the imaginary part of the complex number.
     double Imaginary() {
     return imag;
     }
};
 
ostream &operator << ( ostream &out_file, complx number );
 
extern istream &operator >> ( istream &in_file, complx &number );   //needsfixed
 
extern ifstream &operator >> ( ifstream &in_file, complx &number );
 
complx &operator + (double, complx); //needs fixed
complx &operator - (double, complx); //needs fixed
complx &operator * (double, complx); //needs fixed 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Complx.cpp
#include "complx.h"
// define I/O stream //needs fixed
 
extern ifstream &operator >> ( ifstream &in_file, complx &number )
{
     double re, is;
     char ch;
     if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
          && in_file >> is >> ch && ch == ')')
              number.Set(re,is);
     else cerr << "Finish the input"<<endl; //needs fixed
     return in_file;
}
 
ostream &operator<< ( ostream &out_file, complx number )
{
  out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
  return out_file;
}
 
// define constructor
complx::complx( double r, double i )
{
      real = r; imag = i;
}
 
// define overloaded + (plus) operator
complx complx::operator+ (complx c)
{
      complx result;
      result.real = (this->real + c.real);
      result.imag = (this->imag + c.imag);
      return result;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 //call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
          int i=0;
          complx in[7];
          double d = 4.5; 
          cout<< "The input numbers are: " << endl;
 
          while (infile  >> in[i]){
              cout << in[i] << endl;
              i++;
          }        
          complx s1 =  in[0] + in[1]; // calls complx::operator+()
          complx s2 =  d + in[2]; // overload operator+()
          complx s3 =  in[3] + d; // overload operator+()
          complx a = in[4] - in[5];
          complx mm=in[3]*in[4];
          complx dm=d*in[4] ;
          complx b=d-in[0] ;
 
          cout << "The sum is a complex number " << s1 <<endl;
          cout << "The sum is a complex number " << s2 <<endl;
          cout << "The sum is a complex number " << s3 <<endl;
          cout << "The subtract is a complex number " << a <<endl;
          cout << "The product is a complex number " << mm <<endl;          
          cout << "The subtract is a complex number " << b <<endl;
          cout << "The product is a complex number " << dm <<endl;
          if (in[4] == in[5]) cout << "in[4] and in[5] are the same " << endl;
          system("pause");
          return 0;   //successful termination 



The input numbers provided are: (4,4) (6,7) (2.5, 9.3) (3,8.4) (13, 26.5) (2.2, 3.4)

Thanks for any help you guys provide
Last edited on
Hi guys I've been working on this for a while now and I cannot find any help on this

There is 3 different parts to the code and I will mark where i need to fix.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Complx.h
#include <iostream>
#include <fstream>
using namespace std;
 
class complx
{
      double real,
             imag;
public:
      complx( double real = 0., double imag = 0.); // constructor
      complx operator+(complx);       // operator+()
 
      complx operator+(double);       // operator+()with double //needs fixed
      complx operator- (complx);       // operator-()           //needs fixed
      complx operator* (complx);       // operator*()           //needs fixed
 
      bool operator== (complx);   // operator==()               //needs fixed
 
 
       //Sets private data members.
     void Set(double new_real, double new_imaginary) {
     real = new_real;
     imag = new_imaginary;
     }
 
     //Returns the real part of the complex number.
          double Real() {
          return real;
     }
 
     //Returns the imaginary part of the complex number.
     double Imaginary() {
     return imag;
     }
};
 
ostream &operator << ( ostream &out_file, complx number );
 
extern istream &operator >> ( istream &in_file, complx &number );   //needsfixed
 
extern ifstream &operator >> ( ifstream &in_file, complx &number );
 
complx &operator + (double, complx); //needs fixed
complx &operator - (double, complx); //needs fixed
complx &operator * (double, complx); //needs fixed  



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Complx.cpp
#include "complx.h"
// define I/O stream //needs fixed
 
extern ifstream &operator >> ( ifstream &in_file, complx &number )
{
     double re, is;
     char ch;
     if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','
          && in_file >> is >> ch && ch == ')')
              number.Set(re,is);
     else cerr << "Finish the input"<<endl; //needs fixed
     return in_file;
}
 
ostream &operator<< ( ostream &out_file, complx number )
{
  out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
  return out_file;
}
 
// define constructor
complx::complx( double r, double i )
{
      real = r; imag = i;
}
 
// define overloaded + (plus) operator
complx complx::operator+ (complx c)
{
      complx result;
      result.real = (this->real + c.real);
      result.imag = (this->imag + c.imag);
      return result;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 //call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
          int i=0;
          complx in[7];
          double d = 4.5; 
          cout<< "The input numbers are: " << endl;
 
          while (infile  >> in[i]){
              cout << in[i] << endl;
              i++;
          }        
          complx s1 =  in[0] + in[1]; // calls complx::operator+()
          complx s2 =  d + in[2]; // overload operator+()
          complx s3 =  in[3] + d; // overload operator+()
          complx a = in[4] - in[5];
          complx mm=in[3]*in[4];
          complx dm=d*in[4] ;
          complx b=d-in[0] ;
 
          cout << "The sum is a complex number " << s1 <<endl;
          cout << "The sum is a complex number " << s2 <<endl;
          cout << "The sum is a complex number " << s3 <<endl;
          cout << "The subtract is a complex number " << a <<endl;
          cout << "The product is a complex number " << mm <<endl;          
          cout << "The subtract is a complex number " << b <<endl;
          cout << "The product is a complex number " << dm <<endl;
          if (in[4] == in[5]) cout << "in[4] and in[5] are the same " << endl;
          system("pause");
          return 0;   //successful termination  



The input numbers provided are: (4,4) (6,7) (2.5, 9.3) (3,8.4) (13, 26.5) (2.2, 3.4)

Thanks for any help you guys provide



complx operator- (complx); and complx operator* (complx); are similar to complx operator+ (complx);.

And so is complx operator- (double);, you just need to decide whether the double is the real or imaginary part.

If you can't figure it out, post your attempt and someone will help you fix it.
Last edited on
Topic archived. No new replies allowed.