Sie sind auf Seite 1von 29

COMPUTER

SCIENCE
PRACTICAL FILE

Submitted by: Ayush Sharma


Class: 12 D
Roll No: 13
Session:
School: Sardar Patel Vidyalaya
INDEX
S. No. Name of Program Page Date Teacher’s Sign
No.
1. Write a function that takes time as three
integer arguments (hours,minutes,seconds)
..
2. Write a menu driven program to calculate
the area of a circle, square and rectangle
using function overloading.

3. Define a class student with the following


specifications…

4. Write a program to define a class Garments in


C++ with the following descriptive…
5. Imagine a publishing company that markets both
books and audio-cassette versions of…
6. Write a program to demonstrate the order in
which base class & derived class constructors are
called
7. Write a function in C++ to print the count of
“the” word as an independent word in a text file
8. Write a program to Copy one file onto the end of
another, adding line numbers
9. Write a menu driven telephone directory program
(Binary File handling) with the following
options..
Program 1 :

1. Write a function that takes time as three integer arguments


(hours,minutes,seconds),and returns the number of seconds
since the clock last struck "12". Use this function to write a
program to calculate the amount of time in seconds between two
times, both of which are within one 12 hour cycle of clock.

#include <iostream>

using std::cout;

using std::cin;

int passedSeconds(int, int, int);

int difference(int, int, int);

int main()

int hour1, minute1, second1;

int hour2, minute2, second2;

cout << "1. Enter hours, minutes and seconds (range 00:00:00 -
23:59:59): ";

cin >> hour1 >> minute1 >> second1;

while(hour1 > 23 || minute1 > 59 || second1 > 59)

{
if(hour1 < 23)

cout << "\nEnter correct hours value!";

if(minute1 < 59)

cout << "\nEnter correct minutes value!";

if(second1 < 59)

cout << "\nEnter correct seconds value!";

cout << "\n1. Enter hours, minutes and seconds: ";

cin >> hour1 >> minute1 >> second1;

cout << "Passed seconds from last 12 (am or pm): " <<
passedSeconds(hour1, minute1,second1);

cout << "\n2. Enter hours, minutes and seconds (range 00:00:00 -
23:59:59): ";

cin >> hour2 >> minute2 >> second2;

while(hour2 > 23 || minute2 > 59 || second2 > 59)


{

if(hour2 < 23)

cout << "\nEnter correct hours value!";

if(minute2 < 59)

cout << "\nEnter correct minutes value!";

if(second2 < 59)

cout << "\nEnter correct seconds value!";

cout << "\n2. Enter hours, minutes and seconds: ";

cin >> hour2 >> minute2 >> second2;

cout << "\nDifference between two times is "

<< difference(hour2, minute2, second2) - difference(hour1,


minute1, second1)

<< " seconds.";

return 0;

}
int passedSeconds(int hour, int minute, int second)

if(hour >= 12)

hour -= 12;

return hour * 60 * 60 + minute * 60 + second;

int difference(int hour, int minute, int second)

return hour * 60 * 60 + minute * 60 + second;

}
1. Enter hours, minutes and seconds (range 00:00:00 - 23:59:59): 2
3
3
Passed seconds from last 12 (am or pm): 7383
2. Enter hours, minutes and seconds (range 00:00:00 - 23:59:59): 5
6
7

Difference between two times is 10984 seconds.Program ended with


exit code: 0
Program 2:

Write a menu driven program to calculate the area of a circle, triangle


and rectangle using function overloading.

#include<iostream>
#include<cstdlib>
using namespace std;

float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(int p, int k)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch,k,p;

do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle : ";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Breadth of Rectangle
: ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(p,k);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}while(ch!=4);
return 0;
}
*****Menu******

1. Area of circle

2. Area of Triangle

3. Area of rectangle

4. Exit

Enter your choice :1

Enter radius of circle :6

Area of circle :113.04

1. Area of circle

2. Area of Triangle

3. Area of rectangle

4. Exit

Enter your choice :2

Enter base and height of triangle :6 5

Area of circle :15

1. Area of circle

2. Area of Triangle

3. Area of rectangle

4. Exit

Enter your choice :3

Enter length and breadth of rectangle :6 5

Area of circle :30


Program 3 :

1. Define a class student with the following specifications:


Private members of the class:
Admission Number - An Integer
Name - string of 20 characters
Class - Integer
Roll Number - Integer
Public members of the class:
getdata() - To input the data
showdata() - To display the data
Write a program to define an array of 10 objects of this class, input
the data in this array and then display this list.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int addno;
char name[21];
int Class;
int rollno;
public:
void getdata()
{
cout<<"Enter Addmission No.:";
cin>>addno;
cout<<"Enter Name:";
gets(name);
cout<<"Enter Class:";
cin>>Class;
cout<<"Enter Roll No.:";
cin>>rollno;
}
void showdata()
{
cout<<"\nAddmission No.:"<<addno;
cout<<"Name:";
puts(name);
cout<<"\nClass:"<<Class;
cout<<"\nRoll No.:"<<rollno;
}
}J[10];

void main()
{
for(int i=0;i<10;i++)
{
cout<<"\nStudent "<<i<<endl;
J[i].getdata();
}

for(i=0;i<10;i++)
{
cout<<"\nStudent "<<i<<endl;
J[i].showdata();
}
getch();
}

PROGRAM 4 :

Define a class clothing in c++ with the following descriptions :

private members :

code of type string


type of type string
size of type intiger
material of type string
price of type float

A function calc_price( )which calculates and assigns the value of


GPrice as follows ; For the value of material as “COTTON” :

Type price (Rs)


TROUSER 1500.
SHIRT 1200.

for material other than “COTTON”, the above mentioned GPprice price
gets reduced by 25%

public members :

 A constructor to assign initial values of code ,type and material


with the word “NOT ASSIGNED “and size and price with 0.
 A function enter() to input the values of the data members code,
type, size and material and invoke the caclPrice () function.
 A function show which displays the content of all the data
members for a clothing.

PTO
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
class clothing
{ char Code[21],Type[21];
int size;
char material[21];
float price;
void calc_price( )
{
if(strcmp(strupr(material),"COTTON")==0)
{ if(strcmp(strupr(Type),"TROUSER")==0)
price=1500;
if(strcmp(strupr(Type),"SHIRT")==0)
price=1200;
}
else
{ if(strcmp(strupr(Type),"TROUSER")==0)
price=1500*0.75;
if(strcmp(strupr(Type),"SHIRT")==0)
price=1200*0.75;
}
}
public:
clothing( )
{ strcpy(Code,"NOT ALLOTED");
strcpy(Type,"NOT ALLOTED");
size=0;
strcpy(material,"NOT ALLOTED");
price=0;
}
void enter( )
{ cout<<"\nEnter the Cloth Code: ";
gets(Code);
cout<<"\nEnter the Cloth Type: ";
gets(Type);
cout<<"\nEnter the Cloth Size: ";
cin>>size;
cout<<"\nEnter the cloth material:";
gets(material);
calc_price( );
}
void show( )
{ cout<<"\nThe Cloth Code: "<<Code;
cout<<"\nThe Cloth Type: "<<Type;cout<<"\nThe Cloth Size:
"<<size;
cout<<"\nThe Cloth Material: “ <<material;
cout<<"\nThe Cloth Price: "<<price; } };
void main( )
{ clothing C;
C.enter( );
C.show( );
}
Program 5:

Imagine a publishing company that markets both books and


audio-cassette versions of its work. Create a class Publication
that stores the title(a string) and price (type float) of a publication.
From this class derive two classes: book, which adds a page
count (type int) and, tape which adds a playing time in minutes
(type float).
Each of these three classes should have a getdata( ) function to
get its data from the user and a putdata( ) function to display its
data. Write a main( ) program to test the book and tape classes by
creating instances of them asking the user to fill in their data with
getdata( ) and then displaying the data with putdata( ).

#include<iostream>
#include<string>
using namespace std;

class Publication
{
private:
string title;
float price;
public:
void getData()
{
cout<<“\nEnter Title: ”; cin>>title;
cout<<“Enter Price: $”; cin>>price;
}

void putData()
{ cout<<“\nTitle: ”<<title<<“\nPrice: ”<<price; }
};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Book : public Publication


{
private:
int pages;
public:
void getData()
{ Publication::getData(); cout<<“Enter Pages: ”; cin>>pages; }

void putData()
{ Publication::putData(); cout<<“\nPages: ”<<pages; }
};

//\////\////\////\////\////\////\////\////\////\////\////\//
//\////\////\////\////\////\////\////\////\////\////\////\//

class Tape : public Publication


{
private:
float minutes;
public:
void getData()
{ Publication::getData(); cout<<“Enter Minutes: ”; cin>>minutes; }

void putData()
{ Publication::putData(); cout<<“\nMinutes: ”<<minutes; }
};

int main()
{
Book b; Tape t;
b.getData();
t.getData();

cout<<“\a”<<endl; // ’\a'produces the beep sound


b.putData();
cout<<endl;
t.putData();
cout<<endl;

return 0;
}
Program 6:

Write a program to demonstrate the order in which base class &


derived class constructors are called

// C++ program to show the order of constructor calls


// in Multiple Inheritance

#include <iostream>
using namespace std;

// first base class


class Parent1
{

public:

// first base class's Constructor


Parent1()
{
cout << "Inside first base class" << endl;
}
};

// second base class


class Parent2
{
public:

// second base class's Constructor


Parent2()
{
cout << "Inside second base class" << endl;
}
};

// child class inherits Parent1 and Parent2


class Child : public Parent1, public Parent2
{
public:

// child class's Constructor


Child()
{
cout << "Inside child class" << endl;
}
};

// main function
int main() {

// creating object of class Child


Child obj1;
return 0;
}
Output:
Inside first base class
Inside second base class
Inside child class
Program 7:

1. Write a function in C++ to print the count of “the” word as an


independent word in a text file STORY.TXT. [Note: count “the” ,
“THE” , “The” ]

#include<iostream.h>
#include<fstream.h>
#include<string.h>

int main()
{
ifstream fin("STORY.txt"); //opening text file
int count=0;
char ch[20],c[20],b[20],q[20];
c[20]= ‘The’;
b[20]=’THE’;
q[20]=’the’;

while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0||strcmp(ch,b)==0||strcmp(ch,a)==0)
count++;
}

cout<<"Occurrence="<<count<<"n";
fin.close(); //closing file

return 0;

}
Program 8 :

Write a program to input a text file name, read the contents of the
file and create a new file named COPY.TXT, which shall contain
only those words from the original file which don’t start with an
uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For example, if the
original file contains

The First Step To Getting The Things You Want Out Of Life
is This: Decide What You Want. - Ben Stein

Then the text file COPY.TXT shall contain

The First Step To Getting The Things You Want Life is This:
Decide What You Want. - Ben Stein

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
FILE *input;
FILE *output;

input=fopen("input.txt","r");
output=fopen("Copy.Txt","w");

char s[100];

while(!feof(input)) {
fscanf(input,"%s",s);
if(!(s[0]=='A'||s[0]=='E'||s[0]=='I'||s[0]=='O'||s[0]=='U')) {
fprintf(output,"%s ",s);
}
}

fclose(input);
fclose(output);

return 0;
Program 9:

Write a program to Copy one file onto the end of another, adding line
numbers

#include <stdio.h>
#include <stdlib.h> // For exit()

int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;

printf("Enter the filename to open for reading \n");


scanf("%s", filename);

// Open one file for reading


fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

printf("Enter the filename to open for writing \n");


scanf("%s", filename);

// Open another file for writing


fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}

// Read contents from file


c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}

printf("\nContents copied to %s", filename);

fclose(fptr1);
fclose(fptr2);
return 0;
}

Output:

Enter the filename to open for reading


a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt
Program 10 :

Write a menu driven telephone directory program (Binary File


handling) with the following options:
a) Add New Record
b) Display All Records
c) Search Telephone Number
d) Search Name
e) Update Telephone Number
f) Delete a record

{Hint: Take a class PhoneBook with the following data members:


name and phone number, both stored as character arrays}

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>

class phoneBook{
char name[20],phno[6];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};
void phoneBook :: getdata(){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno;
}

void phoneBook :: showdata(){


cout<<"\n";
cout<<setw(15)<<name;
cout<<setw(8)<<phno;
}

void main(){
phoneBook rec;
fstream file;
file.open("c:\\phone.dat", ios::ate | ios::in | ios::out | ios::binary);
char ch,nm[20],telno[6];
int choice,found=0;
while(1){
clrscr();
cout<<"\n*****Phone Book*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone No.\n";
cout<<"4) Search Person Name\n";
cout<<"5) Update Telephone No.\n";
cout<<"6) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice){
case 1 : //New Record
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec));
break;

case 2 : //Display All Records


file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n";
while(file){
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear();
getch();
break;

case 3 : //Search Tel. no. when person name is known.


cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;

case 4 : //Search name on basis of tel. no


cout<<"\n\nEnter Telephone No : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(telno,rec.getphno())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;

case 5 : //Update Telephone No.


cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();

cout<<"Enter New Telephone No : ";


cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
}
break;
case 6 : gotoout;
}
}
out:
file.close();
}

Program 11 :

Write a program using pointers to find the length of a string and print
reversed string

#include <stdio.h>
#include <string.h>

// Function to reverse the string


// using pointers
void reverseString(char* str)
{
int l, i;
char *begin_ptr, *end_ptr, ch;

// Get the length of the string


l = strlen(str);

// Set the begin_ptr and end_ptr


// initially to start of string
begin_ptr = str;
end_ptr = str;

// Move the end_ptr to the last character


for (i = 0; i < l - 1; i++)
end_ptr++;

// Swap the char from start and end


// index using begin_ptr and end_ptr
for (i = 0; i < l / 2; i++) {

// swap character
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;

// update pointers positions


begin_ptr++;
end_ptr--;
}
}

// Driver code
int main()
{

// Get the string


char str[100] = "GeeksForGeeks";
printf("Enter a string: %s\n", str);

// Reverse the string


reverseString(str);

// Print the result


printf("Reverse of the string: %s\n", str);

return 0;
}

Das könnte Ihnen auch gefallen