Вы находитесь на странице: 1из 35

11.

++
MS Visual C++
.
11.1. MS Visual C++
File New
Project. (. 11.1),
(Project types) MFC, (Templates) MFC
Application. OK,
(. 11.2).

. 11.1.
(. 11.3)
:
Single document ( );
Multiple documents ( );
Dialog based ( ).
(Dialog based).
(. 11.4) Main frame styles
(
)
1

(Dialog title). .
(. 11.5) ,
.
, ,
, , ,
, ,
.
,
, ,
. Finish
(. 11.6).

. 11.2.
, Ctrl+F5.
First program,
, . Cancel
OK .
.

(. 11.3), ,
Microsoft . http://www.flint-inc.ru/Russian/
Programs.html ,

2

. , (. 3)
Resource Language .
.

. 11.3.

. 11.4.

. 11.5.

. 11.6.

. 11.7. Solution Explorer Solution Explorer


,
(
).

. MS Visual C++ ( . 11.7)
Solution Explorer. Solution Explorer
, , Resource View (.
5

11.8) ,
Dialog Resource View
( ID_ ).

. 11.8. Resource View Solution Explorer


11.1. .
,
. ,
. 11.9. ,
6

,
.

. 11.9.

. 11.10.
7


(. 11.10).
.

. 11.11. ToolBox
TODO: ..., Delete
.
. Static Text Edit
Box . ToolBox (. 11).
ToolBox ,
8

, View
ToolBox ( Ctrl+Alt+X)
.
Static Text Edit Control
ToolBox. (Static Text), (Edit
Control) (Button) ,
, ,
. 11.12.

. 11.12. Static Text Edit


Control
,
, , ,
.
, ,
Properties, Caption
(. 11.13). Caption ,
.

. 11.13.
Caption OK ,
, Properties
Caption.
(. 11.14).
( ),

. ,
10

, ,
.

. 11.14.

. 11.15.
11

,
:
1. ( , Properties)
ID, ID_radian.
ID
( ),
. ID,
, ,
.
2.

(.
11.15),

OnBnClickedRadian

:
MessageBox(" ");
. ,
(. 11.16).

. 11.16.
, , ,
OnBnClickedRadian .
, , ..
Edit Control ( ).
, , ,
, .
3. Edit Control,
, Add Variable,
, . 11.17.
(m_Edit),
Edit Control Variable Name. m_Edit,
,
, (Category
Value), ,
(Variable type - float). Add Member Variable
, . 11.18.
4. OnBnClickedRadian

12

. 11.17. Add Member Variable

. 11.18. Add Member Variable


13

void CVisual_gr_radDlg::Onradian()
{
float a, rad;
char s[80];
// UpdateData
Edit Control
// ,
// ,
, // Edit Control
.
UpdateData(TRUE);
// a ,
//a float, Add Member Variable
//, m_Edit float.
a=m_Edit;
rad=a*3.14159/180;
sprintf(s,"a=%g , %g",a,rad);
// MessageBox
// , s.
MessageBox(s);
}
,
,
(. 11.19).

. 11.19.
,
:
1. .
2. ,
.
3. ID ,
. ID
4. Edit
Control ().
5. ,
(
,
14

).
11.2.
. (. 11.20).

. 11.20.
( Static Text)
, ( Edit Control),
( Radio Button) (
), ( Check Box), ,
( ),
( Static Text),
.
Caption , ,
, . 11.21.
,
, . ,
Add Variable (. 11.22):
Variable name ( ) m_editA;
Category () Value ();
Variable type ( ) float.

(. 11.23, 11.24). m_editA, m_editB, m_editC
15

a, b
.

. 11.21.

. 11.22.
16

. 11.23.

. 11.24.
17

. 11.25.

. 11.26.
18

. 11.27.

. 11.28.
19

. 11.29.
,
. Group Box (
, Properties) Group True,
Group True.
(.
11.25, 11.26).
m_kvadrat
RadioButton, m_bikvadrat
RadioButton.
Group True ,
m_kvadrat, m_bikvadrat
RadioButton. , True
, (
).
, ,

(. 11.27).

Group True. Group
False.
20

, ,
BOOL (. 11.28).
ID ( , Properties)
IDC_STATIC1. ( , Add
Variable), (. 11.29).
ID,
, .
ID , ,
Properties, ID
ID_Solve.
.
OnBnClickedSolve().
( math.h)
void Cglava_11_prim2Dlg::OnBnClickedSolve()
{
char S[80];
FILE *f;
//CRect rect;
double a,b,c,d,x1,x2,y1,y2, y3, y4;
//
UpdateData(TRUE);
// a, b c
// .
a=m_editA;
b=m_editB;
c=m_editC;
// .
d=b*b-4*a*c;
// () GetCheck()
// , ,
// m_kvadrat, ..
// ?
if (m_kvadrat.GetCheck())
// .
if (d<0)
sprintf(S," , !!!");
else
{
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/(2*a);
// s.
sprintf(S,"x1=%g\tx2=%g",x1,x2);
}
21

else
{
if (d<0)
// s.
sprintf(S," , !!!");
else
{
// m_bikvadrat,
//.. ?
// .
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/(2*a);
if ((x1<0)&&(x2<0))
// s.
sprintf(S," !!!");
else
if ((x1>=0)&&(x2>=0))
{
y1=sqrt(x1);
y2=-y1;
y3=sqrt(x2);
y4=-y3;
// s.
sprintf(S," :y1=%f\ty2=%f\ty3=%f\t y4=%f",
y1,y2,y3,y4);
}
else
if (x1>=0)
{
y1=sqrt(x1);
y2=-y1;
// s.
sprintf(S," : y1=%f\ty2=%f",y1,y2);
}
else
{
y1=sqrt(x2);
y2=-y1;
// s.
sprintf(S," : y1=%f\ty2=%f",y1,y2);
}
}
}

// message
22

if (m_message)
{
//
f=fopen("Result.txt","w");
fprintf(f,S);
fclose(f);
}
else
{
//
m_Result=S;
UpdateData(FALSE);
}
}
a, b c,
.
. 11.30.

. 11.30.

OnBnClickedSolve() OnInitDialog,

. return TRUE;
23

, ,
OnInitDialog ,
.
OnInitDialog
m_editA, m_editB, m_editC m_message.
OnInitDialog
RETURN TRUE.
m_EditA=-2;
m_EditB=3;
m_EditC=-4;
m_message=false;
//
UpdateData(FALSE);
,
a, b c 2, 3 4 .
3. x xn xk dx.
x y (y=esin(x)), y.
:
3 ( Static Text), ( Edit Control),
xn, xk, dx ( List
Box),

Caption , ,
(. 11.31).

. 11.31. 3
m_edit_xn, m_edit_xk
24

m_edit_dx ( , Add Variable),


(Value) float.
ID ,
ID_Solve ID_Clear .
List Box,
List Box.
List Box ( ,
Add Variable) m_List_REsult (. 11.32),
(Value) float.

. 11.32. (List Box)


List Box Sort
False ( List Box ),
Vertical scroll True.
,
xn, xk, dx OnInitDialog (
return) :
m_edit_xn=-2;m_edit_xk=2;m_edit_dx=0.2;UpdateData(FALSE);
.
11.33.
, ,
,
25

OnBnClickedSolve ()

. 11.33.
OnInitDialog
//Cgl_11_3Dlg ,
// .
void Cgl_11_3Dlg::OnBnClickedSolve()
{int i;
float x,xn,xk,dx,y,s,p;
//
CString S;
UpdateData(TRUE);
xn=m_edit_xn;
xk=m_edit_xk;
dx=m_edit_dx;
for(s=0, p=1, x=xn,i=0; x<=xk; x+=dx,i++)
{
y=exp(sin(x));s+=y;p*=y;
// x=, y .
S.Format(_T("x=%g y=%g"),x,y);
//
// (ListBox)
m_List_REsult.AddString(S);
26

// s=, p .
S.Format(_T("s=%g p=%g"),s,p);
// S
// (ListBox)
// 0. InsertString(k,S)
// S k.
m_List_REsult.InsertString(0,S);
}
, ,
, OnClear()
void CTsiklDlg::OnClear()
{
int j,N;
//
N=m_List_REsult.GetCount();
// (j 0 N-1)
// 0.
for(j=0;j<=N;j++)
m_List_REsult.DeleteString(0);
}
,
. 11.34.

. 11.34.

27

Microsoft Visual C++. List Box.


Edit Box,
, ,
. ,
.
, MS Visual C++ :
1. .
2. .
3. , .
4. - .

11.2. Turbo C++ Explorer


2. ,
File New VCL Forms Application C++ Builder.
Turbo C++ Explorer , . 11.35.

. 11.35. Turbo C++ Explorer


, File Save
Project As. .
cpp (
bdsproj).
28

, Turbo C++ Explorer


(Object Inspector) (. 11.36),
( Properties).

. 11.36.
().
29

:
1. Name .
2. Caption , ,
, ,
, , ..
Name Caption
, , .
Caption ,
Name
. Caption
() .
:
( TLabel),
, ;
( TEdit) a, b, c;
( TRadioButton)
( );
( TCheckBox), ,
( );
( TButton)
.
, . 11.37.
Caption ,
, , . 11.38.

. 11.37.

30

. 11.38.
Name (TEdit)
(TLabel).
Name m_edit_a, m_edit_b m_edit_c .
label_result Name
. C++
m_edit_a, m_edit_b m_edit_c,
label_result.
Visible,
, true.
label_result false.
,
.
Text label_result true.
Text,
, .
Text 2, 3 4
m_edit_a, m_edit_b m_edit_c .
,
a, b c 2, 3 4.
RadioButton Checked,
true, false .
31

, (
Name) RadioButton1, RadioButton2.
RadioButton1 Checked
true, RadioButton1 false.
( CheckBox1)
Checked, true,
false .
.
, Turbo C++
Explorer :
1. .
2. , (.
11.36), Events ()
OnClick.

++.

Close().
:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Close .
Close();
}

.
Turbo C++ Explorer ( )
->.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ofstream f;
// , ios::out
// .
f.open("result.txt");
float a,b,c,d,x1,x2,y1,y2, y3, y4;
// ,
if (!(CheckBox1->Checked))
// .
label_result->Visible=true;
else
// ,
// .
label_result->Visible=false;
32

// a, b c . //
StrToFloat
// . StrToInt.
a=StrToFloat(m_edit_a->Text);
b=StrToFloat(m_edit_b->Text);
c=StrToFloat(m_edit_c->Text);
// .
d=b*b-4*a*c;
// ,
// .
if (RadioButton1->Checked)
// .
if (d<0)
// ,
// ,
if (CheckBox1->Checked)
f<<" , !!!";
//
label_result.
else
label_result->Caption=
" , !!!";
else
{
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/(2*a);
if (CheckBox1->Checked)
f<<"x1="<<x1<<"\tx2="<<x2;
else
// FloatToStr
//. IntToStr.
label_result->Caption=
"x1="+FloatToStr(x1)+"\tx2="+FloatToStr(x2);
}
else
{
if (d<0)
if (CheckBox1->Checked)
// ,
//
f<<" , !!!";
else
//
//label_result.
33

label_result->Caption=
" , !!!";
else
{
// ,
// , .
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/(2*a);
if ((x1<0)&&(x2<0))
if (CheckBox1->Checked)
// ,
//
f<<" !!!";
//
//label_result.
else
label_result->Caption=" !!!";
else
if ((x1>=0)&&(x2>=0))
{
y1=sqrt(x1);
y2=-y1;
y3=sqrt(x2);
y4=-y3;
// ,
//
if (CheckBox1->Checked)
f<<"
y1="<<y1<<"\ty2="<<y2<<"\ty3="<<y3<<"\ty4="<<y4;
else
//
//label_result.
label_result->Caption=" y1=
" +FloatToStr(y1) + "\ty2=" + FloatToStr(y2) +
"\ty3="+FloatToStr(y3)+"\ty4="+FloatToStr(y4);
}
else
if (x1>=0)
{
y1=sqrt(x1); y2=-y1;
// ,
//
if (CheckBox1->Checked)
f<<" , y1="<<y1<<"\ty2="<<y2;
else
34

//
//label_result.
label_result->Caption=" y1=
" +FloatToStr(y1) +"\ty2=" + FloatToStr(y2);
}
else
{y1=sqrt(x2);
y2=-y1;
// ,
//
if (CheckBox1->Checked)
f<<" , y1="<<y1<<"\ty2="<<y2;
else
//
//label_result.
label_result->Caption=" y1=
"+FloatToStr(y1)+"\ty2="+ FloatToStr(y2);
}
}}}
,
. 11.39.

. 11.39. , Turbo C++


Explorer

Turbo C++ Explorer.
35