C++ Builderda istisnolar va xatoliklarni ushlash usullari
3 ta butun son berilgan. Shu sonlar asosida qanday uchburcha yasash mumkinligini aniqlovchi dastur tuzilsin. Dasturni testlash uchun varianlar ishlab chiqilsin. Dasturni testlalash amalga oshirilsin.
1. C++ Builderda quyidagicha forma hosil qilamiz.
2. Aniqlash tugmasida quyidagicha kod kiritiladi:
//-------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float a, b, c;
String s;
// urinishlar soni
soni++;
s = IntToStr(soni) + ")";
try
{
a = StrToFloat(Edit1->Text);
} // haqiqiy songa o'girishda xatolik sodir bo'lsa
catch (EConvertError &)
{
Memo1->Lines->Add(s + "A = " + Edit1->Text);
Memo1->Lines->Add("A ga qiymat xato kiritildi
qayta kiritng!");
//ShowMessage("A ga qiymat xato kiritildi qayta kiritng!");
Edit1->SetFocus();
return ;
}
try
{
b = StrToFloat(Edit2->Text);
}
catch (EConvertError &)
{
Memo1->Lines->Add(s + "B = " + Edit2->Text);
Memo1->Lines->Add("B ga qiymat xato kiritildi
qayta kiritng!");
//ShowMessage("B ga qiymat xato kiritildi qayta kiritng!");
Edit2->SetFocus();
return ;
}
try
{
c = StrToFloat(Edit3->Text);
}
catch (EConvertError &)
{
Memo1->Lines->Add(s + "C = " + Edit3->Text);
Memo1->Lines->Add("C ga qiymat xato kiritildi
qayta kiritng!");
//ShowMessage("C ga qiymat xato kiritildi qayta kiritng!");
Edit3->SetFocus();
return ;
}
if (a <= 0)
{
Memo1->Lines->Add(s + "A = " + Edit1->Text);
Memo1->Lines->Add("A ga musbat haqiqiy son kiritng!");
Edit1->SetFocus();
return ;
}
if (b <= 0)
{
Memo1->Lines->Add(s + "B = " + Edit2->Text);
Memo1->Lines->Add("B ga musbat haqiqiy son kiritng!");
Edit2->SetFocus();
return ;
}
if (c <= 0)
{
Memo1->Lines->Add(s + "C = " + Edit3->Text);
Memo1->Lines->Add("C ga musbat haqiqiy son kiritng!");
Edit3->SetFocus();
return ;
}
Memo1->Lines->Add(s + " A = " + Edit1->Text + " B = " +
Edit2->Text + " C = " + Edit3->Text);
// uchburchak tengsizligini tekshirish
if (!((a < b + c) && (b < a + c) && (c < a + b)))
{
Memo1->Lines->Add("Kiritilganlar asosida uchburchak
yasash mumkin emas");
return ;
}
// teng tanomli uchburchak ekanini tekshirish
if (a == b && b == c)
{
Memo1->Lines->Add("Teng tanomli uchburchak yasash mumkin");
return ;
}
// teng yonli uchburchak ekanini tekshirish
if (a == b || b == c || a == c)
{
Memo1->Lines->Add("Teng yonli uchburchak yasash mumkin");
return ;
}
Memo1->Lines->Add("Uchburchak yasash mumkin");
}
//--------------------------------------------------------------------
3. Testlash variantini ishlab chiqamiz va dasturni testlaymiz:
Dastur natijasi to’g’ri chiqsa bir xato bo’lsa nol ball beriladi.
Do'stlaringiz bilan baham: |