forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bai6.4F.c
198 lines (170 loc) · 3.91 KB
/
Bai6.4F.c
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
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
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <string.h>
void hienThiTenCot();
struct HoTen {
char ho[20];
char dem[20];
char ten[20];
};
struct DiemMH {
float toan;
float van;
float anh;
float tbc;
};
struct SinhVien {
int ma;
struct HoTen hoVaTen;
int tuoi;
char gioiTinh[10];
struct DiemMH diem;
};
void nhapDiem(struct DiemMH* diem) {
printf("Toan: ");
scanf("%f", &diem->toan);
printf("Van: ");
scanf("%f", &diem->van);
printf("Anh: ");
scanf("%f", &diem->anh);
diem->tbc = (diem->toan + diem->van + diem->anh) / 3;
}
void nhapHoTen(struct HoTen* ten) {
printf("Ho: ");
scanf("%s", ten->ho);
printf("Dem: ");
getchar();
gets(ten->dem);
printf("Ten: ");
scanf("%s", ten->ten);
}
struct SinhVien nhapSV() {
struct SinhVien sv;
printf("Nhap ma: ");
scanf("%d", &sv.ma);
nhapHoTen(&sv.hoVaTen);
printf("Tuoi: ");
scanf("%d", &sv.tuoi);
printf("Gioi tinh: ");
scanf("%s", sv.gioiTinh);
nhapDiem(&sv.diem);
return sv;
}
void hienThiTTSV(struct SinhVien sv) {
printf("%-10d %-10s %-20s %-10s %-10d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",
sv.ma, sv.hoVaTen.ho, sv.hoVaTen.dem, sv.hoVaTen.ten, sv.tuoi, sv.gioiTinh,
sv.diem.toan, sv.diem.van, sv.diem.anh, sv.diem.tbc);
}
void sapXepTheoTen(struct SinhVien* ds, int slsv) {
int i, j;
for(i = 0; i < slsv - 1; i++) {
for(j = slsv - 1; j > i; j --) {
if(strcmp(ds[j].hoVaTen.ten, ds[j-1].hoVaTen.ten) < 0) {
struct SinhVien sv = ds[j];
ds[j] = ds[j - 1];
ds[j - 1] = sv;
}
}
}
}
void sapXepTheoDiem(struct SinhVien* ds, int slsv) {
int i, j;
for(i = 0; i < slsv - 1; i++) {
for(j = slsv - 1; j > i; j --) {
if(ds[j].diem.tbc > ds[j - 1].diem.tbc) {
struct SinhVien sv = ds[j];
ds[j] = ds[j - 1];
ds[j - 1] = sv;
}
}
}
}
void timTheoTen(struct SinhVien* ds, int slsv) {
char ten[20];
printf("Nhap ten: ");
scanf("%s", ten);
hienThiTenCot();
int i, timSV = 0;
for(i = 0; i < slsv; i++) {
if(strcmp(ten, ds[i].hoVaTen.ten) == 0) {
hienThiTTSV(ds[i]);
timSV = 1;
}
}
if(timSV == 0) {
printf("Khong co sinh vien %s trong danh sach!\n", ten);
}
}
void ghiFile(struct SinhVien* ds, int slsv) {
FILE* fOut = fopen("SV.txt", "a");
int i;
for(i = 0; i < slsv; i++) {
struct SinhVien sv = ds[i];
fprintf(fOut, "%-10d %-10s %-20s %-10s %-10d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",
sv.ma, sv.hoVaTen.ho, sv.hoVaTen.dem, sv.hoVaTen.ten, sv.tuoi, sv.gioiTinh,
sv.diem.toan, sv.diem.van, sv.diem.anh, sv.diem.tbc);
}
fclose(fOut);
}
void hienThiTenCot() {
printf("%-10s %-10s %-20s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n",
"Ma SV", "Ho", "Dem", "Ten", "Tuoi", "Gioi Tinh",
"Diem Toan", "Diem Van", "Diem Anh", "Diem TBC");
}
int main() {
struct SinhVien dssv[100];
int slsv = 0;
int luaChon;
do {
printf("=============== MENU ===============");
printf("\n1. Them Sinh vien vao danh sach.");
printf("\n2. Hien thi danh sach sinh vien.");
printf("\n3. Sap xep theo ten.");
printf("\n4. Sap xep theo diem giam dan.");
printf("\n5. Tim sinh vien theo ten.");
printf("\n6. Ghi thong tin sinh vien ra file.");
printf("\n0. Thoat chuong trinh.");
printf("\nBan chon ? ");
scanf("%d", &luaChon);
struct SinhVien sv;
int i;
switch(luaChon) {
case 0:
break;
case 1:
sv = nhapSV();
dssv[slsv++] = sv;
break;
case 2:
hienThiTenCot();
for(i = 0; i < slsv; i++) {
hienThiTTSV(dssv[i]);
}
break;
case 3:
sapXepTheoTen(dssv, slsv);
hienThiTenCot();
for(i = 0; i < slsv; i++) {
hienThiTTSV(dssv[i]);
}
break;
case 4:
sapXepTheoDiem(dssv, slsv);
hienThiTenCot();
for(i = 0; i < slsv; i++) {
hienThiTTSV(dssv[i]);
}
break;
case 5:
timTheoTen(dssv, slsv);
break;
case 6:
ghiFile(dssv, slsv);
break;
default:
printf("Sai chuc nang, vui long chon lai!\n");
break;
}
} while(luaChon);
return 0;
}
// link hoc tap: https://youtu.be/EYMgZHOTdAM