//Write a program to overload the operator < and >such that it compares two strings
//and returns the appropriate result .the string should be read and printed using the
//overlloaded stream operator
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class A{
public:
char s1[100],s2[100];
public:
int operator <(A &obj){
int n=strcmp(s1,obj.s2);
if(n<0) return 1;
else return 0;
}
int operator >(A &obj){
int n=strcmp(s1,obj.s2);
if(n>0) return 1;
else return 0;
}
friend istream& operator >> (istream &in,A &obj){
in>>obj.s1;
in>>obj.s2;
return in;
}
friend ostream& operator << (ostream &out,int n){
out<<n;
return out;
}
};
int main()
{
A a;
cin>>a;
cout<<(a<a)<<endl;
cout<<(a>a)<<endl;
return 0;
}
overloading less than(”) operator April 28, 2010
friend function implemented April 28, 2010
//Write a program using friend function which will add two distances
//stored in two different classes.The first class holds the distance
// in m & cm and the second class holds the distance in feet & inch
#include<iostream>
using namespace std;
class B;
class A{
float m,cm,res;
public:
A():m(20.0),cm(2.0){}
void fun(B obj);
};
class B{
float feet,inch;
public:
B():feet(9),inch(3){}
friend void A::fun(B obj);
};
void A::fun(B obj){
res=m*100.0+cm+obj.feet*12.0*2.54+obj.inch*2.54;
res=res/100;
cout<<res<<"metre\n";
}
int main()
{
A a;
B b;
a.fun(b);
return 0;
}
TopCoder SRM 147 Div2 February 13, 2010
TopCoder SRM 147 Div2
C++
–SUBMITTED–[149 points]
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
class CCipher{
public:
string decode(string cipherText,int shift){
for(int i=0;cipherText[i]!='\0';i++)
cipherText[i]='A'+(cipherText[i]-'A'+26-shift)%26;
return cipherText;
}
};
/*int main()
{
CCipher c;
string s=c.decode("VQREQFGT",2);
cout<<s<<endl;
return 0;
}
*/
TopCoder SRM 262 – DivToZero C++ February 12, 2010
TopCoder Single Round Match 262 Round 1 – Division II, Level One
–AC–
c++
The format in which i submitte it :-
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
class DivToZero{
int i,j;
char s[20];
public:
string lastTwo(int num,int factor){
if(num%factor==0){
j=num%100;
if(j<10)
sprintf(s,"0%d",j);
else
sprintf(s,"%d",j);
//cout<<j<<endl;
return s;
}
else{
num=num/100;
num=num*100;
for(i=num;;i++)
{
if(i%factor==0)
break;
}
j=i%100;
//cout<<num<<endl;
if(j<10)
sprintf(s,"0%d",j);
else
sprintf(s,"%d",j);
return s;
}
}
};
here is a snapshot of the arena

The format in which i tested in g++ on my system for each of test case is below
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
class DivToZero{
int i,j;
char s[20];
public:
string lastTwo(int num,int factor){
if(num%factor==0){
j=num%100;
if(j<10)
sprintf(s,"0%d",j);
else
sprintf(s,"%d",j);
//cout<<j<<endl;
return s;
}
else{
num=num/100;
num=num*100;
for(i=num;;i++)
{
if(i%factor==0)
break;
}
j=i%100;
//cout<<num<<endl;
if(j<10)
sprintf(s,"0%d",j);
else
sprintf(s,"%d",j);
return s;
}
}
};
int main()
{
DivToZero a;
string p=a.lastTwo(32442,99);
cout<<p<<endl;
return 0;
}
TopCoder Arena launching in fedora(linux) February 12, 2010
Today i launched Topcoder arena in feodra. To do this just save the tocoderapplet file and then open it with firefox and then enter option …/../jawas which is located in java folder or jre folder of java……………
Now i will start doing problems over here also. One important thing about topcoder is that it doesnot use ” main ” function .However method has to be defined public.
TopCoder!!!!!!!!!!!!!
Nested Friends c++ February 12, 2010
[..from Eckel c++]
Making a structure(or class) nested doesnot automatically give it access to private members.To accomplish this,we must follow a particular form, first declare (without defining ) the nested structure ,then declare it as s friend ,and finally define the structure.The structure definition must be separate from the friend declaration ,otherwise it would be seen by the compiler as a non-member.
Friend c++ demonstration February 12, 2010
[..from C++ Eckel]
//Friend -demonstration
#include<iostream>
using namespace std;
struct X;
struct Y{
void f(X*);
};
struct X{
private:
int i;
public:
void initialize();
friend void g(X*,int);//global friend
friend void Y::f(X*);//struct member friend
friend struct Z; //entire struct is a friend
friend void h();
};
(more…)
Operators that we can’t overload.Why? February 12, 2010
[..from Bruce Eckel c++]
There are certain operators that cannot be overloaded.The general reason for the restriction is safety.If these operators were overloadable ,it would somehow jeopardizez or break safety mechanisms,make things harder ,or confuse existing practise. (more…)
printing something before & after main February 11, 2010
//This program prints some output before main &some output after main
//using constructor and destructor
#include<iostream>
using namespace std;
class A{
public:
A(){
cout<<"Before main\n";
}
~A(){
cout<<"\nAfter main\n";
}
}a,b;
int main()
{
cout<<"In main";
return 0;
}
Returning Reference to a local variable February 8, 2010
While returning reference to a local varible in c++ may show warning or have undefined behaviour if not handled proprly.
The problem listed in the below forum will arisse if not hadled propely
Link
TJU 3013. Alfredo’s Pizza Restaurant January 26, 2010
TJU 3013. Alfredo’s Pizza Restaurant
–AC–
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
class A{
int r,w,l,i,j;
public:
void input(){
cin>>r;
if(r==0) exit(0);
cin>>w>>l;
}
void output(){
if((float)(2*r)>=(float)sqrt(w*w+l*l))
cout<<" fits on the table.\n";
else
cout<<" does not fit on the table.\n";
}
};
int main(){
A obj;
int i=0;
while(1){
obj.input();
cout<<"Pizza "<<++i;
obj.output();
}
return 0;
}
SPOJ 5872. Anagram January 16, 2010
SPOJ 5872. Anagram
Problem code: ANAG
c++/oops implemented
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class A{
char s1[100],s2[100];
int i,j,k;
public:
void input(){
cin>>s1>>s2;
}
void cal(){
int a[26],b[26];
for(i=0;i<26;i++)
a[i]=b[i]=0;
for(i=0;s1[i]!='\0';i++)
{
a[s1[i]-'a']++;
}
for(i=0;s2[i]!='\0';i++)
{
b[s2[i]-'a']++;
}
output(a,b);
}
void output(int a[],int b[]){
int flag=0;
for(i=0;i<26;i++)
{
if(a[i]==b[i]){
flag=1;
continue;
}
else{
flag=0;
break;
}
}
if(flag==1) cout<<"YES\n";
else cout<<"NO\n";
}
};
int main()
{
A obj;
int cases;
cin>>cases;
while(cases--)
{
obj.input();
obj.cal();
}
return 0;
}
c++ struct vs classes January 13, 2010
1. c++struct by default is public while class is default private.
this is the only major difference. It means if we leave the default scenerio then c++ struct and class are very same. It was done so as to
keep the feature of “C” in “C++”.
http://blog.stevedoria.net/20050913/differences-between-cpp-classes-and-structs



Recent Comments