06 August, 2010

ADD WITHOUT ADD OPERATOR

 #include<stdio.h>
int main()
{
int a,b;
int sum;
printf(“Enter any two integers: “);
scanf(“%d%d”,&a,&b);
//sum = a – (-b);
sum = a – ~b -1;
printf(“Sum of two integers:%d”,sum);
return 0;
}

05 August, 2010

ADD COMPLEX NUMBERS

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct complex
{
int real;
int img;
};
int main()
{
struct complex a, b, c;
printf(” Enter a and b where a + ib is the first complex number. “);
printf(“\n a = “);
scanf(” %d “, &a.real);
printf(” b = “);
scanf(” %d “, &a.img);
printf(” Enter c and d where c + id is the second complex number. “);
printf(“\n c = “);
scanf(” %d “, &b.real);
printf(” d = “);
scanf(” %d “, &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf(” Sum of two complex numbers = %d + %di “, c.real, c.img);
else
printf(” Sum of two complex numbers = %d %di “, c.real, c.img);
getch();
return 0;
}

04 August, 2010

ACCESSING STATIC MEMBERS WITHOUT AN OBJECT

#include <iostream>
using namespace std;
class Cat
{
public:
Cat(int age):itsAge(age){count++; }
virtual ~Cat() { count–; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int count;
private:
int itsAge;
};
int Cat::count = 0;
void TelepathicFunction();
int main()
{
const int MaxCats = 5; int i;
Cat *CatHouse[MaxCats];
for (i = 0; i< MaxCats; i++)
{
CatHouse[i] = new Cat(i);
TelepathicFunction();
}
for ( i = 0; i< MaxCats; i++)
{
delete CatHouse[i];
TelepathicFunction();
}
return 0;
}
void TelepathicFunction()
{
cout << “There are “;
cout << Cat::count << ” cats alive!\n”;
}

03 August, 2010

ACCESSING DATA IN A FILE

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
template <class T>
void print(T & c)
{
for( typename T::iterator i = c.begin(); i != c.end(); i++ )
{
std::cout << *i << endl;
}
}
int main()
{
vector <int> output_data( 10 );
generate( output_data.begin(), output_data.end(), rand );
transform( output_data.begin(), output_data.end(),output_data.begin(), bind2nd( modulus<int>(), 10 ) );
ofstream out( “data.txt” );
if( !out )
{
cout << “Couldn’t open output file\n”;
return 0;
}
copy( output_data.begin(), output_data.end(),ostream_iterator<int>( out, “\n” ) );
out.close();
ifstream in( “data.txt” );
if( !in )
{
cout << “Couldn’t open input file\n”;
return 0;
}
vector<int> input_data( (istream_iterator<int>( in )),istream_iterator<int>() );
in.close();
print( output_data );
print( input_data );
}

02 August, 2010

ACCESSING CHARACTERS IN STRINGS

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string text;
cout << “Counts words. Enter a text and terminate with a period and return:\n”;
getline( cin, text, ‘.’); // Reads a text up to the first ‘.’
int i, // Index
numberOfWhiteSpace = 0, // Number of white spaces
numberOfWords = 0; // Number of words
bool fSpace = true; // Flag for white space
for( i = 0; i < text.length(); ++i)
{
if( isspace( text[i]) ) // white space?
{
++numberOfWhiteSpace;
fSpace = true;
}
else if( fSpace) // At the beginning of a word?
{
++numberOfWords;
fSpace = false;
}
}
cout << “\nYour text contains (without periods)”
<< “\ncharacters: ” << text.length()
<< “\nwords: ” << numberOfWords
<< “\nwhite spaces: ” << numberOfWhiteSpace
<< endl;
return 0;
}