//---------------------------------------------------------------------------
// Проект 14. Номинал резистора по цвету
// Четыре цветные полосы → сопротивление и допуск.
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------

// Декодер цветовой маркировки. Индексы 0..9 — цифры, отдельно множитель
// и допуск. ComboBox-ы на форме хранят выбор цвета каждой полосы.
double bandValue(int b1, int b2, int mult)
{
    return (b1 * 10 + b2) * pow(10.0, mult);
}
AnsiString tolText(int t)   // 0=±5% (золотой),1=±10%(серебр),2=±1%,3=±2%
{
    switch (t){ case 0:return "±5%"; case 1:return "±10%"; case 2:return "±1%"; case 3:return "±2%"; }
    return "±20%";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonCalcClick(TObject *Sender)
{
    int b1=ComboB1->ItemIndex, b2=ComboB2->ItemIndex;
    int m =ComboMul->ItemIndex, t=ComboTol->ItemIndex;
    if (b1<0||b2<0||m<0){ LabelR->Caption="Выберите цвета полос"; return; }
    double r = bandValue(b1,b2,m);
    AnsiString s;
    if (r>=1e6) s=FormatFloat("0.##",r/1e6)+" МОм";
    else if (r>=1e3) s=FormatFloat("0.##",r/1e3)+" кОм";
    else s=FormatFloat("0.##",r)+" Ом";
    LabelR->Caption = s + "  " + tolText(t);
}
//---------------------------------------------------------------------------
