c5_hvtc
Họ tên:LE van huy
Lớp :CQ46/41.01
BÀI TẬP CHƯƠNG 5
Bài 1:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct SV
{
char hoten[40];
char masv[20];
int khoa;
char lop[10];
int diem[6];
float dtb;
char *phanloai;
};
typedef struct SV sv;
sv DSSV[200];
int n;
a.void nhap()
{
int i,j;
printf("Nhap so SV:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("SV thu %d:",i);
printf("
Ten SV:");
fflush(stdin);
gets(DSSV[i].hoten);
printf("\Ma SV:");
gets(DSSV[i].masv);
printf("Khoa:");
scanf("%d",&DSSV[i].khoa);
fflush(stdin);
printf("Lop:");
gets(DSSV[i].lop);
printf("Nhap diem thi nam mon:
");
for(j=1;j<=5;j++)
{
printf("Mon thu %d:",j);
scanf("%d",&DSSV[i].diem[j]);
}
}
}
b.void tinhdiem()
{
int i;
clrscr();
printf("Ket qua tinh toan:
");
for(i=1;i<=n;i++)
{
DSSV[i].dtb=(4*DSSV[i].diem[1]+5*DSSV[i].diem[2]+6*DSSV[i].diem[3]
+7*DSSV[i].diem[4]+8*DSSV[i].diem[5])/30.0;
DSSV[i].phanloai=(char*)malloc(15*sizeof(char));
if(DSSV[i].dtb<5)DSSV[i].phanloai="Kem";
if((DSSV[i].dtb>=5)&&(DSSV[i].dtb<6))DSSV[i].phanloai="TB";
if((DSSV[i].dtb>=6)&&(DSSV[i].dtb<7))DSSV[i].phanloai="TBK";
if((DSSV[i].dtb>=7)&&(DSSV[i].dtb<8))DSSV[i].phanloai="Kha";
if((DSSV[i].dtb>=8)&&(DSSV[i].dtb<9))DSSV[i].phanloai="Gioi";
if((DSSV[i].dtb>=9)&&(DSSV[i].dtb<=10))DSSV[i].phanloai="Xuat sac";
printf("
Sinh vien:%s-----Lop:%s",DSSV[i].hoten,DSSV[i].lop);
printf("
Diem TB:%0.2f",DSSV[i].dtb);
printf("
Xep loai:%s",DSSV[i].phanloai);
}
}
c.void sapxep()
{
sv temp;
int i,j;
printf("Sap xep theo thu tu DTB giam dan:
");
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(DSSV[i].dtb<DSSV[j].dtb)
{
temp=DSSV[i];
DSSV[i]=DSSV[j];
DSSV[j]=temp;
}
for(i=1;i<=n;i++)
{
printf("
Ho ten:%s-----Lop:%s
",DSSV[i].hoten,DSSV[i].lop);
printf("DTB:%0.2f
",DSSV[i].dtb);
}
}
d.void hienthi()
{
int i;
char lop[20];
printf("Nhap ten lop can hien thi:");
fflush(stdin);
gets(lop);
for(i=1;i<=n;i++)
if(strcmp(DSSV[i].lop,lop)==0)
printf("
%s",DSSV[i].hoten);
}
d.int main()
{
int chon;
clrscr();
do
{
clrscr();
printf("Chuong Trinh Quan Li SV:
");
printf("
1:Nhap du lieu");
printf("
2:Tinh DTB va phan loai");
printf("
3:Sap xep theo thu tu diem giam");
printf("
4:Hien Thi DS mot lop");
printf("
5:Thoat");
printf("
Bam phim de lua chon:");
scanf("%d",&chon);
switch(chon)
{
case 1:nhap();getch();break;
case 2:tinhdiem();getch();break;
case 3:sapxep();getch();break;
case 4:display();getch();break;
case 5:return 0;
default:printf("Ban da nhap sai");getch();
}
}
while(1);
}
Bài 2.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define chitieu 2 /*chi tieu la 2*/
struct date
{
int ngay,thang,nam;
};
struct hovaten
{
char ho[30];
char ten[15];
};
struct TS
{
struct hovaten hoten;
int sbd;
struct date ngaysinh;
float diem[4];
float dut;
float tongdiem;
int pass;
};
typedef struct TS ts;
ts DSTS[200];
int n;
a.void nhap()
{
int i,j;
printf("Nhap so thi sinh:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
clrscr();
printf("TS thu %d:",i);
printf("
Nhap Ho va Ten TS:
");
fflush(stdin);
printf("Ho:");
gets(DSTS[i].hoten.ho);
printf("Ten:");
gets(DSTS[i].hoten.ten);
printf("Ngay thang nam sinh:");
scanf("%d%d%d",&DSTS[i].ngaysinh.ngay,&DSTS[i].ngaysinh.thang,&DSTS[i].ngaysinh.nam);
}
printf("
DA NHAP DU LIEU XONG!!!");
}
b.void danh_so_bao_danh()
{
ts temp;
int i,j;
printf("Sap xep theo thu tu alphabet giam dan:
");
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(strcmpi(DSTS[i].hoten.ten,DSTS[j].hoten.ten)>0)
{
temp=DSTS[i];
DSTS[i]=DSTS[j];
DSTS[j]=temp;
}
for(i=1;i<=n;i++)
{
DSTS[i].sbd=i;
printf("
Ho ten:%s %s-----SBD:%d
",DSTS[i].hoten.ho,DSTS[i].hoten.ten,DSTS[i].sbd);
}
}
c.void tinhdiem()
{
int i,j;
clrscr();
for(i=1;i<=n;i++)
{
DSTS[i].tongdiem=0;
clrscr();
printf("
SBD %d:----Ho ten:%s %s
",i,DSTS[i].hoten.ho,DSTS[i].hoten.ten);
printf("Nhap diem thi 3 mon:
");
for(j=1;j<=3;j++)
{
printf("Mon thu %d:",j);
scanf("%f",&DSTS[i].diem[j]);
DSTS[i].tongdiem+=DSTS[i].diem[j];
}
}
}
d.void hienthiketqua()
{
int i,j;
float temp[100],tg,diemsan;
for(i=1;i<=n;i++)temp[i]=DSTS[i].tongdiem;
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if(temp[i]<temp[j])
{
tg=temp[i];
temp[i]=temp[j];
temp[j]=tg;
}
diemsan=temp[chitieu];
for(i=1;i<=n;i++)
if(DSTS[i].tongdiem>=diemsan)DSTS[i].pass=1;
else DSTS[i].pass=0;
printf("
Chi tieu %d",chitieu);
printf("
Diem san %0.3f
",diemsan);
printf("Ket qua cuoi cung:
");
for(i=1;i<=n;i++)
{
printf("
Thi sinh:%s %s
",DSTS[i].hoten.ho,DSTS[i].hoten.ten);
printf("Tong diem:%0.2f
",DSTS[i].tongdiem);
if(DSTS[i].pass==1)printf("Ket qua: DO");
else printf("Ket qua: TRUOT");
}
}
e.void hien_thi_danh_sach_do()
{
int i;
display();
clrscr();
printf("Chi tieu la %d
",chitieu);
printf("Danh sach thi sinh da trung tuyen
");
for(i=1;i<=n;i++)
if(DSTS[i].pass)
printf("%s %s
",DSTS[i].hoten.ho,DSTS[i].hoten.ten);
}
int main()
{
int chon;
clrscr();
do
{
clrscr();
printf("Chuong Trinh Quan Li Tuyen Sinh:
");
printf("
1:Nhap du lieu");
printf("
2:Danh so bao danh");
printf("
3:Nhap diem 3 mon va tinh tong diem");
printf("
4:Xac Dinh Ket Qua");
printf("
5:Hien Thi Danh Sach Do");
printf("
6:Thoat");
printf("
Bam phim de lua chon:");
scanf("%d",&chon);
switch(chon)
{
case 1:nhap();getch();break;
case 2:danh_bao_danh();getch();break;
case 3:tinhdiem();getch();break;
case 4:clrscr();display();getch();break;
case 5:hienthido();getch();break;
case 6:return 0;
default:printf("Ban da nhap sai");getch();
}
}
while(1);
}
Bài 3:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int a[50],n;
char bit[30];
FILE *f;
char filename[30];
void nhap()
{
int i;
clrscr();
printf("Nhap day nhi phan:");
fflush(stdin);
gets(bit);
n=strlen(bit);
for(i=0;i<=n-1;i++)
a[i]=bit[i]-48;
printf("Nhap ten file ghi du lieu:");
fflush(stdin);
scanf("%s",filename);
}
void ghidulieu()
{
int i=0,he10=0;/*He co so 10*/
nhap();
f=fopen(filename,"w");
for(i=n-1;i>=0;i--)
he10+=(int)(a[i]*pow(2,n-i-1));
fprintf(f,"%x",he10);
printf("
Du lieu da duoc ghi vao file:%s",filename);
fclose(f);
}
int docdulieu()
{
int i=0,j,x;
printf("Nhap file dau vao:");
fflush(stdin);
scanf("%s",filename);
f=fopen(filename,"r");
if(f==NULL)
{printf("!!!Tep chua ton tai!!!");return 0;}
fscanf(f,"%x",&x);
fclose(f);
printf("Day nhi phan chua trong tep:%s la:",filename);
while(x)
{
a[i++]=x%2;
x=x/2;
}
for(j=i-1;j>=0;j--)printf("%d",a[j]);
return 0;
}
main
{
int chon;
do
{
clrscr();
printf("1:Nhap mot day nhi phan va dua vao file duoi dang ma HECXA:");
printf("
2:Doc mot day nhi phan tu file");
printf("
3:Thoat");
printf("
Bam phim de chon:");
scanf("%d",&chon);
switch(chon)
{
case 1:ghidulieu();getch();break;
case 2:docdulieu();getch();break;
case 3:return 0;
default:printf("Ban da nhap sai");getch();
}
}while(1);
return 0;
}
Bài 4
#include <stdio.h>
#include <conio.h>
#include <string.h>
/********************************************/
typedef struct
{
int ngay,thang,nam;
} date;
typedef struct
{
int sbd;
char hten[25],kq[20];
date ngaysinh;
float d1,d2,d3,d4,d;
} sv;
sv ds[50];
int n;
float chuan;
FILE *fp;
/********************************************/
void nhap_du_lieu()
{
int i;
float t;
fp=fopen("b4.in","wb");
printf("Moi ban nhap du lieu
");
printf("Nhap diem chuan xet tuyen: "); scanf("%f%*c",&chuan);
fwrite(&chuan,sizeof(chuan),1,fp);
printf("Nhap so luong sinh vien: "); scanf("%d%*c",&n);
fwrite(&n,sizeof(n),1,fp);
printf("
");
for (i=0;i<n;i++)
{
fflush(stdin);
printf("Sinh vien thu %d
",i+1);
printf("Ho va ten: "); gets(ds[i].hten);
fwrite(&ds[i].hten,sizeof(ds[i].hten),1,fp);
printf("Ngay sinh: "); scanf("%d",&ds[i].ngaysinh.ngay);
fwrite(&ds[i].ngaysinh.ngay,sizeof(ds[i].ngaysinh.ngay),1,fp);
printf("Thang sinh: "); scanf("%d",&ds[i].ngaysinh.thang);
fwrite(&ds[i].ngaysinh.thang,sizeof(ds[i].ngaysinh.thang),1,fp);
printf("Nam sinh: "); scanf("%d",&ds[i].ngaysinh.nam);
fwrite(&ds[i].ngaysinh.nam,sizeof(ds[i].ngaysinh.nam),1,fp);
printf("Diem mon 1: "); scanf("%f",&t); ds[i].d1=t;
fwrite(&ds[i].d1,sizeof(&ds[i].d1),1,fp);
printf("Diem mon 2: "); scanf("%f",&t);ds[i].d2=t;
fwrite(&ds[i].d2,sizeof(&ds[i].d2),1,fp);
printf("Diem mon 3: "); scanf("%f",&t); ds[i].d3=t;
fwrite(&ds[i].d3,sizeof(&ds[i].d3),1,fp);
printf("Diem uu tien: "); scanf("%f",&t); ds[i].d4=t;
fwrite(&ds[i].d4,sizeof(&ds[i].d4),1,fp);
printf("
");
}
fclose(fp);
}
/********************************************/
void tinh_diem()
{
int i;
for (i=0;i<n;i++)
{
ds[i].d=ds[i].d1+ds[i].d2+ds[i].d3+ds[i].d4;
if (ds[i].d<chuan) strcpy(ds[i].kq,"Truot");
else strcpy(ds[i].kq,"Trung tuyen!!!");
}
}
/********************************************/
void tinh_diem_lai(int h)
{
int i;
for (i=0;i<n+h;i++)
{
ds[i].d=ds[i].d1+ds[i].d2+ds[i].d3+ds[i].d4;
if (ds[i].d<chuan) strcpy(ds[i].kq,"Truot");
else strcpy(ds[i].kq,"Trung tuyen!!!");
}
}
/********************************************/
void sap_xep()
{
int i,k,p,m[100];
char ten[100][25],t[25];
sv tg;
tinh_diem();
for (i=0;i<n;i++) m[i]=strlen(ds[i].hten);
for (i=0;i<n;i++) /*tach ten*/
while (ds[i].hten[m[i]] != ' ') m[i]--;
for (i=0;i<n;i++)
for (k=m[i]+1;k<=strlen(ds[i].hten);k++)
ten[i][k-m[i]-1]=ds[i].hten[k];
for (i=0;i<n-1;i++)
for (p=i+1;p<n;p++)
if (strcmp(ten[i],ten[p])>0)
{
tg=ds[i];
ds[i]=ds[p];
ds[p]=tg;
strcpy(t,ten[i]);
strcpy(ten[i],ten[p]);
strcpy(ten[p],t);
}
for (i=0;i<n;i++) ds[i].sbd=i+1;
}
/********************************************/
void sap_xep_lai()
{
int i,k,p,h,m[100];
char ten[100][25],t[25];
sv tg;
tinh_diem_lai(h);
for (i=0;i<n+h;i++) m[i]=strlen(ds[i].hten);
for (i=0;i<n+h;i++) /*tach ten*/
while (ds[i].hten[m[i]] != ' ') m[i]--;
for (i=0;i<n+h;i++)
for (k=m[i]+1;k<=strlen(ds[i].hten);k++)
ten[i][k-m[i]-1]=ds[i].hten[k];
for (i=0;i<n+h-1;i++)
for (p=i+1;p<n+h;p++)
if (strcmp(ten[i],ten[p])>0)
{
tg=ds[i];
ds[i]=ds[p];
ds[p]=tg;
strcpy(t,ten[i]);
strcpy(ten[i],ten[p]);
strcpy(ten[p],t);
}
for (i=0;i<n+h;i++) ds[i].sbd=i+1;
}
/********************************************/
void chi_tieu()
{
int i,j;
sv tg;
sap_xep();
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
if (ds[i].d<ds[j].d)
{
tg=ds[i];
ds[i]=ds[j];
ds[j]=tg;
}
}
/********************************************/
void hien_thi(int i)
{
printf("Sinh vien thu %d
",i+1);
printf("Ho va ten: %s
",ds[i].hten);
printf("So bao danh: %d
",ds[i].sbd);
printf("Ngay sinh: %2d Thang sinh: %2d Nam sinh: %4d
",ds[i].ngaysinh.ngay,ds[i].ngaysinh.thang,ds[i].ngaysinh.nam);
printf("Diem mon 1: %.3f
",ds[i].d1);
printf("Diem mon 2: %.3f
",ds[i].d2);
printf("Diem mon 3: %.3f
",ds[i].d3);
printf("Diem uu tien: %.3f
",ds[i].d4);
printf(" --------
");
printf("Tong diem: %.3f
",ds[i].d);
printf("Ket qua: %s
",ds[i].kq);
printf("
");
}
/********************************************/
void bo_sung()
{
int i,h;
float t;
fp=fopen("b4.in","ab");
printf("Ban muon bo sung vao danh sach bao nhieu thi sinh: ");
scanf("%d%*c",&h);
printf("
");
for (i=n;i<n+h;i++)
{
fflush(stdin);
printf("Sinh vien thu %d
",i-n+1);
printf("Ho va ten: "); gets(ds[i].hten);
fwrite(&ds[i].hten,sizeof(ds[i].hten),1,fp);
printf("Ngay sinh: "); scanf("%d%*c",&ds[i].ngaysinh.ngay);
fwrite(&ds[i].ngaysinh.ngay,sizeof(ds[i].ngaysinh.ngay),1,fp);
printf("Thang sinh: "); scanf("%d%*c",&ds[i].ngaysinh.thang);
fwrite(&ds[i].ngaysinh.thang,sizeof(ds[i].ngaysinh.thang),1,fp);
printf("Nam sinh: "); scanf("%d%*c",&ds[i].ngaysinh.nam);
fwrite(&ds[i].ngaysinh.nam,sizeof(ds[i].ngaysinh.nam),1,fp);
printf("Diem mon 1: "); scanf("%f%*c",&t); ds[i].d1=t;
fwrite(&ds[i].d1,sizeof(&ds[i].d1),1,fp);
printf("Diem mon 2: "); scanf("%f%*c",&t);ds[i].d2=t;
fwrite(&ds[i].d2,sizeof(&ds[i].d2),1,fp);
printf("Diem mon 3: "); scanf("%f%*c",&t); ds[i].d3=t;
fwrite(&ds[i].d3,sizeof(&ds[i].d3),1,fp);
printf("Diem uu tien: "); scanf("%f%*c",&t); ds[i].d4=t;
fwrite(&ds[i].d4,sizeof(&ds[i].d4),1,fp);
printf("
");
}
fclose(fp);
sap_xep_lai();
clrscr();
printf("Ket qua:
");
for (i=0;i<n+h;i++) hien_thi(i);
printf("Bye bye");
}
/********************************************/
void tim_kiem()
{
int i,sbd;
sap_xep();
printf("Moi ban nhap so bao danh thi sinh can tim: ");
scanf("%d",&sbd);
printf("Ket qua:
");
for (i=0;i<n+1;i++)
if (sbd==ds[i].sbd) hien_thi(i);
}
/********************************************/
void main()
{
char chon;
int i;
clrscr();
printf("Bai tap 2 trang 153
");
nhap_du_lieu();
clrscr();
printf("Moi ban chon
");
printf("1-Xem danh sach vua nhap (xep ten theo thu tu bang chu cai)
");
printf("2-Xem danh sach 10 sinh vien co diem cao nhat
");
printf("3-Bo sung them thi sinh vao danh sach
");
printf("4-Tim kiem mot thi sinh theo so bao danh
");
printf("5-Ket thuc");
chon=getche();
switch(chon)
{
case '1' : clrscr();
sap_xep();
printf("Ket qua:
");
for (i=0;i<n;i++) hien_thi(i);
printf("Bye bye");
break;
case '2' : /*neu muon in ket qua ra may in ban hay thay tat ca cac lenh printf
bang lenh fprinft(stdprn,<xau dieu khien>[,danh sach bieu thuc])*/
clrscr();
chi_tieu();
printf("Ket qua:
");
if (n<=10)
for (i=0;i<n;i++) hien_thi(i);
else
for (i=0;i<10;i++) hien_thi(i);
break;
case '3' : clrscr();
bo_sung();
break;
case '4' : clrscr();
tim_kiem();
break;
case '5' : clrscr();
printf("Bye bye");
break;
}
getch();
}
Bài 5:.
#include<stdio.h>
#include<conio.h>
const char *fio="day_so.txt";
FILE *f;
int k;
struct DAYSO
{ int n;
int a[100];
};
struct DAYSO ds[100];
void nhap()
{
int i,j;
clrscr();
printf("nhap k: ");
scanf("%d",&k);
for(i=0;i<=k-1;i++)
{ printf("nhap cho day thu %d
",i+1);
printf("n=");
scanf("%d",&ds[i].n);
for(j=0;j<=ds[i].n-1;j++)
{ printf("a[%d]=",j);
scanf("%d",&ds[i].a[j]);
}
}
f=fopen(fio,"wb");
for(i=0;i<=k-1;i++)
fwrite(&ds[i],sizeof(ds[i]),2,f);
printf("du lieu da ghi vao file %s.An enter de tiep tuc");
getch();
fflush(f);
fclose(f);
}
void sapxep(int a[],int n)
{
int temp,i,j;
for(i=0;i<=n;i++)
for(j=i+1;j<n;j++)
if(a[i]<a[j])
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
void docfile()
{
int i=0,j;
printf("
ket qua sau khi sap xep
");
f=fopen(fio,"rb");
while(fread(&ds[i],sizeof(ds[i]),1,f))
{ sapxep(ds[i].a,ds[i].n);
printf("
");
for(j=0;j<=ds[i].n-1;j++)
printf("%3d",ds[i].a[j]); i++;
}
fclose(f);
f=fopen(fio,"w");
for(i=0;i<=k-1;i++)
{ for(j=0;j<=ds[i].n-1;j++)
fprintf(f,"
");
}
printf("
da ghi tro lai file %s duoi dang vb",fio);
getch();
fclose(f);
}
void main()
{ nhap();
docfile();
}
Bài 6:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int i,n;
double a[100],tong=0;
if(argc==1)
{ printf("chua co tham so dau vao");
getch();
exit(1);
}
n=argc-1;
for(i=1;i<=n;i++)
{ a[i]=atof(argv[i]);
tong=tong+a[i];
};
printf("tong cua %d so tren bang %0.3f",n,tong);
}
Bạn đang đọc truyện trên: Truyen247.Pro