/* Slip no: 17_1 Write a C++
program to create a class Student which contains data members as Roll_Number,
Stud_Name, Percentage. Write member functions to accept Student
information. Display all details of student along with a class
obtained depending on percentage. (Use array of objects) */
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class student
{
char
name[10];
float
per;
int
rno;
public:
void
accept()
{
cout<<"\nEnter
rno : ";
cin>>rno;
cout<<"\nEnter
name : ";
cin>>name;
cout<<"\nEnter
percentage : ";
cin>>per;
}
void
display()
{
cout<<"\nrno
= "<<rno;
cout<<"\nName
= "<<name;
cout<<"\nPercantage
= "<<per<<"%";
if(per>=70&&per<=100)
cout<<"\nDistinction";
else
if(per<70&&per>=60)
cout<<"\n1st
class";
else
if(per<60&&per>=50)
cout<<"\n2nd
class";
else
if(per<50&&per>40)
cout<<"\npass
class";
else
cout<<"\nfail";
cout<<"\n=================================";
}
};
void main()
{
int
n,i;
student
ob[10];
clrscr();
cout<<"enter
no of students";
cin>>n;
for(i=0;i<n;i++)
{
ob[i].accept();
}
for(i=0;i<n;i++)
{
ob[i].display();
}
getch();
}
/* Slip no : 17_2 Create a class
Distance which contains data members as: kilometer, meter. Write C++ program to
perform following functions:
To accept a distance
To display a distance
To overload += operator to add two
distances.
To overload > operator to
compare two
distances */
#include<iostream.h>
#include<conio.h>
class distance
{
public:
int km,m;
void getdata()
{
cout<<"enter distance in
kilometer\t";
cin>>km;
cout<<"enter distance in
meters\t";
cin>>m;
}
void display()
{
cout<<"entered distance
is\t";
cout<<km<<"."<<m;
cout<<endl;
}
void operator+=(distance d)
{
int kilometer=0,meter=0;
kilometer=km+d.km;
meter=m+d.m;
if(meter>=1000)
{
kilometer=km+d.km+(meter/1000);
meter=meter%1000;
}
cout<<"\naddition of two
distances is\t"<<kilometer<<"."<<meter;
}
void operator>(distance d)
{
if(km>d.km)
{
cout<<"\ngreater distance
is\t"<<km<<"."<<m;
}
else
{
cout<<"\ngreater
distance is\t"<<d.km<<"."<<d.m;
}
}
};
void main()
{
clrscr();
distance
d,d1;
d.getdata();
d1.getdata();
d.display();
d1.display();
d+=d1;
d>d1;
getch();
}
/* Slip no : 18_2 Write a C++
program to read the contents of a text file. Count and display number of
characters, words and lines from a file. Find the number of occurrences of a
given word present in a file. */
#include <iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
main()
{
char
character,file[20],word[20];
ifstream
in;
int
charcnt,wcnt,lcnt; //clrscr();
cout<<"\n
Enter file name : ";
cin>>file;
in.open(file);
if(in.fail())
{
cout<<"\n
file does not exists :"<<endl;
exit(1);
}
charcnt
= 0;
lcnt
= 0;
wcnt
= 0;
char
ch;
while(!in.eof())
{
ch=in.get();
if(ch
== '\n')
{
lcnt++; wcnt++;
}
else
if(ch==' ' ||ch=='\t' )
{wcnt++;
charcnt++;
}
else
charcnt++;
}
cout
<< "Characters: " << charcnt << endl;
cout
<< "Lines: " << lcnt << endl;
cout
<< "Words: " << wcnt << endl;
in.close();
cout<<"\n
Enter word yo be search : ";
cin>>word;
in.open(file);
char
str[20];int f=0;
while(!in.eof())
{
in>>str;
if(strcmp(str,word)==0)
{
f++;
}
}
if(f!=0)
{cout<<"\n
word found : "<<f;}
else
cout<<"\n NOT found";
getch();
}
slip 17
Reviewed by Dinesh Varal
on
January 19, 2018
Rating:
No comments: