Vous êtes sur la page 1sur 32

APPEND

#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()

{ cout << "Rollno : " ; cin >> rollno ;

cout << "Name : " ; cin >> name ;

cout << "Class : " ; cin >> Class ;

cout << "Marks : " ; cin >> marks ;

if(marks >= 75) grade = 'A' ;

else if(marks >= 60) grade = 'B';

else if(marks >= 50) grade = 'C';

else if(marks >= 40) grade = 'D';

else grade = 'F' ;

void putdata()

cout <<"Rollno" << rollno << "\tName :" << name ;

cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ;

int getrno()

{ return rollno ;
}

} s1 ;

void main()

ofstream fo("stu.dat",ios::app|ios::binary);

char ans = 'y';

while( ans == 'y')

{ s1.getdata() ;

fo.write((char*)&s1,sizeof(s1)) ;

cout << "Record added to file \n" ;

cout << "Want to enter more records?(y/n)..." ;

cin >> ans ;

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";

for(int i=0; i<10; i++)

cout<<"A["<<i+1<<"]: ";

cin>>A[i];

for(i=0; i<10; i++)

for(int j=0; j<9-i; j++)

if(A[j]>A[j+1])

tmp=A[j];

A[j]=A[j+1];

A[j+1]=tmp;

cout<<"A after loop "<<i+1<<": ";

for(j=0;j<10;j++)
cout<<A[j]<<" ";

cout<<"\n";

cout<<"Sorted A is:\n";

for(i=0; i<10; i++)

cout<<A[i]<<", ";

getch();

}
BINARY SEARCH
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()

clrscr();

randomize();

int A[30], Srch, tmp, ans;

for(int i=0; i<30; i++)

A[i]=20+random(30);

cout<<"A is:\n";

for(i=0; i<30; i++)

cout<<A[i]<<" ";

cout<<"\nEnter the no. to be searched(in [20,50)): ";

cin>>Srch;

for(i=0; i<30; i++)

for(int j=0; j<29-i; j++)

if(A[j]>A[j+1])

tmp=A[j];

A[j]=A[j+1];

A[j+1]=tmp;

}
}

cout<<"\nSorted A is:\n";

for(i=0; i<30; i++)

cout<<A[i]<<" ";

cout<<"\n";

int mid, last=30, strt=0;

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;

cout<<"\nYour Element is found at position \'"<<ans+1<<"\' of the sorted Array";

getch();

}
CLASS
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Book

int Sno;

char bname[25];

char auth[25];

public:

void Entry()

cout<<"\nEnter Serial No.:";

cin>>Sno;

cout<<"Enter the name of the Book:";

gets(bname);

cout<<"Enter the name of the Author:";

gets(auth);

void Display()

cout<<"\nSerial No: "<<Sno;

cout<<"\nName of the book: ";

puts(bname);

cout<<"Name of the author: ";

puts(auth);

};

void main()
{

clrscr();

Book B1, B2;

B1.Entry();

B2.Entry();

B1.Display();

B2.Display();

getch();

}
CONSTRUCTOR
#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Emp

int iD;

char Name[25], dsgn[25];

public:

Emp()

cout<<"\nEnter iD No.:";

cin>>iD;

cout<<"Enter Name:";

gets(Name);

cout<<"Enter Designation:";

gets(dsgn);

void Display()

cout<<"\n\niD No.: "<<iD;

cout<<"\nName: ";

puts(Name);

cout<<"Designation: ";

puts(dsgn);

};

void main()
{

clrscr();

Emp E[2];

for(int i=0; i<2; i++)

E[i].Display();

getch();

}
CONTRUCTOR 2
#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class Employee

int iD;

char name[25], dsgn[25];

public:

Employee(int id, char n[], char d[])

iD=id;

strcpy(name, n);

strcpy(dsgn, d);

void Disp()

cout<<"\nThe Details of the Employee are: ";

cout<<"\n\nID No. is: "<<iD;

cout<<"\nName of the Employee: ";

puts(name);

cout<<"Designation of the Employee: ";

puts(dsgn);

};

void main()

{
clrscr();

Employee E1(487, "Aarun", "Senior Manager"), E2(625, "Rajesh", "General Manager");

E1.Disp();

E2.Disp();

getch();

}
DESTRUCTOR
#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

class Marks

{ float mrks;

int roll, cls;

char name[25];

public:

Marks(float m, int r, int c, char n[])

{ 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()

{ cout<<"\n\nName of the Student: ";

puts(name);

cout<<"Roll No.: "<<roll;

cout<<"\nClass is: "<<cls;

cout<<"\nMARKS of the Student is: "<<mrks;

}
~Marks()

{ cout<<"\n\nEnd of Program!!";

};

void main()

{ clrscr();

Marks M1(95.8, 14, 12, "Gaurav");

cout<<"The values stored in M1:";

M1.Disp();

Marks M2(M1);

cout<<"\n\nThe values stored in M2:";

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()

{ cout << "Rollno : " ; cin >> rollno ;

cout << "Name : " ; cin >> name ;

cout << "Class : " ; cin >> Class ;

cout << "Marks : " ; cin >> marks ;

if(marks >= 75) grade = 'A' ;

else if(marks >= 60) grade = 'B';

else if(marks >= 50) grade = 'C';

else if(marks >= 40) grade = 'D';

else grade = 'F' ;

void putdata()

cout <<"Rollno" << rollno << "\tName :" << name ;

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) ;

int rno ; char found = 'f' ;char confirm = 'n' ;

cout << "Enter rollno of student whose record is to be deleted \n" ;

cin >> rno ;

while(!fio.eof())

fio.read((char*)&s1,sizeof(s1));

if(s1.getrno() == rno )

{ s1.putdata() ;

found = 't' ;

cout <<"Are you sure, you want to delete this record?(y/n)" ;

cin >> confirm ;

if(confirm == 'n')

file.write((char*)&s1,sizeof(s1));

else

file.write((char*)&s1,sizeof(s1));

if(found == 'f')

cout << "Record not found!!\n" ;

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)

for(int i=0; i<10; i++)

for(int j=0; j<=(10-i); j++)

cout<<" ";

for(j=0; j<=i; j++)

cout<<n<<" ";

cout<<"\n";

void frmPtr(char m)

for(int i=0; i<10; i++)

for(int j=0; j<=(10-i); j++)

cout<<" ";

for(j=0; j<=i; j++)

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";

cout<<"\n\n Enter your choice(1/2):";

cin>>ch;

if(ch==1)

cout<<"\nEnter the specific Alphabet:";

cin>>z;

frmPtr(z);

else if(ch==2)

cout<<"\nEnter the specific Number:";

cin>>x;

frmPtr(x);

getch();

}
INSERTION SORT
#include<iostream.h>

#include<limits.h>

#include<conio.h>

void InSort(int [] , int ) ;

void main()

int A[101] , N ;

cout << "How many elements do you want to create array with ?(max 100 )";

cin >> N ;

cout << "Enter Array elements... " ;

for(int i = 1 ; i <= N ;i++)

cin >> A[i] ;

InSort(A,N) ;

cout << "\n\nThe Sorted Array is as shown below...\n" ;

for(i = 1 ; i <= N ; i++)

cout << A[i] << " " ;

cout << endl ;

getch() ;

void InSort(int B[] , int size )

int temp ,j ;

B[0] = INT_MIN ;

for(int i = 0 ; i <= size ; i++)

{ temp = B[i] ;

j=i-1;

while( temp < B[j] )

{ B[j +1] = B[j] ;

j--;
}

B[j+1] = temp ;

cout << "Array after pass -" << i << "-is :" ;

for(int k = 1; k <= size; k++)

cout<<B[k]<< " " ;

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()

{ cout << "Rollno : " ; cin >> rollno ;

cout << "Name : " ; cin >> name ;

cout << "Class : " ; cin >> Class ;

cout << "Marks : " ; cin >> marks ;

if(marks >= 75) grade = 'A' ;

else if(marks >= 60) grade = 'B';

else if(marks >= 50) grade = 'C';

else if(marks >= 40) grade = 'D';

else grade = 'F' ;}

void putdata()

{cout <<"Rollno" << rollno << "\tName :" << name ;

cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ;}

int getrno()

{ return rollno ;}

void modify() ;

} s1,stud ;
void stu::modify()

{cout <<"Rollno" << rollno << endl ;

cout << "\tName :" << name << "\tClass : " << Class

<< "\n Marks : " << marks << "\t Grade :" << grade << endl ;

cout << " Enter new details. " << endl ;

char nm[20] = " " , Cl[4] = " " ;

float mks ;

cout << " New Name :(Enter '.' to retain old one)" ;

cin >> nm ;

cout << " New Class :(Enter '.' to retain old one)" ;

cin >> Cl ;

cout << " New Marks :(Enter -1 to retain old one)" ;

cin >> mks ;

if(strcmp(nm,".")!= 0)

strcpy(name,nm);

if(strcmp(Cl,".")!= 0)

strcpy(Class,Cl);

if(mks != -1)

{ marks = mks ;

if(marks >= 75) grade = 'A' ;

else if(marks >= 60) grade = 'B';

else if(marks >= 50) grade = 'C';

else if(marks >= 40) grade = 'D';

else grade = 'F' ;}}

void main()

{clrscr();

fstream fio("stu.dat" , ios::in |ios::out |ios::binary);

int rno;long pos ; char found = 'f';

cout << "Enter rollno of student whose record is to be modified\n" ;

cin >> rno ;

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')

{ cout << "Record not found!!\n" ;getch();exit(0);}

fio.seekg(0) ;

cout << "Now the file contains\n" ;

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() ;

cout << "This program will dispaly the number of 'this'" ;

cout << "and 'these' in file\n" ;

ifstream fin ;

fin.open("ARTICLE.txt",ios::in);

char word[50] ;

int c1=0 ,c2 = 0;

while(!fin.eof())

fin >> word ;

if(strcmp(word,"this") == 0)

c1 ++;

if(strcmp(word,"these") == 0)

c2++;

cout << "Count of \'this\' in file: " << c1 ;

cout << "\nCount of \'these\' in file: " << c2 ;

fin.close() ;

getch() ;

}
SELECTION SORT
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int A[10], tsm, pos, tmp;

cout<<"Enter A:\n";

for(int i=0; i<10; i++)

cout<<"A["<<i+1<<"]: ";

cin>>A[i];

for(i=0; i<10; i++)

tsm=A[i];

pos=i;

for(int j=i; j<10; j++)

if(A[j]<tsm)

tsm=A[j];

pos=j;

tmp=A[i];

A[i]=A[pos];

A[pos]=tmp;

cout<<"\nA after loop "<<i+1<<": ";


for(j=0;j<10;j++)

cout<<A[i]<<" ";

cout<<"\nSorted A is:\n";

for(i=0; i<10; i++)

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()

{ cout << "Rollno : " ; cin >> rollno ;

cout << "Name : " ; cin >> name ;

cout << "Class : " ; cin >> Class ;

cout << "Marks : " ; cin >> marks ;

if(marks >= 75) grade = 'A' ;

else if(marks >= 60) grade = 'B';

else if(marks >= 50) grade = 'C';

else if(marks >= 40) grade = 'D';

else grade = 'F' ;

void putdata()

{ cout <<"Rollno" << rollno << "\tName :" << name ;

cout << "\n Marks : " << marks << "\t Grade :" << grade << endl ; }

int getrno()

{ return rollno ;}

} s1 ;

void main()

{
int rn ;

char found = 'n';

ifstream fi("stu.dat" , ios::in) ;

cout << "Enter rollno to be searched for:" ;

cin >> rn ;

while(!fi.eof())

{ fi.read((char*)&s1,sizeof (s1));

if(s1.getrno() == rn)

{ s1.putdata() ;

found = 'y' ;

break ;

if(found =='n')

cout << "Rollno not found in file!!" ;

fi.close();

getch() ;

}
RANDOM
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<math.h>

long num[3], res;

void main()

clrscr();

void develop();

void check();

int ch;

long choice;

develop();

cout<<"\t\tLet's Play the LG";

cout<<"\n\nSelect from the following random numbers:";

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

cout<<"\nDoesn't Work Like that!!";

check();
cout<<"The Lucky Number is: "<<res;

if(res==choice)

cout<<"\nLucky af!!";

else

cout<<"\nBetter luck next time!!";

getch();

void develop()

{ randomize();

for(int j=0; j<3; j++)

{ num[j]=0;

for(int i=0; i<4; i++)

{num[j]+=(random(10) * pow(10,i));}}return;}

void check()

{randomize();

int x;

x=random(3);

res=num[x];

return;

Vous aimerez peut-être aussi