Bài tập TH Kỹ thuật lập trình C\C++ (16-11-2012)




/* Bài 1: Viết các hàm:
1- Nhap 1 day so nguyen co n so (1<=n<=100, n nhap vao tu bp)
2- In day
3- Tach day thanh 2, 1 day chua so chan, 1 day chua so le
4- sap xep tang (select-sort)
5- Sap xep giam (insert-sort)
*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

// khai bao
int a[100],n, dayChan[100],nChan, dayLe[100],nLe ;

// Nhap day
void NhapDay(){
// Nhap so phan tu
do{ cout<<"\n n="; cin>>n;
if (n<1 || n>100)
cout<<"\n Nhap lai n!";
}while (n<1 || n>100);

// Nhap day so
cout<<"\n Nhap day so:";
for(int i=0;i<n;i++)
{
cout<<"\n a["<<i<<"]="; cin>>a[i];
}
}

// In day
void InDay(int a[], int n){
cout<<"\n In day:\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"; ";
}
// Tach day
void TachDay(int a[], int n){
cout<<"\n + Tach day:\n";
int chan=0,le=0;
for (int i=0;i<n;i++)
if(a[i]%2==0)
dayChan[chan++]=a[i];
else
dayLe[le++]=a[i];

if(le!=0) nLe=le;
if (chan!=0) nChan=chan;
}


// sap xep Select_sort
void Select_Sort(int a[], int n){
cout<<"\n + Sap xep tang dan:\n";
int m;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{ // doi cho
int t=a[i];
a[i]=a[j];
a[j]=t;
}

}

// Sap xep Insert_sort
void Insert_Sort(int a[], int n){
cout<<"\n + Sap xep giam dan:\n";
for(int i=0;i<n;i++)
{
int post=i;
int x=a[post+1];
// tim vi tri chen va dich phai
while(post>-1&&x>a[post]){
a[post+1]=a[post];
post--;
}
// chen vao
a[post+1]=x;
}
}

// Lop chinh

void main(){
clrscr();
NhapDay();
InDay(a,n);

TachDay(a,n);
InDay(dayChan,nChan);
InDay(dayLe,nLe);

Select_Sort(a,n);
InDay(a,n);

Insert_Sort(a,n);
InDay(a,n);

getch();
}


//------------------------------------------------------------------------------------------------------------------

/* Bài 2: Viêt các hàm:
1- Nhap vao toa do 3 diem A,B,C trong khong gian
2- Tinh do dai AB,BC, AC
3- Neu 3 diem khong thang hang di tinh chu vi dien tich tam giac
*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>

// khai bao
struct ToaDo{
float x,y,z;
};

ToaDo A,B,C;


// Nhap toa do 3 diem
void NhapToaDo(){
cout<<"\n Nhap toa do A: "; cin>>A.x>>A.y>>A.z;
cout<<"\n Nhap toa do B: "; cin>>B.x>>B.y>>B.z;
cout<<"\n Nhap toa do C: "; cin>>C.x>>C.y>>C.z;
}

// Tinh do dai 3 canh
float AB(){
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z));
}
float AC(){
return sqrt((A.x-C.x)*(A.x-C.x)+(A.y-C.y)*(A.y-C.y)+(A.z-C.z)*(A.z-C.z));
}
float CB(){
return sqrt((C.x-B.x)*(C.x-B.x)+(C.y-B.y)*(C.y-B.y)+(C.z-B.z)*(C.z-B.z));
}

// Kiem tra tinh thang hang

int ThangHang(){
if (AB()+CB()==AC()||AB()+CB()==AB()||AB()+AC()==CB())
return 1;
return 0;
}
// Tinh chu vi dien tich
void CV_DT(){
if (ThangHang()==1)
cout<<"\n 3 diem A,B,C thang hang";
else{
cout<<"\n Chu vi: "<<(AB()+CB()+AC());
float p=(AB()+CB()+AC())/2;

cout<<"\n Dien tich: "<<sqrt(p*(p-AB())*(p-AC())*(p-CB()));

}
}

void main(){
clrscr();
NhapToaDo();

CV_DT();

getch();
}

1 nhận xét:

Cảm ơn bạn đã quan tâm tới bài viết này!