Over the weekend I received an automated email message from HackerRank to solve the following challenge: https://www.hackerrank.com/challenges/overload-operators?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign
If interested, please take a look at the challenge description.
As usual, I try to address the challenge without consulting the provided code or look at the discussions. After losing some time on my own, I check the discussions and the code provided to solve the challenge. In this case the following read only code was provided:
//Operator Overloading
#include<iostream>
using namespace std;
class Complex
{
public:
int a,b;
void input(string s)
{
int v1=0;
int i=0;
while(s[i]!=’+’)
{
v1=v1*10+s[i]-‘0′;
i++;
}
while(s[i]==’ ‘ || s[i]==’+’||s[i]==’i’)
{
i++;
}
int v2=0;
while(i<s.length())
{
v2=v2*10+s[i]-‘0’;
i++;
}
a=v1;
b=v2;
}
};
There was no way to alter the Complex class definition. Instead of declaring in the class friend functions I had to switch to global functions.
Not that it matters, but the main() for testing was also read only code as illustrated by the following code snippet:
int main()
{
Complex x,y;
string s1,s2;
cin>>s1;
cin>>s2;
x.input(s1);
y.input(s2);
Complex z=x+y;
cout<<z<<endl;
}
I used the Visual Studio 2013 Professional 2013 IDE to tackle this challenge. My main() function follows:
/*
Test funcion.
*/
int main()
{
Complex x, y;
//string s1, s2;
//cin >> s1;
//cin >> s2;
//cout << “s1 ==>” << s1 << “<==” << endl;
//cout << “s2 ==>” << s2 << “<==” << endl;
//x.input(s1);
//y.input(s2);
// **** ****
cin >> x;
cin >> y;
// **** ****
cout << x << endl;
cout << y << endl;
// **** ****
Complex z = x + y;
// **** ****
cout << z.a << “+i” << z.b << endl;
cout << z << endl;
// **** ****
return 0;
}
As you can see in the previous code snippet, I decided to overload the ‘>>’ operator. The challenge uses the input() function / method in the Complex class to read strings holding the complex input numbers.
The following is a screen capture of the output of my test:
3+i4 <== sample input
5+i6 <== sample input
3+i4 <== overloaded output
5+i6 <== overloaded output
8+i10 <== output
8+i10 <== overloaded output
It seems that for at least the sample input values, the software is working.
Following is the code for my solution:
#include <iostream>
#include <string>
using namespace std;
class Complex
{
public:
int a, b;
// **** ****
void input(string s)
{
int v1 = 0;
int i = 0;
while (s[i] != ‘+’)
{
v1 = v1 * 10 + s[i] – ‘0’;
i++;
}
while (s[i] == ‘ ‘ || s[i] == ‘+’ || s[i] == ‘i’)
{
i++;
}
int v2 = 0;
while (i < (int)s.length())
{
v2 = v2 * 10 + s[i] – ‘0’;
i++;
}
a = v1;
b = v2;
}
#ifdef CAKE
// **** ‘+’ operator overloading (prototype) ****
Complex operator + (const Complex& rightHandSide);
// **** ‘<<‘ insertion operator stream overloading (prototype) ****
friend ostream& operator << (ostream &os, const Complex &rightHandSide);
// **** ‘>>’ extraction operator stream overloading (prototype) ****
friend istream& operator >> (istream &is, Complex &rightHandSide);
#endif
};
#ifdef CAKE
/*
+ add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)
*/
Complex Complex::operator + (const Complex& rightHandSide) {
Complex temp;
temp.a = this->a + rightHandSide.a;
temp.b = this->b + rightHandSide.b;
return temp;
}
#endif
/*
+ add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)
*/
Complex operator + (const Complex& lhs, const Complex& rhs)
{
Complex temp;
temp.a = lhs.a + rhs.a;
temp.b = lhs.b + rhs.b;
return temp;
}
/*
Overload ‘<<‘ operator.
*/
ostream& operator << (ostream& os, const Complex& rightHandSide) {
os << rightHandSide.a << “+i” << rightHandSide.b;
return os;
}
/*
Overload ‘>>’ operator.
*/
istream& operator >> (istream& is, Complex& rightHandSide) {
// **** ****
is >> rightHandSide.a;
// **** ****
is.ignore(); // skip ‘+’
is.ignore(); // skip ‘i’
// **** ****
is >> rightHandSide.b;
return is;
}
I copied the two global functions to the HackerRank web page and was able to pass all 10 test cases.
My personal preference is to modify the base class to include as friends the override functions.
If you have comments or questions regarding this or any other post in this blog, please do not hesitate and send me a message via email. I will respond as soon as possible and will not use your name unless you explicitly allow me to do so.
John
john.canessa@gmail.com
Follow me on Twitter: @john_canessa