Hãy tính điểm trung bình và xếp loại cho học sinh.. Sắp xếp danh sách học sinh theo điểm trung bình tăng dần... Viết chương trình nhập danh sách lớp gồm các thông tin: Họ và tên; điểm cá
Trang 1Phần VII : STRUCT VÀ UNION
1 Hãy nhập danh sách 1 lớp gồm 45 sinh viên ( Nhập họ tên, ngày tháng năm sinh và điểm 3 môn thi học kỳ Toán ,lý , Hóa) Hãy tính điểm trung bình và xếp loại cho học sinh Sắp xếp danh sách học sinh theo điểm trung bình tăng dần
* Chương trình
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <math.h>
#include <stdlib.h>
/* -*/
typedef struct
{
char hoten[20],xl[6];
int namsinh;
float t,l,h,tb;
}ds;
/* -*/
void nhap(int n,ds *);
ds x[50];
int i,n;
/* -*/
void nhap(int n,ds *x)
{
int i;
float tl;
for(i=1;i<=n;i++)
{
printf("\n Vao so lieu nguoi thu %2d:",i);
printf("\n Ho va ten:");
scanf("%s",&x[i].hoten); strupr(x[i].hoten);
printf("\n nam sinh:");
scanf("%d",&x[i].namsinh);
printf("\nDiem tung mon");
tt:
printf("\nDiem toan :"); scanf("%f",&tl);x[i].t=tl;
printf(" Diem ly :"); scanf("%f",&tl);x[i].l=tl;
printf(" Diem hoa :"); scanf("%f",&tl);x[i].h=tl;
Trang 2x[i].tb=(x[i].t+x[i].l+x[i].h)/3;
if(x[i].tb<4.0) strcpy(x[i].xl,"kem");
if(x[i].tb<5.0 && x[i].tb>=4.0) strcpy(x[i].xl,"yeu");
if(x[i].tb<6.5 && x[i].tb>=5.0) strcpy(x[i].xl,"Trung
Binh");
if(x[i].tb<8.0 && x[i].tb>=6.5) strcpy(x[i].xl,"Kha");
if(x[i].tb<9.0 && x[i].tb>=8.0) strcpy(x[i].xl,"Gioi");
if(x[i].tb<=10.0 && x[i].tb>=9.0) strcpy(x[i].xl,"Xuat
sac");
if(x[i].tb>10)
{
putch(7);putch(7);
printf("nhap lai:");
goto tt;
}
}
}
/* -*/
void Xuat(int n,ds *x)
{
for(i=1;i<=n;i++)
{
printf("\n nguoi thu %2d:",i);
printf(" Ho va ten : %s",x[i].hoten);
printf("\nnam sinh : %d",x[i].namsinh);
printf("\n diem tung mon : %3.1f\t %3.1f\t
%3.1f",x[i].t,x[i].l,x[i].h);
printf("\n diem trung binh: %4.2f",x[i].tb);
printf("\n xep loai :%s",x[i].xl);
}
}
/* -*/
void sapxep(int n,ds *x)
{
int j;
ds tam;
for(i=1; i<=n-1;i++)
for(j=i+1; j<=n;j++)
{
tam=x[i];
x[i]=x[j];
x[j]=tam;
Trang 3}
}
/* -*/
main()
{
clrscr();
printf("\n so sinh vien:");
scanf("%d",&n);
nhap(n,x);
clrscr();
printf(" Danh sach truoc khi sap xep\n");
Xuat(n,x);
printf("\n");
printf("\n");
printf("Danh sach sau khi sap xep\n");
sapxep(n,x);
Xuat(n,x);
getch();
}
2 Viết chương trình nhập danh sách lớp gồm các thông tin: Họ và tên; điểm các môn: cấu trúc dữ liệu,Pascal, ôtômat
a Xuất bảng điểm theo thú tụ giảm dần của điểm trung bình
b Đếm số sinh viên thiếu điểm môn : Cấu trúc dữ liệu
c Xuất danh sách thi lại môn PASCAL
* Chương trình
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 70
/* -*/
/* khai bao 1 cau truc */
struct sv
{ char hoten[40];
int ctdl,pas,otomat;
float dtb;
} lop[MAX];
int n;
/* -*/
Trang 4void nhap(void)
{
char s[20];
n=0;
do
{
printf("\nsinh vien thu %d\n",n+1);
printf("Ho ten:"); scanf(" %[^\n]",&lop[n].hoten );
printf("diem mon CTDL "); scanf("%d",&lop[n].ctdl); printf("diem mon PASCAL "); scanf("%d",&lop[n].pas);
printf("diem mon OTOMAT "); scanf("%d",&lop[n].otomat); printf("\n");
lop[n].dtb=(lop[n].ctdl+lop[n].pas+lop[n].otomat)/3.0; n++;
printf("Ban muon tiep tuc khong(anykey/k)? ");
} while(getch()!='k' && n<=MAX);
}
/* -*/
void xuat(void)
{
char i,j,dem=0;
struct sv tmp;
clrscr();
// sap xep theo thu tu diem trung binh giam
// Neu dtb bang nhau thi sx theo ten tang dan
for(i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
if((lop[i].dtb< lop[j].dtb)||(lop[i].dtb==lop[j].dtb && strcmpi(lop[i].hoten,lop[j].hoten)>0))
tmp=lop[i];
lop[i]=lop[j];
lop[j]=tmp ;
/* -*/
printf("\nDanh sach sinh vien theo thu tu diem trung
binh\n");
printf(" Ho Ten CTDL PASCAL OTOMAT
DTB\n");
for(i=0;i<n;i++)
{
Trang 5printf("%-25s",lop[i].hoten);
printf(" %1d",lop[i].ctdl);
printf(" %7d",lop[i].pas);
printf(" %7d",lop[i].otomat);
printf("%10.2f\n",lop[i].dtb);
}
/* dem so sinh vien thieu mon CTDL va in danh sach thi lai mon PASCAL*/
printf("\nDanh sach thi lai mon Pascal\n ");
for(i=0;i<n;i++)
{
if( lop[i].ctdl<5) dem++;
if(lop[i].pas <5) { j=0;
putchar(lop[i].hoten[j++]);
printf("\n");
} }
printf("\nSo sinh vien thieu mon CTDL la: %d ",dem);
}
/* -*/
void main()
{
clrscr();
nhap();
xuat();
getch();
}