Hi Guys, I need your help here, I'm working on a assignment, and had done up the source code, but I'm getting wrong results:
Summarised below:
I screenshot the output:
[IMG]http://i4.photobucket.com/albums/y101/clement_twk/Output.png[/IMG]
I noticed under the output on these 2 lines are wrong:
Addition A + B = 100+103i+106j+109k
I supposed to get 4+7i+10j+13k
Prefix ++A = 52+54i+56j+58k
I supposed to get 4+6i+8j+10k
The rest are correct.
Attached the below source code:
#include <iostream>
#include <string>
using namespace std;
class Quarterian
{
friend istream& operator>>(istream&, Quarterian&);
friend ostream& operator<<(ostream&, Quarterian&);
public:
Quarterian operator+(Quarterian);
Quarterian operator-(Quarterian);
bool operator==(Quarterian&);
bool operator>(Quarterian&);
Quarterian& operator++();
private:
int A, B, C, D, E;
};
istream& operator>>(istream& in, Quarterian& num)
{
string input;
in >> input;
num.A = input[0];
num.B = input[2];
num.C = input[5];
num.D = input[8];
return in;
}
ostream& operator<<(ostream& out, Quarterian& num)
{
out << num.A << "+" << num.B << "i" << "+" << num.C << "j" << "+" << num.D << "k";
return out;
}
Quarterian Quarterian::operator+(Quarterian t)
{
Quarterian temp_result;
temp_result.A = A + t.A;
temp_result.B = B + t.B;
temp_result.C = C + t.C;
temp_result.D = D + t.D;
return temp_result;
}
Quarterian Quarterian::operator-(Quarterian t)
{
Quarterian temp_result;
temp_result.A = A - t.A;
temp_result.B = B - t.B;
temp_result.C = C - t.C;
temp_result.D = D - t.D;
return temp_result;
}
bool Quarterian::operator==(Quarterian& num)
{
bool truth = false;
if(A == num.A && B == num.B && C == num.C && D == num.D)
{
truth = true;
}
return truth;
}
bool Quarterian::operator>(Quarterian& num)
{
bool truth = false;
if(A > num.A && B > num.B && C > num.C && D > num.D)
{
truth = true;
}
return truth;
}
Quarterian& Quarterian::operator++()
{
A++;
B++;
C++;
D++;
return *this;
}
int main()
{
Quarterian A, B, C, D, E;
cout << "Enter first quaternion, format a + bi + cj + dk: ";
cin >> A;
cout << "Enter second quaternion, format a + bi + cj + dk: ";
cin >> B;
C = A + B;
D = A - B;
cout << "Addition A + B = " << C << endl;
cout << "Subtraction A - B = " << D << endl;
if (A == B)
cout << "A = B: True" << endl;
else
cout << "A = B: False" << endl;
if (A > B)
cout << "A > B: True" << endl;
else
cout << "A > B: False" << endl;
E = ++A;
cout << "Prefix ++A = " << E << endl;
system("pause");
return 0;
}