Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ThiSinh.

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

10/18/2010 2:10 PM

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <fstream.h>
class ThiSinh {
char
sobd[9];
char* hoten;
int
manganh;
int
khuvuc;
float
diem[3];
public:
ThiSinh() {
strcpy(sobd,"TCTA");
hoten = new char[50];
manganh=khuvuc=0;
diem[0]=diem[1]=diem[2]=-1;
}
ThiSinh(const ThiSinh& t) {
strcpy(sobd, t.sobd);
hoten = new char[50]; strcpy(hoten, t.hoten);
manganh = t.manganh; khuvuc = t.khuvuc;
diem[0]=t.diem[0];diem[1]=t.diem[1];diem[2]=t.diem[2];
}
~ThiSinh() {
delete[] hoten;
}
ThiSinh& operator=(const ThiSinh& t) {
delete[] hoten;
strcpy(sobd, t.sobd);
hoten = new char[50]; strcpy(hoten, t.hoten);
manganh = t.manganh; khuvuc = t.khuvuc;
diem[0]=t.diem[0];diem[1]=t.diem[1];diem[2]=t.diem[2];
return *this;
}
void NhapTT() {
cout<<" Nhap so bao danh: "; gets(sobd);
Page 1 of 5

ThiSinh.cpp
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

10/18/2010 2:10 PM
cout<<" Nhap ho ten: "; gets(hoten);
cout<<" Nhap ma nganh: "; cin>>manganh;
cout<<" Nhap khu vuc: "; cin>>khuvuc;

}
void HienThi() {
cout<<"Thi sinh "<<hoten<<" (SBD "<<sobd
<<"), ma nganh "<<manganh<<", khu vuc "<<khuvuc
<<", diem thi "<<diem[0]<<" "<<diem[1]
<<" "<<diem[2]<<endl;
}
float TongDiem() {
return (diem[0]+diem[1]+diem[2]);
}
void NhapDiem();
friend ostream& operator << (ostream& os, ThiSinh t) {
os<<t.sobd<<endl;
os<<t.hoten<<endl;
os<<t.manganh<<endl;
os<<t.khuvuc<<endl;
os<<t.diem[0]<<endl;
os<<t.diem[1]<<endl;
os<<t.diem[2]<<endl;
return os;
}
friend istream& operator >> (istream& is, ThiSinh& t) {
is>>t.sobd;
is.ignore();
// Bo qua ky tu xuong dong
is.getline(t.hoten, 49);
is>>t.manganh;
is>>t.khuvuc;
is>>t.diem[0];
is>>t.diem[1];
is>>t.diem[2];
return is;
}
int BiDiemLiet() {
Page 2 of 5

ThiSinh.cpp
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

10/18/2010 2:10 PM
return (diem[0]==0 || diem[1]==0 || diem[2]==0);

}
int TrungTuyen(float diemchuan, int nganh);
};
void ThiSinh::NhapDiem() {
cout<<hoten<<"(SBD "<<sobd<<")"<<endl;
for(int i=0;i<3;i++) {
cout<<"Nhap diem mon thu " << (i+1)<<" : ";
cin>>diem[i];
float phanle = diem[i] - (int)diem[i];
if(diem[i]<0 || diem[i]>10 || (phanle!=0 && phanle!=0.5)) {
cout<<"Nhap sai - Nhap lai"<<endl;
i--;
}
}
}
int ThiSinh::TrungTuyen(float diemchuan, int nganh) {
if(BiDiemLiet()) return 0;
if(manganh==nganh)
if( TongDiem() >= (diemchuan - (khuvuc-1)*0.5) )
return 1;
return 0;
}
void main() {
cout<<"Nhap so luong thi sinh: ";
int n; cin>>n;
ThiSinh *ds = new ThiSinh[n];
for(int i=0;i<n;i++) {
cout<<"Thi sinh thu "<<(i+1)<<endl;
ds[i].NhapTT();
}
cout<<"Nhap diem thi cho cac thi sinh"<<endl;
for( i=0;i<n;i++) {
Page 3 of 5

ThiSinh.cpp
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

10/18/2010 2:10 PM

cout<<(i+1)<<". ";
ds[i].NhapDiem();
}
int count=0;
cout<<"DS thi sinh bi diem liet"<<endl;
for( i=0;i<n;i++)
if(ds[i].BiDiemLiet()) {
cout<<++count<<". ";
ds[i].HienThi();
}
if(count==0)
cout<<"
Khong co TS nao bi diem liet"<<endl;
count=0;
cout<<"DS trung tuyen nganh 108, diem chuan 18"<<endl;
for( i=0;i<n;i++)
if(ds[i].TrungTuyen(18,108)) {
cout<<++count<<". ";
ds[i].HienThi();
}
if(count==0)
cout<<" Khong co TS nao trung tuyen nganh 108"<<endl;
///////////////////// PHAN GHI FILE ///////////////////////////////
ofstream f1("TSDH.txt");
f1<<n<<endl;
// Luu dong dau tien cua file la` so luong TS
for(i=0;i<n;i++)
f1<<ds[i];
// Luu tung doi tuong vao tiep theo trong file
f1.close();
delete[] ds;
///////////////// PHAN DOC NOI DUNG FILE ////////////////
ifstream f2("TSDH.txt");
int n1=0;
f2>>n1; // Doc so luong thi sinh vao bien n1
ThiSinh* ds1 = new ThiSinh[n1];
for( i=0;i<n1;i++) {
Page 4 of 5

ThiSinh.cpp
145
146
147
148
149
150
151
152
153
154
155
156 }

f2.ignore();
f2>>ds1[i];

10/18/2010 2:10 PM
// Bo qua ky tu \n co`n so't lai khi doc 1 so

}
f2.close();
cout<<endl<<"Danh sach thi sinh doc tu file"<<endl;
for( i=0;i<n1;i++) {
cout<<(i+1)<<". ";
ds1[i].HienThi();
}
delete[] ds1;

Page 5 of 5

You might also like