I need help with my project

(Author: Ramin Melikov)

I wrote the class file, the definitions file, and the main program file, and there is something there that is not working and I can't figure out what it is.

Basically, we were supposed to write a class for complex numbers with overloaded operators. Please see my reply to this post for project requirements.

Now, here is my code:

Complex.h
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
#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
public:
	Complex()
		{ real = 0; imag = 0; }
	Complex(double a)
		{ real = a; imag = 0; }
	Complex(double a, double b)
		{ real = a; imag = b; }
private:
	double real;
	double imag;
public:
	void setReal(double a)
		{ real = a; }
	void setImag(double b)
		{ imag = b; }
	double getReal() const
		{ return real; }
	double getImag() const
		{ return imag; }
	Complex operator + (const Complex &);
	Complex operator - (const Complex &);
	Complex operator * (const Complex &);
	Complex operator / (const Complex &);
	Complex& operator ++ ();
	Complex& operator -- ();
	Complex& operator ++ (int);
	Complex& operator -- (int);
	void operator=(const Complex &right)
	{
		real = right.real;
		imag = right.imag;
	}
	class DivisionByZero
		{ };
};

#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
#include "Complex.h"

Complex Complex::operator+(const Complex &right)
{
	Complex temp;
	temp.real = real + right.real;
	temp.imag = imag + right.imag;
	return temp;
}
Complex Complex::operator-(const Complex &right)
{
	Complex temp;
	temp.real = real - right.real;
	temp.imag = imag - right.imag;
	return temp;
}
Complex Complex::operator*(const Complex &right)
{
	Complex temp;
	temp.real = real * right.real - imag * right.imag;
	temp.imag = imag * right.real + real * right.imag;
	return temp;
}
Complex Complex::operator/(const Complex &right)
{
	if (right.real == 0 && right.imag == 0)
		throw DivisionByZero();
	Complex temp;
	temp.real = (real * right.real + imag * right.imag) /
		(right.real * right.real + right.imag * right.imag);
	temp.imag = (imag * right.real - real * right.imag) / 
		(right.real * right.real + right.imag * right.imag);
	return temp;
}
Complex& Complex::operator ++ ()
{
	++real;
	return *this;
}
Complex& Complex::operator -- ()
{
	--real;
	return *this;
}
Complex& Complex::operator ++ (int)
{
	Complex temp(real, imag);
	real++;
	return temp;
}
Complex& Complex::operator -- (int)
{
	Complex temp(real, imag);
	real--;
	return temp;
}


Source.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
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include "Complex.h"

using namespace std;

int main()
{
	int number, index, operation, index2,  unaryOpType, binaryOpType, store;
	int re, im;
	char quit;

	do
	{
		cout << "Please enter the number of complex numbers: ";
		cin >> number;

		Complex **t = new Complex*[number];
		cout << endl;

		for (int i = 0; i < number; i++)
		{
			cout << "Please enter the real and the imaginary parts" << endl;
			cout << "separated by spaces for complex number " << i + 1 << ": ";
			cin >> re >> im;
			cout << endl;
			t[i] = new Complex(re, im);
			cin.clear();
			cin.ignore(256, '\n');
		}

		cout << "Here are the numbers you entered: " << endl;
		for (int i = 0; i < number; i++)
		{
			cout << i + 1 << ") " << t[i]->getReal() << " + " << t[i]->getImag() << "i" << endl;
		}
		cout << number + 1 << ") Quit?" << endl << endl;
		cout << "Now, please pick the index of the number you wish to work with: ";
		cin >> index;

		if (index == (number + 1))
			return 0;

		cout << endl;
		cout << "OK, you picked " << t[index - 1]->getReal() << " + " << t[index - 1]->getImag() << "i" << endl << endl;

		cout << "Now select which operation you'd like to perform: " << endl << endl;
		cout << "1) Unary" << endl << "2) Binary" << endl;
		cin >> operation;

		if (operation == 1)
		{
			cout << "Please select the operation type:" << endl << endl;
			cout << "1) ++Prefix" << endl;
			cout << "2) --Prefix" << endl;
			cout << "3) Postfix++" << endl;
			cout << "4) Postfix--" << endl;
			cin >> unaryOpType;
			cout << "Where would you like to store the result?" << endl;
			cin >> store;


			//This is where I think the problem lies
			if (unaryOpType == 1)
				t[store - 1] = ++t[index - 1];
			if (unaryOpType == 2)
				t[store - 1] = --t[index - 1];
			if (unaryOpType == 3)
				t[store - 1] = t[index - 1]++;
			if (unaryOpType == 4)
				t[store - 1] = t[index - 1]--;
			cout << "Here are the numbers you entered: " << endl;
			for (int i = 0; i < number; i++)
			{
				cout << i + 1 << ") " << t[i]->getReal() << " + " << t[i]->getImag() << "i" << endl;
			}
		}
		if (operation == 2)
		{
			cout << "Please select the operation type:" << endl << endl;
			cout << "1) + (addition)" << endl;
			cout << "2) - (subtraction)" << endl;
			cout << "3) * (multiplication)" << endl;
			cout << "4) / (division)" << endl;
			cin >> binaryOpType;
			cout << "Now, please pick the index of the second operand:" << endl;
			for (int i = 0; i < number; i++)
			{
				cout << i + 1 << ") " << t[i]->getReal() << " + " << t[i]->getImag() << "i" << endl;
			}
			cout << number + 1 << ") Quit?" << endl << endl;
			cin >> index2;
			if (index2 == (number + 1))
				return 0;
			cout << "Where would you like to store the result?" << endl;
			cin >> store;

			if (binaryOpType == 1)
				t[store - 1] = t[index - 1] + t[index2 - 1];
			if (binaryOpType == 2)
				t[store - 1] = t[index - 1] - t[index2 - 1];
			if (binaryOpType == 3)
				t[store - 1] = t[index - 1] * t[index2 - 1];
			
			try
			{
				if (binaryOpType == 4)
					t[store - 1] = t[index - 1] / t[index2 - 1];
			}
			catch (Complex::DivisionByZero)
			{
				cout << "Error: Denominator must not be 0.";
			}
			
			cout << "Here are the numbers you entered: " << endl;
			for (int i = 0; i < number; i++)
			{
				cout << i + 1 << ") " << t[i]->getReal() << " + " << t[i]->getImag() << "i" << endl;
			}
		}
		cout << endl;
		cout << "Start over? (Y/N)" << endl;
		cin >> quit;
	} while (quit == 'Y' || quit == 'y');
	
	system("PAUSE");
	return 0;
}
Last edited on
Here is the project requirement:

Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following formulas:
a + bi + c + di = (a+c) + (b+d)i
a + bi - (c + di) = (a-c) + (b - d)i
(a + bi)*(c + di) = (ac - bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c^2 + d^2) + (bc - ad)i/(c^2 + d^2)
For example, 5 + 3i + 6 + 2i = 11 + 5i
(5+3i)*(6+2i) = (5*6 - 3*2) + (3*6 + 5*2)i = 24 + 28i
++ and -- should simply add or subtract one to the real part (a)

Provide 3 constructors Complex (a,b), Complex (a) and Complex(). Complex() gives a number with a and b = 0, Complex(a) gives a number with a set but b=0, and Complex(a,b) sets both values. Your class should also have getRealPart() and getImaginaryPart() methods for returning the value of a and b respectively, as well as a method that outputs a string representation of the number (to be used for displaying purposes later).

For your main program that will use the Complex class, prompt the user for how many complex numbers they wish to use. Then create an array of Complex numbers and prompt the user for values a and b for the entire array.

Now display the entire array and ask the user which number they would like to work with. Once the user has selected a number, ask the user if they would like to do a unary or binary operation.

If they answer unary, prompt for ++prefix, postfix++,--prefix,postfix-- and then what number to store the result in. Then perform that operation. If they say they want to perform a binary operation, prompt for an operation (+,-,*,/) and then the second operand. Lastly, ask them in which number they wish to store the result. Perform the calculation and put the result where they told you to put it. Internally to your code, use the overloaded operators to do the work.

So if users have the following list of numbers
1) 5 + 3i
2) 2 - 4i
3) 3 + 5i
4) quit
and they select 1,
then prompt them for
1) unary
2) binary
if they select 2,
then ask them what type of operation
1) +
2) -
3) *
4) /
if they enter +, then redisplay the numbers and prompt them for the second operand
1) 5 + 3i
2) 2 - 4i
3) 3 + 5i
4) quit
If they enter 2, then ask them for where to store the result. Suppose they enter 3 for the result. Then you want to do number 1 + number 2 and store the result in 3.
(5+3i) + (2 - 4i) and store the result in number 3

Your program should continue with this in a menu until the user selects quit.

Exception handling: Your program should deal with the divide by zero error by throwing an exception in the divide overload, catching it in the main body and outputting an appropriate error message, after which you can let the program terminate.
Topic archived. No new replies allowed.