#include<windows.h>
#include "resource.h"
long _stdcall myfun(HWND,UINT,UINT,long);
long _stdcall dlg(HWND,UINT,UINT,long);
WNDCLASS a;
AINSTANCE h;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
HWND h;
MSG m;
a.hInstance=i;
a.lpszClassName="my";
a.lpfnWndProc=myfunc;
a.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(200,400,500));
RegisterClass(&a);
h=CreateWindow("my","lower",WS_OVERLAPPEDWINDOW,150,200,250,300,0,0,i,0);
ShowWindow(h,3);
while(GetMessage(&m,0,0,0)
DispatchMessage(&m);
return 0;
}
long _stdcall myfunc(HWND w,UINT x,UINT y,long z)
{
switch(x)
{
case WM_LBUTTONDOWN:
DialogBox(h,MAKEINTRESOURCE(IDD_DIALOG1),w,(DLGPROC)dlg);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(w,x,y,z);
return 0L;
}
long _stdcall dlg(HWND w1,WINT x1,WINT y1,long z1)
{
int r,s,n1,n;
switch(x1)
{
case WM_COMMAND:
switch(y1)
case sum:
n1=GetDlgItemInt(w1,EDIT,NULL,true);
do
{
s=0;
n=n1;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
n1=s;
}
while(n1>10)
SetDlgItemInt(w1,EDIT2,n,true);
break;
case EXIT:
EndDialog(w1,false);
break;
case RESET:
SetDlgItemText(w1,EDIT," ");
SetDlgItemText(w1,EDIT2," ");
}
break;
}
return 0L;
}
Thursday, December 16, 2010
V C++ Program to Display Welcome on Clicking Area
#include<windows.h>
long _stdcall myfunc(HWND,UINT,UINT,long);
WNDCLASS a;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
HWND h;
MSG m;
a.hInstance=i;
a.lpszClassName="my";
a.lpfnWndProc=myfunc;
a.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
RegisterClass(&a);
h=CreateWindow("my","Click on the Window Area",WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,i,0);
ShowWindow(h,3);
while(GetMessage(&m,0,0,0))
DispatchMessage(&m);
return 0;
}
long _stdcall myfunc(HWND w,UINT x,UINT y,long z)
{
HDC d;
int x1,y1;
switch(x)
{
case WM_LBUTTONDOWN:
x1=LOWORD(z);
y1=HIWORD(z);
d=GetDC(w);
TextOut(d,x1,y1,"Welcome",7);
ReleaseDC(w,d);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(w,x,y,z);
}
return 0L;
}
long _stdcall myfunc(HWND,UINT,UINT,long);
WNDCLASS a;
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
HWND h;
MSG m;
a.hInstance=i;
a.lpszClassName="my";
a.lpfnWndProc=myfunc;
a.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
RegisterClass(&a);
h=CreateWindow("my","Click on the Window Area",WS_OVERLAPPEDWINDOW,10,10,150,100,0,0,i,0);
ShowWindow(h,3);
while(GetMessage(&m,0,0,0))
DispatchMessage(&m);
return 0;
}
long _stdcall myfunc(HWND w,UINT x,UINT y,long z)
{
HDC d;
int x1,y1;
switch(x)
{
case WM_LBUTTONDOWN:
x1=LOWORD(z);
y1=HIWORD(z);
d=GetDC(w);
TextOut(d,x1,y1,"Welcome",7);
ReleaseDC(w,d);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(w,x,y,z);
}
return 0L;
}
Tuesday, December 14, 2010
Saturday, November 13, 2010
Search element in binary tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void search();
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----SEARCH.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
search();
getch();
break;
case '3':
exit(0);
break;
}
} while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
elseif(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
int t=0;
void src(node *tree,int);
void search()
{
int sr;
cout<<"Enter data to search\n";
cin>>sr;
src(tree,sr);
if(t==1)
cout<<"Element found";
else
cout<<"Not Found";
t=0;
}
void src(node *tree,int sr)
{
if(tree!=NULL)
{
if(tree->data==sr)
{
t=1;
}
src(tree->left,sr);
src(tree->right,sr);
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void search();
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1----INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2----SEARCH.";
cout<<"\n\t3----EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
search();
getch();
break;
case '3':
exit(0);
break;
}
} while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
elseif(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
int t=0;
void src(node *tree,int);
void search()
{
int sr;
cout<<"Enter data to search\n";
cin>>sr;
src(tree,sr);
if(t==1)
cout<<"Element found";
else
cout<<"Not Found";
t=0;
}
void src(node *tree,int sr)
{
if(tree!=NULL)
{
if(tree->data==sr)
{
t=1;
}
src(tree->left,sr);
src(tree->right,sr);
}
}
Search in linked list
#include"iostream.h"
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
#include"conio.h"
class tree
{
public:
int num,l,c;
struct node
{
int info;
node *next;
}*head,*nn,*temp,*p;
tree()
{
head=NULL;
c=0;
}
void allo()
{
nn=new node;
cout<<"\n\t\tENTER THE NUMBER TO INSERT =";
cin>>num;
nn->info=num;
nn->next=NULL;
}
void create()
{
allo();
if(head==NULL)
{
head=nn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nn;
}
}
void search()
{
int f=0;
if(head==NULL)
{
cout<<"\n\t\tLIST IS EMPTY";
}
else
{
cout<<"\n\t\tENTER THE NUMBER TO SEARCH =";
cin>>num;
temp=head;
do
{
if(temp->info==num)
{
f=1;
goto k;
}
else
{
temp=temp->next;
}
}while(temp->next!=NULL);
}
if(temp->info==num)
{
f=1;
}
k:
if(f==1)
{
cout<<"\n\t\tTHE GIVEN ITEM IS FOUND";
}
else
{
cout<<"\n\t\tTHE GIVEN ITEM IS NOT FOUND";
}
}
};
void main()
{
clrscr();
int ch;
tree t;
do
{
cout<<"\n\t\t\tSINGLE LISTED OPERATION";
cout<<"\n\t\t\t~~~~~~ ~~~~~~ ~~~~~~~~~";
cout<<"\n\t\tOPTION";
cout<<"\n\t\t1-CREATING";
cout<<"\n\t\t2-SEARCH";
cout<<"\n\t\t3-EXIT";
cout<<"\n\t\tENTER YOUR CHOICE =";
cin>>ch;
switch(ch)
{
case 1:
t.create();
break;
case 2:
t.search();
break;
}
}while(ch<3);
getch();
}
Queue Operation
#include<conio.h>
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
#include<iostream.h>
#include<process.h>
# define MAX_SIZE 150
class queues
{
int front,rear;
int queue[MAX_SIZE];
public:
queues() // Constructor
{
front=(-1);
rear=(-1);
}
void insert_rear(int);
void delete_front();
void display();
};
void queues::insert_rear(int item)
{
if((front==0 && rear==MAX_SIZE) || (front==(rear+1)))
{
cout<<"Queue is full!\nOverflow";
getch();
exit(0);
}
else
{
if(front==(-1) && rear==(-1))
{
rear=0;
front=0;
}
else if(front!=0 && rear==MAX_SIZE)
rear=0;
else
rear=rear+1;
queue[rear]=item;
}
}
void queues::delete_front()
{
if(front==(-1) && rear==(-1))
{
cout<<"Queue is empty!\nUnderflow";
return;
}
else
{
if(front==rear)
front=rear=(-1);
else if(front==MAX_SIZE && rear==MAX_SIZE)
front=0;
else
front=front+1;
}
cout<<"\nItem deleted.";
getch();
}
void queues:: display()
{
int ptr;
if(front==(-1) && rear==(-1))
{
cout<<"\nQueue is empty";
getch();
}
/*if(front<=MAX_SIZE && rear<=front)
{
for(ptr=front;ptr<MAX_SIZE;ptr++)
cout<<queue[ptr]<<"\n";
for(ptr=0;ptr<=rear;ptr++)
cout<<queue[ptr]<<"\n";
} */
else
{
cout<<"\nThe queue is\n";
for(ptr=front;ptr<=rear;ptr++)
cout<<queue[ptr]<<" ";
}
}
int main()
{
clrscr();
int length,i,element,choice;
queues q1;
while(1)
{
clrscr();
cout<<"1: Insert an item.\n2: Delete an item.";
cout<<"\n3: Display elements\n4: Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"How many elements are in the queue: ";
cin>>length;
cout<<"Enter "<<length<<" elements: ";
for(i=0;i<length;i++)
{
cin>>element;
q1.insert_rear(element);
}
q1.display();
getch();
break;
case 2:
q1.delete_front();
q1.display();
getch();
break;
case 3:
q1.display();
getch();
break;
case 4:
exit(0);
break;
default:
cout<<"Please re-enter tour choice.";
getch();
break;
}
}
getch();
}
Stack Operation using array
#include
#include
#include
class stack
{
private:
int stack_array[10];
int top;
public:
stack()
{
top = -1;
}
int full()
{
if(top == 9)
return(1);
else
return(0);
}
int empty()
{
if(top == -1)
return(1);
else
return(0);
}
void push(int passed_value)
{
top++;
stack_array[top]=passed_value;
cout<=0;i--)
{
cout<>choice;
switch(choice)
{
case 1:
if(mystack.full())
{
cout<<"\nStack Full"; break; } cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
mystack.pop();
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
#include
#include
class stack
{
private:
int stack_array[10];
int top;
public:
stack()
{
top = -1;
}
int full()
{
if(top == 9)
return(1);
else
return(0);
}
int empty()
{
if(top == -1)
return(1);
else
return(0);
}
void push(int passed_value)
{
top++;
stack_array[top]=passed_value;
cout<
{
cout<
switch(choice)
{
case 1:
if(mystack.full())
{
cout<<"\nStack Full"; break; } cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
mystack.pop();
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
Stack Operation Using Linked List
#include
#include
#include
class node
{
public:
int info;
node *next;
};
class stack
{
private:
node *top;
public:
stack()
{
top=NULL;
}
int empty()
{
if(top==NULL)
return(1);
else
return(0);
}
void push(int passed_value)
{
node *temp;
if(top==NULL)
{
top=new node;
top->info=passed_value;
top->next=NULL;
}
else
{
temp=new node();
temp->info=passed_value;
temp->next=top;
top=temp;
}
cout<<"\n"<next;
int x=tmp->info;
delete(tmp);
return(x);
}
void display()
{
if(top==NULL)// if stack is empty
cout<<"\nStack Empty\n"; else { node *currentnode; cout<<"The Contents of the Stack are:\n"; for(currentnode=top;currentnode!=NULL;currentnode=currentnode->next)
cout<info<<"\t";
}
}
};
void main()
{
clrscr();
int choice,x;
stack mystack;
while(1)
{
cout<<"\n**STACK MENU**\n\n"
<<"1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n"
<<"Enter ur Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
x=mystack.pop();
cout<<"\nThe Popped element is : "<
getch();clrscr();
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
#include
#include
class node
{
public:
int info;
node *next;
};
class stack
{
private:
node *top;
public:
stack()
{
top=NULL;
}
int empty()
{
if(top==NULL)
return(1);
else
return(0);
}
void push(int passed_value)
{
node *temp;
if(top==NULL)
{
top=new node;
top->info=passed_value;
top->next=NULL;
}
else
{
temp=new node();
temp->info=passed_value;
temp->next=top;
top=temp;
}
cout<<"\n"<
int x=tmp->info;
delete(tmp);
return(x);
}
void display()
{
if(top==NULL)// if stack is empty
cout<<"\nStack Empty\n"; else { node *currentnode; cout<<"The Contents of the Stack are:\n"; for(currentnode=top;currentnode!=NULL;currentnode=currentnode->next)
cout<
switch(choice)
{
case 1:
cout<<"Enter an element: "; cin>>choice;
mystack.push(choice);
getch();clrscr();
break;
case 2:
if(mystack.empty())
{
cout<<"\nStack Empty\n";
break;
}
x=mystack.pop();
cout<<"\nThe Popped element is : "<
break;
case 3:
mystack.display();
getch();clrscr();
break;
case 4:
exit(0);
break;
default:
cout<<"\nEnter Correct Choice:";
}
}
}
To display numbers in triangular format
#include"iostream.h"
#include"conio.h"
void main()
{
int n,i,j;
clrscr();
cout<<"\n\t\tENTER RANGE:";
cin>>n;
cout<<"\nFORMAT - 1\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<j;
}
cout<<"\n";
}
cout<<"\nFORMAT - 2\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<i;
}
cout<<"\n";
}
cout<<"\nFORMAT - 3\n";
for(i=0;i<=n-1;i++)
{
for(j=0;j<=i;j++)
{
cout<<"\t"<<(n-j);
}
cout<<"\n";
}
getch();
}
#include"conio.h"
void main()
{
int n,i,j;
clrscr();
cout<<"\n\t\tENTER RANGE:";
cin>>n;
cout<<"\nFORMAT - 1\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<j;
}
cout<<"\n";
}
cout<<"\nFORMAT - 2\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"\t"<<i;
}
cout<<"\n";
}
cout<<"\nFORMAT - 3\n";
for(i=0;i<=n-1;i++)
{
for(j=0;j<=i;j++)
{
cout<<"\t"<<(n-j);
}
cout<<"\n";
}
getch();
}
Student details using multiple inheritance
#include<iostream.h>
#include<conio.h>
class basic_info
{
public:
int roll;
char name[20],gender;
};
class academic_fit
{
public:
char course[10],semester[2],rank[2];
};
class financial_assist:public basic_info, public academic_fit
{
public:
float amt;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\t Course :";
cin>>course;
cout<<"\t Semester :";
cin>>semester;
cout<<"\t Rank : ";
cin>>rank;
cout<<"\t Amt Paid :";
cin>>amt;
}
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Course : "<<course;
cout<<"\n\t Semester: "<<semester;
cout<<"\n\t Rank : "<<rank;
cout<<"\n\t Amt.Paid: "<<amt;
}
};
void main()
{
financial_assist s;
clrscr();
s.getdata();
s.disp();
getch();
}
#include<conio.h>
class basic_info
{
public:
int roll;
char name[20],gender;
};
class academic_fit
{
public:
char course[10],semester[2],rank[2];
};
class financial_assist:public basic_info, public academic_fit
{
public:
float amt;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\t Course :";
cin>>course;
cout<<"\t Semester :";
cin>>semester;
cout<<"\t Rank : ";
cin>>rank;
cout<<"\t Amt Paid :";
cin>>amt;
}
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Course : "<<course;
cout<<"\n\t Semester: "<<semester;
cout<<"\n\t Rank : "<<rank;
cout<<"\n\t Amt.Paid: "<<amt;
}
};
void main()
{
financial_assist s;
clrscr();
s.getdata();
s.disp();
getch();
}
Student details using single inheritance
#include<iostream.h>
#include<conio.h>
class studentbase
{
public:
int roll,height,weight;
char name[20],gender;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\tHeight(in cm) :";
cin>>height;
cout<<"\tWeight(in Kg) :";
cin>>weight;
}
};
class studderived:public studentbase
{
public:
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Height : "<<height<<" cm";
cout<<"\n\t Weight : "<<weight<<" Kg";
}
};
void main()
{
studderived s;
clrscr();
s.getdata();
s.disp();
getch();
}
#include<conio.h>
class studentbase
{
public:
int roll,height,weight;
char name[20],gender;
void getdata()
{
cout<<"Enter Student Details:";
cout<<"\n\tRoll No :";
cin>>roll;
cout<<"\tName :";
cin>>name;
cout<<"\tGender(M or F) :";
cin>>gender;
cout<<"\tHeight(in cm) :";
cin>>height;
cout<<"\tWeight(in Kg) :";
cin>>weight;
}
};
class studderived:public studentbase
{
public:
void disp()
{
clrscr();
cout<<"\n\t\tStudent Details";
cout<<"\n\t\t*-*-*-*-*-*-*-*";
cout<<"\n\n\t Name : "<<name;
cout<<"\n\t RollNo.: "<<roll;
cout<<"\n\t Gender : "<<gender;
cout<<"\n\t Height : "<<height<<" cm";
cout<<"\n\t Weight : "<<weight<<" Kg";
}
};
void main()
{
studderived s;
clrscr();
s.getdata();
s.disp();
getch();
}
Operator overloading
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float real,float img)
{
x=real;
y=img;
}
complex operator+(complex);
void disp(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::disp(void)
{
cout<<x<<"+i"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 = ";c1.disp();
cout<<"c2 = ";c2.disp();
cout<<"Sum= ";c3.disp();
getch();
}
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{
}
complex(float real,float img)
{
x=real;
y=img;
}
complex operator+(complex);
void disp(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::disp(void)
{
cout<<x<<"+i"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1 = ";c1.disp();
cout<<"c2 = ";c2.disp();
cout<<"Sum= ";c3.disp();
getch();
}
Adam number
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,sqr,n1,r=0,r1=0,rsq;
cout<<"Enter a number:";
cin>>n;
n1=n;
sqr=n*n;
while(sqr>0)
{
r=r*10+sqr%10;
sqr=sqr/10;
}
rsq=sqrt(r);
while(rsq>0)
{
r1=r1*10+rsq%10;
rsq=rsq/10;
}
if(r1==n1)
cout<<"\nThe Given Number is an Adam No";
else
cout<<"\nThe Given Number is not an Adam no";
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n,sqr,n1,r=0,r1=0,rsq;
cout<<"Enter a number:";
cin>>n;
n1=n;
sqr=n*n;
while(sqr>0)
{
r=r*10+sqr%10;
sqr=sqr/10;
}
rsq=sqrt(r);
while(rsq>0)
{
r1=r1*10+rsq%10;
rsq=rsq/10;
}
if(r1==n1)
cout<<"\nThe Given Number is an Adam No";
else
cout<<"\nThe Given Number is not an Adam no";
getch();
}
Binary to decimal
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int p,q,r,bi[20];
long num;
cout<<"Enter Binary number:";
cin>>num;
p=0;
while(num>0)
{
bi[p]=num%10;
num=num/10;
p++;
}
q=p-1;
r=0;
while(q>=0)
{
r=r+(bi[q]*pow(2,q));
q--;
}
cout<<r;
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int p,q,r,bi[20];
long num;
cout<<"Enter Binary number:";
cin>>num;
p=0;
while(num>0)
{
bi[p]=num%10;
num=num/10;
p++;
}
q=p-1;
r=0;
while(q>=0)
{
r=r+(bi[q]*pow(2,q));
q--;
}
cout<<r;
getch();
}
String Palindrome
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s[10],a[10],b[10];
cout<<"enter any string:";
cin>>s;
strcpy(a,s);
strrev(a);
if(strcmp(a,s)==0)
cout<<"\nThe given String is Palindrome";
else
cout<<"\nThe given String is not Palindrome";
getch();
}
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s[10],a[10],b[10];
cout<<"enter any string:";
cin>>s;
strcpy(a,s);
strrev(a);
if(strcmp(a,s)==0)
cout<<"\nThe given String is Palindrome";
else
cout<<"\nThe given String is not Palindrome";
getch();
}
Armstrong number
#include<iostream.h>
#include<conio.h>
void main()
{
int n,n1,s=0,d;
clrscr();
cout<<"Enter the Value:";
cin>>n;
n1=n;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==n1)
{
cout<<"The given Number "<<n1<<" is an Armstrong Number";
}
else
{
cout<<"The given Number "<<n1<<" is not an Armstrong Number";
}
getch();
}
#include<conio.h>
void main()
{
int n,n1,s=0,d;
clrscr();
cout<<"Enter the Value:";
cin>>n;
n1=n;
while(n!=0)
{
d=n%10;
s=s+(d*d*d);
n=n/10;
}
if(s==n1)
{
cout<<"The given Number "<<n1<<" is an Armstrong Number";
}
else
{
cout<<"The given Number "<<n1<<" is not an Armstrong Number";
}
getch();
}
Calculating time by incrementing seconds
#include<iostream.h>
#include<conio.h>
#include<time.h>
void main()
{
clrscr();
time_t t,t1;
t=time(NULL);
cout<<"The current time is:"<<ctime(&t);
t1=t;
cout<<"\nenter the seconds:";
int sec;
cin>>sec;
t1-=sec;
t+=sec;
cout<<"\nthe incremented time is:"<<ctime(&t);
cout<<"\nthe decremented time is:"<<ctime(&t1);
getch();
}
#include<conio.h>
#include<time.h>
void main()
{
clrscr();
time_t t,t1;
t=time(NULL);
cout<<"The current time is:"<<ctime(&t);
t1=t;
cout<<"\nenter the seconds:";
int sec;
cin>>sec;
t1-=sec;
t+=sec;
cout<<"\nthe incremented time is:"<<ctime(&t);
cout<<"\nthe decremented time is:"<<ctime(&t1);
getch();
}
Leap year calculating day of month
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
cout<<"Enter a Day number of Leap year : ";
cin>>n;
if(n>=1 && n<=31)
{
cout<<"\nJanuary";
cout<<"\nDay of month :"<<n;
}
else if(n>31 && n<=60)
{
cout<<"\nFeburary";
cout<<"\nDay of month :"<<n-31;
}
else if(n>60 && n<=91)
{
cout<<"\nMarch";
cout<<"\nDay of month :"<<n-60;
}
else if(n>91 && n<=121)
{
cout<<"\nApril";
cout<<"\nDay of month :"<<n-91;
}
else if(n>121&&n<=152)
{
cout<<"\nMay";
cout<<"\nDay of month :"<<n-120;
}
else if(n>152&&n<=182)
{
cout<<"\nJune";
cout<<"\nDay of month :"<<n-152;
}
else if(n>182&&n<=213)
{
cout<<"\nJuly";
cout<<"\nDay of month :"<<n-182;
}
else if(n>213&&n<=244)
{
cout<<"\nAugust";
cout<<"\nDay of month :"<<n-213;
}
else if(n>244&&n<=274)
{
cout<<"\nSeptember";
cout<<"\nDay of month :"<<n-244;
}
else if(n>274&&n<=305)
{
cout<<"\nOctober";
cout<<"\nDay of month :"<<n-274;
}
else if(n>305&&n<=335)
{
cout<<"\nNovember";
cout<<"\nDay of month :"<<n-305;
}
else if(n>335&&n<=366)
{
cout<<"\nDecember";
cout<<"\nDay of month :"<<n-335;
}
else
{
cout<<"\nEnter Day number of Leap year..";
}
getch();
}
#include<conio.h>
void main()
{
int n,i;
clrscr();
cout<<"Enter a Day number of Leap year : ";
cin>>n;
if(n>=1 && n<=31)
{
cout<<"\nJanuary";
cout<<"\nDay of month :"<<n;
}
else if(n>31 && n<=60)
{
cout<<"\nFeburary";
cout<<"\nDay of month :"<<n-31;
}
else if(n>60 && n<=91)
{
cout<<"\nMarch";
cout<<"\nDay of month :"<<n-60;
}
else if(n>91 && n<=121)
{
cout<<"\nApril";
cout<<"\nDay of month :"<<n-91;
}
else if(n>121&&n<=152)
{
cout<<"\nMay";
cout<<"\nDay of month :"<<n-120;
}
else if(n>152&&n<=182)
{
cout<<"\nJune";
cout<<"\nDay of month :"<<n-152;
}
else if(n>182&&n<=213)
{
cout<<"\nJuly";
cout<<"\nDay of month :"<<n-182;
}
else if(n>213&&n<=244)
{
cout<<"\nAugust";
cout<<"\nDay of month :"<<n-213;
}
else if(n>244&&n<=274)
{
cout<<"\nSeptember";
cout<<"\nDay of month :"<<n-244;
}
else if(n>274&&n<=305)
{
cout<<"\nOctober";
cout<<"\nDay of month :"<<n-274;
}
else if(n>305&&n<=335)
{
cout<<"\nNovember";
cout<<"\nDay of month :"<<n-305;
}
else if(n>335&&n<=366)
{
cout<<"\nDecember";
cout<<"\nDay of month :"<<n-335;
}
else
{
cout<<"\nEnter Day number of Leap year..";
}
getch();
}
Calculating Simple interest
#include<iostream.h>
#include<conio.h>
class simple
{
public:
float p,r,si,amt;
int n;
void getin()
{
cout<<"Enter Principal Amt:";
cin>>p;
cout<<"Enter Intrest Rate :";
cin>>r;
cout<<"Enter No. of Years :";
cin>>n;
si=(p*n*r)/100;
amt=p+si;
cout<<"\n\t*-*-*-*\n";
cout<<" Intrest : Rs."<<si<<"\n";
cout<<" Amount : Rs."<<amt;
}
};
void main()
{
clrscr();
simple s;
cout<<"*-*-*-*Calulate Simple Interest*-*-*-*\n\n";
s.getin();
getch();
}
#include<conio.h>
class simple
{
public:
float p,r,si,amt;
int n;
void getin()
{
cout<<"Enter Principal Amt:";
cin>>p;
cout<<"Enter Intrest Rate :";
cin>>r;
cout<<"Enter No. of Years :";
cin>>n;
si=(p*n*r)/100;
amt=p+si;
cout<<"\n\t*-*-*-*\n";
cout<<" Intrest : Rs."<<si<<"\n";
cout<<" Amount : Rs."<<amt;
}
};
void main()
{
clrscr();
simple s;
cout<<"*-*-*-*Calulate Simple Interest*-*-*-*\n\n";
s.getin();
getch();
}
Factorial of a given number
#include<iostream.h>
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
cout<<"Enter a Number:";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial :"<<fact;
getch();
}
#include<conio.h>
void main()
{
int n,fact=1,i;
clrscr();
cout<<"Enter a Number:";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial :"<<fact;
getch();
}
Hybrid inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void geta()
{
cout<<"Enter A: ";
cin>>a;
}
};
class B:public A
{
public:
int b;
void getb()
{
cout<<"Enter B: ";
cin>>b;
}
};
class D
{
public:
int sum;
};
class C:public D,public B
{
public:
void disp()
{
geta();
getb();
sum=a+b;
cout<<"A+B="<<sum;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a;
void geta()
{
cout<<"Enter A: ";
cin>>a;
}
};
class B:public A
{
public:
int b;
void getb()
{
cout<<"Enter B: ";
cin>>b;
}
};
class D
{
public:
int sum;
};
class C:public D,public B
{
public:
void disp()
{
geta();
getb();
sum=a+b;
cout<<"A+B="<<sum;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
Multi-level inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void disp()
{
getdata();
add();
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
};
class C:public B
{
public:
void disp()
{
getdata();
add();
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
C c;
clrscr();
c.disp();
getch();
}
Single inheritance
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
void disp()
{
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
B b;
clrscr();
b.getdata();
b.add();
b.disp();
getch();
}
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter two numbers:";
cin>>a;
cin>>b;
}
};
class B:public A
{
public:
int c;
void add()
{
c=a+b;
}
void disp()
{
cout<<"sum of two numbers:";
cout<<c;
}
};
void main()
{
B b;
clrscr();
b.getdata();
b.add();
b.disp();
getch();
}
Student details using inside the class and outside the class
#include<iostream.h>
#include<conio.h>
class stud //Outside Class
{
public:
int regno,m1,m2,m3,tot;
float avg;
char name[20];
};
void main() //Main Function
{
clrscr();
stud s; //Object For Outside Class
class studin //Inside Class
{
public:
void stumark()
{
cout<<"Enter Student Details:";
cout<<"\n\tReg.No :";
cin>>s.regno;
cout<<"\tName:";
cin>>s.name;
cout<<"\tMark1:";
cin>>s.m1;
cout<<"\tMark1:";
cin>>s.m2;
cout<<"\tMark1:";
cin>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\n\tStudent Details";
cout<<"\n\t Number : "<<s.regno;
cout<<"\n\t Name : "<<s.name;
cout<<"\n\t Mark1 : "<<s.m1;
cout<<"\n\t Mark2 : "<<s.m2;
cout<<"\n\t Mark3 : "<<s.m3;
cout<<"\n\t Total : "<<s.tot;
cout<<"\n\t Average : "<<s.avg;
}
};
studin s1; //Object For inside Class
s1.stumark();
getch();
}
#include<conio.h>
class stud //Outside Class
{
public:
int regno,m1,m2,m3,tot;
float avg;
char name[20];
};
void main() //Main Function
{
clrscr();
stud s; //Object For Outside Class
class studin //Inside Class
{
public:
void stumark()
{
cout<<"Enter Student Details:";
cout<<"\n\tReg.No :";
cin>>s.regno;
cout<<"\tName:";
cin>>s.name;
cout<<"\tMark1:";
cin>>s.m1;
cout<<"\tMark1:";
cin>>s.m2;
cout<<"\tMark1:";
cin>>s.m3;
s.tot=s.m1+s.m2+s.m3;
s.avg=s.tot/3;
cout<<"\n\tStudent Details";
cout<<"\n\t Number : "<<s.regno;
cout<<"\n\t Name : "<<s.name;
cout<<"\n\t Mark1 : "<<s.m1;
cout<<"\n\t Mark2 : "<<s.m2;
cout<<"\n\t Mark3 : "<<s.m3;
cout<<"\n\t Total : "<<s.tot;
cout<<"\n\t Average : "<<s.avg;
}
};
studin s1; //Object For inside Class
s1.stumark();
getch();
}
Employee Details using class & member function
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_no;
char emp_name[20];
float emp_sal;
public:
void get();
void dis();
};
void employee::get()
{
cout<<"Enter employee number:";
cin>>emp_no;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter salary:";
cin>>emp_sal;
}
void employee::dis()
{
cout<<"\n\tEmployee Details";
cout<<"\n\t Number : "<<emp_no;
cout<<"\n\t Name : "<<emp_name;
cout<<"\n\t Salary : "<<emp_sal;
}
void main()
{
clrscr();
employee e;
e.get();
e.dis();
getch();
}
#include<conio.h>
class employee
{
int emp_no;
char emp_name[20];
float emp_sal;
public:
void get();
void dis();
};
void employee::get()
{
cout<<"Enter employee number:";
cin>>emp_no;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter salary:";
cin>>emp_sal;
}
void employee::dis()
{
cout<<"\n\tEmployee Details";
cout<<"\n\t Number : "<<emp_no;
cout<<"\n\t Name : "<<emp_name;
cout<<"\n\t Salary : "<<emp_sal;
}
void main()
{
clrscr();
employee e;
e.get();
e.dis();
getch();
}
Sum of two numbers using increment operator
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i=1;
clrscr();
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
while(i<=b)
{
a=a++;
i++;
}
cout<<"The sum is:";
cout<<a;
getch();
}
#include<conio.h>
void main()
{
int a,b,i=1;
clrscr();
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
while(i<=b)
{
a=a++;
i++;
}
cout<<"The sum is:";
cout<<a;
getch();
}
Student Details Using Single Linked List
#include
#include
#include
#include
class node
{
public:
int rollnumber;
char name[20];
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnode()
{
int rollno;
char nm[20];
cout<<"\nEnter the roll number of the student:"; cin>>rollno;
cout<<"\nEnter the name of the student:"; cin>>nm;
node *newnode=new node;
newnode->rollnumber=rollno;
strcpy(newnode->name,nm);
if(start==NULL || rollno<=start->rollnumber)
/* If the node to be inserted is the first node */
{
if(start!=NULL && (rollno == start->rollnumber))
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } newnode->next=start;
start=newnode;
return;
}
node *previous,*current;
previous=start;
current=start;
while((current!=NULL) && (rollno>=current->rollnumber))
{
if(rollno==current->rollnumber)
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } previous=current; current=current->next;
}
newnode->next=current;
previous->next=newnode;
}
int listempty()
{
if(start==NULL)
return(1);
else
return(0);
}
int delnode(int rollno)
{
node *current,*previous;
if(search(rollno,&previous,¤t)==0)
return 0;
previous->next=current->next;
if(current==start)
start=start->next;
delete current;
return 1;
}
/* int search(int rollno,node **previous,node **current)
{
*previous=start;
*current=start;
while((*current!=NULL)&&((rollno!=current->rollnumber)))
{
*previous=*current;
*current=(*current)->next;
}
if(*current!=NULL)
return 1;
else
return 0;
}
*/ void traverse()
{
if(listempty())
cout<<"\nList is empty"; else { cout<<"The records in the list are:\n"; node *currentnode; for(currentnode=start;currentnode!=NULL;currentnode=currentnode->next)
{
cout<rollnumber<<"\t"<name<<"\n";
}
cout<>ch;
switch(ch)
{
case '1':
obj.addnode();
break;
case '2':
if(obj.listempty()==0)
{
cout<<"List Is Empty:"; break; } cout<<"\nEnter Rollno. to delete record:"; cin>>rollno;
if(obj.delnode(rollno)==0)
cout<<"Record not found"; else cout<<"Record deleted."; break; case '3': obj.traverse(); break; case '4': if(obj.listempty()==0) { cout<<"\nList Empty"; break; } node *previous,*current; cout<<"\nEnter rollno. to search:"; cin>>rollno;
if(obj.search(rollno,&previous,¤t)==0)
{
cout<<"\n Record not Found"; break; } else { cout<<"\n Record found\n"; cout<<"Roll Number: "<rollnumber;
cout<<"\nName: "<name;
}
break;
case '5':
exit(0);
default:
cout<<"Enter correct choice:";
break;
}
}
}
#include
#include
#include
class node
{
public:
int rollnumber;
char name[20];
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnode()
{
int rollno;
char nm[20];
cout<<"\nEnter the roll number of the student:"; cin>>rollno;
cout<<"\nEnter the name of the student:"; cin>>nm;
node *newnode=new node;
newnode->rollnumber=rollno;
strcpy(newnode->name,nm);
if(start==NULL || rollno<=start->rollnumber)
/* If the node to be inserted is the first node */
{
if(start!=NULL && (rollno == start->rollnumber))
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } newnode->next=start;
start=newnode;
return;
}
node *previous,*current;
previous=start;
current=start;
while((current!=NULL) && (rollno>=current->rollnumber))
{
if(rollno==current->rollnumber)
{
cout<<"\nDuplicate Roll Numbers not allowed."; return; } previous=current; current=current->next;
}
newnode->next=current;
previous->next=newnode;
}
int listempty()
{
if(start==NULL)
return(1);
else
return(0);
}
int delnode(int rollno)
{
node *current,*previous;
if(search(rollno,&previous,¤t)==0)
return 0;
previous->next=current->next;
if(current==start)
start=start->next;
delete current;
return 1;
}
/* int search(int rollno,node **previous,node **current)
{
*previous=start;
*current=start;
while((*current!=NULL)&&((rollno!=current->rollnumber)))
{
*previous=*current;
*current=(*current)->next;
}
if(*current!=NULL)
return 1;
else
return 0;
}
*/ void traverse()
{
if(listempty())
cout<<"\nList is empty"; else { cout<<"The records in the list are:\n"; node *currentnode; for(currentnode=start;currentnode!=NULL;currentnode=currentnode->next)
{
cout<
switch(ch)
{
case '1':
obj.addnode();
break;
case '2':
if(obj.listempty()==0)
{
cout<<"List Is Empty:"; break; } cout<<"\nEnter Rollno. to delete record:"; cin>>rollno;
if(obj.delnode(rollno)==0)
cout<<"Record not found"; else cout<<"Record deleted."; break; case '3': obj.traverse(); break; case '4': if(obj.listempty()==0) { cout<<"\nList Empty"; break; } node *previous,*current; cout<<"\nEnter rollno. to search:"; cin>>rollno;
if(obj.search(rollno,&previous,¤t)==0)
{
cout<<"\n Record not Found"; break; } else { cout<<"\n Record found\n"; cout<<"Roll Number: "<
cout<<"\nName: "<
}
break;
case '5':
exit(0);
default:
cout<<"Enter correct choice:";
break;
}
}
}
Monday, November 8, 2010
Converting Expression from Infix to Postfix using C++
#include<iostream.h>
#include<conio.h>
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{
if(full())
{
cout<<"\nStack overflow:\n";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{
if(empty())
return('#'); // Return value '#' indicates stack is empty
else
return(stack_array[top--]);
}
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
int full()
{
if(top==49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout<<"\nEnter an infix expression:";
cin>>infix;
}
int white_space(char symbol)
{
if(symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l,precedence,p;
char entry1,entry2;
p=0;
for(int i=0;infix[i]!='\0';i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while((entry2=s.pop())!='(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=prec(entry1);
entry2=s.pop();
while(precedence<=prec(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1;
break;
}
}
}
while(!s.empty()) //while stack is not empty
postfix[p++]=s.pop();
postfix[p]='\0';
cout<<"\nThe postfix expression is: "<<postfix<<endl;
}
int prec(char symbol)
{
switch(symbol)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
};
void main()
{
char choice='y';
Expression expr;
while(choice=='y')
{
expr.read();
expr.ConvertToPostfix();
cout<<"\n\nDo you want to continue ? (y/n): ";
cin>>choice;
}
}
#include<conio.h>
class stack
{
public:
char stack_array[50];
int top;
stack()
{
top=-1;
}
void push(char symbol)
{
if(full())
{
cout<<"\nStack overflow:\n";
}
else
{
top=top+1;
stack_array[top]=symbol;
}
}
char pop()
{
if(empty())
return('#'); // Return value '#' indicates stack is empty
else
return(stack_array[top--]);
}
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
int full()
{
if(top==49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout<<"\nEnter an infix expression:";
cin>>infix;
}
int white_space(char symbol)
{
if(symbol==' ' || symbol=='\t' || symbol=='\0')
return 1;
else
return 0;
}
void ConvertToPostfix()
{
stack s;
int l,precedence,p;
char entry1,entry2;
p=0;
for(int i=0;infix[i]!='\0';i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while((entry2=s.pop())!='(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=prec(entry1);
entry2=s.pop();
while(precedence<=prec(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1;
break;
}
}
}
while(!s.empty()) //while stack is not empty
postfix[p++]=s.pop();
postfix[p]='\0';
cout<<"\nThe postfix expression is: "<<postfix<<endl;
}
int prec(char symbol)
{
switch(symbol)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
};
void main()
{
char choice='y';
Expression expr;
while(choice=='y')
{
expr.read();
expr.ConvertToPostfix();
cout<<"\n\nDo you want to continue ? (y/n): ";
cin>>choice;
}
}
Binary Search using C++
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[20];
int n;
int i;
while(1)
{
cout<<"\nEnter the number of elements in the array:";
cin>>n;
if((n>1)&&(n<=20))
break;
else
cout<<"\nArray should have minimum 1 and maximum 20 elements\n\n";
}
cout<<"Enter elements in ascending order:\n";
for(i=0;i<n;i++)
{
cout<<"<"<< (i+1) <<">";
cin>>arr[i];
}
char ch;
do
{
int item,ctr;
cout<<"\nEnter the element to be searched:";
cin>>item;
int lowerbound=0;
int upperbound=n-1;
int mid=(lowerbound+upperbound)/2;
while((item!=arr[mid])&&(lowerbound<=upperbound))
{
if(item>arr[mid])
lowerbound=mid+1;
else
upperbound=mid-1;
mid=(lowerbound+upperbound)/2;
ctr++;
}
if(item==arr[mid])
cout<<"\n"<<item<<" found in the position "<<(mid+1)<<endl;
else
cout<<"\n"<<item<<" not found in the array"<<endl;
cout<<"\nNumber of comparisions:"<<ctr;
cout<<"\n\nContinue search (y/n):";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
}
#include<conio.h>
void main()
{
int arr[20];
int n;
int i;
while(1)
{
cout<<"\nEnter the number of elements in the array:";
cin>>n;
if((n>1)&&(n<=20))
break;
else
cout<<"\nArray should have minimum 1 and maximum 20 elements\n\n";
}
cout<<"Enter elements in ascending order:\n";
for(i=0;i<n;i++)
{
cout<<"<"<< (i+1) <<">";
cin>>arr[i];
}
char ch;
do
{
int item,ctr;
cout<<"\nEnter the element to be searched:";
cin>>item;
int lowerbound=0;
int upperbound=n-1;
int mid=(lowerbound+upperbound)/2;
while((item!=arr[mid])&&(lowerbound<=upperbound))
{
if(item>arr[mid])
lowerbound=mid+1;
else
upperbound=mid-1;
mid=(lowerbound+upperbound)/2;
ctr++;
}
if(item==arr[mid])
cout<<"\n"<<item<<" found in the position "<<(mid+1)<<endl;
else
cout<<"\n"<<item<<" not found in the array"<<endl;
cout<<"\nNumber of comparisions:"<<ctr;
cout<<"\n\nContinue search (y/n):";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
}
Monday, November 1, 2010
DMBS Queries
1. select * from route_header where origin='madras' and destination='cochin';
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select date_add(doi,interval 2 month) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select period_diff(dot,doi) from ticket_header;
13. SELECT DAY,CASE when (4-weekday(day))>0 then day + interval(4-weekday(day))day else day+interval(11-weekday(day))day end "Next Friday"from fleet_header;
14.
i. select day,case when(month(day)<6) then makedate(year(day),1) else makedate(year(day),12) end "Rounded date" from fleet_header;
ii. select day,case when(day(day))>15 then last_day(day)else date_add(day,interval(1-day(day)) day)end "Round of day"from fleet_header;
iii. select day,case when(weekday(day)>3) then date_sub(day,interval(weekday(day))day) else date_add(day,interval(6-weekday(day))day) end "Round of day" from fleet_header;
iv. select day, case when (year(day)<1900) then date_\c select day, case when(year(day)<=1950)then date_add(day,interval (2000-year(day))day) else date_sub(day,interval(year(day))day) end "round of year" from fleet_header;
15. select date_format(day, '%D %m %y') from fleet_header;
16. select max(period_diff(dot,doi))"Max.Differance" from ticket_header;
17. select date_add(doi,interval 31 day) from ticket_header;
18. select date_sub(day,interval 60 day) from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select ucase(origin)"upper of Orgin",ucase(destination)"upper of Destination" from route_header;
21. select lcase(origin),lcase(destination) from route_header;
22. select substring('bangalore',1,3) from place_header;
23. select substring('hyderabad',-3)from place_header;
24. select replace(place_name,'m','v') from place_header where place_name like 'm%';
25. select substring(name,4,3) from ticket_detail;
26. select replace(place_name,'s','e'),replace(place_name,'a','e') from place_header;
27. select round(sum(fare),2) from route_header ;
28. select round(sum(fare))"Total Fare" from route_header;
29. select date_format(curdate(), '%D %M %Y');
30. select date_format(curdate(),'%d %m %Y');
31.
32. select avg(fare)"Average Fare" from route_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by ticket_no;
37. update fleet_header set day=date_add(day,interval 3 day) where weekday(day)=6;
Procedure
mysql> delimiter //
mysql> create procedure k1()
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k1();
mysql> create procedure k2(k int)
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k2(5);
Trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
-> FOR EACH ROW SET @sum = @sum + NEW.amount;
mysql> SET @sum = 0;
mysql> INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
mysql> SELECT @sum AS 'Total amount inserted';
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48 |
+-----------------------+
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN -> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter ;
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select date_add(doi,interval 2 month) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select period_diff(dot,doi) from ticket_header;
13. SELECT DAY,CASE when (4-weekday(day))>0 then day + interval(4-weekday(day))day else day+interval(11-weekday(day))day end "Next Friday"from fleet_header;
14.
i. select day,case when(month(day)<6) then makedate(year(day),1) else makedate(year(day),12) end "Rounded date" from fleet_header;
ii. select day,case when(day(day))>15 then last_day(day)else date_add(day,interval(1-day(day)) day)end "Round of day"from fleet_header;
iii. select day,case when(weekday(day)>3) then date_sub(day,interval(weekday(day))day) else date_add(day,interval(6-weekday(day))day) end "Round of day" from fleet_header;
iv. select day, case when (year(day)<1900) then date_\c select day, case when(year(day)<=1950)then date_add(day,interval (2000-year(day))day) else date_sub(day,interval(year(day))day) end "round of year" from fleet_header;
15. select date_format(day, '%D %m %y') from fleet_header;
16. select max(period_diff(dot,doi))"Max.Differance" from ticket_header;
17. select date_add(doi,interval 31 day) from ticket_header;
18. select date_sub(day,interval 60 day) from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select ucase(origin)"upper of Orgin",ucase(destination)"upper of Destination" from route_header;
21. select lcase(origin),lcase(destination) from route_header;
22. select substring('bangalore',1,3) from place_header;
23. select substring('hyderabad',-3)from place_header;
24. select replace(place_name,'m','v') from place_header where place_name like 'm%';
25. select substring(name,4,3) from ticket_detail;
26. select replace(place_name,'s','e'),replace(place_name,'a','e') from place_header;
27. select round(sum(fare),2) from route_header ;
28. select round(sum(fare))"Total Fare" from route_header;
29. select date_format(curdate(), '%D %M %Y');
30. select date_format(curdate(),'%d %m %Y');
31.
32. select avg(fare)"Average Fare" from route_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by ticket_no;
37. update fleet_header set day=date_add(day,interval 3 day) where weekday(day)=6;
Procedure
mysql> delimiter //
mysql> create procedure k1()
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k1();
mysql> create procedure k2(k int)
-> begin
-> select * from fleet_header;
-> end
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call k2(5);
Trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
mysql> CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account
-> FOR EACH ROW SET @sum = @sum + NEW.amount;
mysql> SET @sum = 0;
mysql> INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
mysql> SELECT @sum AS 'Total amount inserted';
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48 |
+-----------------------+
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN -> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter ;
What's New in Visual C++ 6.0
What's New in Visual C++ 6.0
Visual C++ offers many new features and improvements over its predecessor, Visual C++ 5.0. The new features covered in this book follow:- The compiler offers improved support for the ANSI C++ standard. Boolean types are supported, and template support is improved.
- The development system includes new enhancements to MFC, the Microsoft Foundation Class Library. These enhancements include classes for Internet programming and support for new common controls introduced in Internet Explorer 4.0 and Windows 98.
- The Developer Studio Editor is much improved, and it takes advantage of IntelliSense features originally released as part of Visual Basic. These features include statement completion, which greatly improves your efficiency.
- The debugger included with Visual C++ includes a new feature called Debug and Continue, which enables you to make small changes while debugging and then immediately continue debugging without restarting the application.
- An improved online help system puts the MSDN in easy reach, just a mouse click away. The online help system automatically uses the latest version of the MSDN Library if it's installed on your computer.
MySQL Cursors
Cursors are supported inside stored procedures and functions and triggers. The syntax is as in embedded SQL. Cursors in MySQL have these properties:
* Asensitive: The server may or may not make a copy of its result table
* Read only: Not updatable
* Nonscrollable: Can be traversed only in one direction and cannot skip rows
Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers.
DECLARE cursor_name CURSOR FOR select_statement
This statement declares a cursor. Multiple cursors may be declared in a stored program, but each cursor in a given block must have a unique name.
The SELECT statement cannot have an INTO clause.
OPEN cursor_name
This statement opens a previously declared cursor.
FETCH cursor_name INTO var_name [, var_name] ...
This statement fetches the next row (if a row exists) using the specified open cursor, and advances the cursor pointer.
If no more rows are available, a No Data condition occurs with SQLSTATE value 02000. To detect this condition, you can set up a handler for it (or for a NOT FOUND condition).
CLOSE cursor_name
This statement closes a previously opened cursor.
If not closed explicitly, a cursor is closed at the end of the compound statement in which it was declared.
Example:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
* Asensitive: The server may or may not make a copy of its result table
* Read only: Not updatable
* Nonscrollable: Can be traversed only in one direction and cannot skip rows
Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers.
DECLARE cursor_name CURSOR FOR select_statement
This statement declares a cursor. Multiple cursors may be declared in a stored program, but each cursor in a given block must have a unique name.
The SELECT statement cannot have an INTO clause.
OPEN cursor_name
This statement opens a previously declared cursor.
FETCH cursor_name INTO var_name [, var_name] ...
This statement fetches the next row (if a row exists) using the specified open cursor, and advances the cursor pointer.
If no more rows are available, a No Data condition occurs with SQLSTATE value 02000. To detect this condition, you can set up a handler for it (or for a NOT FOUND condition).
CLOSE cursor_name
This statement closes a previously opened cursor.
If not closed explicitly, a cursor is closed at the end of the compound statement in which it was declared.
Example:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END;
Friday, October 29, 2010
Procedure Language in MySQL
Procedure Language(PL)
**********************
mysql> delimiter //
mysql> CREATE PROCEDURE arul (OUT param1 INT)
-> BEGIN
-> select * from place_header;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> call arul(@a);
-> //
+----------+------------+----------------------+-------------+
| place_id | place_name | place_address | bus_station |
+----------+------------+----------------------+-------------+
| 1 | madras | 10, ptc road | parrys |
| 2 | madurai | 21,canalbank road | kknagar |
| 3 | trichy | 11, first cross road | bheltown |
| 4 | bangalore | 15, first main road | cubbon park |
| 5 | hyderabad | 115,lake view road | charminar |
| 6 | thanjavur | 12, temple road | railway jn |
+----------+------------+----------------------+-------------+
6 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
**********************
mysql> delimiter //
mysql> CREATE PROCEDURE arul (OUT param1 INT)
-> BEGIN
-> select * from place_header;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> call arul(@a);
-> //
+----------+------------+----------------------+-------------+
| place_id | place_name | place_address | bus_station |
+----------+------------+----------------------+-------------+
| 1 | madras | 10, ptc road | parrys |
| 2 | madurai | 21,canalbank road | kknagar |
| 3 | trichy | 11, first cross road | bheltown |
| 4 | bangalore | 15, first main road | cubbon park |
| 5 | hyderabad | 115,lake view road | charminar |
| 6 | thanjavur | 12, temple road | railway jn |
+----------+------------+----------------------+-------------+
6 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Thursday, October 28, 2010
Queries to create tables & insert values into them
create table category_header(cat_code int,cat_desc varchar(20));
create table route_header(route_id int,route_no int,cat_code int,origin varchar(20),destination varchar(20),fare float,distance int,capacity int);
create table place_header(place_id int,place_name varchar(20),place_address varchar(20),bus_station varchar(20));
create table fleet_header(fleet_id int,day date,route_id int,cat_code int);
create table ticket_header(fleet_id int,ticket_no int,doi date,dot date,time_travel varchar(12),board_place varchar(20),origin varchar(20),destination varchar(20),adults int,children int,total_fare float,route_id int);
create table ticket_detail(ticket_no int,name varchar(20),sex char(1),age int,fare float);
insert into category_header values(01,'superdeluxe');
insert into category_header values(02,'deluxe');
insert into category_header values(03,'superfast');
insert into category_header values(04,'normal');
insert into route_header values(101,33,01,'Madurai','Madras',35,250,50);
insert into route_header values(102,25,02,'Trichy','Madurai',40,159,50);
insert into route_header values(103,15,03,'Thanjavur','Madurai',59,140,50);
insert into route_header values(104,36,04,'Madras','Bangalore',79,375,50);
insert into route_header values(105,40,01,'Bangalore','Madras',80,375,50);
insert into route_header values(106,38,02,'Madras','Madurai',39,250,50);
insert into route_header values(107,39,03,'Hydrabad','Madras',50,430,50);
insert into route_header values(108,41,04,'Madras','Cochin',47,576,50);
insert into place_header values(01,'Madras','10 PTC Road','Parrys');
insert into place_header values(02,'Madurai','21 Canalbank Road','KK nagar');
insert into place_header values(03,'Trichy','11 First Cross Road','Bheltown');
insert into place_header values(04,'Bangalore','15 First Main Road','Cubbonpark');
insert into place_header values(05,'Hydrabad','115 Lake view Road','Charminar');
insert into place_header values(06,'Thanjavur','12 Temple Road','Railway Jn');
insert into fleet_header values(01,'2010-08-04',101,01);
insert into fleet_header values(02,'2010-08-04',101,01);
insert into fleet_header values(03,'2010-08-04',101,01);
insert into fleet_header values(04,'2010-08-04',101,02);
insert into fleet_header values(05,'2010-08-04',101,03);
insert into fleet_header values(06,'2010-08-04',101,04);
insert into ticket_header values(01,01,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Madurai',1,1,60,101);
insert into ticket_header values(02,02,'2010-08-04','2010-09-04','09:00:00','KK Nagar','Madurai','Madras',2,1,60,102);
insert into ticket_header values(03,03,'2010-08-04','2010-09-04','21:00:00','Cubbonpark','Bangalore','Madras',4,2,400,101);
insert into ticket_header values(04,04,'2010-08-04','2010-09-04','10:00:00','Charminar','Hydrabad','Madras',10,0,500,103);
insert into ticket_header values(05,250,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Cochin',2,2,141,103);
insert into ticket_detail values(01,'Charu','f',24,'14.00');
insert into ticket_detail values(01,'Latha','f',10,'15.55');
insert into ticket_detail values(02,'anand','M',28,'17.80');
insert into ticket_detail values(02,'gautham','M',28,'16.00');
insert into ticket_detail values(02,'bala','M',09,'17.65');
insert into ticket_detail values(05,'sandeep','M',30,'18.00');
alter table route_header add comments long;
desc rout_header;
select distinct cat_code from route_header order by cat_code desc;
alter table route_header modify(distance int);
create table route_detail(route_id int,place_id int,nonstop char);
insert into route_detail values(105,01,'n');
insert into route_detail values(102,02,'s');
insert into route_detail values(106,01,'s');
insert into route_detail values(108,05,'n');
create table route_header(route_id int,route_no int,cat_code int,origin varchar(20),destination varchar(20),fare float,distance int,capacity int);
create table place_header(place_id int,place_name varchar(20),place_address varchar(20),bus_station varchar(20));
create table fleet_header(fleet_id int,day date,route_id int,cat_code int);
create table ticket_header(fleet_id int,ticket_no int,doi date,dot date,time_travel varchar(12),board_place varchar(20),origin varchar(20),destination varchar(20),adults int,children int,total_fare float,route_id int);
create table ticket_detail(ticket_no int,name varchar(20),sex char(1),age int,fare float);
insert into category_header values(01,'superdeluxe');
insert into category_header values(02,'deluxe');
insert into category_header values(03,'superfast');
insert into category_header values(04,'normal');
insert into route_header values(101,33,01,'Madurai','Madras',35,250,50);
insert into route_header values(102,25,02,'Trichy','Madurai',40,159,50);
insert into route_header values(103,15,03,'Thanjavur','Madurai',59,140,50);
insert into route_header values(104,36,04,'Madras','Bangalore',79,375,50);
insert into route_header values(105,40,01,'Bangalore','Madras',80,375,50);
insert into route_header values(106,38,02,'Madras','Madurai',39,250,50);
insert into route_header values(107,39,03,'Hydrabad','Madras',50,430,50);
insert into route_header values(108,41,04,'Madras','Cochin',47,576,50);
insert into place_header values(01,'Madras','10 PTC Road','Parrys');
insert into place_header values(02,'Madurai','21 Canalbank Road','KK nagar');
insert into place_header values(03,'Trichy','11 First Cross Road','Bheltown');
insert into place_header values(04,'Bangalore','15 First Main Road','Cubbonpark');
insert into place_header values(05,'Hydrabad','115 Lake view Road','Charminar');
insert into place_header values(06,'Thanjavur','12 Temple Road','Railway Jn');
insert into fleet_header values(01,'2010-08-04',101,01);
insert into fleet_header values(02,'2010-08-04',101,01);
insert into fleet_header values(03,'2010-08-04',101,01);
insert into fleet_header values(04,'2010-08-04',101,02);
insert into fleet_header values(05,'2010-08-04',101,03);
insert into fleet_header values(06,'2010-08-04',101,04);
insert into ticket_header values(01,01,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Madurai',1,1,60,101);
insert into ticket_header values(02,02,'2010-08-04','2010-09-04','09:00:00','KK Nagar','Madurai','Madras',2,1,60,102);
insert into ticket_header values(03,03,'2010-08-04','2010-09-04','21:00:00','Cubbonpark','Bangalore','Madras',4,2,400,101);
insert into ticket_header values(04,04,'2010-08-04','2010-09-04','10:00:00','Charminar','Hydrabad','Madras',10,0,500,103);
insert into ticket_header values(05,250,'2010-08-04','2010-09-04','15:00:00','parrys','Madras','Cochin',2,2,141,103);
insert into ticket_detail values(01,'Charu','f',24,'14.00');
insert into ticket_detail values(01,'Latha','f',10,'15.55');
insert into ticket_detail values(02,'anand','M',28,'17.80');
insert into ticket_detail values(02,'gautham','M',28,'16.00');
insert into ticket_detail values(02,'bala','M',09,'17.65');
insert into ticket_detail values(05,'sandeep','M',30,'18.00');
alter table route_header add comments long;
desc rout_header;
select distinct cat_code from route_header order by cat_code desc;
alter table route_header modify(distance int);
create table route_detail(route_id int,place_id int,nonstop char);
insert into route_detail values(105,01,'n');
insert into route_detail values(102,02,'s');
insert into route_detail values(106,01,'s');
insert into route_detail values(108,05,'n');
MYSQL Lab List Solutions
1. select * from route_header where origin='madras' and destination='cochin';
2. update route_header set distance=300 where origin in('madras','madurai') and destination in('madras','madurai');
3. delete from route_header where origin = 'madras' and destination = 'bangalore';
4. select * from route_header where origin like 'm%';
5. select * from route_header where fare between 30 and 50;
6. select fare,origin from route_header where route_no>15;
7. select * from place_header where place_name like 'm%';
8. select * from place_header where place_name like 'a%';
9. create table pseudo as (select * from category_header);
10. select add_months(doi,2) from ticket_header;
11. select last_day(dot) from ticket_header;
12. select months_between(dot,doi) from ticket_header;
13. select next_day(dot,'friday') from ticket_header;
14.
I. select round(to_date(day,'dd-mon-yy'),'year') from fleet_header;
II. select round(to_date(day,'dd-mon-yy'),'month') from fleet_header;
III. select round(to_date(day,'dd-mon-yy'),'day') from fleet_header;
IV. select round(to_date(day,'dd-mon-yy')) from fleet_header;
15.
I. select trunc(to_date(day,'dd-mon-yy'),'year') from fleet_header;
II. select trunc(to_date(day,'dd-mon-yy'),'month') from fleet_header;
III. select trunc(to_date(day,'dd-mon-yy'),'day') from fleet_header;
IV. select trunc(to_date(day,'dd-mon-yy')) from fleet_header;
16. select greatest(to_date(doi),to_date(dot)) from ticket_header;
17. select day+31 from fleet_header;
18. select day-60 from fleet_header;
19. select concat(upper(left(name,1)),substring(name,2)) from ticket_detail;
20. select upper(name) from ticket_detail;
21. select lower(place_name) from place_header;
22. select rtrim(place_name,'galore') from place_header where place_name='bangalore';
23. select ltrim(place_name,'hydera') from place_header where place_name='hyderabad';
24. select translate(place_name,'m','v') from place_header where place_name like 'm%';
25. select substr(name,3,3) from ticket_detail;
26. select replace(place_name,'a','e') from place_header;
27. select round(total_fare,2) from ticket_header;
28. select floor(total_fare) from ticket_header;
29. select to_char(sysdate,'ddth "of" fmmonth yyyy')) from dual;
30. select to_char(september 20 1996,'month-dd-yyyy')) from dual;
31. insert into category_header(cat_code) values(80);select nvl(cat_desc,0) from category_header;
32. select avg(total_fare) from ticket_header;
33. select name from ticket_detail where fare=(select max(fare) from ticket_detail);
34. select * from route_header where distance=(select min(distance) from route_header);
35. select sum(total_fare) from ticket_header;
36. select name,count(name) from ticket_detail group by name;
37. update (tablename) set day=’wednesday’ where day=’sunday’
Tuesday, October 26, 2010
Programs List
Single Linked List
Double Linked List
Circular Linked list
Insertion Sort
Merge Sort
Bubble Sort
Quick sort
Binary Tree Traversal
In-Order Traversal of Binary Tree
Pre-Order Traversal of Binary Tree
Post-Order Traversal of Binary Tree
Binary Search
Hashing
Binary Search
#include<iostream.h>
#include<conio.h>
class binary
{
public:
int a[50],n,x;
void getdata();
void getitem();
};
class search:public binary
{
public:
int high,j,low,temp;
search()
{
temp=0,low=0;
}
void findata();
void display();
};
void binary::getdata()
{
cout<<"\n Enter the total no of value:";
cin>>n;
cout<<"\n Enter "<<n<<" values one by one:";
for(int i=0;i<n;i++)
{
cout<<"\n Position "<<i<<":";
cin>>a[i];
}
}
void binary::getitem()
{
cout<<"\n Enter the value to be searched:";
cin>>x;
}
void search::findata()
{
high=n-1;
do
{
j=(low+high)/2;
if(a[j]==x)
temp=1;
else if(x<a[j])
high=j-1;
else
low=j+1;
}
while(low<=high&&temp==0);
}
void search::display()
{
if(temp==1)
cout<<"\n The value is located the position:"<<j;
else
cout<<"\n The value is not found";
}
void main()
{
clrscr();
search s;
s.getdata();
s.getitem();
s.findata();
s.display();
getch();
}
#include<conio.h>
class binary
{
public:
int a[50],n,x;
void getdata();
void getitem();
};
class search:public binary
{
public:
int high,j,low,temp;
search()
{
temp=0,low=0;
}
void findata();
void display();
};
void binary::getdata()
{
cout<<"\n Enter the total no of value:";
cin>>n;
cout<<"\n Enter "<<n<<" values one by one:";
for(int i=0;i<n;i++)
{
cout<<"\n Position "<<i<<":";
cin>>a[i];
}
}
void binary::getitem()
{
cout<<"\n Enter the value to be searched:";
cin>>x;
}
void search::findata()
{
high=n-1;
do
{
j=(low+high)/2;
if(a[j]==x)
temp=1;
else if(x<a[j])
high=j-1;
else
low=j+1;
}
while(low<=high&&temp==0);
}
void search::display()
{
if(temp==1)
cout<<"\n The value is located the position:"<<j;
else
cout<<"\n The value is not found";
}
void main()
{
clrscr();
search s;
s.getdata();
s.getitem();
s.findata();
s.display();
getch();
}
Merge Sort
#include <iostream.h>
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
void main()
{
int num,i;
cout<<"*************************"<<endl;
cout<<"MERGE SORT PROGRAM"<<endl;
cout<<"**************************"<<endl<<endl<<endl;
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;
cin>>num;
cout<<endl;
cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;
for(i=1;i<=num;i++)
{
cin>>a[i] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
cout<<endl<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
}
int a[50];
void merge(int,int,int);
void merge_sort(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++) a[k]=b[k];
}
void main()
{
int num,i;
cout<<"*************************"<<endl;
cout<<"MERGE SORT PROGRAM"<<endl;
cout<<"**************************"<<endl<<endl<<endl;
cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;
cin>>num;
cout<<endl;
cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;
for(i=1;i<=num;i++)
{
cin>>a[i] ;
}
merge_sort(1,num);
cout<<endl;
cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
cout<<endl<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<endl<<endl;
}
Monday, October 25, 2010
Post-Order Traversal of Binary Tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2---POST-ORDER TRAVERSAL.";
cout<<"\n\t3---EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tPOST-ORDER TRAVERSAL OF A TREE\n\t";
postorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void postorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n\t1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n\t2---POST-ORDER TRAVERSAL.";
cout<<"\n\t3---EXIT.";
cout<<"\n\tENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\n\tENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\n\tPOST-ORDER TRAVERSAL OF A TREE\n\t";
postorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
Pre-Order Traversal of Binary Tree
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n2---PRE-ORDER TRAVERSAL.";
cout<<"\n3---EXIT.";
cout<<"\nENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\nENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\nPRE-ORDER TRAVERSAL OF A TREE\n\t";
preorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *left;
node *right;
}*tree;
//*tree=NULL;
node *insert(node *tree,int ele);
void preorder(node *tree);
int count=1;
void main()
{
clrscr();
char ch;
int ele;
do
{
clrscr();
cout<<"\n1---INSERT A NODE IN A BINARY TREE.";
cout<<"\n2---PRE-ORDER TRAVERSAL.";
cout<<"\n3---EXIT.";
cout<<"\nENTER CHOICE::";
ch=getch();
switch(ch)
{
case '1':
cout<<"\nENTER THE ELEMENT::";
cin>>ele;
tree=insert(tree,ele);
break;
case '2':
cout<<"\nPRE-ORDER TRAVERSAL OF A TREE\n\t";
preorder(tree);
getch();
break;
case '3':
exit(0);
}
}while(ch!='3');
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
Subscribe to:
Posts (Atom)