Об'єктно-орієнтоване програмування. Розробка програмного забезпечення
Міністерство
освіти і науки України
Львівський
національний університет ім. Івана Франка
Факультет
прикладної математики та інформатики
кафедра
теорії оптимальних процесів
Звіт з
навчальної практики
Об’єктно-орієнтоване
програмування. Розробка програмного забезпечення
Виконав: Сомик Григорій Тарасович,
студент ІІ курсу, групи ПМА-21
Перевірив: ас. Ковальчук О.В.,
доц. Мельничин А.В.
Львів - 2014
1. Програма зчитує з
файлу дві матриці, додає, множить (якщо це можливо) розв’язок виводить на
екран, та записує у файл.
#include <iostream>
#include <fstream>
#include "windows.
h"namespace std;tofile (ofstream& fout, float** A, int n, int m)
{(int i=0; i<n; i++)
{(int j=0; j<m; j++)
{<<A [i] [j] <<"
";
}<<'\n';
}<<'\n';
}** fromfile (ifstream& fin,
int& n, int& m)
{pos=fin. tellg
();k;c;=0;=0;p=true;(p)
{>>c;++;(fin. get () =='\n')
{
++n;(fin. get () =='\n') p=false;{
k=fin. tellg (); k--; fin. seekg (k); }
};
};/=n;**A=new float * [n];(int i=0;
i<n; i++)
{[i] =new float [m];
};. seekg (pos);(int i=0; i<n;
i++)
{(int j=0; j<m; j++)
{>>A [i] [j];. get ();
}
}. get ();A;
}output (float** A, int n, int m)
{if (A! =NULL)
{(int i=0; i<n; i++)
{(int j=0; j<m; j++)<<A [i]
[j] <<flush<<" ";<<endl;
}<<endl;
}
}** ADD (float** A, float** B, int
n1, int n2, int m1, int m2)
{( (n1==n2) && (m1==m2))
{float **C = new float * [n1];(int
i=0; i<n1; i++)[i] = new float [m1];(int i=0; i<n1; i++)(int j=0;
j<m1; j++)[i] [j] =A [i] [j] +B [i] [j];C;
}
{<<"ADD is not possible"<<endl;NULL;
}
}** MUL (float** A, float** B, int
n1, int n2, int m1, int m2)
{(m1==n2)
{**C = new float * [n1];(int i=0;
i<n1; i++)[i] = new float [m2];(int i=0; i<n1; i++)
{(int j=0; j<m2; j++)
{[i] [j] = 0;(int k=0; k<m1; k++)
{[i] [j] += (A [i] [k] *B [k] [j]);
}
}
}C;
}
{<<"MUL is not
possible"<<endl;NULL;
}
}main ()
{(1251);fin ("input.
txt");fout;. open ("output. txt");**A,**B,
**C,**D;n1,n2,n3,m1,m2,m3,n4,m4;n,m,pos;<<"Вхідні дані: \nA:
\n";=fromfile (fin,n1,m1);(A,n1,m1);=fromfile (fin,n2,m2);<<"B:
\n";(B,n2,m2);=ADD (A,B,n1,n2,m1,m2);=MUL
(A,B,n1,n2,m1,m2);<<"Результат: \nA+B: \n";(C! =NULL)
{=n1;=m1;(C,n3,m3);(fout,C,n3,m3);
}<<"A*B\n";(D!
=NULL)
{=n1;=m2;(D,n4,m4);(fout,D,n4,m4);
}. close ();. close ();. get ();.
get ();0;
}
. Програма яка видаляє з
рядку всі слова які починаються та закінчуються на одну й ту саму букву, рядок
вводимо з клавіатури.
#include <iostream>
#include <string>
#include "windows.
h"namespace std;edit (string& s)
{ss="",
word="";i=0;(i<s. length ())
{(isalpha ( (unsigned char) s [i])
&& i<s. length ()) {word+=s [i]; if (i==s. length () - 1) break;
else i++; }(word [0]! =word [word. length () - 1]) ss+=word;i++;="";
}{ss+=s [i]; i++; }
}=ss;
}main ()
{(1251);(1251);s,a;<<"Введіть
рядок: \n";(cin,s);(s);<<"Редагований рядок:
"<<endl;<<s;. get ();. get ();0;
}
. Програма сортує слова
у рядку, рядок вводимо з клавіатури.
#include <iostream>
#include <string>
#include "windows.
h"namespace std;sort (string& s)
{ss="",
word="";i=0;k=0;+=" ";(i<s. length ())
{(isalpha ( (unsigned char) s [i]))
{(isalpha ( (unsigned char) s [i])
&& i<s. length ()) {word+=s [i]; if (i==s. length () - 1) break; else
i++; }++;="";
}{i++; }
}*smas=new string [k];=0;=0;(i<s.
length ())
{(isalpha ( (unsigned char) s [i]))
{(isalpha ( (unsigned char) s [i])
&& i<s. length ()) {word+=s [i]; if (i==s. length () - 1) break;
else i++; }[k++] =word;="";
}{i++; }
}="";s5="";(int
j=0; j<k-1; j++)
{for (int l=0; l<k-j-1; l++)
{if (smas [l+1] <smas [l])
{s5=smas [l]; smas [l] =smas [l+1]; smas [l+1] =s5; }}}(int j=0; j<k;
j++)+=smas [j] +" ";
}main ()
{(1251);(1251);s,a;<<"Введіть
рядок: \n";(cin,s);(s);<<"Редагований рядок: "<<endl;<<s;.
get ();. get ();0;
}
. Програма, яка у
кожному слові рядка переставляє усі цифри в кінець слова, без зміни порядку.
#include <iostream>
#include <string>
#include "windows.
h"namespace std;edit (string& s)
{ss="", word="",
digit="";i=0;(i<s. length ())
{(isalnum ( (unsigned char) s [i]))
{(s [i] ==' ') break;(isalnum (
(unsigned char) s [i]) && i<s. length ())
{(s [i] ==' ') break;(isdigit (
(unsigned char) s [i])) digit+=s [i];word+=s [i];(i==s. length () - 1) break;
else i++;
}=ss+word+digit+"
";="";="";++;
}{ss+=s [i]; i++; }
}(int i=0; i<ss. length () - 1;
i++)[i] =ss [i];
}main ()
{(1251);(1251);s,a;<<"Введіть
рядок: \n";(cin,s);(s);<<"Редагований рядок:
"<<endl;<<s;. get ();. get ();0;
}
. Програма, яка з
заданого масиву виводить на екран ті елементи які є степенями двійки. Для
введення масиву та перевірки елементу ми використовуємо покажчики на функцію,
передаючи їх параметрами функції.
#include <iostream>
#include "windows.
h"namespace std;* input (int n)
{*mas=new double [n];(int i=0;
i<n; i++)
{<<"mas
["<<i+1<<"] =";>>mas [i];
}mas;
}check (double a)
{p=false;(double i=1; i<=1000;
i++)(a==pow (2, i)) { p=true; break; }p;
}dosmth (int n, bool (*p1) (double),
double* (*p2) (int))
{* mas=p2
(n);<<"Результат: "<<endl;(int i=0; i<n; i++)(p1 (mas
[i])) cout<<mas [i] <<" ";
}main ()
{(1251);(1251);(*f1) (double);*
(*f2) (int);=check;=input;n=0;(n<=0)
{<<"Введіть кількість
n=";>>n;
}(n,f1,f2);. get ();. get ();0;
}
. Програма, яка зчитує з
файлу структури, в яких міститься інформація про об’єкти, записує кожну
структуру в окремий файл, сортує їх (подвійне сортування), та від сортовані
структури записує у файл.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "windows.
h"namespace std;struct
{name;type;
}port;struct
{producer;category;portcount;priority,
price;* ports;
}product;tofile (ofstream&
fout,product p)
{<<p.
category<<endl;<<p. producer<<endl;<<p.
priority<<endl;<<p. price<<endl;<<p.
portcount<<endl;(int i=0; i<p. portcount; i++)
{<<p. ports [i].
name<<endl;<<p. ports [i]. type<<endl;
}
{<<p.
category<<endl;<<p. producer<<endl;<<p.
priority<<endl;<<p. price<<endl;<<p.
portcount<<endl;(int i=0; i<p. portcount; i++)
{<<p. ports [i].
name<<endl;<<p. ports [i]. type<<endl;
}<<endl;
}* fromfile (ifstream& fin,
int& n)
{k=0;p;(! fin. eof ())
{(fin,p. category);(fin,p.
producer);>>p. priority;>>p. price;>>p. portcount;. get
();(int i=0; i<p. portcount; i++)
{(fin,p. producer);(fin,p.
producer);
}. get ();++;
}=k;* A=new product [n];=0;. clear
();. seekg (0);(! fin. eof ())
{(fin,p. category);(fin,p.
producer);>>p. priority;>>p. price;>>p. portcount;. get ();.
ports = new port [p. portcount];(int i=0; i<p. portcount; i++)
{(fin,p. ports [i]. name);(fin,p.
ports [i]. type);
}. get ();[k] =p;++;
}A;
}main ()
{(1251);(1251);n,a,b;* A;fin
("input. txt");=fromfile (fin,n);(int i=0; i<n; i++)
{:: stringstream out;<< i;s=
out. str ();="product"+s+". txt";fout (s);(fout,A [i]);.
close ();
}<<"сортувати за ціною
введіть 0, за пріорітитем введіть інше ціле число ";>>a;(a! =0)
{for (int j=0; j<n-1; j++)
{(int i=0; i<n-1; i++)(A [i].
priority>A [i+1]. priority) {product p=A [i]; A [i] =A [i+1]; A [i+1] =p; }
}
}
{for (int j=0; j<n-1; j++)
{(int i=0; i<n-1; i++)(A [i].
price>A [i+1]. price) {product p=A [i]; A [i] =A [i+1]; A [i+1] =p; }
}
};<<"сортувати за
категорією введіть 0, за виробником введіть інше ціле число ";>>b;(b==0)
{for (int j=0; j<n-1; j++)
{(int i=0; i<n-1; i++)(A [i].
category>A [i+1]. category) {product p=A [i]; A [i] =A [i+1]; A [i+1] =p; }
}
}
{for (int j=0; j<n-1; j++)
{(int i=0; i<n-1; i++)(A [i].
producer>A [i+1]. producer) {product p=A [i]; A [i] =A [i+1]; A [i+1] =p; }
}
};fout ("sorteddata.
txt");(int i=0; i<n; i++)
{(fout,A [i]);(i! =n-1)
fout<<endl;(A [i]);
}. close ();. get ();. get ();.
close ();0;
}
7. Програма, яка за
допомогою класу працює зі стеком, додає елемент у стек, забирає елемент зі
стеку, додає вектор у стек, видаляє усі елементи зі стеку, перевіряє чи стек не
пустий.
struct list
{val;list* next;
};Stack
{:list* item;:();
~Stack ();isempty ();pop ();push
(int);delall ();pushvect (int*, int);
};
#include <iostream>
#include "classheader.
h":: Stack ()
{=new list;=NULL;::
cout<<"стек створено\n";
}Stack:: isempty ()
{p;(item==NULL) {p=true; std::
cout<<"стек пустий\n"; } else p=false;p;
}Stack:: push (int a)
{list* p = new list;>val =
a;(isempty ()) p->next = NULL;p->next = item;=p;::
cout<<"елемент "<<a<<" додано у стек\n";
}Stack:: pop ()
{a;(isempty ()) {a=0; }(! isempty
())
{= item->val;list* p =item;=
item->next;p;:: cout<<"елемент "<<a<<"
забрано зі стеку\n";
};a;
}Stack:: delall ()
{:: cout<<"видалено усі
елементи зі стеку, ";list*p;(! isempty ())
{=item;=item->next;p;
}
}Stack:: pushvect (int* a, int n)
{(int i=0; i<n; i++)(a [i]);::
cout<<"вектор з "<<n<<" елементів відправлено
у стек\n";
}:: ~Stack ()
{:: cout<<"пам'ять
звільнено, ";();item;
}
#include <iostream>
#include "classheader. h"
#include "windows. h"main
()
{(1251);(1251);* b=new int
[10];a;(int i=0; i<10; i++)[i] =i;. pushvect (b,10);c;(int i=0; i<10;
i++)=a. pop ();. push (10);. ~Stack ();:: cin. get ();0;
}
#include <iostream>
#include <fstream>
#include <string>
#include "windows.
h"Engine
{::: string name;count, power;()
{=0;=0;="";
}GetSumPower () {return power*count;
}
};main ()
{(1251);(1251);*A=new Engine
[4];*B=new Engine [4];:: ofstream fout ("data. txt");(int i=0;
i<4; i++)
{
{:: cout<<"Введіть назву
"<<i+1<<" двигуна: ";:: cin>>A [i]. name;
}while (A [i]. name=="");
{:: cout<<"Введіть
потужність "<<i+1<<" двигуна: ";:: cin>>A [i].
power;
}while (A [i]. power<=0);
{:: cout<<"Введіть
кількість кінських сил "<<i+1<<" двигуна: ";::
cin>>A [i]. count;
}while (A [i]. count<=0);. write
( (char*) & (A [i]),sizeof (A [i]));:: cout<<std:: endl;
}. close ();:: ifstream fin
("data. txt");(int i=0; i<4; i++)
{. read ( (char*) & (B
[i]),sizeof (B [i]));:: cout<<B [i]. name<<"
кількість*потужність: "<<B [i]. GetSumPower () <<std:: endl;
}. close ();:: cin. get ();:: cin.
get ();0;
}
. Програма, в який є
базовий абстрактний клас фігура та два похідні класи: трикутник та
чотирикутник. Зчитуємо з клавіатури n фігур (прямокутників,
трикутників) та рахуємо їхню площу.
#include <iostream>
#include <string>
#include "windows.
h"Figure
{::: string name;:float Square ()
=0;void Show () =0;
};Triangle: public Figure
{:a,b,c;:(int a1=1, int b1=1, int
c1=1)
{="Трикутник";
(a1<=0)? a=1: a=a1;
(b1<=0)? b=1: b=b1;
(c1<=0)? c=1: c=c1;
}
~Triangle ()
{:: cout<<name<<"
"<<a<<" "<<b<<"
"<<c<<" said bye\n";
}Square ()
{p= (a+b+c) /2;std:: sqrt (p* (p-a)
* (p-b) * (p-c));
}Show ()
{:: cout<<name<<"
"<<a<<" "<<b<<" "<<c;
}
};Rect: public Figure
{:a,b;:(int a1=1, int b1=1)
{="Прямокутник";
(a1<=0)? a=1: a=a1;
(b1<=0)? b=1: b=b1;
}
~Rect ()
{:: cout<<name<<"
"<<a<<" "<<b<<" said bye\n";
}Square ()
{a*b;
}Show ()
{:: cout<<name<<"
"<<a<<" "<<b;;
}
};main ()
{(1251);(1251);n;
{:: cout<<"Введіть
кількість фігур n=";:: cin>>n;
}(n<=0);** B= new Figure*
[n];(int i=0; i<n; i++)
{p;:: cout<<i+1<<":
Введіть 0, щоб створити трикутник, або інше ціле число, щоб створити
прямокутник ";:: cin>>p;(p)
{a,b;:: cout<<"Введіть
сторону прямокутника a: ";:: cin>>a;:: cout<<"Введіть
сторону прямокутника b: ";:: cin>>b;[i] =new Rect (a,b);
}
{a,b,c;:: cout<<"Введіть
сторону трикутника a: ";:: cin>>a;:: cout<<"Введіть
сторону трикутника b: ";:: cin>>b;:: cout<<"Введіть
сторону трикутника c: ";:: cin>>c;[i] =new Triangle (a,b,c);
}
}(int i=0; i<n; i++)
{:: cout<<i+1<<"
"; B [i] - >Show (); std:: cout<<" площа: "<<B
[i] - >Square () <<"см кв. \n";
}:: cin. get ();:: cin. get ();0;
}
. Програма, яка
демонструє реалізацію множинного наслідування, є два батьківських класи
(чоловік, жінка) та похідний від них (дитина), створюємо екземпляр похідного
класу.
#include <iostream>
#include <ostream>
#include "windows. h"Man
{:tall, age;:: string name;(int
t=170, int a=18,std:: string n="Tasik")
{
(t<10)? tall=170: tall=t;
(a<0)? age=18: age=a;
(n=="")?
name="Tasik": name=n;
}void Show ()
{:: cout<<name<<"
"<<age<<" "<<age<<'\n';
}
};Woman
{:width, size;:: string name;(int
w=90, int s=40,std:: string n="Babinka")
{
(w<10)?: width=w;
(s<0)? s=40: size=s;
(n=="")?
name="Babinka": name=n;
}void Show ()
{:: cout<<name<<"
"<<width<<" "<<size<<'\n';
}
};Child: public Man, public Woman
{::Show ()
{:: cout<<Man::
name<<" "<<Man:: age<<" "<<Man::
tall<<" "<<Woman:: size<<"
"<<Woman:: width<<'\n';
}(int t=170, int a=18,std:: string
n="Tasik", int w=90, int s=40): Man (t,a,n),Woman (w,s,n) {}
};main ()
{(1251);(1251);tall,age,size,width;::
string name;
{:: cout<<"Введіть ім'я
дитини =";:: cin>>name;
}while (name=="" ||
name==" ");
{:: cout<<"Введіть вік
дитини =";:: cin>>age;
}while (age<=0);
{:: cout<<"Введіть висоту
дитини =";:: cin>>tall;
}while (tall<=0);
{:: cout<<"Введіть розмір
стопи дитини =";:: cin>>size;
}while (size<=0);
{:: cout<<"Введіть ширину
дитини =";:: cin>>width;
}while (width<=0);a
(tall,age,name,width,size);:: cout<<std:: endl;. Show ();:: cin. get
();:: cin. get ();0;
}
. Програма яка зчитує
дані з файлу, та створює прогресії та виводить їх суму.
#include <iostream>
#include <fstream>
#include "windows. h"base
{:q,a0;:double sumup (int)
=0;(double a01=1, double q1=0.5)
{
(a01==0)? a0=1: a0=a01;
(q1==0 || q1==1)? q=0.5: q=q1;
}
~base ()
{
};void show (std:: ostream& os)
{<<a0<<"
"<<q;
}void fromfile (std:: ifstream&
os)
{a01,q1;>>a01;>>q1;
(a01==0)? a0=1: a0=a01;
(q1==0 || q1==1)? q=0.5: q=q1;
}std:: ostream& operator<<
(std:: ostream& os, base& a)
{. show (os);os;
}std:: ifstream&
operator>> (std:: ifstream& os, base& a)
{. fromfile (os);os;
}
};progression: public base
{:(double a01=1, double q1=0.5):
base (a01,q1)
{
};double sumup (int n)
{(n<0) n=1;(a0-a0*pow (q,n) *q) /
(1-q);
}getN (int n)
{pow (q,n) *a0;
}
~progression () {};show (std::
ostream& os)
{<<"геометрична прогресія
";:: show (os);
}fromfile (std:: ifstream& os)
{:: fromfile (os);
{. show (os);os;
}std:: ifstream&
operator>> (std:: ifstream& os, progression& a)
{. fromfile (os);os;
}
};infinity_progression: public base
{::_progression (double a01=1,double
q1=0.5): base (a01,q1)
{
}sumup (int n=0)
{a0/ (1-q);
}show (std:: ostream& os)
{<<"нескінченна геометрична
прогресія ";:: show (os);
}fromfile (std:: ifstream& os)
{:: fromfile (os);
}std:: ostream& operator<<
(std:: ostream& os, infinity_progression& a)
{. show (os);os;
}std:: ifstream&
operator>> (std:: ifstream& os, infinity_progression& a)
{. fromfile (os);os;
}
~infinity_progression () {}
};main ()
{(1251);(1251);_progression a;b;::
ifstream fin ("input. txt");n,k=0;
{:: cout<<"Введіть n для
сум скінченних геометричних прогресій n=";:: cin>>n;
}(n<=0);(! fin. eof ())
{>>a;++;
}. clear ();. seekg (0);**mas=new
base* [k];(int i=0; i<k; i++)
{(i % 2==0)
{fin>>a; mas [i] =&a; }
{fin>>b; mas [i] =&b; }::
cout<<*mas [i] <<" "<<mas [i] - >sumup (n)
<<"\n";
}:: cin. get ();:: cin. get ();0;
}
об'єктне орієнтоване
програмування програма
. Програма, яка
демонструє переваження операторів для класу дробів.
#include <iostream>fraction
{:num;den;& simplify ();:();(int
a, unsigned b = 1);(const fraction& f);
~fraction ();show (std::
ostream& os) const;add (const fraction& f) const;operator+ (const
fraction& f) const;operator- (const fraction& f) const;operator/ (const
fraction& f) const;operator* (const fraction& f) const;operator= (const
fraction& f) const;bool operator== (const fraction& a, const fraction&
b);bool operator! = (const fraction& a, const fraction& b);bool
operator<= (const fraction& a, const fraction& b);bool operator>=
(const fraction& a, const fraction& b);bool operator< (const
fraction& a, const fraction& b);bool operator> (const fraction&
a, const fraction& b);
};:: ostream& operator<<
(std:: ostream& os, const fraction& f);
#include "fraction. h"
#include <iostream>:: fraction
(): num (0), den (1)
{= 0; den = 1;
}:: fraction (int a, unsigned b):
num (a)
{(b > 0) den = b;
{= 1;:: cerr << "error:
wrong denominator\n";
}();
}:: fraction (const fraction& f)
{= f. num; den = f. den;
}:: ~fraction ()
{
}fraction:: show (std:: ostream&
os) const
{<< num << '/' <<
den;
}fraction:: add (const fraction&
f) const
{a = this->num*f. den +
this->den*f. num;b = this->den * f. den;fraction (a,b). simplify ();
}fraction:: operator+ (const
fraction& f) const
{a = this->num*f. den +
this->den*f. num;b = this->den * f. den;fraction (a,b). simplify ();
}fraction:: operator= (const
fraction& f) const
{fraction (f);
}fraction:: operator/ (const
fraction& f) const
{a = this->num * f. den;b =
this->den * f. num;fraction (a,b). simplify ();
}fraction:: operator- (const
fraction& f) const
{a = this->num * f. den -
this->den * f. num;b = this->den * f. den;fraction (a,b). simplify ();
}fraction:: operator* (const
fraction& f) const
{a = this->num * f. num;b =
this->den * f. den;fraction (a,b). simplify ();
}& fraction:: simplify ()
{a = this->num;b =
this->den;(a! = b)
{(a > b) a - = b;b - = a;
}(a > 1)
{/= a;/= a;
}*this;
}:: ostream& operator<<
(std:: ostream& os, const fraction& f)
{. show (os);os;
{(a. num==b. num && a.
den==b. den) return true;return false;
}operator! = (const fraction& a,
const fraction& b)
{! (a==b);
}operator< (const fraction&
a, const fraction& b)
{a1=float (a. num) /a. den, b1=float
(b. num) /b. den;(a1<b1);
}operator> (const fraction&
a, const fraction& b)
{a1=float (a. num) /a. den, b1=float
(b. num) /b. den;(a1>b1);
}operator>= (const fraction&
a, const fraction& b)
{! (a<b);
}operator<= (const fraction&
a, const fraction& b)
{! (a>b);
}
#include "fraction. h"
#include
<iostream>makeDefaultFraction ()
{fraction ();
}retCopyFraction (fraction& t)
{t;
}main ()
{b (8,24);c (1,2);d=c;::
cout<<"b: "<<b<<"\n";::
cout<<"d: "<<d<<"\n";::
cout<<"d<b: "<< (d<b) <<"\n";::
cout<<"d>b: "<< (d>b) <<"\n";::
cout<<"d==b: "<< (d==b) <<"\n";::
cout<<"d! =b: "<< (d! =b) <<"\n";::
cout<<"d<=b: "<< (d<=b) <<"\n";::
cout<<"d>=b: "<< (d>=b) <<"\n";::
cout<<"d+b: "<< (d+b) <<"\n";::
cout<<"d-b: "<< (d-b) <<"\n";::
cout<<"d*b: "<< (d*b) <<"\n";::
cout<<"d/b: "<< (d/b) <<"\n";:: cin. get
();:: cin. get ();0;
}
. Програма, яка за
допомогою канви малює чотирикутник та визначає його тип, площу, периметр.
unit Unit1;, Messages, SysUtils,
Variants, Classes, Graphics, Controls, Forms,, Menus, Math, ComCtrls;= class
(TForm): TMainMenu;: TMenuItem;: TStatusBar;: TMenuItem;: TMenuItem;FormCreate
(Sender: TObject);FormMouseDown (Sender: TObject; Button: TMouseButton;:
TShiftState; X, Y: Integer);N1Click (Sender: TObject);FormMouseMove (Sender:
TObject; Shift: TShiftState; X,: Integer);N2Click (Sender: TObject);N3Click
(Sender: TObject);: string;: integer;: real;,b,c,d: real;,yr,zr,gr: real;:
array [1.4] of Tpoint;check (a,b,c,d: Tpoint): boolean;length (a,b: Tpoint):
real;corner (aa,bb,cc: Tpoint): real;checksquare: boolean;checkrect:
boolean;checkromb: boolean;
{ Public declarations };: TForm1;
{$R *. dfm}tform1. checkromb: boolean;(a=b) and (b=c) and (c=d)
and (d=a) then begin square: =1/2*length (pol [1],pol [3]) *length (pol [2],pol
[4]); result: =true end else result: =false;;Tform1. corner (aa,bb,cc: Tpoint):
real;a1,a2: tpoint; cos: real;. X: =aa. X-cc. X;. Y: =aa. Y-cc. Y;. X: =bb.
X-cc. X;. Y: =bb. Y-cc. Y;: = (a1. X*a2. X+a1. Y*a2. Y) / (sqrt (sqr (a1. X)
+sqr (a1. y)) *sqrt (sqr (a2. X) +sqr (a2. Y)));: =round (arccos (cos)
*180/pi);;tform1. checkrect: boolean;( (a=c) and (b=d)) and ( (xr=yr) and
(yr=zr) and (zr=gr) and (gr=xr)) then begin result: =true; square: =a*c; end
else result: =false;;Tform1. checksquare: boolean;( (a=d) and (d=b) and (b=c)
and (c=a)) and ( (xr=yr) and (yr=zr) and (zr=gr) and (gr=xr)) then begin
result: =true; square: =a*a; end else result: =false;;Tform1. length (a,b:
Tpoint): real;: =sqrt (sqr (a. X-b. X) +sqr (a. Y-b. Y));;Tform1. check (a:
TPoint; b: TPoint; c: TPoint; d: TPoint): boolean;A1,B1,C1,A2,B2,C2: integer;:
= (d. x-c. x) * (a. y-c. y) - (d. y-c. y) * (a. x-c. x);: = (d. x-c. x) * (b. y-c.
y) - (d. y-c. y) * (b. x-c. x);: = (b. x-a. x) * (c. y-a. y) - (b. y-a. y) *
(c. x-a. x);: = (b. x-a. x) * (d. y-a. y) - (b. y-a. y) * (d. x-a. x);: = (A1 *
B1 < 0) and (A2 * B2 < 0);;TForm1. FormCreate (Sender: TObject);:
=1;;TForm1. FormMouseDown (Sender: TObject; Button: TMouseButton;: TShiftState;
X, Y: Integer);p: tpoint; i,j: integer; sort: array [1.4] of tpoint; l:
real;k<=4 then begin. Canvas. Pixels [x,y]: =clblack;[k]. x: =x; pol [k]. y:
=y;: =k+1;beginj: = 1 to 4 doi: = 1 to 3 do if (pol [i]. X>pol [i+1]. x)
then begin p: =pol [i+1]; pol [i+1]: =pol [i]; pol [i]: =p; end;(not (check
(pol [1],pol [2],pol [3],pol [4]))) and (not (check (pol [1],pol [4],pol
[2],pol [3]))) then form1. Canvas. Polygon (pol)begin p: =pol [1]; pol [1]:
=pol [4]; pol [4]: =p;(not (check (pol [1],pol [2],pol [3],pol [4]))) and (not
(check (pol [1],pol [4],pol [2],pol [3]))) then form1. Canvas. Polygon
(pol)begin p: =pol [1]; pol [1]: =pol [2]; pol [2]: =p;(not (check (pol [1],pol
[2],pol [3],pol [4]))) and (not (check (pol [1],pol [4],pol [2],pol [3]))) then
form1. Canvas. Polygon (pol) end;: =length (pol [1],pol [2]) /37.79527559055;:
=length (pol [2],pol [3]) /37.79527559055;: =length (pol [3],pol [4])
/37.79527559055;: =length (pol [4],pol [1]) /37.79527559055;: =corner (pol
[2],pol [4],pol [1]);: =corner (pol [1],pol [3],pol [4]);: =corner (pol [4],pol
[2],pol [3]);: =corner (pol [3],pol [1],pol [2]);checksquare then info:
='квадрат' elsecheckromb then info: ='ромб' elsecheckrect then info:
='прямокутник' else begin l: = (a+b+c+d) /2; square: =sqrt ( (l-a) * (l-b) *
(l-c) * (l-d) - a*b*c*d*cos ( (xr+zr) /2)); info: ='чотирикутник';
end;;;TForm1. N1Click (Sender: TObject);(Form1. Canvas. Handle, 0, 0, Form1.
ClientWidth, Form1. ClientHeight, WHITENESS);: =1;;TForm1. FormMouseMove
(Sender: TObject; Shift: TShiftState; X,: Integer);. SimpleText: ='x:
'+inttostr (x) + ' y: '+inttostr (y);;TForm1. N2Click (Sender: TObject);p:
real;k=5 then begin p: =a+b+c+d; showmessage (info+' площа: '+floattostr (
(square)) +'см периметр: '+floattostr ( (p)) +'см'); end else showmessage
('спочатку намалюйте обєкт');;TForm1. N3Click (Sender: TObject);;;.
Програма, яка при натиску мишки на вікно переставляє дві кульки,
так, щоб вони не перетиналися
unit Unit1;, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,, ExtCtrls;= class (TForm): TShape;: TShape;Shape2MouseDown
(Sender: TObject; Button: TMouseButton;: TShiftState; X, Y:
Integer);Shape1MouseDown (Sender: TObject; Button: TMouseButton;: TShiftState;
X, Y: Integer);FormMouseDown (Sender: TObject; Button: TMouseButton;:
TShiftState; X, Y: Integer);
{ Private declarations }
{ Public declarations };: TForm1;
{$R *. dfm}TForm1. FormMouseDown (Sender: TObject; Button:
TMouseButton;: TShiftState; X, Y: Integer);x0,y0: integer;randomize;. Top:
=random (Form1. clientheight-Shape1. height);. Left: =random (Form1.
clientwidth-Shape1. width);: =random (Form1. clientheight-Shape2. height);:
=random (Form1. clientwidth-Shape2. width);not ( ( (Shape1. left+Shape1.
Width<x0) or (Shape1. left-Shape2. Width>x0)) and ( (Shape1. Top+Shape1.
Height<y0) or (Shape1. Top-Shape2. Height>y0))) doy0: =random (Form1.
clientheight-Shape2. height);: =random (Form1. clientwidth-Shape2. width);
end;. Top: =y0;. Left: =x0;;TForm1. Shape1MouseDown (Sender: TObject; Button:
TMouseButton;: TShiftState; X, Y: Integer);x0,y0: integer;randomize;. Top:
=random (Form1. clientheight-Shape1. height);. Left: =random (Form1.
clientwidth-Shape1. width);: =random (Form1. clientheight-Shape2. height);:
=random (Form1. clientwidth-Shape2. width);not ( ( (Shape1. left+Shape1.
Width<x0) or (Shape1. left-Shape2. Width>x0)) and ( (Shape1. Top+Shape1.
Height<y0) or (Shape1. Top-Shape2. Height>y0))) doy0: =random (Form1. clientheight-Shape2.
height);: =random (Form1. clientwidth-Shape2. width); end;. Top: =y0;. Left:
=x0;;TForm1. Shape2MouseDown (Sender: TObject; Button: TMouseButton;:
TShiftState; X, Y: Integer);x0,y0: integer;randomize;. Top: =random (Form1.
clientheight-Shape1. height);. Left: =random (Form1. clientwidth-Shape1.
width);: =random (Form1. clientheight-Shape2. height);: =random (Form1.
clientwidth-Shape2. width);not ( ( (Shape1. left+Shape1. Width<x0) or
(Shape1. left-Shape2. Width>x0)) and ( (Shape1. Top+Shape1. Height<y0) or
(Shape1. Top-Shape2. Height>y0))) doy0: =random (Form1. clientheight-Shape2.
height);: =random (Form1. clientwidth-Shape2. width); end;. Top: =y0;. Left:
=x0;;.
. Програма, яка реалізовує ходи шахових фігур.
program Project1;
{$APPTYPE CONSOLE},in 'unit1. pas';figures: array [1.4] of
Tfigure;,y, i: integer; desk: d; s: string; p: boolean;(desk);: ='';('enter
x=');(x);('enter y=');(y);(s<>'white') and (s<>'black') do begin
writeln ('enter color '); readln (s); end;s='white' then p: =true else p:
=false;[1]: =Tknight. create (x,y,p,desk);
// figures [1]. writeout (desk);: ='';('enter x=');(x);('enter
y=');(y);(s<>'white') and (s<>'black') do begin write ('enter color
'); readln (s); end;s='white' then p: =true else p: =false;[2]: =Tbishop.
create (x,y,p,desk);
// figures [2]. writeout (desk);: ='';('enter x=');(x);('enter
y=');(y);(s<>'white') and (s<>'black') do begin write ('enter color
'); readln (s); end;s='white' then p: =true else p: =false;[3]: =Trook. create
(x,y,p,desk);
// figures [3]. writeout (desk);: ='';('enter x=');(x);('enter
y=');(y);(s<>'white') and (s<>'black') do begin write ('enter color
'); readln (s); end;s='white' then p: =true else p: =false;[4]: =Tpawn. create
(x,y,p,desk);
// figures [4]. writeout (desk);(desk);I: = 1 to 4 dofigures [i].
live then('move ',figures [i]. name,' to x,y');('x='); readln (x);('y=');
readln (y);[i]. movetoplace (x,y,desk);;
// figures [1]. writeout (desk);(desk);;.
. Калькулятор
unit Unit1;, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,, StdCtrls;symbol=set of '0'. '9';= class (TForm): TEdit;:
TButton;: TButton;: TButton;: TButton;: TButton;: TButton;: TButton;: TButton;:
TButton;: TButton;: TButton;: TButton;: TButton;: TButton;: TButton;: TButton;:
TButton;: TButton;: TButton;: TButton;: TButton;Button17Click (Sender:
TObject);FormClose (Sender: TObject; var Action: TCloseAction);FormActivate
(Sender: TObject);Button21Click (Sender: TObject);Button15Click (Sender:
TObject);Button19Click (Sender: TObject);Button18Click (Sender:
TObject);Button20Click (Sender: TObject);Button16Click (Sender:
TObject);Button14Click (Sender: TObject);Button13Click (Sender:
TObject);FormCreate (Sender: TObject);Button12Click (Sender:
TObject);Button11Click (Sender: TObject);Button10Click (Sender:
TObject);Button3Click (Sender: TObject);Button2Click (Sender:
TObject);Button1Click (Sender: TObject);Button6Click (Sender:
TObject);Button5Click (Sender: TObject);Button4Click (Sender:
TObject);Button7Click (Sender: TObject);Button8Click (Sender:
TObject);Button9Click (Sender: TObject);: string;,y: real;: integer;: boolean;
{ Public declarations };: TForm1;
{$R *. dfm}TForm1. FormActivate (Sender: TObject);.
SetFocus;;TForm1. FormClose (Sender: TObject; var Action:
TCloseAction);('Goodbye');;TForm1. FormCreate (Sender: TObject);: =0; y: =0; l:
=-1; k: =false;;TForm1. Button10Click (Sender: TObject);Edit1. text<>'0'
then Edit1. text: =Edit1. text+'0';;TForm1. Button11Click (Sender: TObject);pos
(',',Edit1. Text) =0 then Edit1. text: =Edit1. Text+',';;TForm1. Button12Click
(Sender: TObject);if (x=0) and (y=0) then begin x: =strtofloat (edit1. Text);
Edit1. Text: =''; end else begin: =strtofloat (edit1. text);: =x+y; Edit1.
Text: ='';;k then begin x: =x-y; end;: =floattostr (x);: =1;;TForm1.
Button13Click (Sender: TObject);l of
: begin y: =strtofloat (edit1. text); x: =x+y; a: =floattostr (x);
k: =true; end;
: begin y: =strtofloat (edit1. text); x: =x*y; a: =floattostr (x);
k: =true; end;
: begin y: =strtofloat (edit1. text); if y<>0 then begin x:
=x/y; a: =floattostr (x); k: =true; end else begin showmessage ('error input');
k: =false; x: =0; y: =0; end; end;
: begin y: =strtofloat (edit1. text); x: =x-y; a: =floattostr (x);
k: =true; end;;. Text: =a; y: =0;;TForm1. Button14Click (Sender: TObject);if
(x=0) and (y=0) and (not (k)) then begin x: =strtofloat (edit1. Text); Edit1.
Text: =''; end else begin: =strtofloat (edit1. text);not ( (k) and (x=0)) then
x: =x*y; Edit1. Text: ='';;(k) and (x<>0) then begin x: =x/y; end;:
=floattostr (x);: =2;;TForm1. Button15Click (Sender: TObject);q: real;q:
=strtofloat (edit1. Text);q>0 then q: =ln (q);. Text: =floattostr
(q);;TForm1. Button16Click (Sender: TObject);(x=0) and (y=0) and (not (k)) then
begin x: =strtofloat (edit1. Text); Edit1. Text: =''; end else begin:
=strtofloat (edit1. text);not ( (k) and (x=0)) then if y<>0 then begin x:
=x/y; Edit1. Text: =''; end else begin showmessage ('error input'); edit1.
Text: =''; x: =0; y: =0; k: =false; end;;(k) and (x<>0) and (y<>0)
then x: =x*y;: =floattostr (x);: =3;;TForm1. Button17Click (Sender: TObject);q:
real;q: =strtofloat (edit1. Text);: =sqr (q);. Text: =floattostr (q);;TForm1.
Button18Click (Sender: TObject);. Text: ='0';: =0; y: =0; l: =0; k: =false;;TForm1.
Button19Click (Sender: TObject);q: string;q: =edit1. Text;(length (q) >1)
then delete (q,length (q),1) else q: ='0'; x: =strtofloat (q);. Text:
=q;;TForm1. Button1Click (Sender: TObject);Edit1. text='0' then Edit1. text:
='7' else Edit1. text: =Edit1. text+'7';;TForm1. Button20Click (Sender:
TObject);(x=0) and (y=0) then begin x: =strtofloat (edit1. Text); Edit1. Text:
=''; end else begin: =strtofloat (edit1. text);: =x-y; Edit1. Text: ='';;k then
x: =x+y;: =floattostr (x);: =4;;TForm1. Button21Click (Sender: TObject);q:
real;q: =strtofloat (edit1. Text);: =sqrt (q);. Text: =floattostr (q);;TForm1.
Button2Click (Sender: TObject);Edit1. text='0' then Edit1. text: ='8' else
Edit1. text: =Edit1. text+'8';;TForm1. Button3Click (Sender: TObject);Edit1. text='0'
then Edit1. text: ='9' else Edit1. text: =Edit1. text+'9';;TForm1. Button4Click
(Sender: TObject);Edit1. text='0' then Edit1. text: ='4' else Edit1. text:
=Edit1. text+'4';;TForm1. Button5Click (Sender: TObject);Edit1. text='0' then
Edit1. text: ='5' else Edit1. text: =Edit1. text+'5';;TForm1. Button6Click
(Sender: TObject);Edit1. text='0' then Edit1. text: ='6' else Edit1. text:
=Edit1. text+'6';;TForm1. Button7Click (Sender: TObject);Edit1. text='0' then
Edit1. text: ='3' else Edit1. text: =Edit1. text+'3';;TForm1. Button8Click
(Sender: TObject);Edit1. text='0' then Edit1. text: ='2' else Edit1. text:
=Edit1. text+'2';;TForm1. Button9Click (Sender: TObject);Edit1. text='0' then
Edit1. text: ='1' else Edit1. text: =Edit1. text+'1';;.
. Анкета
Unit1;, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,, StdCtrls, ExtCtrls;= class (TForm): TButton;: TEdit;: TComboBox;:
TGroupBox;: TRadioGroup;: TRadioGroup;: TRadioGroup;: TRadioGroup;:
TRadioGroup;: TRadioGroup;: TButton;: TGroupBox;: TRadioGroup;: TRadioGroup;:
TRadioGroup;: TRadioGroup;: TRadioGroup;: TRadioGroup;: TMemo;ComboBox1Change
(Sender: TObject);Button2Click (Sender: TObject);Edit1Click (Sender:
TObject);Button1Click (Sender: TObject);FormCreate (Sender: TObject);:
integer;: boolean;
{ Public declarations };: TForm1;
{$R *. dfm}TForm1. Button1Click (Sender: TObject);s: string;:
=Edit1. Text;(s='') or (s='Ваше Ім*я') then showmessage ('Введіть своє ім*я')
elsecombobox1. ItemIndex=-1 then showmessage ('Виберіть тест') elsecombobox1.
ItemIndex of
: begin groupbox1. Enabled: =true; groupbox1. Enabled: =true;
start: =true; end;
: begin groupbox1. Visible: =true; groupbox2. Enabled: =true;
groupbox2. Visible: =true; start: =true; groupbox1. Enabled: =true; end;;:
=0;;TForm1. Button2Click (Sender: TObject);s: string;start then begincombobox1.
ItemIndex of
: begin s: ='Тест з знання столиць Європи';radiogroup1.
ItemIndex=2 then score: =score+5;radiogroup2. ItemIndex=0 then score:
=score+5;radiogroup3. ItemIndex=2 then score: =score+5;radiogroup4. ItemIndex=1
then score: =score+5;radiogroup5. ItemIndex=0 then score: =score+5;radiogroup6.
ItemIndex=0 then score: =score+5;;
: begin s: ='Тест з знання столиць Азії';radiogroup7. ItemIndex=1
then score: =score+5;radiogroup8. ItemIndex=0 then score: =score+5;radiogroup9.
ItemIndex=3 then score: =score+5;radiogroup10. ItemIndex=2 then score: =score+5;radiogroup11.
ItemIndex=0 then score: =score+5;radiogroup12. ItemIndex=2 then score:
=score+5;;;: =false;. Lines. add (s);: ='Ім*я: '+edit1. Text;. lines. add (s);:
='очки: '+inttostr (score) +' з 30';. lines. add (s);: =0;. Enabled: =false;.
Text: ='Ваше Ім*я';. ItemIndex: =-1;. Text: ='Виберіть тест';. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;else showmessage ('Спочтаку розпочніть тестування (нажміть кнопку
"Cтарт") ');;TForm1.comboBox1Change (Sender: TObject);combobox1.
ItemIndex of
: begin groupbox1. Enabled: =false; groupbox1. Visible: =true;
groupbox2. Enabled: =false; groupbox2. Visible: =false; end;
: begin groupbox2. Enabled: =false; groupbox2. Visible: =true;
groupbox1. Enabled: =false; groupbox1. Visible: =true; end;;. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;;TForm1. Edit1Click (Sender: TObject);edit1. Text='Ваше Ім*я'
then edit1. Text: ='';;TForm1. FormCreate (Sender: TObject);memo1. Text: ='';.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;. ItemIndex: =0;.
ItemIndex: =0;. ItemIndex: =0;. Enabled: =false;. Enabled: =false;. Top: =0;.
left: =0;. ClientWidth: =screen. Width-18;. clientheight: =screen. Height-85;:
=false;;.