Friday, October 28, 2011

Binary Search TREE

#include<iostream>

#include<stdlib.h>


using namespace std;



struct node

{

int d;

struct node *left;

struct node *right;

};



struct node *root;


void init();

void empty();

int insert(int);

void search(int);


 



void init()

{

root=(struct node *)malloc(sizeof(struct node));

root=NULL;

}



int insert(int x)

{

struct node *p,*previous,*current;


p=(struct node *)malloc(sizeof(struct node));



 if (p==NULL)

 { 

 cout<<"\nOut of Memory";

 return 0;

 }


p->d=x;

p->left=NULL;

p->right=NULL;


 if(root==NULL)

 {

root=p;

 return 1;

 }
 


previous=NULL;

current=root;



 while (current!=NULL)

 {

 previous=current;

 
if (p->d<current->d)
 
  current=current->left;

  else
 
  current=current->right;

 }



if(p->d<previous->d)
 
previous->left=p;

else
 
previous->right=p;


return 1;


}








void search(int sno)

{

struct node*t;

t=(struct node *)malloc(sizeof(struct node));

t=root;



 while(t!=NULL && t->d !=sno)

 {

if(t->d>sno)
{ t=t->left; }

else
 { t=t->right; }

}



if(t)

cout<<"\nThe Search Element is Present";

else

cout<<"\nElement is not Present";

}




int main()

{

int c,no,x;

init();



do

{

cout<<"\n1.Insert";
cout<<"\n2.Search";
cout<<"\n3.Exit";

cout<<"\nEnter Your Choice:\t";

cin>>c;



switch(c)

{

case 1: cout<<"\nEnter the Element to Insert:\t";
        
        cin>>no;

        x=insert(no);

        if(x==1)

         cout<<"\nsucessful Insertion";

        else

         cout<<"\nError in insertion";

        break;

case 2: cout<<"\nEnter the Element to Search: \t";
        cin>>no;

        search(no);

        break;

case 3: exit (0); break;

default: cout<<"\nEnter Any No. Between 1/3";

}


}while(c<3);


return 0;

}

Saturday, February 26, 2011

Files


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>

void main()
{
fstream ob;

ob.open("stud1.dat",ios::out|ios::binary);

int rollno,mark,entry;

clrscr();

cout<<"\n\n\n\n\t\t\tEnter the serial no[0 to exit]:";
cin>>entry;

 while(entry!=0)
 {
 cout<<endl<<"\n\n\t\tEnter the stud no: ";
 cin>>rollno;

 cout<<"\n\n\t\tEnter the mark: ";
 cin>>mark;

 cout<<endl;

 ob<<setw(0)<<rollno<<setw(10) <<mark<<endl;

 cout<<"\n\n\n\t\t\tEnter serial no[0 to exit]";
 cin>>entry;

 }

ob.close();

getch();

}

Friday, February 25, 2011

Inheritance


#include<iostream.h>
#include<conio.h>
#include<math.h>

class shape
{
public:
float a;
};

class triangle: public shape
{
int a,b,c;
float s;

public:
 triangle(int x,int y,int z)
 {
 a=x;b=y;c=z;
 s=(a+b+c)/2.0;
 }

 void area()
 {
 a=sqrt(s*(s-a)*(s-b)*(s-c));
 }

 void show()
 {
 cout<<"\nThe Area of the Triangle is : "<<a;
 }

};

class rectangle: public shape
{
int l,b;

public:
 rectangle(int x,int y)
 {
 l=x;
 b=y;
 }

 void area()
 {
 a=l*b;
 }

 void show()
 {
 cout<<"\n"<<"The Area of the Rectangle is : "<<a;
 }

};

class circle: public shape
{
int r;
public:

 circle(int x)
 {
 r=x;
 }

 void area()
 {
 a=3.14*r*r;
 }

 void show()
 {
 cout<<"\n"<<"Area of the Circle is : "<<a;
 }

};

void main()
{
clrscr();

triangle ob(20,20,20);
ob.area();
ob.show();

rectangle ob1(6,5);
ob1.area();
ob1.show();

circle ob2(7);
ob2.area();
ob2.show();

getch();
}



Thursday, February 24, 2011

Constructors and Destructors


#include<iostream.h>
#include<conio.h>

class fixed
{
int a,b;
public:

 fixed()
 {
 cout<<"\nThe Constructor has Zero Arguments\n";
 cout<<"\n";
 }

 fixed(int x)
 {
 a=x;
 cout<<"The Constructor has One Argument"<<"\n\n";
 cout<<a<<"\n\n";
 }

 fixed(int x,int y)
 {
 a=x;
 b=y;
 cout<<"This Constructor Has Two Arguments:"<<"\n\n";
 cout<<a<<"\n"<<b;
 }

 ~fixed()
 {
 cout<<"\n Objects Destroyed";
 cout<<"\n";
 }

};

void main()
{
clrscr();

fixed ob;
fixed ob1(10);
fixed ob2(20);
fixed ob3(10,20);

getch();
}

Default Arguments


#include<iostream.h>
#include<conio.h>

class find
{
public:
int a,b;

 void get(int x,int y=20)
 {
 a=x;
 b=y;
 }

 void display()
 {
 cout<<"\n A : " <<a<<"\n";
 cout<<"\n B : " <<b<<"\n";
 }

};

void main()
{
clrscr();

find ob;

ob.get(10);

ob.display();
find ob1;

ob1.get(20,30);

ob1.display();

getch();

}


Wednesday, February 23, 2011

Classes and Objects


CPP Programs. Banking Program

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

class banking
{
public:
char name[25];
char type[30];
char number[20];
long int balance;
long int dep,with;

 void create()
 {
 cout<<"\n\t\tEnter name: ";
 cin>>name;
 cout<<"\n\t\tEnter account type: ";
 cin>>type;
 cout<<"\n\t\tEnter account number: ";
 cin>>number;
 cout<<"\n\t\tInitial balance: ";
 cin>>balance;
 }

 void deposit()
 {
 cout<<"\n\t\tEnter the amount to be deposited: ";
 cin>>dep;
 balance=balance+dep;
 cout<<"\n\t\tCurrent balance: "<<balance;
 }

 void withdraw()
 {
 cout<<"\n\t\tEnter the amount to be withdrawn: ";
 cin>>with;
 balance=balance-with;
 cout<<"\n\t\tCurrent Balance: "<<balance;
 }

 void display()
 {
 cout<<"\n\t\tName of the customer: "<<name;
 cout<<endl;
 cout<<"\n\t\tAccount Number: "<<number;
 cout<<endl;
 cout<<"\n\t\tType of account: "<<type;
 cout<<endl;
 cout<<"\n\t\tBalance: "<<balance;
 }

};

void main()
{
banking ob;
int x;

clrscr();


 do
 {
 cout<<endl;
 cout<<"\n\n\n\t\t\tWelcome to State Bank  Of India ";
 cout<<"\n\n\t\t 1.Create a new account";
 cout<<"\n\n\t\t 2.Deposit amount";
 cout<<"\n\n\t\t 3.Withdraw amount";
 cout<<"\n\n\t\t 4.Display";
 cout<<"\n\n\t\t 5.Exit";
 cout<<"\n\n\t\t Select your choice: ";
 cin>>x;
 
  switch(x)
  {
  case 1:ob.create();break;
  case 2:ob.deposit();break;
  case 3:ob.withdraw();break;
  case 4:ob.display();break;
  case 5:break;
  default:cout<<"Enter any value b/w 1-5";
  }

 }while(x!=5);

}



Tuesday, February 22, 2011

Palindrome Program!!

Palindrome is a word which reads the same from both sides. Example is MADAM. This program uses the built-in functions from the directory 'string.h' , to determine whether the given string is a palindrome or not. 

The command 'strrev' reverses the string and 'strcpy' copies one string to another for comparision.


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80],b[80];
clrscr();
printf("\nEnter the String:");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Non-Palindrome");
getch();
}