Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
2
Добавлен:
30.05.2020
Размер:
241.65 Кб
Скачать

Контрольні питання:

1. Знайти помилку в програмі:

2. Визначити результат виконання програми:

3. Скласти програму для обчислення площі кільця по значеннях внутрішнього і зовнішнього радіусів, використовуючи функцію обчислення площі круга.

4. Дано три цілі числа. Визначити, сума цифр якого з них більша. Підрахунок суми цифр організувати через функцію.

5. Скласти функцію, що визначає, чи є її цілий аргумент простим числом. Використовувати цю функцію для підрахунку кількості простих чисел в послідовності з десяти цілих чисел, що вводяться з клавіатури.

6. Описати рекурсивну функцію stepen (x, n) від речового х (х >= 0) і цілого n, яка обчислює величину xn згідно формулі

7. Дані натуральні числа n і m; знайти НОД(n, m). Скласти рекурсивну функцію обчислення НОД, засновану на співвідношенні НОД(n, m)= НОД(m, r), де r — залишок від ділення n на m (n > m).

Відповіді:

  1. #include <iostream.h>

int sum(int,int);

void main()

{ int a=1, b=2, c;

c=sum(a,b);

cout<<c;

int sum(int x, int y)

{return x+y;}

2. S=36;

3.

4.

5.

6.

7.

Код програми:

#include <vcl.h>

#pragma hdrstop

#include "Unit1.h"

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

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

int summ(int);

float Plowa(int);

double stepen (double x, int n){

double k=1;

if (n==0) return 1;

if (n<0) return (1/x)*stepen(x,n+1);

else return x*stepen(x,n-1);

}

int NOD(int n, int m)//Функция

{

if(m==0)

return n;//Выход из рекурсии

return NOD(m,n%m);//Рекурсивный вызов

}

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

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

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

void __fastcall TForm1::FormCreate(TObject *Sender)

{

Edit1->Clear();

Edit2->Clear();

Edit3->Clear();

Edit4->Clear();

Edit5->Clear();

Edit6->Clear();

Edit7->Clear();

Edit8->Clear();

Edit9->Clear();

Memo1->Clear();

Memo2->Clear();

}

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

void __fastcall TForm1::Button1Click(TObject *Sender)

{

int p1, p2, p3;

p1=StrToInt(Edit1->Text);

p2=StrToInt(Edit2->Text);

p3=StrToInt(Edit3->Text);

int s1=summ(p1);

int s2=summ(p2);

int s3=summ(p3);

if(s1>s2)

if(s1>s3) Label5->Caption="1";

//s1 -- наибольшая

else Label5->Caption="3";//s3 -- наибольшая

else if(s2>s3) Label5->Caption="2";

//s2 -- наибольшая

else Label5->Caption="3";//s3 -- наибольшая

}

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

int summ(int p)//Функция

{

int s=0;

while(p!=0)

{

s+=p%10;

p/=10;

}

return s;

}

void __fastcall TForm1::Button2Click(TObject *Sender)

{

Edit1->Clear();

Edit2->Clear();

Edit3->Clear();

Label5->Caption="";

}

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

void __fastcall TForm1::Button3Click(TObject *Sender)

{

int i, a[10], k=0; // Декларація одновимірного масиву

//Заповнення масиву А елементами з таблиці StringGrid1

for(i=0; i<10;i++)

a[i]=StrToInt(StringGrid1 ->Cells[i][0]);

for(i=0; i<10;i++){

if (a[i]==2) k++;

else

if (a[i]==3) k++;

else

if(a[i]==5) k++;

else

if (a[i]%2!=0 && a[i]%5!=0 && a[i]%3!=0) k++;

else k=k+0;

}

Label8->Caption=IntToStr(k);

}

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

void __fastcall TForm1::Button4Click(TObject *Sender)

{

int i;

for(i=0; i<10;i++)

StringGrid1 ->Cells[i][0]="";

Label8->Caption="";

}

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

void __fastcall TForm1::Button5Click(TObject *Sender)

{

int r, R;

float pl;

R=StrToFloat(Edit4->Text);

r=StrToFloat(Edit5->Text);

pl=Plowa(R)-Plowa(r);//Вызов

Label11->Caption="Площа кільця="+FloatToStrF(pl,ffFixed,5,3);

}

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

float Plowa(int r)//Функция

{

return 3.141592*r*r;

}

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

void __fastcall TForm1::Button6Click(TObject *Sender)

{

Edit4->Clear();

Edit5->Clear();

Label11->Caption="Площа кільця=";

}

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

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit1->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit2KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit2->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit3KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit3->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit4KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit4->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit5KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit5->SetFocus(); return;}

}

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

void __fastcall TForm1::StringGrid1KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {StringGrid1->SetFocus(); return;}

}

void __fastcall TForm1::Button7Click(TObject *Sender)

{

int n=StrToInt(Edit7->Text);

double x=StrToFloat(Edit6->Text);

double otv = stepen(x,n);

Memo1->Lines->Add(FloatToStrF(otv,ffFixed,8,2));

}

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

void __fastcall TForm1::Button8Click(TObject *Sender)

{

Edit6->Clear();

Edit7->Clear();

Memo1->Clear();

}

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

void __fastcall TForm1::Button9Click(TObject *Sender)

{

int n=StrToInt(Edit8->Text);

int m=StrToInt(Edit9->Text);

int p=NOD(n,m);//Вызов в основной программе

Memo2->Lines->Add(FloatToStrF(p,ffFixed,8,2));

}

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

void __fastcall TForm1::Button10Click(TObject *Sender)

{

Edit8->Clear();

Edit9->Clear();

Memo2->Clear();

}

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

void __fastcall TForm1::Edit6KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit6->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit7KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit7->SetFocus(); return;}

}

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

void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key)) Key=0;

if(Key==VK_RETURN) { return;}

}

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

void __fastcall TForm1::Memo2KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key)) Key=0;

if(Key==VK_RETURN) { return;}

}

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

void __fastcall TForm1::Edit8KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit8->SetFocus(); return;}

}

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

void __fastcall TForm1::Edit9KeyPress(TObject *Sender, char &Key)

{

if (IsCharAlpha(Key) || Key==',' || Key=='.' || Key=='-') Key=0;

if(Key==VK_RETURN) {Edit9->SetFocus(); return;}

}

Соседние файлы в папке ТПСПП Сдать! Пр3!