HKI - LTCB Tuần 8
Bài 1:
Bài 2:
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define max 100
typedef struct PhanSo{
int tuso, mauso;
};
void nhap(PhanSo &b);
void xuat(PhanSo b);
void nhapmang(PhanSo a[], int &n);
void xuatmang(PhanSo a[], int n);
void chuanhoa(PhanSo &b);
int ucln(int a, int b);
void rutgon(PhanSo &b);
void chuanhoamang(PhanSo a[], int &n);
PhanSo tong(PhanSo ps, PhanSo ps1);
void tongmang(PhanSo a[], int &n);
float giatri(int a, int b);
void timmin(PhanSo a[], int &n);
void xapxep(PhanSo a[], int &n);
int main(){
PhanSo a[max], b; int n;
printf("=========================== NHAP MANG =======================\n"); nhapmang(a,n);
printf("=========================== XUAT MANG =======================\n\n"); xuatmang(a,n);
printf("\n=========================== CHUAN HOA MANG =======================\n\n"); chuanhoamang(a,n);
printf("\n=========================== TONG MANG =======================\n"); tongmang(a,n);
printf("\n======================== TIM MIN TRONG MANG ====================\n\n"); timmin(a,n);
printf("\n=========================== XAP SEP MANG =======================\n"); xapxep(a,n); xuatmang(a,n);
getch();
return 0;
}
void nhap(PhanSo &b){
printf("Nhap Tu So: "); scanf("%i", &b.tuso);
printf("Nhap Mau So: "); scanf("%i", &b.mauso);
}
void xuat(PhanSo b){
printf("%i / %i\n", b.tuso,b.mauso);
}
void nhapmang(PhanSo a[], int &n){
printf("So Luong Phan So: "); scanf("%i", &n);
for(int i=0;i<n;i++){
printf("Phan So Thu [%i]\n", i+1);
nhap(a[i]);
printf("\n");
}
}
void xuatmang(PhanSo a[], int n){
for(int i=0;i<n;i++){
printf("Phan So Thu [%i]: ", i+1);
xuat(a[i]);
// printf("\n");
}
}
void chuanhoa(PhanSo &b){
if(b.mauso < 0){
b.tuso= -b.tuso;
b.mauso= abs(b.mauso);
}
}
int ucln(int a, int b){
a= abs(a);
b=abs(b);
while(b!=0){
if(a>b) a-= b;
else b-= a;
}
return a;
}
void rutgon(PhanSo &b){
int d= ucln(b.tuso,b.mauso);
b.tuso= b.tuso/d;
b.mauso= b.mauso/d;
}
void chuanhoamang(PhanSo a[], int &n){
for(int i=0;i<n;i++){
printf("Phan So [%i]: ", i+1);
rutgon(a[i]);
chuanhoa(a[i]);
xuat(a[i]);
}
}
PhanSo tong(PhanSo ps, PhanSo ps1){
PhanSo b;
b.tuso= ps.tuso*ps1.mauso + ps.mauso*ps1.tuso;
b.mauso= ps.mauso*ps1.mauso;
rutgon(b);
chuanhoa(b);
return b;
}
void tongmang(PhanSo a[], int &n){
PhanSo b, t;
t.mauso= 1; t.tuso= 0;
for(int i=0;i<n;i++){
t= tong(t,a[i]);
}
chuanhoa(t);
rutgon(t);
printf("\nTong Phan So= "); xuat(t);
}
float giatri(PhanSo &b){
return (float)b.tuso/b.mauso;
}
void timmin(PhanSo a[], int &n){
PhanSo min= a[0];
for(int i=0;i<n;i++){
if(giatri(a[i]) < giatri(min)) min= a[i];
}
printf("Phan So Nho Nhat: "); xuat(min);
}
void xapxep(PhanSo a[], int &n){
PhanSo t;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(giatri(a[i]) > giatri(a[j])){
t= a[i];
a[i]= a[j];
a[j]= t;
}
}
}
}
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define max 100
typedef struct XeMay{
char hang[32], bangso[32];
int namsx, giatien;
};
void nhap(XeMay &xe);
void nhapmang(XeMay s[], int &n);
void xuat(XeMay xe);
void xuatmang(XeMay s[], int &n);
void timnamsx(XeMay s[], int &n);
void timhangsx(XeMay s[], int &n);
void xapxep(XeMay s[], int &n);
int main(){
XeMay s[max], xe; int n;
printf("====================== NHAP DS XE =====================\n"); nhapmang(s,n);
printf("====================== XUAT DS XE =====================\n"); xuatmang(s,n);
printf("====================== TIM NAM SX XE ====================\n"); timnamsx(s,n);
printf("====================== TIM HANG SX XE ====================\n"); timhangsx(s,n);
printf("====================== XAP XEP XE ====================\n"); xapxep(s,n); xuatmang(s,n);
return 0;
}
void nhap(XeMay &xe){
printf("Nhap Ten Hang SX: "); fflush(stdin); gets(xe.hang);
printf("Nhap Bang So Xe: "); fflush(stdin); gets(xe.bangso);
printf("Nhap Gia Tien: "); scanf("%i", &xe.giatien);
printf("Nhap Nam SX: "); scanf("%i", &xe.namsx);
}
void nhapmang(XeMay s[], int &n){
printf("So Luong Xe: "); scanf("%i", &n);
for(int i=0; i<n;i++){
printf("Xe May [%i]\n", i+1);
nhap(s[i]);
printf("\n");
}
}
void xuat(XeMay xe){
printf("Hang SX: "); puts(xe.hang);
printf("Bang So Xe: "); puts(xe.bangso);
printf("Gia Tien: %i", xe.giatien);
printf("\nNam SX: %i", xe.namsx);
}
void xuatmang(XeMay s[], int &n){
for(int i=0;i<n;i++){
printf(" Xe May [%i]\n", i+1);
xuat(s[i]);
printf("\n\n");
}
}
void timnamsx(XeMay s[], int &n){
int x;
printf("Nhap Nam Can Tim: "); scanf("%i", &x);
printf("Xe Duoc San Xuat Truoc Nam %i La: ", x);
for(int i=0;i<=n;i++){
if(s[i].namsx <= x)
xuat(s[i]);
printf("\n\n");
}
}
void timhangsx(XeMay s[], int &n){
char x[32]; int dem= 0;
printf("Nhap Hang Xe Can Tim: "); fflush(stdin); gets(x);
for(int i=0;i<n;i++){
if(strcmp(s[i].hang,x) == 0){
dem++;
printf("So Luong Xe: %i\n", dem);
xuat(s[i]);
}
}
}
void xapxep(XeMay s[], int &n){
XeMay t;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(strcmp(s[i].bangso,s[j].bangso) > 0){
t= s[i];
s[i]= s[j];
s[j]= t;
}
}
}
}
No Comment to " HKI - LTCB Tuần 8 "