Computer Science Program File
Computer Science Program File
SCIENCE
PROGRAM FILE
MADE BY:
SRISHTI RAY__
__
____XII-A___
__________________
ACKNOWLEDGEMENT
INDEX
S.NO. QUESTION T.SIGN.
1 Create a class travel plan and enter travel plans
details by const/dest.
2 Create a class clothing to display garment details by
using const/dest.
3 Create a class Tour and enter tour details and display
by using const/dest.
4 Create a class Play and display play details using
const/dest.
5 Enter a string and store it into file char by char.
6 Count total number of spaces from a file.
7 Enter 3 words, store them into a file and display
contents of the file.
8 Count total number of words starting with ‘a’.
9 Count total number of line starting with ‘A’.
10 Enter 3 lines and display them.
11 Write a program having a class student having
roll no, name and age.Enter data and display
the contents of file using structure.
12 Enter employee code, name and salary, store
it into file and display contents from file by
using struct.
13 Enter 5 elements in an array and display them.
14 Enter 5 elements in an array and display twice of
each element.
15 Enter 5 elements and display even numbers.
16 Enter two arrays of 5 elements each, and display
common values of both the arrays.
17 Enter an array and display it in ascending order.
1. Create a class travel plan and enter travel plans details by const/dest.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
classTravelPlan
longPlanCode;
char Place[25];
intNumber_of_travellers;
intNumber_of_busses;
public:
TravelPlan()
PlanCode=1001;
Number_of_travellers=5;
Number_of_busses=1;
strcpy(Place,"Agra");
voidNewPlan()
cin>>PlanCode;
gets(Place);
cout<<"\nEnter the number of travellers : ";
cin>>Number_of_travellers;
if(Number_of_travellers<20)
Number_of_busses=1;
else if(Number_of_travellers>20&&Number_of_travellers<40)
Number_of_busses=2;
else if(Number_of_travellers>=40)
Number_of_busses=3;
voidShowPlan()
cout<<PlanCode<<"\n";
puts(Place);
~TravelPlan()
{ cout<<"\nValues Destroyed";
};
void main()
{ clrscr();
TravelPlan X;
X.NewPlan();
X.ShowPlan();
getch();
#include<conio.h>
#include<string.h>
#include<stdio.h>
class Clothing
private:
char code[30];
char type[30];
int size;
char material[30];
float price;
void Calc_Price()
if(strcmp(material,"COTTON")==0)
price=1100;
if(strcmp(type,"TROUSER")==0)
price=1500;
else if(strcmp(type,"SHIRT")==0)
price=1200;
public :
Clothing()
strcpy(code,"Not assigned");
strcpy(type,"Not assigned");
size=0;
strcpy(material,"Not assigned");
price=0;
void Enter()
{
cout<<"Enter Details";
gets(code);
gets(type);
cout<<"Enter Size";
cin>>size;
gets(material);
Calc_Price();
void Show()
{ cout<<"Showing Details";
cout<<"Code "<<code;
cout<<"Type "<<type;
cout<<"Size "<<size;
cout<<"Material "<<material;
cout<<"Price"<<price;
~Clothing()
cout<<"Destructor at work";
}
};
void main()
Clothing C;
C.Enter();
C.Show();
getche();
3. Create a class Tour and enter tour details and display by using const/dest.
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iostream.h>
class Tour
{
char TCode[2o];
int NoofAdults,NoofKids,Kilometres;
float TotalFare;
public:
Tour( )
{
strcpy(TCode,"NULL");
NoofAdults=NoofKids=Kilometres=TotalFare=0;
}
void AssignFare( )
{
if(Kilometres>=1000)
TotalFare=NoofAdults*500+NoofKids*250;
else if(Kilometres>=500)
TotalFare=NoofAdults*300+NoofKids*150;
else
TotalFare=NoofAdults*200+NoofKids*100;
}
void EnterTour( )
{
clrscr();
tour T;
T.EnterTour( );
T.ShowTour( );
getche();
}
int Playcode;
char playtitle[25];
float Duration;
int Noofscenes;
public:
Play( )
{
Duration=45;
Noofscenes=5;
}
void Newplay( )
{
Duration = D;
Noofscenes = N;
}
void Showplay( )
{
#include<fstream.h>
#include<stdio.h>
void main()
{
ofstream afile("SALONI.txt");
char str[20];
int i;
cout<<"Enter any string "<<endl;
gets(str);
for(i=0;str[i]!='\0';i++)
{
afile.put(str[i]);
}
afile.close();
}
7.Enter 3 words, store them into a file and display contents of the file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i;
ofstream afile("hello.txt",ios::app);
cout<<"Enter 3 words"<<endl;
for(i=0;i<3;i++)
{
gets(str);
afile<<str<<" ";
}
afile.close();
ifstream bfile("ball.txt",ios::app);
while(bfile)
{
bfile>>str;
cout<<str<<" ";
}
bfile.close();
getche();
}
#include<conio.h>
void main()
char str[80];
int i;
ofstream afile("saloni.txt");
for(i=0;i<3;i++)
gets(str);
afile<<str<<'\n';
}
afile.close();
ifstream bfile("saloni.txt");
while(bfile)
bfile.getline(str,79);
cout<<str<<'\n';
bfile.close();
getche();
11.Write a program having a class student having roll no, name and
age.Enter data and display the contents of file using structure.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
struct student
char name[20];
int age;
}s;
void main()
ofstream afile("stud.dat",ios::binary);
cin>>s.rollno;
cout<<"enter name\t";
gets(s.name);
cout<<"enter age\t";
cin>>s.age;
afile.write((char*)&s,sizeof(s));
afile.close();
ifstream bfile("stud.dat",ios::binary);
while(bfile.read(char*)&s,sizeof(s)))
cout<<s.rollno<<"\t"<<s.name<<"\t"<<s.age<<endl;
}
bfile.close();
getche();
12.Enter employee code, name and salary, store it into file and display
contents from file by using struct.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
struct student
char name[20];
int code;
int salary;
}e;
void main()
ofstream afile("emp.dat",ios::binary);
cout<<"enter code";<<"\t";
cin>>e.code;
cout<<"enter name\t";
gets(e.name);
cout<<"enter salary\t";
cin>>e.salary;
afile.write((char*)&e,sizeof(e));
afile.close();
ifstream bfile("emp.dat",ios::binary);
while(bfile.read(char*)&e,sizeof(e)))
cout<<e.rollno<<"\t"<<e.name<<"\t"<<e.age<<endl;
bfile.close();
getche();
}
13.Enter 5 elements in an array and display them.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, ar[5];
cout<<"Enter 5 elements"<<endl;
for(i=0;i<5;i++)
{
cin>>ar[i];
}
for(i=0;i<5;i++)
{
cout<<ar[i]<<"\t";
}
getche();
}
24. Enter n number of elements and search them by using insertion sort
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,list[20];
void insertion(int,int[]);
cout<<"Enter size of list = ";
cin>>n;
cout<<"Enter list\n";
for(i=0;i<n;i++)
cin>>list[i];
insertion(n,list);
getche();
}
void insertion(int n,int list[20])
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=list[i];
j=i-1;
while(temp<list[j] && (j>=0))
{
list[j+1]=list[j];
j=j-1;
}
list[j+1]=temp;
}
cout<<"The sorted list is\n";
for(i=0;i<n;i++)
{
cout<<list[i]<<endl;
}
}
25. Enter n number of elements and search them by using selection sort
technique.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,n,min,temp,a[20];
void selection(int,int[]);
cout<<"Enter size of list =\n";
cin>>n;
cout<<"Enter list \n";
for(i=0;i<n;i++)
cin>>a[i];
selection(n,a);
getche();
}
void selection(int n,int a[20])
{
int i,k,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(k=i+1;k<n;k++)
{
if(a[min]>a[k])
min=k;
}
if(i!=min)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
cout<<"The sorted list is \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}}
32.Write a function in C++ to count and display the number of lines not
starting with alphabet 'A' present in a binary text file "STORY.TXT".
class ITEMS
{
int ID;
char PRODUCT [20];
float Cost;
public :
void GETDATA()
{
cin>ID;
gets(PRODUCT);
cin>> Cost;
}
void PUTDATA()
{
cout<<ID<<":"<<PRODUCT<<":"<<Cost<<endl;
}
float GetCost()
{
return(cost);
}
};
{
ITEMS I;
ifstream afile("ITEMS.DAT",ios::binary);
while(afile.read((char*)&I,sizeof(I)))
if(I.Getcost()<2500)
I.PUTDATA();
afile.close();
34. Write a definition for function COSTLY() in C++ to read each record of a
binary file GIFTS.DAT, find and display those gifts, which are priced more that
200. Assume that the file GIFTS.DAT is created with the help of objects of class
GIFTS, which is defined below:
class GIFTS
int CODE;
char ITEM[20];
float PRICE;
public:
void Procure()
cin>>CODE;
gets(ITEM);
cin>>PRICE;
}
void View()
cout<<CODE<<":"<<ITEM<<":"<<PRICE<200;
G.View();
afile.close();
GIFTS G;
ifstream afile(“GIFTS.DAT”,ios::binary);
if(G.GetPrice()>2000)
G.View();
afile.close();
Getinfo( ) //A function to enter the content Rno, Name, Charges and
Days
Dispinfo( ) //A function to display Rno, Name, Charges and Days and
amount
(Amount to be
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class RESORT
{
int roomno;
char customername[20];
float charges;
int days;
void COMPUTE
{
float=amount;
amount=days*charges;
if(days*charges>11000)
amount=1.02*days*charges;
return (amount);
}
public:
void getinfo();
void dispinfo();
};
void RESORT::getinfo()
{
cout<<"enter roomno";
cin>>Rno;
cout<<"enter name";
gets(name);
cout<<"enter charges";
cin>>charges;
cout<<"enter days";
cin>>days;
}
void RESORT::dispinfo()
{
cout<<Rno<<"\t"<<name<<"\t"<<charges<<"\t"<<days<<"\t"<<amount;
}
void main()
{
RESORT R;
R.getinfo();
R.dispinfo();
getche();
}
GREEN Vegetarian
RED Non-Vegetarian
Public Members
A function FoodIn( ) to allow user to enter values for Code,
FoodName, Sticker and call function GetType( ) to assign respective
FoodType.
A function FoodOut( ) to allow user to view the content of all the data
members
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class SUPPLY
{
int code;
char Foodname[20];
char sticker[20];
char FoodType[20];
void GetType()
{
if(strsmp(Sticker,"GREEN")==0)
strcpy(FoodType,"Vegetarian");
if(strsmp(Sticker,"YELLOW")==0)
strcpy(FoodType,"Contains egg");
if(strsmp(Sticker,"RED")==0)
strcpy(FoodType,"Non-Vegetarian");
}
public:
void FoodIn();
void FoodOut();
};
void SUPPLY::FoodIn()
{
cout<<"enter values for code ";
cin>>code;
cout<<"enter FoodName";
gets(FoodName);
cout<<"enter sticker";
gets(sticker);
GetType();
}
void SUPPLY::FoodOut()
{
cout<<Code <<"\t"<<FoodName<<"\t"<<Sticker<<"\t"<<FoodType;
}
void main()
{
SUPPLY S;
S.FoodIn();
S.FoodOut();
getche(); }
37. . Define a class in C++ with following description:
Private Members
<=1000 500
Public Members
>=80 A
Less than 50 D
Public Members
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class APPLICANT
{
long Ano;
char name[20];
float Agg;
char grade;
void GradeMe()
{
if (Agg>=80)
grade='A';
if (Agg>=65&&Agg<80)
grade='B';
if (Agg>50&&Agg<65)
grade='C';
if(Agg<50)
grade='D';
}
public:
void ENTER();
void RESULT();
};
void Applicant::ENTER()
{
cout<<"enter Ano";
cin>>Ano;
cout<<"enter name";
gets(name);
cout<<"enter Agg";
cin>>Agg;
GradeMe();
}
void APPLICANT::RESULT()
{
cout<<Ano<<"\t"<<name<<"\t"<<Agg<<"\t"<<grade;
}
void main()
{
APPLICANT A;
A.ENTER();
A.RESULT();
getche();
}
39. Define a class CARRENTAL in C++ with following description:
Car ID of type long int
AboutCar of type string
Cartype of type string
Rent of type float
A member function AssignRent( ) to assign the following values for Rent
as per the givenCartype:
Cartype Rent
Small 1000
Van 800
SUV 2500
Public Members
>=50 Selected
Public Members
(i) A function ENTER( ) to allow user to enter values for RNO, Name,
Score & call function AssignRem( ) to assign the remarks.
(ii) A function DISPLAY( ) to allows user to view the content of all the
data members.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class CANDIDATE
{
long int Rno;
char name[20];
float score;
char Remarks[20];
void AssignRem()
{
if(Score>=50)
strcpy(Remarks,"selected");
else if(Score<50)
strcpy(Remarks,"not selected");
}
public:
void getinfo();
void dispinfo();
};
void CANDIDATE::getinfo()
{
cout<<"enter Rno";
cin>>Rno;
cout<<"enter name";
gets(name);
cout<<"enter score";
cin>>score;
AssignRem();
}
void CANDIDATE::dispinfo()
{
cout<<Rno<<"\t"<<name<<"\t"<<score<<"\t"<<Remarks;
}
void main()
{
CANDIDATE C;
C.getinfo();
C.dispinfo();
getche();
}
41. Enter ‘n’ no. of elements and delete particular element from top.
#include<iostream.h>
#include<conio.h>
Void main()
Cin>>n;
For(i=0;i<n;i++)
Cout<<”enter value”;
Cin>>Ar[i];
For(i=pos;i<n-1;i++)
Ar[i]=Ar[i+1];
n--;
for(i=0;i<n;i++)
Cout<<Ar[i]<<endl;
}
Getche();
42.Enter 5 elements in the list and display number of digit in each element .
#include<iostream.h>
#include<conio.h>
Void main()
Inti , A[10] , k;
For(i=0;i<5;i++)
Cin>>A[i];
}
For(i=0;i<5;i++)
k=0;
while(A[i]>0)
A[i]=A[i]/10;
k++;
Cout<<”no. of digits”<<k<<endl;
Getche();
}
43.Enter ‘n’ no. of elements and insert particular element at desired position.
#include<iostream.h>
#include<conio.h>
Void main()
Cin>>n;
For(i=0;i<5;i++)
Cout<<”enter array”;
Cin>>Ar[i];
Cout<<”enter position”;
Cin>>pos;
Cout<<”enter value”;
Cin>>x;
For(i=n;i>pos;i--)
Ar[i]=Ar[i-1];
Ar[pos]=x;
n++;
for(i=0;i<n;i++)
Cout<<Ar[i]<<endl;
Getche();
}
The End