48971 (608859), страница 2
Текст из файла (страница 2)
Рис. 9. Размещение компонентов на форме
После этого выберем на форме компонент Edit и удалим текущее значение свойства Text. Затем установим свойство Caption для Button1 равным "Добавить".
Чтобы добавить обработчик события OnClick для кнопки Добавить, нужно выбрать эту кнопку на форме, открыть страницу событий в инспекторе объектов и дважды щелкнуть мышью на колонке справа от события OnClick. В соответствующей строке ввода появится имя функции. C++ Builder сгенерирует прототип обработчика событий и покажет его в редакторе кода. После этого следует ввести следующий код в операторные скобки { ... } тела функции:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!(Edit1->Text == ""))
{
ListBox1->Items->Add(Edit1->Text);
Edit1->Text = "" ;
}
}
Для компиляции приложения в меню Run выберем пункт Run. Теперь можно что-нибудь ввести в редактируемое поле, нажать мышью на кнопку Добавить и убедиться, что вводимые строки добавляются к списку.
Рис.10. Так выглядит готовое приложение.
Теперь модифицируем приложение, добавив кнопки Удалить и Выход. Для этого добавим еще две кнопки, изменим их свойство Caption и создадим обработчики событий, связанных с нажатием на эти кнопки:
Рис. 11. Модифицированное приложение
Для кнопки Удалить:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (!(ListBox1->ItemIndex == -1))
ListBox1->Items->Delete(ListBox1->ItemIndex);
}
Для кнопки Выход:
Close();
Сохраним и скомпилируем приложение, а затем протестируем его.
Итак, мы познакомились со средой разработки Borland C++ Builder и создали простое приложение. В следующих статьях этого цикла будут описаны приемы манипуляции компонентами на форме и более подробно рассмотрены особенности поведения различных компонентов в приложении
Практична частина
Лістінги програм
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX 50
#define ARRAYMAX 10
void initialize();
void menu1();
void menu2();
void data();
void graphch();
void corre();
void mean();
void median();
void graph();
void makegraph(float p[]);
void pie();
void mode();
void variance();
int input();
void correlation();
void regress();
void curvefit();
int midx = getmaxx()/2;
int midy = getmaxy()/2;
int choice1,choice2,choice3,choice4,choice5,choice6,choice7,choice8;
int a[100],b[100],c[100],d[100],e[100],f[100],g[100];
float men,medin,mod,cxy,vx,vy;
int a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,k1,l1,m1,n1,o1,p1,q1;
long double z1=0,z2=0,z3=0,z4=0,z5=0,z6=0,z7=0,z8=0,byx=0,bxy=0,mx=0,my=0;
static int zzz;
s =0;
float s2 =0.0;
s3 =0;
s1 =0;
float sum =0.0;
float mean,variance,v1,v2,v3;
data();
clrscr();
switch(choice5)
{
case 1:
{
cout<<" Enter the total no of individual observations";
cin>>n;
for(int i=0;i { cout<<" X"< cin>>a[i]; s = s+ a[i]; } mean = s/n; for(i=0;i { sum = sum+ pow((a[i]-mean),2); } clrscr(); cout<<"The values of individual observations are "; for(i=0;i cout< cout< variance = sum/n; break; } case 2: { cout<<"Enter the no of data"; cin>>nn; cout<< "Enter the class interval size"; cin>>ci; cout<<"Enter the upper most level"; cin>>up; cout<<" Enter the lowermost level"; cin>>lo; cout<<"Enter the frequency in order\n"; for(int i=0;i { b[i] = ((up + ci) + up)/2; up = up + ci; cout<<" Freq"< cin>>c[i]; } for(i=0;i { d[i] = b[i] - b[nn/2]; e[i] = d[i]/ci; } for(i=0;i cout< cout< float sum2,sum3,sum4; sum2 = 0.0; sum3 =0.0; sum4 = 0.0; for(i=0;i { sum2 = sum2 +(c[i]*e[i]); sum3 = sum3 +(c[i]*pow(e[i],2)); sum4 = sum4+ c[i]; } cout<<"The sum of f * u = "< cout<<" The sum of f*u2 = "< cout<<" The sum of f = "< v1 = sum3/sum4; cout<<" The value of 1 = "< v2 = sum2/sum4; cout<<" The value of 2 = "< v3 = pow(v2,2); variance = (pow(ci,2)*(v1 -v3)); int u= up; cout<<"Class\t"< cout<<"interval\t"< for( int k=0;k { cout< u = u+ci; } break; } case 3: { cout<<"Enter the total no of data"; cin>>nn; char s[20],s4[20]; cout<<" X denotes "; cin>>s; cout<<" Frequency denotes "; cin>>s4; for(int i=0;i { cout<<"X"< cin>>a[i]; cout<<"Freq"< cin>>b[i]; s3 = s3+b[i]; } cout<<"The sum of f = "< for(i=0;i { s1 =s1+ (a[i]*b[i]); } cout<<"The sum of f*x = "< mean = s1/s3; for(i=0;i { c[i] = pow((a[i]-mean),2); s2 = s2 +(c[i]*b[i]); } cout<<"Mean = "< cout<<"The sum of deviations = "< variance = s2/s3; cout< for(i=0;i { cout< } break; } default: {cout<<" Sorry wrong choice"; getch(); data(); } } cout<<" Variance ="< getch(); int z=1; while(z==1||z==2) { cout<<"\n\n\n\n\nPress 1 to return to previos menu"; cout<<"\nPress 2 to exit"; cin>>z; if(z==1) menu2(); else if(z==2) exit(0); else cout<<"Wrong choice"; } } char s[20],s1[20]; int input() { int n; gotoxy(2,6); cout<<"Enter the total no of data"; cin>>n; cout<<"Enter the variable x and y values\n"; cout<<" X denotes "; cin>>s; cout<<" Y denotes "; cin>>s1; for(int i=0;i { cout<<" X"< cin>>f[i]; cout<<" Y"< cin>>g[i]; } return(n); } void correlation() { int n; corre(); initialize(); switch(choice4) { case 1: { n =input(); clrscr(); cleardevice(); initialize(); settextstyle(7,HORIZ_DIR,0); outtextxy(50,30,"SCATTER DIAGRAM"); line(50,400,600,400); line(50,400,50,50); outtextxy(556,410,s); settextstyle(7, VERT_DIR,0); outtextxy(30,375,s1); settextstyle(7, HORIZ_DIR,0); for(int i=0;i { int x1 = f[i]+ 50; int y1 = 400- g[i]; setfillstyle(SOLID_FILL,14); setcolor(14); circle(x1,y1,1); floodfill(x1,y1,14); } break; } case 2: {// clrscr(); // cleardevice(); float sum2,sum3,sum4,sum5,sum6; n = input(); sum2 = 0.0; sum3 =0.0; sum4 =0.0; sum5 =0.0; sum6 =0.0; clrscr(); cleardevice(); for(int i=0;i { sum2 = sum2 + f[i]; sum3 = sum3 + g[i]; } cout< for(i=0;i { cout< } cout<<" The sum of X = "< cout<<"The sum of Y = "< float mex = sum2/n; float mey = sum3/n; cout<<" The mean of X = "< cout<<"The mean of Y = "< for(i=0;i { sum4 = sum4 +((f[i] -mex)*(g[i]-mey)); sum5 = sum5 + pow((f[i]-mex),2); sum6 = sum6+ pow((g[i]-mey),2); } cout<<" The sum of products of deviation from mean of X and Y = "< cout<<"The sum of squares of deviation from mean of X = "< cout<<" The sum fo squares of deviation from mean of Y = "< float cov = sum4/n; float varx = sqrt(sum5/n); float vary = sqrt(sum6/n); float coeff = ((cov/varx)/vary); cout<<" THE COEFFICIENT OF CORRELATION = "< if(coeff= -1) { if(coeff == -1) cout<<" There is perfect negative correlation between X and Y\n"; else if(coeff ==1) cout<<"There is perfect positive correlation between X and Y\n"; else if(coeff ==0) cout<<" There is no correlation between X and Y\n"; else if (coeff<0) cout<<" There is negative correlation between X and Y\n"; else if (coeff>0) cout<<"There is positive correlation between X and Y\n"; } else cout<<"There seems to be a mistake in the data provided since the value of coefficient of correlation does not lie between -1 and 1"; break; } default: { outtextxy(30,midy +100," Sorry, you have entered the wrong choice"); getch(); cleardevice(); corre(); break; } } getch(); cleardevice(); int z=1; while(z==1||z==2) { cout<<"\n\n\n\n\nPress 1 to return to previos menu"; cout<<"\nPress 2 to exit"; cin>>z; if(z==1) menu1(); else if(z==2) exit(0); else cout<<"Wrong choice"; } } void regress() { int n; float cxy; initialize(); settextstyle(5, HORIZ_DIR, 0); setusercharsize(1, 1, 1, 1); outtextxy(10,midy,"What type of data do you want to enter"); outtextxy(20,midy+30,"1.Discrete data of x and y values\n"); outtextxy(20,midy+60,"2.Data of all summations\n"); cin>>choice7; // return (choice5); closegraph(); switch(choice7) { case 1: { cout<<" Enter the total no of data"; cin>>n; for(int i=0;i { cout<<"X"< cin>>a[i]; cout<<"Y"< cin>>b[i]; } clrscr(); for(i=0;i { z1 +=a[i]; z2 +=b[i]; } mx = z1/n; my = z2/n; for( i=0;i { z3 += ((a[i]-mx)*(b[i]-my)); z4 += pow((a[i]-mx),2); z5 += pow((b[i]-my),2); } cxy = z3/n; vx = z4/n; vy = z5/n; byx = cxy/vx; bxy = cxy/vy; cout< for(i=0;i { cout< } cout<<"The Line Of Regression of Y on X is \n"; cout<<"\t Y - "< cout< cout<<"The Line Of Regression of X on Y is \n"; cout<<"\t X - "< cout< cout<<"\n\nWould you like to estimate values of y or x based on the above equations\n"; cout<<"1. to estimate Y press 1\n"; cout<<"2. To estimate X press 2\n"; cout<<"3. Exit and return to previous menu press 3\n"; cout<<"enter choice?"; cin>>choice8; clrscr(); float ex,ey; if(choice8 == 1) { char ch = 'y'; while(ch=='y') { cout<<" enter the value of X"; cin>>ex; ey = (byx*(ex- mx)) + my; cout<<" The estimated value of Y = "< cout<<" do you want to continue"; cin>>ch; } } else if(choice8 == 2) { char ch = 'y'; while(ch=='y') { cout<<" enter the value of Y"; cin>>ey; ey = (bxy*(ey- my)) + mx; cout<<" The estimated value of X = "< cout<<" do you want to continue"; cin>>ch; } } else break; break; } case 2: { cout<<"Enter the total no of data "; cin>>n; cout<<" Enter the value of sum of x "; cin>>z1; cout<<" Enter the value of sum of y "; cin>>z2; cout<<"Enter the value of sum of products of x znd y "; cin>>z3; cout<<" Enter the value of sum of squares of x "; cin>>z4; cout<<"Enter the values of sum of squares of y "; cin>>z5; byx = (n*z3-z1*z2)/(n*z4 - pow(z1,2)); bxy = (n*z3-z1*z2)/(n*z5 - pow(z2,2)); mx = z1/n; my = z2/n; clrscr(); cout<<"The sum of x = "< cout<<" The sum of y = "< cout<<" The sum of product of x and y = "< cout<<" The sum of squares of x = "< cout<<" The sum of squares of y = "< cout<<"\n\nThe Line Of Regression of Y on X is \n"; cout<<"\t Y - "< cout< cout<<"The Line Of Regression of X on Y is \n"; cout<<"\t X - "< cout< cout<<"\n\nWould you like to estimate values of y or x based on the above equations\n"; cout<<"1. to estimate Y press 1\n"; cout<<"2. To estimate X press 2\n"; cout<<"3. Exit and return to previous menu press 3\n"; cout<<"enter choice?"; cin>>choice8; clrscr(); float ex,ey; if(choice8 == 1) { char ch = 'y'; while(ch=='y') { cout<<" enter the value of X"; cin>>ex; ey = (byx*(ex- mx)) + my; cout<<" The estimated value of Y = "< cout<<" do you want to continue"; cin>>ch; } } else if(choice8 == 2) { char ch = 'y'; while(ch=='y') { cout<<" enter the value of Y"; cin>>ey; ey = (bxy*(ey- my)) + mx; cout<<" The estimated value of X = "< cout<<" do you want to continue"; cin>>ch; } } else break; break; } default: { cout<<" Sorry wrong choice"; clrscr(); getch(); regress(); break; } } int z=1; while(z==1||z==2) { cout<<"\n\n\n\n\nPress 1 to return to previos menu"; cout<<"\nPress 2 to exit"; cin>>z; if(z==1) menu1(); else if(z==2) exit(0); else cout<<"Wrong choice"; } } void curvefit() { int n; cout<<" Enter the total no of points "; cin>>n; cout<<"Enter the values of x and y coordinates of the points\n"; float a[100],b[100]; for( int i=0;i { cout<<" X"< cin>>a[i]; cout<<" Y"< cin>>b[i]; } long float aa,bb; cout<<"\nENTER THE TYPE EQUATION YOU WISH TO CURVE FIT"; cout<<"\nFOR LINEAR FORM, y = ax + b,INPUT 2"; cout<<"\nFOR QUADRATIC FORM, y= ax^2 + bx + c,INPUT 3"; cout<<" \nENTER TYPE ?"; cin>>choice6; switch(choice6) { case 2: { for(i=0;i { z1 += a[i]; z2 += b[i]; z3 += a[i]*b[i]; z4 += pow(a[i],2); z5 += pow(b[i],2); } float mex = z1/n; float mey = z2/n; float cyx = ((z3/n) - mex*mey); float vx = ((z4/n) - pow(mex,2)); float vy = ((z5/n) - pow(mey,2)); aa = cyx/vx; bb = mey - mex*aa; cout<<"\n\n\n a = "< line(50,400,600,400); line(50,400,50,50); outtextxy(556,410,"x"); settextstyle(7, VERT_DIR,0); outtextxy(10,375,"y"); settextstyle(7, HORIZ_DIR,0); for(i=0;i { int x1 = a[i]+50; int y1 = 400 - b[i]; setfillstyle(SOLID_FILL,14); setcolor(14); circle(x1,y1,2); floodfill(x1,y1,14); } setcolor(WHITE); int x2,y2; line(50,400,600,400); line(50,50,50,400); moveto(50,400); float y; for( int i=0;i<198;i++) { y = bb + aa*i; x2 = i+50; y2 = 400-y; lineto(x2,y2); } break; } case 3: { for(i=0;i { z1 += a[i]; z2 += b[i]; z3 += a[i]*b[i]; z4 += pow(a[i],2)*b[i]; z5 += pow(a[i],2); z6 += pow(a[i],3); z7 += pow(a[i],4); } long double dx1 = z4*((n*z5)-pow(z1,2)); long double dx2 = z6*((n*z3)-(z1*z2)); long double dx3 = z5*((z3*z1)- (z5 *z2)); long double dx = dx1 - dx2 + dx3; long double d1 = z7*((n* z5)- pow(z1,2)); long double d2 = z6 *((n*z6)- (z1*z5)); long double d3 = z5*((z6*z1) - pow(z5,2)); long double d = d1-d2 +d3; long double dy1 = z7*((n*z3) - (z1*z2)); long double dy2 = z6*((n*z6) - (z1*z5)); long double dy3 = z5*((z6*z1)- pow(z5,2)); long double dy = dy1-dy2+dy3; long double dz1 = z7*((z2*z5) - (z1*z3)); long double dz2 = z6*((z2*z6)- (z1*z3)); long double dz3 = z4*((z6*z1)- pow(z5,2)); long double dz = dz1-dz2+dz3; long double aa = dx/d; long double bb = dy/d; long double cc = dz/d; cout<<"\n\n\n a = "< line(50,400,600,400); line(50,400,50,50); outtextxy(556,410,"x"); settextstyle(7, VERT_DIR,0); outtextxy(10,375,"y"); settextstyle(7, HORIZ_DIR,0); for(i=0;i { int x1 = a[i]+50; int y1 = 400 - b[i]; setfillstyle(SOLID_FILL,14); setcolor(14); circle(x1,y1,1); floodfill(x1,y1,14); } setcolor(WHITE); int x2,y2; line(50,400,600,400); line(50,50,50,400); moveto(50,400); float y; for( int i=0;i<198;i++) { y = (aa*pow(i,2)) +(bb*i) + cc; x2 = i+50; y2 = 400-y; lineto(x2,y2); } break; } default: {cout<<"Sorry wrong choice"; getch(); clrscr(); break; } // getch(); // closegraph(); } getch(); closegraph(); //cleardevice(); clrscr(); int z=1; while(z==1||z==2) { cout<<"\n\n\n\n\nPress 1 to return to previos menu"; cout<<"\nPress 2 to exit"; cin>>z; if(z==1) menu1(); else if(z==2) exit(0); else cout<<"Wrong choice"; } getch(); } Робота з програмою Робота з програмою починається із стану вибору операції Наступний крок вибору операції з даними Висновки Була розроблена комп’ютерна програма для простої обробки даних з можливістю її графічної інтерпретації При розробці програми використовувався пакет BORLAND C++ Bilder та бібліотека BGI. Відмітимо лише в порівнянні з іншими подібними програмними продуктами – дана програма має простий командний інтерфейс (це в значний мірі спростило етап розробки даної програми). Література 1. Касаткин А.И., Вальвачев А.Н. Профессиональное прогрпммирование на языке Си. Мн., 1992. 240 С. 2. Нейбауэр А. Моя первая программа на С/С++. П., 1995. 368 С. 3. Бруно Бабэ. Просто и ясно о Borland C++. М., 1996. 400 С. 4. Шамас Н.К. Основы С++ и обьектно-ориентированного программирования. К., 1996. 448 С. 5. Справочник по классам Borland C++ 4.0. К., 1994. 256 С.
(
The main menu) (Screen 2 General statistics menu)















