Розділ 2. Програмування на Delphi

Задача 1

В заданий наступний текст входять тільки цифри та букви. Визначити, чи задовольняє він наступній умові: текст співпадає з кінцевим відрізком ряду 0123456789 (наприклад: 9, 89, 789).

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string text;

Console.WriteLine("Введiть текст");

text = Convert.ToString(Console.ReadLine());

switch (text)

{

case "9": Console.WriteLine("Текст спiвпадає");

break;

case "89": Console.WriteLine("Текст спiвпадає");

break;

case "789": Console.WriteLine("Текст спiвпадає");

break;

case "6789": Console.WriteLine("Текст спiвпадає");

break;

case "56789": Console.WriteLine("Текст спiвпадає");

break;

case "456789": Console.WriteLine("Текст спiвпадає");

break;

case "3456789":Console.WriteLine("Текст спiвпадає");

break;

case "23456789":Console.WriteLine("Текст спiвпадає");

break;

case "123456789":Console.WriteLine("Текст спiвпадає");

break;

case "0123456789":Console.WriteLine("Текст спiвпадає");

break;

default: Console.WriteLine("Текст не спiвпадає");

break;

}

Console.ReadKey();

}

}

}

Задача 2

Дано послідовність із 100 цілих чисел. Визначити кількість чисел в найбільш довгій послідовності із нулів, що йдуть підряд.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

int[] num = new int[100];

int[] m = new int[100];

Random rang = new Random();

int i, k, j = 0,max;

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

{

k = rang.Next(-1, 1);

num[i] = k;

}

foreach (int x in num)

{

Console.Write(x + " ");

}

Console.WriteLine();

k = 0;

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

{

if (num[i] == 0)

m[j]++;

if (num[i]!= 0)

j++;

}

/* foreach (int x in m)

{

Console.Write(x + " ");

}*/

max = m[0];

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

{

if (m[i] > max)

max = m[i];

}

Console.WriteLine("Кiлькiсть чисел в найдовшiй послiдовностi нулiв = " + max);

Console.ReadKey();

}

}

}

Задача 3

Описати рекурсивну функцію pow(x,n) від натурального x (x≠0) та цілого n, яка обраховує величину x^n за формулою:

x^n=

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class a

{

public double p;

public double pow(int x, int n)

{

if (n == 0)

p = 1;

else

if (n < 0)

p = 1 / pow(x, -n);

else

p = x * pow(x, n-1);

return p;

}

}

class Program

{

static void Main(string[] args)

{

a A =new a();

Console.WriteLine("Введiть x та n");

int x = Int32.Parse(Console.ReadLine());

int n = Int32.Parse(Console.ReadLine());

if (x == 0)

{

Console.WriteLine("Данi введено не коректно");

}

else

{

double b = A.pow(x, n);

Console.WriteLine(b);

}

Console.ReadKey();

}

}

}

Задача 4

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string str;

string strpat = "1";

Console.WriteLine("Введiть текст");

str = Convert.ToString(Console.ReadLine());

if (str[0] == ('1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'))

{

strpat = "[0-9]{1}([a-zA-Zа-яА-Я])";

}

else

{

strpat = @"\b[0-9]{1}([a-zA-Zа-яА-Я])";

}

Regex pat = new Regex(strpat);

Match match = pat.Match(str);;

if (match.Success)

{

if ((str[0]!= '0') & (Int32.Parse(Convert.ToString(str[0])) == str.Length - 1))

Console.WriteLine("Текст задовольняє умовi");

else

Console.WriteLine("Текст не задовольняє умовi");

}

else

Console.WriteLine("Текст не задовольняє умовi");

Console.ReadKey();

}

}

}

Задача 5

Дано натуральне і дійсні числа x1,y1,…,xn,yn. Розглядаючи пари xi,yi як координати точок на площині, визначити радіус найменшого круга (з центром в точці початку координат), в середину якого потрапляють всі ці точки.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Введiть кiлькiсть пар");

int p = int.Parse(Console.ReadLine());

Console.WriteLine("Введiть пари чисел, кожна пара з нового рядка");

int[,] num = new int[p, 2];

double[] len = new double[p];

int i;

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

{

string str = Console.ReadLine();

for (int j = 0; j < 2; j++)

{

int El = Convert.ToInt32(str.Split(' ')[j]);

num[i, j] = El;

}

}

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

{

len[i] = Math.Pow(num[i, 0] * num[i, 0] + num[i, 1] * num[i, 1], 0.5);

}

Console.WriteLine("Радiус шуканого круга = "+len.Max());

Console.ReadLine();

}

}

}

Задача 6

Var A: array [1..15,1..20] of integer;

B: array [1..15] of Boolean;

За допомогою масиву A отримати масив B, присвоївши його k–му елементу значення TRUE якщо виконується вказана нижче умова, і значення FALSE інакше. k–тий рядок масиву – симетричний.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

int[,] mas = {

{1,2,3,5,6,6,5,3,2,1},

{3,4,5,6,7,7,6,5,1,3},

{2,4,5,6,7,7,6,5,1,2},

{1,1,1,1,1,1,1,1,1,1},

{3,4,5,6,7,2,2,6,7,3}};

bool[] bo = new bool[5];

int i,j,k;

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

bo[i] = true;

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

{

j = 9;

for (k = 0; k < 5;k++)

{

if (mas[i, k]!= mas[i, j])

bo[i] = false;

j--;

}

}

foreach (bool x in bo)

{

Console.WriteLine(x);

}

Console.ReadKey();

}

}

}

Задача 7

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

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

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

public partial class Form1: Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

}

private void label1_Click(object sender, EventArgs e)

{

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

Bitmap image1 = new Bitmap("F://man_in_red_tsh.bmp");

Bitmap image2 = new Bitmap("F://man_in_gre_tsh.bmp");

Bitmap image3 = new Bitmap("F://man_in_yel_tsh.bmp");

if (comboBox1.SelectedIndex == 0)

pictureBox3.Image = image1;

else

if (comboBox1.SelectedIndex == 1)

pictureBox3.Image = image2;

else

if (comboBox1.SelectedIndex == 2)

pictureBox3.Image = image3;

pictureBox3.Visible = true;

}

private void Form1_Shown(object sender, EventArgs e)

{

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)

{

Bitmap image4 = new Bitmap("F://man_in_blu_pan.bmp");

Bitmap image5 = new Bitmap("F://man_in_bro_pan.bmp");

Bitmap image6 = new Bitmap("F://man_in_gre_pan.bmp");

if (comboBox2.SelectedIndex == 0)

pictureBox4.Image = image4;

else

if (comboBox2.SelectedIndex == 1)

pictureBox4.Image = image5;

else

if (comboBox2.SelectedIndex == 2)

pictureBox4.Image = image6;

pictureBox4.Visible = true;

}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)

{

Bitmap image7 = new Bitmap("F://wom_in_red.bmp");

Bitmap image8 = new Bitmap("F://wom_in_blu.bmp");

Bitmap image9 = new Bitmap("F://wom_in_gre.bmp");

if (comboBox3.SelectedIndex == 0)

pictureBox5.Image = image7;

else

if (comboBox3.SelectedIndex == 1)

pictureBox5.Image = image8;

else

if (comboBox3.SelectedIndex == 2)

pictureBox5.Image = image9;

pictureBox5.Visible = true;

}

private void label5_Click(object sender, EventArgs e)

{

}

private void label5_DragLeave(object sender, EventArgs e)

{

}

private void label5_DragOver(object sender, DragEventArgs e)

{

}

private void label5_Leave(object sender, EventArgs e)

{

}

private void label5_MouseLeave(object sender, EventArgs e)

{

pictureBox6.Visible = false;

}

private void label5_MouseUp(object sender, MouseEventArgs e)

{

}

private void label5_MouseHover(object sender, EventArgs e)

{

}

private void label6_MouseHover(object sender, EventArgs e)

{

}

private void label6_MouseLeave(object sender, EventArgs e)

{

pictureBox7.Visible = false;

}

private void label6_MouseClick(object sender, MouseEventArgs e)

{

}

private void label6_Click(object sender, EventArgs e)

{

}

private void label5_MouseMove(object sender, MouseEventArgs e)

{

Bitmap image10 = new Bitmap("F://bro.bmp");

pictureBox6.Image = image10;

pictureBox6.Visible = true;

}

private void label6_MouseMove(object sender, MouseEventArgs e)

{

Bitmap image11 = new Bitmap("F://smile.bmp");

pictureBox7.Image = image11;

pictureBox7.Visible = true;

}

private void label7_MouseMove(object sender, MouseEventArgs e)

{

Bitmap image12 = new Bitmap("F://wom_hand.bmp");

pictureBox8.Image = image12;

pictureBox8.Visible = true;

Bitmap image13 = new Bitmap("F://man_hand.bmp");

pictureBox9.Image = image13;

pictureBox9.Visible = true;

}

private void label7_MouseLeave(object sender, EventArgs e)

{

pictureBox8.Visible = false;

pictureBox9.Visible = false;

}

private void label8_MouseMove(object sender, MouseEventArgs e)

{

Bitmap image14 = new Bitmap("F://man_dia.bmp");

pictureBox10.Image = image14;

pictureBox10.Visible = true;

}

private void label8_MouseLeave(object sender, EventArgs e)

{

pictureBox10.Visible = false;

}

private void label9_MouseMove(object sender, MouseEventArgs e)

{

Bitmap image15 = new Bitmap("F://wom_dia.bmp");

pictureBox11.Image = image15;

pictureBox11.Visible = true;

}

private void label9_MouseLeave(object sender, EventArgs e)

{

pictureBox11.Visible = false;

}

}

}


Розділ 2. Програмування на Delphi

Задача 1

В заданий наступний текст входять тільки цифри та букви. Визначити, чи задовольняє він наступній умові: текст співпадає з кінцевим відрізком ряду 0123456789 (наприклад: 9, 89, 789).

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Edit1: TEdit;

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

var s:string;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

s:=form1.Edit1.Text;

if ((s='9') or (s='89') or (s='789') or (s='6789') or (s='56789') or (s='456789')

or (s='3456789') or (s='23456789') or (s='123456789') or (s='0123456789'))

then showmessage('Текст співпадає')

else showmessage('Текст не співпадає');

end;

end.

Задача 2

Дано послідовність із 100 цілих чисел. Визначити кількість чисел в найбільш довгій послідовності із нулів, що йдуть підряд.

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Memo1: TMemo;

procedure FormShow(Sender: TObject);

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

var i,j,k,max:integer;

a:array[0..49] of integer;

m:array[0..49] of integer;

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);

begin

randomize;

for i:=0 to 49 do

form1.Memo1.Lines.Add(inttostr(random(2)));

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

max:=0;

for i:=0 to 49 do

begin

a[i]:=strtoint(form1.Memo1.Lines[i]);

m[i]:=0;

end;

j:=0;

k:=0;

for i:= 0 to 49 do

if (a[i] = 0) then

m[j]:=m[j]+1

else

if (a[i] <> 0) then

j:=j+1;

max:=m[0];

for i:= 0 to 49 do

if (max<m[i]) then

max:=m[i];

showmessage(inttostr(max));

end;

end.

Задача 3

Var A: array [1..15,1..20] of integer;

B: array [1..15] of Boolean;

За допомогою масиву A отримати масив B, присвоївши його k–му елементу значення TRUE якщо виконується вказана нижче умова, і значення FALSE інакше. k–тий рядок масиву – симетричний.

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, Grids;

type

TForm1 = class(TForm)

StringGrid1: TStringGrid;

StringGrid2: TStringGrid;

Button1: TButton;

procedure FormShow(Sender: TObject);

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

i,j,k:integer;

implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);

begin

randomize;

for i:=0 to 9 do

for j:=0 to 4 do

form1.StringGrid1.cells[i,j]:=inttostr(random(2));

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

for i:= 0 to 4 do

form1.StringGrid2.cells[0,i]:='true';

for i:= 0 to 4 do

begin

j:= 9;

for k:= 0 to 4 do

begin

if (strtoint(form1.StringGrid1.cells[k, i]) <> strtoint(form1.StringGrid1.cells[j, i]))

then

form1.StringGrid2.cells[0,i]:= 'false';

j:=j-1;

end;

end;

end;

end.



Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: