bueno Vịt bầu
Tổng số bài gửi : 7 Cảm ơn : 14 Danh vọng : 0 Join date : 04/01/2011 Age : 32
| Tiêu đề: BT nhap tam giac & BT nhập thông tin thí sinh (kiểu struct) Fri Mar 11, 2011 11:26 am | |
| | Fri Mar 11, 2011 11:26 am BT nhap tam giac & BT nhập thông tin thí sinh (kiểu struct) | | | ******nhap tam giac****** - Code:
-
#include<stdio.h> #include<math.h> #include<malloc.h> #define MAX 100 typedef struct { double Canh1,Canh2,Canh3; }TamGiac; TamGiac *Nhap(int N); bool LaTG(TamGiac t); void Xuat1TG(TamGiac TG); void XuatMangTG(TamGiac *MTG,int N); double ChuVi(TamGiac TG); double DienTich(TamGiac TG); TamGiac TGMAX(TamGiac *MTG,int N); void main() { TamGiac *mtg; int n; do { printf("Nhap so luong tam giac: "); scanf("%d",&n); }while(n<=0||n>MAX); printf("Nhap cac tam giac: \n"); mtg=Nhap(n); printf("Cac tam giac da nhap: \n"); XuatMangTG(mtg,n); printf("Tam giac MAX: \n"); Xuat1TG(TGMAX(mtg,n)); } TamGiac *Nhap(int N) { TamGiac *tmp; tmp=(TamGiac*)malloc(N*sizeof(TamGiac)); for(int i=0;i<N;i++) { do { printf("Tam giac thu %d: ",i+1); scanf("%lf%lf%lf",&tmp[i].Canh1,&tmp[i].Canh2,&tmp[i].Canh3); }while(!LaTG(tmp[i])); } return tmp; } bool LaTG(TamGiac TG) { if(TG.Canh1<=0||TG.Canh2<=0||TG.Canh3<=0||TG.Canh1+TG.Canh2<=TG.Canh3||TG.Canh2+TG.Canh3<=TG.Canh1||TG.Canh1+TG.Canh3<=TG.Canh2) return false; else return true; } void Xuat1TG(TamGiac TG) { printf("(%.2lf %.2lf %.2lf) - %.2lf - %.2lf\n",TG.Canh1,TG.Canh2,TG.Canh3,ChuVi(TG),DienTich(TG)); } void XuatMangTG(TamGiac *MTG,int N) { for(int i=0;i<N;i++) { printf("Tam giac thu %d ",i+1); Xuat1TG(MTG[i]); printf("\n"); } } double ChuVi(TamGiac TG) { return (TG.Canh1+TG.Canh2+TG.Canh3); } double DienTich(TamGiac TG) { double p=ChuVi(TG)/2; return sqrt(p*(p-TG.Canh1)*(p-TG.Canh2)*(p-TG.Canh3)); } TamGiac TGMAX(TamGiac *MTG,int N) { double dtmax=DienTich(MTG[0]); TamGiac tgmax=MTG[0]; for(int i=1;i<N;i++) if(dtmax<DienTich(MTG[i])) { dtmax=DienTich(MTG[i]); tgmax=MTG[i]; } return tgmax; } *****Nhập thông tin thí sinh******* - Code:
-
#include<stdio.h> #include<malloc.h> #include<string.h> #define MAX 100 typedef struct { char SBD[10]; char HoTen[20]; double D1,D2,D3; }ThiSinh; ThiSinh *Nhap(int N); void Xuat1TS(ThiSinh TS); void XuatMangTS(ThiSinh *MTS,int N); double DTB(ThiSinh TS); void Swap(ThiSinh *TS1,ThiSinh *TS2); ThiSinh *SapXep(ThiSinh *MTS,int N); void main() { int n; ThiSinh *mts; do { printf("Nhap so luong thi sinh: "); scanf("%d",&n); }while(n<=0||n>MAX); mts=Nhap(n); printf("Cac thi sinh da nhap: \n"); XuatMangTS(mts,n); printf("\n"); printf("Cac thi sinh da sap xep\n"); XuatMangTS(SapXep(mts,n),n); } ThiSinh *Nhap(int N) { ThiSinh *tmp; tmp=(ThiSinh*)malloc(N*sizeof(ThiSinh)); for(int i=0;i<N;i++) { printf("Thi sinh thu %d:\n",i+1); flushall(); printf("_ SBD: "); gets(tmp[i].SBD); flushall(); printf("_ Ho Ten: "); gets(tmp[i].HoTen); flushall(); do { printf("_ Diem 3 mon: "); scanf("%lf%lf%lf",&tmp[i].D1,&tmp[i].D2,&tmp[i].D3); }while(tmp[i].D1<0||tmp[i].D1>10||tmp[i].D2<0||tmp[i].D2>10||tmp[i].D3<0||tmp[i].D3>10); } return tmp;
} void Xuat1TS(ThiSinh TS) { printf("_ SBD: %s\n",TS.SBD); printf("_ Ho va ten: %s\n",TS.HoTen); printf("_ Diem trung binh: %.2lf",DTB(TS)); } void XuatMangTS(ThiSinh *MTS,int N) { for(int i=0;i<N;i++) { printf("Thi sinh thu %d\n",i+1); Xuat1TS(MTS[i]); printf("\n"); } } double DTB(ThiSinh TS) { return (TS.D1+TS.D2+TS.D3)/3; } void Swap(ThiSinh *TS1,ThiSinh *TS2) { ThiSinh tmp=*TS1; *TS1=*TS2; *TS2=tmp; } ThiSinh *SapXep(ThiSinh *MTS,int N) { ThiSinh *mts; mts=(ThiSinh*)malloc(N*sizeof(ThiSinh)); for(int k=0;k<N;k++) mts[k]=MTS[k]; for(int i=0;i<N-1;i++) for(int j=i+1;j<N;j++) if(DTB(mts[i])<DTB(mts[j])) Swap(&mts[i],&mts[j]); return mts; }
bueno
| | | | | |
|