c++常用程序初学者的选择.docx

上传人:b****6 文档编号:7836958 上传时间:2023-01-26 格式:DOCX 页数:20 大小:20.06KB
下载 相关 举报
c++常用程序初学者的选择.docx_第1页
第1页 / 共20页
c++常用程序初学者的选择.docx_第2页
第2页 / 共20页
c++常用程序初学者的选择.docx_第3页
第3页 / 共20页
c++常用程序初学者的选择.docx_第4页
第4页 / 共20页
c++常用程序初学者的选择.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

c++常用程序初学者的选择.docx

《c++常用程序初学者的选择.docx》由会员分享,可在线阅读,更多相关《c++常用程序初学者的选择.docx(20页珍藏版)》请在冰豆网上搜索。

c++常用程序初学者的选择.docx

c++常用程序初学者的选择

C++常用程序案例_初学者的选择

1.类和对象虚函数和多态输入输出人员的各种信息

#include

#include

classCPerson

{public:

voidintput()

{

cin>>name;

cin>>id;

cin>>xb;

}

voidoutput()

{

if(xb==1)

cout<

elsecout<

}

private:

charname[10];

doubleid;

intxb;

};

classCstudent:

publicCPerson

{public:

voidintput1()

{

intput();

cin>>score;

}

voidoutput1()

{

output();

cout<

}

private:

doublescore;

};

classCteacher:

publicCPerson

{public:

voidintput2()

{intput();

cin>>nl;

}

voidoutput2()

{

output();

cout<

private:

intnl;

};

main()

{intn,i;

charch[100];

Cteachera[100];

Cstudentb[100];

cout<<"请输入所需统计的人数:

\n";

cin>>n;

cout<<"教师请输入t学生请输入s\n";

for(i=1;i<=n;i++)

cin>>ch[i];

for(i=1;i<=n;i++)

{if(ch[i]=='t')

{for(i=1;i<=n;i++)

a[i].intput2();

for(i=1;i<=n;i++)

a[i].output2();}

if(ch[i]=='s')

{for(i=1;i<=n;i++)

b[i].intput1();

for(i=1;i<=n;i++)

b[i].output1();}}}

2.多态类与虚函数计算各种图形的面积

#include

classCshape

{

public:

virtualfloatarea()=0;

};

classCTriangle:

publicCshape

{

public:

CTriangle(floath,floatw)

{

H=h;W=w;

}

floatarea()

{

return(float)(H*W*0.5);

}

private:

floatH,W;

};

classcCircle:

publicCshape

{

public:

cCircle(floatr)

{

R=r;

}

floatarea()

{

return(float)(3.1415*R*R);

}

private:

floatR;

};

classCRect:

publicCshape

{

public:

CRect(floath,floatw)

{

H=h;W=w;

}

floatarea()

{

return(float)(H*W);

}

private:

floatH,W;

};

voidmain()

{

Cshape*pShape;

CTriangletri(3,4);

cCirclecir(5);

CRectrect(3,4);

pShape=&tri;

cout<area()<

pShape=○

cout<area()<

pShape=▭

cout<area()<

}

 

3.C语言简单程序输入输出

#include

main()

{

intr1;

floatr2;

doublec1,c2,s1,s2;

doublesums,sumc;

cout<<"请输入第一个圆的半径:

"<

cin>>r1;

cout<<"请输入第二个圆的半径:

"<

cin>>r2;

c1=3.1415*2*r1;

c2=3.1415*2*r2;

s1=3.1415*r1*r1;

s2=3.1415*r2*r2;

sums=s1+s2;

sumc=c1+c2;

cout<<"第一个圆的周长为:

"<

"<

cout<<"第二个圆的周长为:

"<

"<

cout<<"两圆的周长之和为:

"<

"<

4.程序结构和数组应用统计最大值最小值排序

#include

floatB[100];

inti;

voidMax(floatA[],intN);

voidMin(floatA[],intN);

voidTj(floatA[],intN);

main()

{intn;

cout<<"请输入学生人数(注意人不超过100)"<

cin>>n;

cout<<"请输入成绩"<

for(i=0;i

cin>>B[i];

Max(B,n);

Min(B,n);

Tj(B,n);

}

voidMax(floatA[],intN)

{floatmax=A[0];

for(i=0;i

if(max

max=A[i];

cout<<"最高分:

"<

voidMin(floatA[],intN)

{floatmin=A[0];

for(i=0;i

if(min>A[i])

min=A[i];

cout<<"最低分:

"<

voidTj(floatA[],intN)

{inta=0,b=0,c=0,d=0,e=0;

floata1,b1,c1,d1,e1;

for(i=0;i

{if(A[i]<60)

++a;

elseif(A[i]<70)

++b;

elseif(A[i]<80)

++c;

elseif(A[i]<90)

++d;

else++e;}

a1=(float)a/N;

b1=(float)b/N;

c1=(float)c/N;

d1=(float)d/N;

e1=(float)e/N;

cout<<"不及格人数为:

"<

"<

cout<<"及格人数为:

"<

"<

cout<<"等级为中人数为:

"<

"<

cout<<"等级为良人数为:

"<

"<

cout<<"等级为有人数为:

"<

"<

5.实验4win32应用程序求一元二次方程的根创建编辑框逗号截取按钮

#include

#include

#include

charstr[10];

LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);

intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,

LPSTRlpCmdLine,intnCmdShow)

{

HWNDhWnd;

MSGMsg;

WNDCLASSWndClass;

WndClass.style=CS_HREDRAW|CS_VREDRAW;

WndClass.lpfnWndProc=WndProc;

WndClass.cbClsExtra=0;

WndClass.cbWndExtra=0;

WndClass.hInstance=hInstance;

WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);

WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);

WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

WndClass.lpszMenuName=NULL;

WndClass.lpszClassName="HelloWin";//窗口类名

//注册窗口

if(!

RegisterClass(&WndClass))

{

MessageBox(NULL,"窗口注册失败!

","HelloWin",0);

return0;

}

//创建窗口

hWnd=CreateWindow("HelloWin",

"MyWindow",

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL);

//显示窗口

ShowWindow(hWnd,nCmdShow);

//更新窗口

UpdateWindow(hWnd);

//进入消息循环:

当从应用程序消息队列中捡取的消息是WM_QUIT时,则推出循环

while(GetMessage(&Msg,NULL,0,0))

{

TranslateMessage(&Msg);//转换键盘消息

DispatchMessage(&Msg);//分发消息

}

returnMsg.wParam;

}

LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam)

{

HDChDC;

PAINTSTRUCTPs;

charstrEdit[10];

charstrsub[10];

staticHWNDhWndButton,hWndEdit;

switch(message)

{

caseWM_CREATE:

hWndEdit=CreateWindow("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,

10,60,100,25,hWnd,NULL,NULL,NULL);

hWndButton=CreateWindow("button","Display",WS_CHILD|WS_VISIBLE|WS_BORDER,

340,60,100,25,hWnd,NULL,NULL,NULL);

return0;

caseWM_COMMAND:

if(((HWND)lParam==hWndButton)&&(HIWORD(wParam)==BN_CLICKED))

//按下按键

{

GetWindowText(hWndEdit,strEdit,10);//获取编辑框控件的内容

待续

sprintf(str,"Theresultis:

%s",strEdit);

InvalidateRect(hWnd,NULL,TRUE);

}

caseWM_PAINT:

//设计编辑框

hDC=BeginPaint(hWnd,&Ps);

TextOut(hDC,10,10,"Pleaseentrythenum:

",21);

TextOut(hDC,10,40,"三个序数",1);

待续

TextOut(hDC,10,90,strsub,strlen(strsub));

EndPaint(hWnd,&Ps);

return0;

caseWM_DESTROY:

PostQuitMessage(0);

return0;

}

returnDefWindowProc(hWnd,message,wParam,lParam);

}

6.对话框和控件有关上网信息任务等、、、、

//Ex_ResearchDlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"Ex_Research.h"

#include"Ex_ResearchDlg.h"

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

//CAboutDlgdialogusedforAppAbout

classCAboutDlg:

publicCDialog

{

public:

CAboutDlg();

//DialogData

//{{AFX_DATA(CAboutDlg)

enum{IDD=IDD_ABOUTBOX};

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CAboutDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg:

:

CAboutDlg():

CDialog(CAboutDlg:

:

IDD)

{

//{{AFX_DATA_INIT(CAboutDlg)

//}}AFX_DATA_INIT

}

voidCAboutDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CAboutDlg)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

//{{AFX_MSG_MAP(CAboutDlg)

//Nomessagehandlers

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//CEx_ResearchDlgdialog

CEx_ResearchDlg:

:

CEx_ResearchDlg(CWnd*pParent/*=NULL*/)

:

CDialog(CEx_ResearchDlg:

:

IDD,pParent)

{

//{{AFX_DATA_INIT(CEx_ResearchDlg)

//NOTE:

theClassWizardwilladdmemberinitializationhere

//}}AFX_DATA_INIT

//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32

m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

voidCEx_ResearchDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CEx_ResearchDlg)

//NOTE:

theClassWizardwilladdDDXandDDVcallshere

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CEx_ResearchDlg,CDialog)

//{{AFX_MSG_MAP(CEx_ResearchDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//CEx_ResearchDlgmessagehandlers

BOOLCEx_ResearchDlg:

:

OnInitDialog()

{

CDialog:

:

OnInitDialog();

CheckRadioButton(IDC_AGE_L18,IDC_AGE_M38,IDC_AGE_18TO27);

CheckRadioButton(IDC_CM_FTTL,IDC_CM_OTHER,IDC_CM_FTTL);

CButton*pBtn=(CButton*)GetDlgItem(IDC_DO_POP);

pBtn->SetCheck

(1);

CheckRadioButton(IDC_TIME_L1,IDC_TIME_M3,IDC_TIME_L1);

//Add"About..."menuitemtosystemmenu.

//IDM_ABOUTBOXmustbeinthesystemcommandrange.

ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX<0xF000);

CMenu*pSysMenu=GetSystemMenu(FALSE);

if(pSysMenu!

=NULL)

{

CStringstrAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if(!

strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

}

//Settheiconforthisdialog.Theframeworkdoesthisautomatically

//whentheapplication'smainwindowisnotadialog

SetIcon(m_hIcon,TRUE);//Setbigicon

SetIcon(m_hIcon,FALSE);//Setsmallicon

//TODO:

Addextrainitializationhere

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

voidCEx_ResearchDlg:

:

OnSysCommand(UINTnID,LPARAMlParam)

{

if((nID&0xFFF0)==IDM_ABOUTBOX)

{

CAboutDlgdlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog:

:

OnSysCommand(nID,lParam);

}

}

//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow

//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,

//thisisautomaticallydoneforyoubytheframework.

voidCEx_ResearchDlg:

:

OnPaint()

{

if(IsIconic())

{

CPaintDCdc(this);//devicecontextforpainting

SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);

//Centericoninclientrectangle

intcxIcon=GetSystemMetrics(SM_CXICON);

intcyIcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

intx=(rect.Width()-cxIcon+1)/2;

inty=(rect.Height()-cyIcon+1)/2;

//Drawtheicon

dc.DrawIcon(x,y,m_hIcon);

}

else

{

CDialog:

:

OnPaint();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 化学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1