Skip to content

Instantly share code, notes, and snippets.

@Evshved
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save Evshved/8a967a121eb1e76eba09 to your computer and use it in GitHub Desktop.

Select an option

Save Evshved/8a967a121eb1e76eba09 to your computer and use it in GitHub Desktop.
Building graphics from lab #3
//Libraries
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include<math.h>
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//Main
TForm1 *Form1;
//описание(декларация) прототипов функций
//*Ufun без скобок это декларация функции которая возвращает указатель
//тип указателя на функцию - typedef double (*Ufun)(double);
typedef double(*TypeFun)(double);
double fun(double);
double sum(double);
double razn(double);
void Clear_Edit(void);
void Clear_Memo(void);
void Filling_Edit(void);
void Out(TypeFun,double,double,double,TMemo*,int,int);
int n;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void Clear_Edit()
{
Form1->Edit1->Clear();
Form1->Edit2->Clear();
Form1->Edit3->Clear();
Form1->Edit4->Clear();
}
void Clear_Memo()
{
Form1->Memo1->Clear();
Form1->Memo2->Clear();
Form1->Memo3->Clear();
}
void Filling_Edit()
{
Form1->Edit1->Text="0,1";
Form1->Edit2->Text="1,0";
Form1->Edit3->Text="0,1";
Form1->Edit4->Text="10";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
double a, b, h;
TryStrToFloat(Edit1->Text,a);
TryStrToFloat(Edit2->Text,b);
TryStrToFloat(Edit3->Text,h);
TryStrToInt(Edit4->Text,n);
switch(RadioGroup1->ItemIndex) {
case 0: Out(fun, a, b, h, Memo1, 0, 1);
break;
case 1: Out(sum, a, b, h, Memo2, 1, 2);
break;
case 2: Out(razn, a, b, h, Memo3, 2, 3);
break;
case 3: Out(fun, a, b, h, Memo1, 0, 1);
Out(sum, a, b, h, Memo2, 1, 2);
Out(razn, a, b, h, Memo3, 2, 3);
break;
}
}
//---------------------------------------------------------------------------
double fun(double x)
{
return 2*( cos( x )*cos( x )-1 );
}
//---------------------------------------------------------------------------
double razn(double x)
{
return fabs(fun(x) - sum(x));
}
//---------------------------------------------------------------------------
double sum(double x)
{
int k,sup;
double r,s;
r=1;
s=0;
for ( k=1; k<=n; k++ ) {
r=-r*4*x*x/( ( 2*k-1 )*2*k );
s+=r;
}
return s;
}
//---------------------------------------------------------------------------
void Out(TypeFun p_fun, double xn, //и сделать защиту
double xk, double h, TMemo *m, int kod, int number_of_memo)
{
double x, y;
Form1->Chart1->Series[kod]->Clear();
m->Lines->Add("Start");
if ((xn>=xk) || (n<=0) || (h>fabs(xk-xn))) {
m->Lines->Add(" Uncorrect input");
}
else
{
for(x = xn; x <= xk; x += h) {
y = p_fun(x);
if ((number_of_memo == 1) || (number_of_memo == 2)) {
m->Lines->Add(" x="+ FloatToStrF(x, ffFixed, 7,3)
+ " y= " + FloatToStrF(y, ffFixed, 8,6));
}
else
{
m->Lines->Add( "|S-Y| = " + FloatToStrF(y, ffFixed, 8,6));
}
Form1->Chart1->Series[kod]->AddXY(x, y);
} //зачем чистить Series?
}
m->Lines->Add("End");
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Chart1->Series[0]->Clear();
Chart1->Series[1]->Clear();
Chart1->Series[2]->Clear();
Clear_Edit();
Clear_Memo();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Clear_Edit();
Clear_Memo();
Filling_Edit();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Clear_Edit();
Clear_Memo();
Filling_Edit();
}
//---------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment