structHS
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#define MAX 100
typedef struct hocsinh {
char hoten[35];
char lop[7];
float diemtoan, diemly, diemhoa;
} HOCSINH;
HOCSINH danhsach[MAX];
int r = 0;
int menu()
{
char c;
clrscr();
cout<<"+----- Menu -----+";
cout<<"
+----------------+";
cout<<"
+ 1. Nhap +";
cout<<"
+ 2. Xoa +";
cout<<"
+ 3. Sua doi +";
cout<<"
+ 4. Liet ke +";
cout<<"
+----------------+";
cout<<"
+ 0. back menu +";
cout<<"
+----------------+";
cout<<"
Chon muc : ";
do {
c = getch();
} while (c<'0' || c>'6');
return c-'0';
}
void nhapmoi()
{
int ok = 0;
char hoten[35];
float diem;
do {
cout<<"
Ho ten (enter = back menu) : ";
gets(hoten);
if (strlen(hoten) == 0)
ok = 1;
else
{
strcpy(danhsach[r].hoten, hoten);
printf("
Lop : ");
gets(danhsach[r].lop);
printf("
Diem toan : ");
scanf("%f", &diem);
danhsach[r].diemtoan = diem;
printf("
Diem ly : ");
scanf("%f", &diem);
danhsach[r].diemly = diem;
printf("
Diem hoa : ");
scanf("%f", &diem);
danhsach[r].diemhoa = diem;
r++;
}
} while (!ok);
}
void xoa()
{
int i, ok = 0, found, b[MAX];
char hoten[35], ht[35];
while (!ok)
{
if (r != 0)
{
printf("
Ho ten hoc sinh can xoa (enter = back menu) : ");
gets(hoten);
if (strlen(hoten) == 0)
ok = 1;
else
{
i = found = 0;
do {
strcpy(ht,danhsach[i].hoten);
if (strcmp(strupr(hoten), strupr(ht)) == 0)
b[found++] = i;
i++;
} while (i<r);
if (!found)
printf("
Khong tim thay hoc sinh co ten %s", hoten);
else
{
for (i=0; i<found; i++)
{
printf("
%3d %-35s %-7s %4.1f %4.1f %4.1f", i+1, danhsach[b[i]].hoten,danhsach[b[i]].lop,
danhsach[b[i]].diemtoan, danhsach[b[i]].diemly, danhsach[b[i]].diemhoa);
}
printf("
Tim thay %d hoc sinh.", found);
printf("
Chon thu tu muon xoa (neu muon huy bo thi chon so khong nam trong pham vi): ");
scanf("%d", &i);
if (i >= 1 && i <= found)
{
for (found=b[i-1]; found<r-1; found++)
danhsach[found] = danhsach[found+1];
r--;
printf("
Da xoa!");
}
else
printf("
Viec xoa bi huy bo.");
}
ok = 1;
}
getch();
}
else
ok = 1;
}
}
void sua()
{
int i, ok = 0, found, b[MAX];
char hoten[35], ht[35];
float diem;
while (!ok)
{
if (r != 0)
{
printf("
Ho ten hoc sinh can sua (enter = back menu) : ");
gets(hoten);
if (strlen(hoten) == 0)
ok = 1;
else
{
i = found = 0;
do {
strcpy(ht,danhsach[i].hoten);
if (strcmp(strupr(hoten), strupr(ht)) == 0)
b[found++] = i;
i++;
} while (i<r);
if (!found)
printf("
Khong tim thay hoc sinh co ten %s", hoten);
else
{
for (i=0; i<found; i++)
{
printf("
%3d %-35s %-7s %4.1f %4.1f %4.1f", i+1, danhsach[b[i]].hoten,danhsach[b[i]].lop,
danhsach[b[i]].diemtoan, danhsach[b[i]].diemly, danhsach[b[i]].diemhoa);
}
printf("
Tim thay %d hoc sinh.", found);
printf("
Chon thu tu muon sua doi (neu muon huy bo thi chon so khong nam trong pham vi): ");
scanf("%d%*c", &i);
if (i >= 1 && i <= found)
{
printf("
%s
Ho ten : ", danhsach[b[i-1]].hoten);
gets(danhsach[b[i-1]].hoten);
printf("
%s
Lop : ", danhsach[b[i-1]].lop);
gets(danhsach[b[i-1]].lop);
printf("
%4.2f
Diem toan : ",danhsach[b[i-1]].diemtoan);
scanf("%f", &diem);
danhsach[b[i-1]].diemtoan = diem;
printf("
%4.2f
Diem ly : ", danhsach[b[i-1]].diemly);
scanf("%f", &diem);
danhsach[b[i-1]].diemly = diem;
printf("
%4.2f
Diem hoa : ", danhsach[b[i-1]].diemhoa);
scanf("%f%*c", &diem);
danhsach[b[i-1]].diemhoa = diem;
printf("
Da thay doi");
}
else
printf("
Viec thay doi bi huy bo.");
}
ok = 1;
}
getch();
}
else
ok = 1;
}
}
void lietke()
{
int i;
if (r != 0)
{
printf("
+-----+------------------------------------+--------+------+------+------+");
printf("
| STT | H O V A T E N | L O P | TOAN | LY | HOA |");
printf("
+-----+------------------------------------+--------+------+------+------+");
for (i=0; i<r; i++)
{
printf("
| %3d | ", i+1);
printf("%-35s| %-7s| %4.1f | %4.1f | %4.1f |", danhsach[i].hoten,danhsach[i].lop,
danhsach[i].diemtoan, danhsach[i].diemly, danhsach[i].diemhoa);
}
printf("
+-----+------------------------------------+--------+------+------+------+");
}
getch();
}
void main()
{
int chon, ok = 0;
do {
chon = menu();
switch(chon)
{
case 1 : nhapmoi();
break;
case 2 : xoa();
break;
case 3 : sua();
break;
case 4 : lietke();
break;
case 0 : ok = 1;
}
} while (!ok);
}
Bạn đang đọc truyện trên: AzTruyen.Top