Bugs

I have some trouble with my program.

I'm want to get this at the end:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(123.4568 + 567.8970i) + (112.3457 + 6654.1235i) = (235.8025 + 7222.0205i)
(123.4568 + 567.8970i) - (112.3457 + 6654.1235i) = (11.1111 - 6086.2264i)
(123.4568 + 567.8970i) * (112.3457 + 6654.1235i) = (-3764986.9881 + 885297.4896i)
(123.4568 + 567.8970i) / (112.3457 + 6654.1235i) = (0.0856 - 0.0171i)
(-1.0000 - 2.0000i) + (-3.0000 - 4.0000i) = (-4.0000 - 6.0000i)
(-1.0000 - 2.0000i) - (-3.0000 - 4.0000i) = (2.0000 + 2.0000i)
(-1.0000 - 2.0000i) * (-3.0000 - 4.0000i) = (-5.0000 + 10.0000i)
(-1.0000 - 2.0000i) / (-3.0000 - 4.0000i) = (0.4400 + 0.0800i)
(573.4560 + 872.3320i) + (9000.0000 - 9000.0000i) = (9573.4560 - 8127.6680i)
(573.4560 + 872.3320i) - (9000.0000 - 9000.0000i) = (-8426.5440 + 9872.3320i)
(573.4560 + 872.3320i) * (9000.0000 - 9000.0000i) = (13012092.0000 + 2689884.0000i)
(573.4560 + 872.3320i) / (9000.0000 - 9000.0000i) = (-0.0166 + 0.0803i)
(10.0000 + 20.0000i) + (-10.0000 + 20.0000i) = (0.0000 + 40.0000i)
(10.0000 + 20.0000i) - (-10.0000 + 20.0000i) = (20.0000)
(10.0000 + 20.0000i) * (-10.0000 + 20.0000i) = (-500.0000)
(10.0000 + 20.0000i) / (-10.0000 + 20.0000i) = (0.6000 - 0.8000i)


But I got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(123.4568 + 567.8970i) + (112.3457 + 6654.1235i) = (235.8025 + 7222.0205i)
(123.4568 + 567.8970i) + (112.3457 + 6654.1235i) = (235.8025 + 7222.0205i)
(123.4568 + 567.8970i) + (112.3457 + 6654.1235i) = (235.8025 + 7222.0205i)
(123.4568 + 567.8970i) + (112.3457 + 6654.1235i) = (235.8025 + 7222.0205i)
(-1.0000 - 2.0000i) + (-3.0000 - 4.0000i) = (-4.0000 - 6.0000i)
(-1.0000 - 2.0000i) + (-3.0000 - 4.0000i) = (-4.0000 - 6.0000i)
(-1.0000 - 2.0000i) + (-3.0000 - 4.0000i) = (-4.0000 - 6.0000i)
(-1.0000 - 2.0000i) + (-3.0000 - 4.0000i) = (-4.0000 - 6.0000i)
(573.4560 + 872.3320i) + (9000.0000 - 9000.0000i) = (9573.4560 - 8127.6680i)
(573.4560 + 872.3320i) + (9000.0000 - 9000.0000i) = (9573.4560 - 8127.6680i)
(573.4560 + 872.3320i) + (9000.0000 - 9000.0000i) = (9573.4560 - 8127.6680i)
(573.4560 + 872.3320i) + (9000.0000 - 9000.0000i) = (9573.4560 - 8127.6680i)
(10.0000 + 20.0000i) + (-10.0000 + 20.0000i) = (0.0000 + 40.0000i)
(10.0000 + 20.0000i) + (-10.0000 + 20.0000i) = (0.0000 + 40.0000i)
(10.0000 + 20.0000i) + (-10.0000 + 20.0000i) = (0.0000 + 40.0000i)
(10.0000 + 20.0000i) + (-10.0000 + 20.0000i) = (0.0000 + 40.0000i)
Here's my program

main.cpp
1
2
3
4
5
6
7
#include "ComplexTest.hpp"

int main() {
	ComplexTest test;
	test.Run();
	return 0;
}


Complex.hpp
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
#ifndef COMPLEX_HPP
#define COMPLEX_HPP

#include <string>

using namespace std;

class Complex
{
private:
    double mReal;
    double mImag;
public:
    Complex();
    Complex(double pInitReal, double pInitImag);
    Complex Add(Complex& pRHSOp);
    Complex Div(Complex& pRHSOp);
    double GetImag();
    double GetReal();
    Complex Invert();
    Complex Mult(Complex& pRHSOp);
    Complex Negate();
    void SetImag(double pNewImag);
    void SetReal(double pNewReal);
    Complex Sub(Complex& pRHSOp);
    string ToString();
    void Init(double pInitReal, double pInitImag);
};

#endif  

Complex.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iomanip>
#include <sstream>
#include "Complex.hpp"

using namespace std;

Complex::Complex()
{
    Init(0, 0);
}

Complex::Complex(double pInitReal, double pInitImag)
{
    Init(pInitReal, pInitImag);
}

Complex Complex::Add(Complex& pRHSOp)
{
    double sumReal = GetReal() + pRHSOp.GetReal();
    double sumImag = GetImag() + pRHSOp.GetImag();
    Complex sum;
    sum.Complex::Complex(sumReal, sumImag);
    return sum;
}

Complex Complex::Div(Complex& pRHSOp)
{
    Complex invRHSOp;
    invRHSOp = pRHSOp.Invert();
    Mult(invRHSOp);
    return Mult(invRHSOp);
}

double Complex::GetImag()
{
    return mImag;
}


double Complex::GetReal()
{
    return mReal;
}

void Complex::Init(double pInitReal, double pInitImag)
{
    SetReal(pInitReal);
    SetImag(pInitImag);
}


Complex Complex::Invert()
{
    double denom = GetReal() * GetReal() + GetImag() * GetImag();
    double invReal = GetReal() / denom;
    double invImag = -GetImag() / denom;
    Complex inv;
    inv.Complex::Complex(invReal, invImag);
    return inv;
}


Complex Complex::Mult(Complex& pRHSOp)
{
    double prodReal = GetReal() * pRHSOp.GetReal() - GetImag() * pRHSOp.GetImag();
    double prodImag = GetImag() * pRHSOp.GetReal() + GetReal() * pRHSOp.GetImag();
    Complex product;
    product.Complex::Complex(prodReal, prodImag);
    return product;
}


Complex Complex::Negate()
{
    Complex neg;
    neg.Complex::Complex(-GetReal(), -GetImag());
    return neg;
}


void Complex::SetImag(double pNewImag)
{
    mImag = pNewImag;
}


void Complex::SetReal(double pNewReal)
{
    mReal = pNewReal;
}


Complex Complex::Sub(Complex& pRHSOp)
{
    Complex negRHSOp = pRHSOp.Negate();
    Complex diff = Add(negRHSOp);
    return diff;
}

string Complex::ToString()
{
    stringstream strstr;
    strstr << fixed << setprecision(4);
    strstr << "(" << GetReal();
    double imag = GetImag();
    if (imag < 0)
    {
        strstr << " - " << -imag << 'i';
    }
    else if (imag > 0)
    {
        strstr << " + " << imag << 'i';
    }
    strstr << ")";
    strstr.str();
    return strstr.str();
}

ComplexTest.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef COMPLEXTEST_HPP
#define COMPLEXTEST_HPP

#include "Complex.hpp"

class ComplexTest 
{
public:
	ComplexTest();
	void Run();
};

#endif  


ComplexTest.cpp
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
47
48
49
50
51
#include <fstream> 
#include "Complex.hpp"
#include "ComplexTest.hpp"

using namespace std;

ComplexTest::ComplexTest()
{
}

void ComplexTest::Run()
{
    ifstream fin;
    fin.open("complex-in.txt");
    ofstream fout;
    fout.open("complex-out.txt");
    char operation;
    while (fin >> operation)
    {
        double real1, imag1, real2, imag2;
        fin >> real1;
        fin >> imag1;
        fin >> real2;
        fin >> imag2;
        Complex c1;
        c1.Complex::Complex(real1, imag1);
        Complex c2;
        c2.Complex::Complex(real2, imag2);
        Complex result;
        result.Complex::Complex();
        if (operation = '+')
        {
            result = c1.Add(c2);
        }
        else if (operation = '-')
        {
            result = c1.Sub(c2);
        }
        else if (operation = '*')
        {
            result = c1.Mult(c2);
        }
        else
        {
            result = c1.Div(c2);
        }
        fout << c1.ToString() << ' ' << operation << ' ' << c2.ToString() << " = " << result.ToString() << endl;
    }
    fin.close();
    fout.close();
}

complex-in.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+ 123.456789 567.897012 112.345678 6654.12345
- 123.456789 567.897012 112.345678 6654.12345
* 123.456789 567.897012 112.345678 6654.12345
/ 123.456789 567.897012 112.345678 6654.12345
+ -1 -2 -3 -4
- -1 -2 -3 -4
* -1 -2 -3 -4
/ -1 -2 -3 -4
+ 573.456 872.332 9000.0 -9000.0
- 573.456 872.332 9000.0 -9000.0
* 573.456 872.332 9000.0 -9000.0
/ 573.456 872.332 9000.0 -9000.0
+ 10 20 -10 20
- 10 20 -10 20
* 10 20 -10 20
/ 10 20 -10 20
I need it fix ASAP.
@rjryan94

Have you tried using the debugger? It's just that looking at over 200 LOC, to find runtime errors takes too long. Much quicker with a debugger to see where things go wrong.

Something to investigate is the operator they are all + in the output.

I have some advice to make your code better, though.

Remember that class functions have direct access to member variables, so no need to use get / set functions from within the class function, like you do here for example:

1
2
3
4
5
void Complex::Init(double pInitReal, double pInitImag)
{
    SetReal(pInitReal);
    SetImag(pInitImag);
}


Also, consider using initialisation rather than assignment in your functions - you may not need the Init function at all:

1
2
3
Complex::Complex() : mReal(0.0), mImag(0.0){}

Complex::Complex(double pInitReal, double pInitImag): mReal(pInitReal), mImag(pInitImag){}



It is worth explicitly in-lining very trivial functions:

inline double Complex::GetImag() const { return mImag;}

Having said that, be careful with in-lining, it's probably only worth it for very trivial functions like this one.

So now you can do this:

double prodReal = mReal * pRHSOp.GetReal() - mImag * pRHSOp.GetImag();

Even better would be to overload the operators in the class, rather than writing functions for them.

With lines 31 - 48 in ComplexTest.cpp, consider using a switch instead, with the division operation being a case. That way you can use the default clause to catch bad input, rather than the division. Always use the else of default to catch bad input. I prefer to use a switch whenever I can (for integral types).

Another thing is that it is a common convention to use a leading 'p' in a variable name that means that it is a pointer type.

Hope all goes well.
closed account (D80DSL3A)
Classic error.
Use == not = on lines 31, 35 and 39 in ComplexTest.cpp
Last edited on
Thank you, fun2code.

Also, TheIdeasMan, I appreciate your help, but I have no idea what you're saying.
@rjryan94

So which part don't you understand? I always try to explain things well - but not well enough here it seems.

I am happy to explain further if you like :)
Topic archived. No new replies allowed.