Palindrome in C++

Solution to Project Euler problem 4

#include<iostream>
using namespace std;

bool isPalindrome(char* x);

int main()
{
	char prodStr[100];
    int max = 0;
    int i, j, prod;
    for (i = 100; i < 1000; i++)
    {
        for (j = 100; j < 1000; j++)
        {
            prod = i * j;
            sprintf_s(prodStr, 100, "%i", prod);
            if(isPalindrome(prodStr) == 1)
            {
                if(prod > max)
                    max = prod;
            }
        }
    }
	cout<<max<<endl;
    return 0;
}

bool isPalindrome(char* x)
{
    int i = 0;
    int j = strlen(x) - 1;
 
    while (i <= j)
    {
        if (x[i] != x[j])
            return false;
        i++;
        j--;
    }
 
    return true;
}

Operator Overload in C++

#include<iostream>
using namespace std;

class SuperBigInt
{
private:
int value;
public:
SuperBigInt(int i):
value(i)
{
}
SuperBigInt operator+(const SuperBigInt& a);
SuperBigInt operator*(const SuperBigInt& a);
SuperBigInt operator-(const SuperBigInt& a);
SuperBigInt operator/(const SuperBigInt& a);
friend ostream &operator<<(ostream& out, SuperBigInt c);
};

SuperBigInt SuperBigInt::operator+(const SuperBigInt& a)
{
return SuperBigInt(value + a.value);
}

SuperBigInt SuperBigInt::operator*(const SuperBigInt& a)
{
return SuperBigInt(value * a.value);
}

SuperBigInt SuperBigInt::operator-(const SuperBigInt& a)
{
return SuperBigInt(value – a.value);
}

SuperBigInt SuperBigInt::operator/(const SuperBigInt& a)
{
return SuperBigInt(value / a.value);
}

ostream & operator<<(ostream& out, SuperBigInt c)
{
out<<c.value;
return out;
}

int main()
{
SuperBigInt a(12);
SuperBigInt b(6);
SuperBigInt c = a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
c = a-b;
cout<<a<<"-"<<b<<"="<<c<<endl;
c = a*b;
cout<<a<<"*"<<b<<"="<<c<<endl;
c = a/b;
cout<<a<<"/"<<b<<"="<<c<<endl;
int sdsdad = 0 ;
}

Posted in C++

Callback (using an abstract class) in C++

#include<iostream>
using namespace std;

class callbackClass
{
public:
	virtual void callBack() = 0;
};

class ClassB
{
public:
	void DoSomething();
	void setCallback(callbackClass *c);
private:
	callbackClass *cb;
};

void ClassB::DoSomething()
{
	cout<<"Doing something"<<endl;
	if(cb != NULL)
		cb->callBack();
}

void ClassB::setCallback(callbackClass *c)
{
	cb = c;
}

class ClassA : callbackClass
{
public:
	void doSomething();
	void callBack();
};

void ClassA::callBack()
{
	cout<<"I am all done!"<<endl;
}

void ClassA::doSomething()
{
	ClassB *b = new ClassB();
	b->setCallback(this);
	b->DoSomething();

	delete b;
}

int main()
{
	ClassA *a = new ClassA();
	a->doSomething();

	delete a;
	return 0;
}
Posted in C++

Callback (using an ordinary function) in C++

#include<iostream>
using namespace std;

void CallMeWhenDone()
{
	cout<<"I am all done!"<<endl;
}

class ClassB
{
public:
	void DoSomething();
	void setCallback(void (*c)());
private:
	void (*callWhenDone)();
};

void ClassB::DoSomething()
{
	cout<<"Doing something"<<endl;
	if(callWhenDone != NULL)
		callWhenDone();
}

void ClassB::setCallback(void (*c)())
{
	callWhenDone = c;
}

class ClassA
{
public:
	void doSomething();
};

void ClassA::doSomething()
{
	ClassB *b = new ClassB();
	b->setCallback(CallMeWhenDone);
	b->DoSomething();

	delete b;
}

int main()
{
	ClassA *a = new ClassA();
	a->doSomething();

	delete a;
	return 0;
}
Posted in C++