Académique Documents
Professionnel Documents
Culture Documents
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class stu
int rollno ;
char name[25] ;
char Class[4] ;
float marks ;
char grade ;
public:
void getdata()
void putdata()
cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ;
int getrno()
{ return rollno ;
}
} s1 ;
void main()
ofstream fo("stu.dat",ios::app|ios::binary);
{ s1.getdata() ;
fo.write((char*)&s1,sizeof(s1)) ;
fo.close() ;
getch();
}
BUBBLE SORT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
clrscr();
int A[10];
int tmp;
cout<<"Enter A:\n";
cout<<"A["<<i+1<<"]: ";
cin>>A[i];
if(A[j]>A[j+1])
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
for(j=0;j<10;j++)
cout<<A[j]<<" ";
cout<<"\n";
cout<<"Sorted A is:\n";
cout<<A[i]<<", ";
getch();
}
BINARY SEARCH
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
clrscr();
randomize();
A[i]=20+random(30);
cout<<"A is:\n";
cout<<A[i]<<" ";
cin>>Srch;
if(A[j]>A[j+1])
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
}
}
cout<<"\nSorted A is:\n";
cout<<A[i]<<" ";
cout<<"\n";
while(strt<last)
{ mid=(last+strt)/2;
if(Srch==A[mid])
{ ans=mid;
break;
if(Srch<A[mid])
last=mid;
if(Srch>A[mid])
strt=mid;
getch();
}
CLASS
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Book
int Sno;
char bname[25];
char auth[25];
public:
void Entry()
cin>>Sno;
gets(bname);
gets(auth);
void Display()
puts(bname);
puts(auth);
};
void main()
{
clrscr();
B1.Entry();
B2.Entry();
B1.Display();
B2.Display();
getch();
}
CONSTRUCTOR
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Emp
int iD;
public:
Emp()
cout<<"\nEnter iD No.:";
cin>>iD;
cout<<"Enter Name:";
gets(Name);
cout<<"Enter Designation:";
gets(dsgn);
void Display()
cout<<"\nName: ";
puts(Name);
cout<<"Designation: ";
puts(dsgn);
};
void main()
{
clrscr();
Emp E[2];
E[i].Display();
getch();
}
CONTRUCTOR 2
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Employee
int iD;
public:
iD=id;
strcpy(name, n);
strcpy(dsgn, d);
void Disp()
puts(name);
puts(dsgn);
};
void main()
{
clrscr();
E1.Disp();
E2.Disp();
getch();
}
DESTRUCTOR
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Marks
{ float mrks;
char name[25];
public:
{ mrks=m;
roll=r;
cls=c;
strcpy(name, n);
Marks(Marks &M)
{ mrks=M.mrks;
roll=M.roll;
cls=M.cls;
strcpy(name, M.name);
void Disp()
puts(name);
}
~Marks()
{ cout<<"\n\nEnd of Program!!";
};
void main()
{ clrscr();
M1.Disp();
Marks M2(M1);
M2.Disp();
getch();
}
DELETE FROM FILE
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class stu
int rollno ;
char name[25] ;
char Class[4] ;
float marks ;
char grade ;
public:
void getdata()
void putdata()
cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ;
int getrno()
{ return rollno ;
} s1,stud ;
void main()
ifstream fio("stu.dat",ios::in) ;
ofstream file("temp.dat",ios::out) ;
while(!fio.eof())
fio.read((char*)&s1,sizeof(s1));
if(s1.getrno() == rno )
{ s1.putdata() ;
found = 't' ;
if(confirm == 'n')
file.write((char*)&s1,sizeof(s1));
else
file.write((char*)&s1,sizeof(s1));
if(found == 'f')
fio.close() ;
file.close() ;
remove("stu.dat") ;
rename("temp.dat","stu.dat") ;
fio.open("stu.dat",ios::in);
cout << "Now the file contains\n" ;
while(!fio.eof())
fio.read((char*)&s1,sizeof(s1));
if(fio.eof()) break ;
stud.putdata() ;
fio.close() ;
getch() ;
}
FUNCTION OVERLOADING
#include<iostream.h>
#include<conio.h>
void frmPtr(int n)
cout<<" ";
cout<<n<<" ";
cout<<"\n";
void frmPtr(char m)
cout<<" ";
cout<<m<<" ";
cout<<"\n";
void main()
clrscr();
int x, ch;
char z;
cout<<"Types of pattern(of 10):";
cout<<"\n 1.Alphabetical";
cout<<"\n 2.Numerical";
cin>>ch;
if(ch==1)
cin>>z;
frmPtr(z);
else if(ch==2)
cin>>x;
frmPtr(x);
getch();
}
INSERTION SORT
#include<iostream.h>
#include<limits.h>
#include<conio.h>
void main()
int A[101] , N ;
cout << "How many elements do you want to create array with ?(max 100 )";
cin >> N ;
InSort(A,N) ;
getch() ;
int temp ,j ;
B[0] = INT_MIN ;
{ temp = B[i] ;
j=i-1;
j--;
}
B[j+1] = temp ;
cout << "Array after pass -" << i << "-is :" ;
cout<< endl ;
}
MODIFY IN A TEXT FILE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#include<fstream.h>
class stu
{int rollno ;
char name[25] ;
char Class[4] ;
float marks ;
char grade ;
public:
void getdata()
void putdata()
cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ;}
int getrno()
{ return rollno ;}
void modify() ;
} s1,stud ;
void stu::modify()
cout << "\tName :" << name << "\tClass : " << Class
<< "\n Marks : " << marks << "\t Grade :" << grade << endl ;
float mks ;
cout << " New Name :(Enter '.' to retain old one)" ;
cin >> nm ;
cout << " New Class :(Enter '.' to retain old one)" ;
cin >> Cl ;
if(strcmp(nm,".")!= 0)
strcpy(name,nm);
if(strcmp(Cl,".")!= 0)
strcpy(Class,Cl);
if(mks != -1)
{ marks = mks ;
void main()
{clrscr();
while(!fio.eof())
{ pos = fio.tellg() ;
fio.read((char*)&s1,sizeof(s1)) ;
if( s1.getrno() == rno)
{s1.modify();
fio.seekg(pos) ;
fio.write((char*)&s1,sizeof(s1)) ;
found = 't' ;
break; }}
if(found == 'f')
fio.seekg(0) ;
while(!fio.eof())
{ fio.read((char*)&stud,sizeof(stud)) ;
stud.putdata() ;}
fio.close() ;
getch() ;}
TEXT FILE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
void main()
{ clrscr() ;
ifstream fin ;
fin.open("ARTICLE.txt",ios::in);
char word[50] ;
while(!fin.eof())
if(strcmp(word,"this") == 0)
c1 ++;
if(strcmp(word,"these") == 0)
c2++;
fin.close() ;
getch() ;
}
SELECTION SORT
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
cout<<"Enter A:\n";
cout<<"A["<<i+1<<"]: ";
cin>>A[i];
tsm=A[i];
pos=i;
if(A[j]<tsm)
tsm=A[j];
pos=j;
tmp=A[i];
A[i]=A[pos];
A[pos]=tmp;
cout<<A[i]<<" ";
cout<<"\nSorted A is:\n";
cout<<A[i]<<" ";
getch();
}
SEARCHING
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class stu
{ int rollno ;
char name[25] ;
char Class[4] ;
float marks ;
char grade ;
public:
void getdata()
void putdata()
cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ; }
int getrno()
{ return rollno ;}
} s1 ;
void main()
{
int rn ;
cin >> rn ;
while(!fi.eof())
{ fi.read((char*)&s1,sizeof (s1));
if(s1.getrno() == rn)
{ s1.putdata() ;
found = 'y' ;
break ;
if(found =='n')
fi.close();
getch() ;
}
RANDOM
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
clrscr();
void develop();
void check();
int ch;
long choice;
develop();
cout<<"\n\t1. "<<num[0];
cout<<"\n\t2. "<<num[1];
cout<<"\n\t3. "<<num[2];
cout<<"\nEnter Choice:";
cin>>ch;
if(ch==1)
choice=num[0];
else if(ch==2)
choice=num[1];
else if(ch==3)
choice=num[2];
else
check();
cout<<"The Lucky Number is: "<<res;
if(res==choice)
cout<<"\nLucky af!!";
else
getch();
void develop()
{ randomize();
{ num[j]=0;
{num[j]+=(random(10) * pow(10,i));}}return;}
void check()
{randomize();
int x;
x=random(3);
res=num[x];
return;