Sie sind auf Seite 1von 36

BINARY SEARCH

#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct std
{
int prodno;char name[20];
int qty;float unitprice;
void input()
{
cout<<"enter the prodno";
cin>>prodno;
cout<<"enter prodname";
gets(name);
cout<<"enter qty";
cin>>qty;
cout<<"enter unit price";
cin>>unitprice;
}
void output()
{
cout<<"the product no is"<<prodno<<endl;
cout<<"the product name is"<<name<<endl;
cout<<"the qty is"<<qty<<endl;
cout<<"the unit price is"<<unitprice<<endl;
}
};
int bin(std s[],char *data, int );
void main()
{
clrscr();
int n; char rep='y';char *data;
std s[25];
cout<<"enter the total number";
cin>>n;
for(int i=0;i<n;i++)
s[i].input();
cout<<"enter the search name";
gets(data);
int ch,f=0;
while(rep=='y'||rep=='Y')
{
cout<<"1.binary 2.exit ";
cin>>ch;
switch(ch)
{
case 1:
f=bin(s,data,n);break;
case 2:
exit(0);
default:
cout<<"invalid option";
}
if(f==0)
cout<<"no name";
else
cout<<"the pos"<<f;
cout<<"do u want to continue";
cin>>rep;}
getch();}
int bin(std s[],char *item,int n)
{
int mid,beg=0,last=n-1;
for(;beg<=last;){
mid=(beg+last)/2;
if(strcmp(s[mid].name,item)==0)
return mid+1;
else
if(strcmp(s[mid].name,item)<0)
beg=mid+1;
else
if(strcmp(s[mid].name,item)>0)
last=mid-1;}
return 0; }

3. EXCHANGE SELECTION SORT


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct EMPLOYEE{
int empid;
char name[40];
float salary;
char designation[40];
void INPUT(void){
cout<<"\nEnter the Employee's ID no.: "; cin>>empid;
cout<<"\nEnter the name: "; gets(name);
cout<<"\nEnter the salary (in Rs): "; cin>>salary;
cout<<"\nEnter the designation: "; gets(designation);}
void OUTPUT(void){

cout<<"\nID No. : "<<empid;


cout<<"\nNAME : "; puts(name);
cout<<"SALARY : "<<salary;
cout<<"\nDESIGNATION: "; puts(designation);}}
EMPLOYEE emp[20];
void main()
{clrscr();
void exsort(int);
int i,n,ch;
char rep;
do{
cout<<"\n Exchange Selection sort";
cout<<"\nEnter the no. of employee's: ";
cin>>n;
for(i=0;i<n;++i) emp[i].INPUT();
cout<<"\t MENU"<<"\n (1)Exchange selection sort"<<"\n
(2)Exit\n";
cin>>ch;
if(ch==1) exsort(n);
else if(ch==2) exit(0);
else cout<<"\nINVALID CHOICE";
cout<<"\nWant to enter more? (y/n): ";
cin>>rep;
} while(rep=='y'||rep=='Y');

getch();}
void exsort(int N){
int i,j,pos;
float small;
EMPLOYEE tmp;
for(i=0;i<N;++i)
{small=emp[i].salary;
pos=i;
for(j=i+1;j<N;++j)
{if(emp[j].salary<small)
{small=emp[j].salary;
pos=j;}}
tmp=emp[i];
emp[i]=emp[pos];
emp[pos]=tmp;
}cout<<"\nThe arranged employee order is: \n";
for(i=0;i<N;++i) emp[i].OUTPUT();}

4.BUBBLE SORT

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct GAME
{
int plno;
char plnm[40];
char gmnm[40];
int points;
void INPUT(void)
{
cout<<"\nEnter the player's no.: "; cin>>plno;
cout<<"\nEnter the player's name: "; gets(plnm);
cout<<"\nEnter the game's name: "; gets(gmnm);
cout<<"\nEnter the points: "; cin>>points;
}
void OUTPUT(void)
{
cout<<"\nPlayer's No.: "<<plno;
cout<<"\nName: "; puts(plnm);
cout<<"\nGame's name: "; puts(gmnm);
cout<<"\nPoints: "<<points;
}
};
GAME G[20];
void main()
{
clrscr();
void bubblesort(int);
int i,n,ch;
char rep;
do
{
cout<<"\nEnter the no. of games: "; cin>>n;
for(i=0;i<n;++i) G[i].INPUT();
cout<<"\t MENU\n"<<" (1)BUBBLE SORT"<<"\n (2)EXIT\n";
cin>>ch;
if(ch==1) bubblesort(n);
else if(ch==2) cout<<"\nINVALID CHOICE";
cout<<"\nWant to enter more? (y/n): ";
cin>>rep;
} while(rep=='y'||rep=='Y');
getch();
}
void bubblesort(int N)
{
int i,j,k;
GAME tmp;
for(i=0;i<N;++i)
{
for(j=0;j<(N-1)-i;++j)
{
if(G[j].points<G[j+1].points)
{
tmp=G[i];
G[i]=G[i+1];
G[i+1]=tmp;
}
}
}
cout<<"\nThe Arranged game scores are: \n";
for(i=0;i<N;++i) G[i].OUTPUT();}

5. INSERTION SORT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class MOVIE{
int movieno;
char title[40];
float price;
float rating;
public:
void INPUT(void){
cout<<"\nEnter the movie no.: ";
cin>>movieno;
cout<<"\nEnter the movie's title: ";
gets(title);
cout<<"\nEnter the price: ";
cin>>price;
cout<<"\nEnter the rating (out of 5): ";
cin>>rating;}
void OUTPUT(void){
cout<<"\n"<<movieno<<"\t";
puts(title);
cout<<"\t"<<price<<" Rs"<<"\t";
cout<<rating<<" stars";}
char* retname(void) { return title; }
};MOVIE M[20];
void main(){
clrscr();
void inssort(int);
int i,n,ch;
char rep;do {
cout<<"\t MENU"<<"\n (1)INSERTION SORT"<<"\n (2)EXIT\n";
cin>>ch;
if(ch==1)

{
cout<<"\nEnter the no. of movies: ";
cin>>n;
for(i=0;i<n;++i)
M[i].INPUT();
inssort(n);}
else if(ch==2) break;
else cout<<"\nINVALID CHOICE";
cout<<"\nWant to enter more? (y/n): ";
cin>>rep;
} while(rep=='y'||rep=='Y');
getch();
}void inssort(int N)
{int i,j;
MOVIE tmp;
for(i=0;i<N;++i)
{tmp=M[i];
j=i-1;
while(strcmp(tmp.retname(),M[j].retname())<0 && j>=0)
{M[j+1]=M[j];
--j;
}M[j+1]=tmp;
}cout<<"\nThe arranged movie order is: \n";
for(int k=0;k<N;++k)
M[k].OUTPUT();
}
6. MERGING OF TWO ARRAYS

#include<iostream.h>
#include<conio.h>
#include<process.h>
void merge1(int[ ],int[ ],int,int);
void merge2(int[ ],int[ ],int,int);

void main(){
clrscr();
int a[20],b[20],m,n,i;
cout<<"\n\t Enter the total elements of p";
cin>>m;
cout<<"\n\t Enter the elements ascending order";
for(i=0;i<m;i++)
cin>>a[i];
cout<<"\n\t Enter total elements of q";
cin>>n;
cout<<"\n\t Enter the elements in descending order";
for(i=0;i<n;i++)
cin>>b[i];
int ch;
char rep='y';
while(rep=='y'||rep=='Y'){
cout<<"\n\t1.Merge(a in ascending b in descending)\n\t2.Merge2(a
in descending b in ascending)\n\t3.Exit";
cin>>ch;
switch(ch){
case 1: merge1(a,b,m,n);
break;
case 2:merge2(a,b,m,n);
break;
case 3:
exit(0);
break;
default: cout<<"\n\tInvalid choice";}
cout<<"\n\t Do you want to continue?";
cin>>rep;}
getch();
}
void merge1(int a[ ],int b[ ],int m,int n)
{
int c[20];
int ca=0,cb=n-1,cc=0;
while(ca<m && cb>=0)
{
if(a[ca]<=b[cb])
{
c[cc]=a[ca];
cc++;
ca++;
}
else
{
c[cc]=b[cb];
cc++;
cb--;}}
if(cb>=0){
while(cb>=0){
c[cc]=b[cb];
cc++;
cb--;}}
if(ca<=n-1){
c[cc]=a[ca];
cc++;
cb++;}
for(int i=0;i<m+n;i++)
cout<<c[i]<<" ";}
void merge2(int a[ ],int b[ ],int m,int n){
int c[20];
int ca=m-1,cb=0,cc=0;
while(cb<=n-1&&ca>=0){
if(c[ca]>=b[cb])
{ c[cc]=a[ca];
cc++;
ca--;}
else
{ c[cc]=b[cb];
cb++;
cc++;}}
if(ca>=0)
{ while(ca>=0)
{ c[cc]=a[ca];
ca--;
cc++; }}
if(cb<=n-1)
{ while(cb<=n-1)
{ c[cc]=b[cb];
cc++;
cb++; } }
for(int i=0;i<m+n;++i)
cout<<c[i]<<" "; }

7. MATRIX MANIPULATION
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
void sumprime(int[][10],int,int);
void transpose(int[][10],int,int);
void middlerow(int[][10],int,int);
int a[10][10],i,j,m,n,ch;
char rep;
cout<<"\nEnter the row size and the column size for the
matrix:"<<"\n";
cin>>m>>n;
cout<<"\nEnter the matrix:"<<"\n";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];
do{
cout<<"\t MATRIX MENU"<<"(1) Sum of prime numbers in the
left diagonal\n"<<"\n (2)Transpose of Matrix"<<"\n (3) Display
the middle row elements"<<"\n";
cin>>ch;
if(ch==1)sumprime(a,m,n);
else if(ch==2)transpose(a,m,n);
else if(ch==3)middlerow(a,m,n);
else cout<<"\n INVALID REQUEST!";
cout<<"\n WANT TO ENTER MORE?(Y/N):";
cin>>rep;}
while(rep=='y'||rep=='y');
getch();}
void sumprime(int a[][10],int r,int c)
{ int f=0,sum=0,k,i;
for(i=0;i<r;++i)
{for(k=2;k<c/2;++k)
{ if(a[i][i]%k==0)
{++f;
break;}}
if(f==0)sum+=a[i][i];}
cout<<"\n The sum is:"<<sum;}
void transpose(int a[][10],int r,int c)
{int b[10][10],i,j;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
b[i][j]=a[j][i];
cout<<"\n The transposed matrix is:";
for(i=0;i<r;++i){
cout<<"\n";
for(j=0;j<c;++j)
cout<<b[i][j]<<"";}}
void middlerow(int a[][10],int r,int c)
{int i,j;
for(j=0;j<c;++j)
if(r%2) cout<<a[r/2][j]<<"";
else cout<<a[(r-2)-1][j]<<""<<a[r/2][j]<<"";}

8. STACK AS AN ARRAY OF OBJECTS


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{clrscr();
void push(int &,char[][20],char[]);
void pop(int &,char[][20]);
void display(int,char[][20]);
char rep,a[20][20],b[20];
int n,top=-1;
do{
cout<<"\t\tmenu"<<"\n\t1.push"<<"\n\t2.pop"<<"\n\t3.display"<
<"\n\t4.exit";
cin>>n;
if(n==1)
{cout<<"\nEnter a string";
gets(b);

push(top,a,b);
}else if(n==2)
pop(top,a);
else if(n==3)
display(top,a);
else if(n==4)
break;
else cout<<"invalid choice";
cout<<"\n want to enter more(y/n)";
cin>>rep;
}while(rep=='y'||rep=='Y');
getch();
}
void push(int &t,char a[][20],char b[])
{if(t==19)
{cout<<"\noverflow";
return;
}else
{++t;
strcpy(a[t],b);}}
void pop(int &t,char a[][20]){
if(t==-1){
cout<<"\n underflow";
return;}
else
puts(a[t]);
--t;}

void display(int t,char a[][20])


{cout<<"\n the strings in the stack are";
for(int i=t;i>=0;i--)
puts(a[i]);}

9. CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct donor{
long int donorid;
char dname[35];
char bloodgroup[20];
void input(void){
cout<<"\nEnter donors id";cin>>donorid;
cout<<"\nEnter donors name";gets(dname);
cout<<"\nEnter donors bloodgroup";gets(bloodgroup);}
void output(){
cout<<"\n donors id";cout<<donorid;
cout<<"\n donors name";puts(dname);
cout<<"\n donors bloodgroup";puts(bloodgroup);}}
d[25];
void main(){
clrscr();
void insert(int &,int &);
void pop(int &,int &);
void display(int & ,int &);
int front=-1,rear=-1,ch;
char rep;
do{
cout<<"\t\n Menu"<<"\t\n
1.insert"<<"\n\t2.delete"<<"\n\t3.display"<<"\n\t4.exit";
cin>>ch;
if(ch==1)
insert(front,rear);
else if(ch==2)
pop(front,rear);
else if(ch==3)
display(front,rear);
else if(ch==4)
break;
else cout<<"\n\t invalid choice";
cout<<"\n\twnat to enter more(y/n)";
cin>>rep;}
while(rep=='y'||rep=='Y');
getch();}
void insert(int &f,int &r){
if((f==0 && r==24)||(f==r+1)){
cout<<"\nOverflow";
return;}
else if(r==-1)f=r=0;
else if(r==24)r=0;
else ++r;
d[r].input();}
void pop(int &f,int &r){
if(f==-1){
cout<<"\nUnderflow";
return;}
else{
if(f==r)f=r=-1;
else if(f==24)f=0;
else ++f;}}
void display(int &f,int &r){
cout<<"\n\t the circular queue of donor are";
if(f<=r)
for(int i=f;i<=r;i++)

d[i].output();
else{
for(int j=f;j<=24;j++)
d[j].output();
for(int k=0;k<=r;++k)
d[k].output();}}

10.STRUCTURE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct Picture
{int pno;
char category[25],location[25];
void fixlocation(void);
void enter(void);
void seeall(void);
}p[20];
void Picture::enter(void)
{ cout<<"\nEnter the picture number";
cin>>pno;
cout<<"\nEnter the category: ";
gets(category);
fixlocation();
}void Picture::fixlocation(void)
{ if(strcmpi(category,"Classic")==0)
{strcpy(location,"Amina");
}else
if(strcmpi(category,"Modern")==0)
strcpy(location,"Jim Plaq");
else
if(strcmpi(category,"Antique")==0)
strcpy(location,"Ustad Khan");
}void Picture::seeall()
{ cout<<"\nDetails:"<<endl;
cout<<pno<<endl;
puts(category);
puts(location); }
void main(){
char rep;
int i,n,ch;
do
{clrscr();
cout<<"\n\t Structure program";
cout<<"\t\nMENU"<<"\n\t(1)create pictures"<<"\n\t(2)list of
classic pictures\n";
cin>>ch;
if(ch==1){
cout<<"\n Enter the no of pictures";
cin>>n;
for(i=0;i<n;++i)
p[i].enter();}
else if(ch==2){
for(i=0;i<n;++i){
if(strcmpi(p[i].category,"classic")==0)
p[i].seeall();}}
else cout<<"\nInvalid choice";
cout<<"\n Do you want to contiue more(y/n)";
cin>>rep;
} while(rep=='y'||rep=='Y');
getch();}

10.STRUCTURE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct Picture
{int pno;
char category[25],location[25];
void fixlocation(void);
void enter(void);
void seeall(void);
}p[20];
void Picture::enter(void)
{ cout<<"\nEnter the picture number";
cin>>pno;
cout<<"\nEnter the category: ";
gets(category);
fixlocation();
}void Picture::fixlocation(void)

{ if(strcmpi(category,"Classic")==0)
{strcpy(location,"Amina");
}else
if(strcmpi(category,"Modern")==0)
strcpy(location,"Jim Plaq");
else
if(strcmpi(category,"Antique")==0)
strcpy(location,"Ustad Khan");
}void Picture::seeall()
{ cout<<"\nDetails:"<<endl;
cout<<pno<<endl;
puts(category);
puts(location); }
void main(){
char rep;
int i,n,ch;
do
{clrscr();
cout<<"\n\t Structure program";
cout<<"\t\nMENU"<<"\n\t(1)create pictures"<<"\n\t(2)list of classic
pictures\n";
cin>>ch;
if(ch==1){
cout<<"\n Enter the no of pictures";
cin>>n;
for(i=0;i<n;++i)
p[i].enter();}
else if(ch==2){
for(i=0;i<n;++i){
if(strcmpi(p[i].category,"classic")==0)
p[i].seeall();}}
else cout<<"\nInvalid choice";
cout<<"\n Do you want to contiue more(y/n)";
cin>>rep;
} while(rep=='y'||rep=='Y');
getch();}

12: CONSTRUCTOR OVERLOADING


#include<iostream.h>
#include<conio.h>
class box
{
public:
float l,b,h;
void volume()
{
float Volume;
Volume=l*b*h;
cout<<"Volume="<<Volume;}
box()
{
l=1;
b=1;
h=1;}
box(int le,int br,int he){
l=le;
b=br;
h=he;}
box(box&t){

l=t.l;

b=t.b;
h=t.h;}
};
void main(){
clrscr();
float l,b,h;
box bo;
char rep='y';
int ch;
while(rep=='y')
{
cout<<"enter your choice"<<endl;
cout<<"1.default 2.parameter 3.copy"<<endl;
cin>>ch;
switch(ch)
{
case 1:
box b1;
b1.volume();
break;
case 2:
cout<<"enter the length and breadth and height"<<endl;
cin>>l>>b>>h;
box b2(l,b,h);
b2.volume();
break;
case 3:
box b3(b2);
b3.volume();
break;
default:
cout<<"invalid option";
}
cout<<"do u want to continue"<<endl;
cin>>rep;
}
getch();
}

13. FUNCTION OVERLOADING

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<math.h>
void operation(char A[]){
int i,lc=0,uc=0;
for(i=0;A[i]!='\0';++i){
if(isupper(A[i]))
++uc;
else if(islower(A[i]))
++lc;}
Cout<<"\nThe no. of lowercase alphabets are: "<<lc;
cout<<"\nThe no. of uppercase alphabets are: "<<uc;}
void operation(int n){
int n1=n,r=0,revnum=0;
while(n){
r=n%10;
revnum=(revnum*10)+r;
n/=10;}
if(revnum==n1)
cout<<"\nThe given no. is a palindrome";
else
cout<<"\nThe given no. is not a palindrome";}
void operation(float x,int n){
int i,j,sum=0;
for(i=1;i<=n;++i){
int fact=1;
for(j=1;j<=2*(i-1);++j)
fact*=j;
sum+=((pow(x,i))/fact);}
cout<<"\nThe sum of the series is: "<<sum;}
void main()
{
clrscr();
int ch,n,no;
float x;
char rep,S[70];
do {
cout<<"\t\tMENU\n"<<"(1) To find the no. of uppercase and
lowercase alphabets\n"
<<"(2) To check for number palindrome\n"
<<"(3) To find the sum of the series\n";
cin>>ch;
if(ch==1){
cout<<"\nEnter the string:\n";
gets(S);
operation(S);}
else if(ch==2){
cout<<"\nEnter the number: ";
cin>>no;
operation(no);}
else if(ch==3){
cout<<"\nEnter the value of x: ";
cin>>x;
cout<<"\nEnter the value of n: ";
cin>>n;
operation(x,n);}
else cout<<"\nINVALID CHOICE";
cout<<"\nWant to enter more? (y/n): ";
cin>>rep;
} while(rep=='y'||rep=='Y');
getch();
}

15. BINARY FILE


#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class consumer{
char cname[30];
char caddress[40];
char cphone[20];
char area[20];
public:
void ledger(void){
cout<<"\n enter the name\t";
gets(cname);
cout<<"\n enter the address\t";
gets(caddress);
cout<<"\n enter the phone\t ";
gets(cphone);
cout<<"\n enter the area\t";
gets(area);}
void show(void)
{
cout<<"name\t";puts(cname);
cout<<"address\t";puts(caddress);
cout<<"phone\t";puts(cphone);
cout<<"area\t";puts(area);
}
char *rscode(void)
{
return area;
}
int checkcode(char ac[])
{
return strcmpi(area,ac);}
}c;
void writeconsumer(void)
{
ofstream fout("consumer.dat",ios::out|ios::binary);
char rep;
do{
c.ledger();
fout.write((char*)&c,sizeof (c));
cout<<"\n\tDo You Want to enter more ?(y/n)";
cin>>rep;
}
while (rep=='y'||rep=='Y');
fout.close();
}
void rewrite(void)
{
ifstream fin("consumer.dat",ios::in|ios::binary);
int f=0;
fin.seekg(0);
char ar[20],rs[20],rep;
do
{
cout<<"\n enter the area:"; gets(ar);
while (!fin.eof())
{
fin.read ((char*)&c,sizeof(c));
if (c.checkcode (ar)==0)
{ofstream fout("backup.dat",ios::out|ios::binary|ios::app);
cout<<"\n\tEnter the Area name to change";
gets (c.rscode());
fout.seekp(-1*sizeof(c),ios::cur);
fout.write((char*)&c,sizeof(c));
fout.close();++f;
}
}
if (f==0)
cout<<"\n\t Record not found";
cout<<"Do you want continue(y/n)";
cin>>rep;
}while(rep=='y'||rep=='Y');
}
void readbackup(void)
{
ifstream fin;
fin.open("backup.dat",ios::in|ios::binary);
int f=0;
fin.seekg(0);
while (fin.read((char*)&c,sizeof(c)))
{
c.show();
++f; }
fin.close();
if (f==0)
cout<<"\n Record not found";
}
void main()
{
clrscr();
int ch;
char rep;
do
{
cout<<"\n\tBinary file";
cout<<"\n\t Menu \n ";
cout<<"(1)Write data\n";
cout<<"(2)Rewrite data \n";
cout<<"(3)Read backup data \n";
cin>>ch;
if(ch==1) writeconsumer();
else if(ch==2) rewrite();
else if(ch==3) readbackup();
else cout<<"\nINVALID CHOICE";
cout<<"\n want to enter more ?(y/n):";
cin>>rep;
}
while (rep=='y'||rep=='Y');
getch();
}

17-INHERITANCE
#include<iostream.h>
#include<conio.h>
#include<string.h>
class publication
{protected:
char title[40]; float price;
public:
publication(char ch[], float pr)
{strcpy(title, ch); price= pr;}
void putdata(){cout<<title<<price;}
};
class book :public publication
{int page_count;
public:
book(char t[], float price, int pc): publication(t, price)
{page_count=pc;}
void putdata( )
{publication::putdata( );
cout<<"The page count="<<page_count;}
};
class Tape :public publication
{int play_time;
public:
Tape(char t[], float pr, int pt):publication(t,pr)
{play_time= pt;}
void putdata( )
{publication::putdata( );
cout<<"The play time="<<play_time;}
};
void main( )
{int ch; char rep='y';
char t[50]; float price; int pc;
clrscr( );
while(rep=='y' || rep =='Y')
{cout<<"1.... Book\n";
cout<<"2.... Tape\n";
cin>>ch;
switch(ch)
{case 1: book b1("C++ Tutorial",210, 430);
b1.putdata( );
break;
case 2: Tape T("I Robot",540, 2);
T.putdata( );
break;
default: cout<<"Invalid Choice";}
cout<<"Conitnue??"; cin>>rep;}
getch();}

18.STRING USING POINTERS

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<conio.h>
#include<ctype.h>
int occur(char *,char);
int count(char *);
void main(){
char rep='y',ch;int choice;
char *str=new char;
do{
cout<<"\n\tstring using pointers";
cout<<"\n\t1.To count no of character occurance\n\t2.No of 3 & 5

words\n\t3.Exit";
cout<<"\n\tEnter the choice";
cin>>choice;
switch(choice){
case 1:
cout<<"\n\tEnter the string";
gets(str);
cout<<"\n\tEnter the character to count";
cin>>ch;
occur(str,ch);
break;
case 2:
cout<<"\n\tEnter the string";
gets(str);
cout<<"\n\tNo of 3 letter and 5 letter word";
count(str);
break;
case 3:
exit(0);
break;
}}while(rep=='y'||rep=='Y');}
int occur(char *p,char c){

int i=0,ct=0;
for(;p[i]!='\0';i++){
if(p[i]==c)
ct++;}
cout<<"\n\t"<<ct<<endl;
return 0;}
int count(char *p){
int i=0,a=0,b=0;
while(p[i]!='\0')
{if(isalpha(p[i])&&isalpha(p[i+1])&&isalpha(p[i+2])&&!isalpha(p[i+3
]))
{++a;i=i+3;continue;}
else if(isalpha(p[i])&&isalpha(p[i+1])&&isalpha(p[i+2])&&isalpha(p[i

+3])&&isalpha(p[i+4])&&!isalpha(p[i+5]))
{++b;i=i+5;continue;}
++i;}
cout<<"\n\t"<<a<<" "<<b;
return 0;

19 .STACK AS A LINKED LIST


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct voter
{int voterid;
char Name[40];
int age;
voter *link;}*top=NULL;
void push( )
{voter *ptr=new voter;
cout<<"Enter the voter id, name and age";
cin>>ptr->voterid>>ptr->Name>>ptr->age;
ptr->link=NULL;
if(top==NULL) top = ptr;
else ptr->link=top; top=ptr;
}void pop( )
{voter *t;
if (top==NULL) cout<<"Empty stack";
else
{t=top; top=top->link;}
cout<<t->voterid<<"is deleted";}
void display( )
{voter *t=top;
if(t==NULL) cout<<"Empty stack";
else
while(t!=NULL)
{cout<<t->voterid<<t->Name<<t->age; t=t->link;}
}void main( )
{int ch; char rep='y';
voter temp;
clrscr( );
while(rep=='y' || rep =='Y')
{cout<<"1.... push\n";
cout<<"2.... pop\n";
cout<<"3.... Display\n";
cin>>ch;
switch(ch)
{case 1: push( );
break;
case 2: pop( );
break;
case 3: display( );
break;
default: cout<<"Invalid Choice";}
cout<<"Conitnue??"; cin>>rep;}
getch();}
20. QUEUE AS A LINKED LIST
#include<iostream.h>
#include<string.h>
#include<stdio.h>

#include<conio.h>
struct Node
{int ticketno;
char Pname[40];
Node *next;};
class Bus
{Node *Rear,*Front;
public:
Bus( ){Front=Rear=NULL;}
void Insert(int L,char p[])
{Node *ptr=new Node;
ptr->ticketno=L;
strcpy(ptr->Pname,p);
ptr->next=NULL;
if(Rear==NULL) Rear=Front=ptr;
else Rear->next=ptr; Rear=ptr;
}
void Del( )
{Node *t;
if (Front==NULL) cout<<"Empty Queue";
else
{t=Front; Front=Front->next;}
cout<<t->ticketno<<"is deleted";
delete t;}
void display( ); };

void Bus::display()
{Node *t=Front;
if(t==NULL) cout<<"Empty Queue";
else
while(t!=NULL)
{cout<<t->ticketno<<t->Pname; t=t->next;} }
void main( )
{int ch; char rep='y';
Bus temp;
clrscr( );
while(rep=='y' || rep =='Y')
{cout<<"1.... Insert\n";
cout<<"2.... Delete\n";
cout<<"3.... Display\n";
cin>>ch;
switch(ch)
{case 1: temp.Insert(1001,"ARUN" );
break;
case 2: temp.Del( );
break;
case 3: temp.display( );
break;
default: cout<<"Invalid Choice";}
cout<<"Conitnue??"; cin>>rep; }
getch(); }

Das könnte Ihnen auch gefallen