Monday, August 28, 2006

CPL TW3

TW#3
// Adjust the number of students by replacing 3 by any value u want
#include
#include
#include

class STUDENT
{
private :
int rollno,m1,m2,m3,ttl;
char gr;
char name[31];
public :
void getdata();
void dispdata();
friend void list(STUDENT*);
};
void STUDENT :: getdata()
{
cout << "ENTER DATA IN SEQUENCE OF ROLLNO,NAME,MARKS IN 3 SUBJECTS :";
cin >> rollno >> name >> m1 >> m2 >> m3;
ttl=m1+m2+m3;
if(ttl>=180)
{
gr='A';
}
if(ttl<180 && (ttl>=150))
{
gr='B';
}
if(ttl<150 && (ttl>=105))
{
gr='C';
}
if(ttl<105)
{
gr='D';
}
}
void STUDENT :: dispdata()
{
cout << "DATA :";
cout << rollno << " " << name << " " << m1 << " " << m2 << " " << m3 << " " << ttl << " " << gr << endl;
}
void list(STUDENT s[])
{
int n=3;
int i,j,pass,swtch=1;
STUDENT temp;
for(pass=1;pass {
for(i=0;i { swtch=0;
if(s[i].ttl {
temp=s[i];
s[i]=s[i+1];
s[i+1]=temp;
swtch=1;
}
}

}

}
void main()
{
STUDENT s1;
STUDENT s[3];
int i;

ofstream f2 ("trial.bf",ios::binary);
if(!f2)
{
cout << "Can not open the file";
getch();
};

for(i=0;i<3;i++)
{
s[i].getdata();
s[i].dispdata();
}
list(s);
for(i=0;i<3;i++)
{
f2.write((char *) &s[i],sizeof(STUDENT));
}
cout << "File is created" << endl;
f2.close();

ifstream f1 ("trial.bf");
if(!f1)
{
cout <<"can not open the file";
}
f1.read((char*) &s,sizeof(s));
cout << "Reading is complete:";
cout << endl << "WELCOME";
cout << endl << "LIST IS" << endl;
list(s);
for(i=0;i<3;i++)
{
s[i].dispdata();
}
getch();
}
/*
********OUTPUT***********
ENTER DATA IN SEQUENCE OF ROLLNO,NAME,MARKS IN 3 SUBJECTS :126
FRANK
78
87
60
DATA :126 FRANK 78 87 60 225 A
ENTER DATA IN SEQUENCE OF ROLLNO,NAME,MARKS IN 3 SUBJECTS :120
RON
56
54
50
DATA :120 RON 56 54 50 160 B
ENTER DATA IN SEQUENCE OF ROLLNO,NAME,MARKS IN 3 SUBJECTS :134
RIA
80
89
90
DATA :134 RIA 80 89 90 259 A
File is created
Reading is complete:
WELCOME
LIST IS
DATA :134 RIA 80 89 90 259 A
DATA :126 FRANK 78 87 60 225 A
DATA :120 RON 56 54 50 160 B
*/
The BINARY FILE CREATED—TRIAL.BF

† P Y Z ARIA ¬ À“0 z òÿ
7z Ä ¬ …#~ N W < á AFRANK À“0 | òÿ
7| Æ ® …#x 8 6 2 BRON ž
D z
Ä ¬ Ä ¸ä •

No comments: