Mình có 2 bài toán, một là của bạn hai là của mình
Không hiểu là một trong hai bài nó sai ở chỗ nào
Nên mình up lên đây để mọi người tìm lỗi giúp mình với
Bài toán: Nhập tọa độ 4 điểm ABCD, kiểm tra xem D có nằm trong tam giác ABC hay không
Bài thứ nhất
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main()
{
float xa,xb,xc,xd,ya,yb,yc,yd,ab,ac,bc,ad,bd,cd,s,s1,s2,s3,p;
cout<<"\nNhap toa do cua 4 diem(xa,ya,xb,yb,xc,yc,xd,yd)\n";
cin>>xa>>ya>>xb>>yb>>xc>>yc>>xd>>yd;
ab=sqrt(pow((xa-xb),2)+pow((ya-yb),2));
cout<<"\nab = "<<ab;
ac=sqrt(pow((xa-xc),2)+pow((ya-yc),2));
cout<<"\nac= "<<ac;
bc=sqrt(pow((xb-xc),2)+pow((yb-yc),2));
cout<<"\nBC = "<<bc;
ad=sqrt(pow((xa-xd),2)+pow((ya-yd),2));
cout<<"\nAD = "<<ad;
bd=sqrt(pow((xb-xd),2)+pow((yb-yd),2));
cout<<"\nBD = "<<bd;
cd=sqrt(pow((xc-xd),2)+pow((yc-yd),2));
cout<<"\nCD = "<<cd;
p=(ab+ac+bc)/2;
s=sqrt(p*(p-ab)*(p-bc)*(p-ac));
cout<<"\ns = " <<s;
p=(ad+ac+cd)/2;
s1=sqrt(p*(p-ad)*(p-ac)*(p-cd));
cout<<"\ns1 = "<<s1;
p=(ad+ab+bd)/2;
s2=sqrt(p*(p-ad)*(p-ab)*(p-bd));
cout<<"\n s2 = "<<s2;
p=(bd+cd+bc)/2;
s3=sqrt(p*(p-bd)*(p-cd)*(p-bc));
cout<<"\n s3 = "<<s3;
if(s==(s1+s2+s3)) cout<<"\nD thuoc tam giac ABC";
else cout<<"\nD khong thuoc tam giac ABC";
getch();
return 0;
}
Bài thứ hai
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int xA,xB,xC,yA,yB,yC,xD,yD;
float SS,AB,AC,BC,DA,DB,DC,S,S1,S2,S3;
float chieu_dai_canh(int x1,int y1, int x2, int y2){
return (sqrt((pow((x2-x1),2))+(pow((y2-y1),2))));
}
float tinh_dien_tich(float x,float y, float z){
float p;
p=(x+y+z)/2;
return (sqrt(p*(p-x)*(p-y)*(p-z)));
}
main(){
cout<<"nhap toa do diem A, lan luot x:y ";cin>>xA;cin>>yA;
cout<<"nhap toa do diem B, lan luot x:y ";cin>>xB;cin>>yB;
cout<<"nhap toa do diem C, lan luot x:y ";cin>>xC;cin>>yC;
cout<<"nhap toa do diem D, lan luot x:y ";cin>>xD;cin>>yD;
AB=chieu_dai_canh(xA,yA,xB,yB);cout<<"AB ="<<AB;
AC=chieu_dai_canh(xA,yA,xC,yC);cout<<"\nAC ="<<AC;
BC=chieu_dai_canh(xB,yB,xC,yC);cout<<"\nBC ="<<BC;
DA=chieu_dai_canh(xD,yD,xA,yA);cout<<"\nDA ="<<DA;
DB=chieu_dai_canh(xD,yD,xB,yB);cout<<"\nDB ="<<DB;
DC=chieu_dai_canh(xD,yD,xC,yC);cout<<"\nDC ="<<DC;
// tinh dien tich cua cac tam giac
S=(int)(tinh_dien_tich(AB,AC,BC));cout<<"\nS="<<S;
S1=tinh_dien_tich(AB,DB,DA);cout<<"\nS1="<<S1;
S2=tinh_dien_tich(BC,DB,DC);cout<<"\nS2="<<S2;
S3=tinh_dien_tich(AC,DA,DC);cout<<"\nS3="<<S3;
SS=(int)(S1+S2+S3);
if (S==SS) cout<<" \nD thuoc tam giac ABC";
else cout<<"\nD khong thuoc tam giac ABC";
getch();
}
Cảm ơn các bác các anh chị em và các cô chú nhiều
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?