quan ly danh sach sinh vien
/* Bai tap 1_61 - Quan ly danh sach hoc sinh */
#include <stdio.h>
#define MAX 100
typedef struct hocsinh {
char hoten[35];
char lop[7];
float diemtoan, diemly, diemhoa;
} HOCSINH;
HOCSINH danhsach[MAX];
int numrecords = 0;
char filename[] = "DANHSACH.DAT";
int menu()
{
char c;
clrscr();
printf("+----- Menu -----+");
printf("
+----------------+");
printf("
+ 1. Nhap +");
printf("
+ 2. Xoa +");
printf("
+ 3. Sua doi +");
printf("
+ 4. Liet ke +");
printf("
+----------------+");
printf("
+ 5. Luu len dia +");
printf("
+ 6. Lay tu dia +");
printf("
+----------------+");
printf("
+ 0. Thoat +");
printf("
+----------------+");
printf("
Chon muc : ");
do {
c = getch();
} while (c<'0' || c>'6');
return c-'0';
}
void nhapmoi()
{
int done = 0;
char hoten[35];
float diem;
do {
printf("
Ho ten (trong de thoat) : ");
gets(hoten);
if (strlen(hoten) == 0)
done = 1;
else
{
strcpy(danhsach[numrecords].hoten, hoten);
printf("
Lop : ");
gets(danhsach[numrecords].lop);
printf("
Diem toan : ");
scanf("%f", &diem);
danhsach[numrecords].diemtoan = diem;
printf("
Diem ly : ");
scanf("%f", &diem);
danhsach[numrecords].diemly = diem;
printf("
Diem hoa : ");
scanf("%f%*c", &diem);
danhsach[numrecords].diemhoa = diem;
numrecords++;
}
} while (!done);
}
void xoa()
{
int i, done = 0, found, dump[MAX];
char hoten[35], hotentmp[35];
while (!done)
{
if (numrecords != 0)
{
printf("
Ho ten hoc sinh can xoa (trong de thoat) : ");
gets(hoten);
if (strlen(hoten) == 0)
done = 1;
else
{
i = found = 0;
do {
strcpy(hotentmp,danhsach[i].hoten);
if (strcmp(strupr(hoten), strupr(hotentmp)) == 0)
dump[found++] = i;
i++;
} while (i<numrecords);
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[dump[i]].hoten,danhsach[dump[i]].lop,
danhsach[dump[i]].diemtoan, danhsach[dump[i]].diemly, danhsach[dump[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=dump[i-1]; found<numrecords-1; found++)
danhsach[found] = danhsach[found+1];
numrecords--;
printf("
Da huy");
}
else
printf("
Viec xoa bi huy bo.");
}
done = 1;
}
getch();
}
else
done = 1;
}
}
void sua()
{
int i, done = 0, found, dump[MAX];
char hoten[35], hotentmp[35];
float diem;
while (!done)
{
if (numrecords != 0)
{
printf("
Ho ten hoc sinh can sua (trong de thoat) : ");
gets(hoten);
if (strlen(hoten) == 0)
done = 1;
else
{
i = found = 0;
do {
strcpy(hotentmp,danhsach[i].hoten);
if (strcmp(strupr(hoten), strupr(hotentmp)) == 0)
dump[found++] = i;
i++;
} while (i<numrecords);
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[dump[i]].hoten,danhsach[dump[i]].lop,
danhsach[dump[i]].diemtoan, danhsach[dump[i]].diemly, danhsach[dump[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[dump[i-1]].hoten);
gets(danhsach[dump[i-1]].hoten);
printf("
%s
Lop : ", danhsach[dump[i-1]].lop);
gets(danhsach[dump[i-1]].lop);
printf("
%4.2f
Diem toan : ",danhsach[dump[i-1]].diemtoan);
scanf("%f", &diem);
danhsach[dump[i-1]].diemtoan = diem;
printf("
%4.2f
Diem ly : ", danhsach[dump[i-1]].diemly);
scanf("%f", &diem);
danhsach[dump[i-1]].diemly = diem;
printf("
%4.2f
Diem hoa : ", danhsach[dump[i-1]].diemhoa);
scanf("%f%*c", &diem);
danhsach[dump[i-1]].diemhoa = diem;
printf("
Da thay doi");
}
else
printf("
Viec thay doi bi huy bo.");
}
done = 1;
}
getch();
}
else
done = 1;
}
}
void lietke()
{
if (numrecords != 0)
{
printf("
+-----+------------------------------------+--------+------+------+------+");
printf("
| STT | H O V A T E N | L O P | TOAN | LY | HOA |");
printf("
+-----+------------------------------------+--------+------+------+------+");
for (i=0; i<numrecords; 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 getdata()
{
char c;
int start, nrec;
FILE *fp;
long curpos, length;
start = 0;
if (numrecords != 0)
{
printf("
Dang co du lieu trong bo nho. Co muon noi them vao khong?(C/K)");
c = toupper(getch());
if (c == 'C')
start = numrecords;
else
start = 0;
}
if ((fp = fopen(filename, "r")) == NULL)
printf("
Khong the mo tap tin %s", filename);
else
{
curpos = ftell(fp);
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, curpos, SEEK_SET);
nrec = length / sizeof(HOCSINH);
fread(&danhsach[start], sizeof(HOCSINH), nrec, fp);
fclose(fp);
printf("
Them vao %d records.", nrec);
numrecords = numrecords + nrec;
getch();
}
}
void putdata()
{
FILE *fp;
if ((fp = fopen(filename, "w+")) == NULL)
printf("
Khong thay ghi tap tin %s", filename);
else
{
fwrite(&danhsach[0], sizeof(HOCSINH), numrecords, fp);
fclose(fp);
printf("
Ghi %d records.", numrecords);
getch();
}
}
void main()
{
int chon, done = 0;
do {
chon = menu();
switch(chon)
{
case 1 : nhapmoi();
break;
case 2 : xoa();
break;
case 3 : sua();
break;
case 4 : lietke();
break;
case 5 : putdata();
break;
case 6 : getdata();
break;
case 0 : done = 1;
}
} while (!done);
}
Bạn đang đọc truyện trên: Truyen247.Pro