Visual C++实例

上传人:gui****hi 文档编号:93327167 上传时间:2022-05-20 格式:DOC 页数:28 大小:122.50KB
收藏 版权申诉 举报 下载
Visual C++实例_第1页
第1页 / 共28页
Visual C++实例_第2页
第2页 / 共28页
Visual C++实例_第3页
第3页 / 共28页
资源描述:

《Visual C++实例》由会员分享,可在线阅读,更多相关《Visual C++实例(28页珍藏版)》请在装配图网上搜索。

1、第一章实例1 Hello, Visual C+/*/ program1.cpp/*#include “iostream.h”#include “stdio.h”int main()printf(“Hello, World!n”);cout”Hello,Visual C+ 6.0!n”;return 0;实例2 Welcome to Win32!/*/ program2.cpp/*#include “stdafx.h”#include “iostream.h”int main(int argc,char *argv)char str20;coutstr;coutstr”, Welcome to

2、Win32!n”;return 0;实例4 程序调试/*/ program4.cpp/*#include “stdafx.h”include “iostream.h”int ain()int main()int max,a,b,c;cout”依次输入三个整数,本程序将对其比较大小,并输出最大的数.n”;couta;coutb;coutc;if (ab)max=a;else max=b;if (cmax)max=c;cut”最大的数是: “maxendl;return 0;第二章实例5 进入C+的密码/*/ program5.cpp/*#include “iostream.h”int main(

3、)int key;coutkey;if (key=1234)cout”密码正确! “endl;cout”欢迎进入C+ !”endl;return 1;实例6 有提示音的密码程序/*/ program6.cpp/*#include “iostream.h”int main()int key;coutkey;if (key=0)cout”密码正确! “”n”;cout”欢迎进入C+ !”n”;cout”07”n”;return 1;实例7 闰年的计算/*/ program7.cpp/*#include “iostream.h”int main()int year;int f1,f2,f3;cout

4、year;f1=(int)(year/4)*4=year;f2=(int)(year/100)*100=year;f3=(int)(year/400)*400=year;if (f1&(!f2)|f2&f3) coutyear”年是闰年. n”;elsecoutyear”年不是闰年. n”;return 1;实例9 多用户密码程序/*/ program9.cpp/*#include “iostream.h”int main()int key;coutkey;if (key=123) cout”欢迎您, Wizard.n”;else if (key=246)cout”欢迎您, Joe.n”;el

5、se if (key=999)cout”欢迎您, David.n” else cout”密码错误!n”程序终止! 07n”;return 1;实例10 另一种多用户密码程序/*/ program10.cpp/*#include “iostream.h”int main()int key;coutkey;switch(key) case 123:cout”欢迎您, Wizard.n”;break;case 246:cout”欢迎您, Joe.n”;break;case 999: cout”欢迎您, David.n”;break;default:cout”密码错误!n”程序终止! 07n”;bre

6、ak;return 1;实例11 成绩分段统计/*/ program11.cpp/*#include “iostream.h”int main()int cj;coutcj;switch(cj)case 90: cout”90100有12人.n”;case 80: cout”8090有25人.n”;case 70: cout”7080有17人.n”;case 60: cout”6070有5人.n”;default:cout”60分以下有7人.n”;return 1;实例12 连续数字的求和/*/ program12.cpp/*#include “iostream.h”int main()con

7、st int MAX=100;int n,s;s=0;n=1;while (n=MAX)s=s+n;n+;cout”1100的总和为 ”s”n”;return 1;实例13 韩信点兵/*/ program13.cpp/*#include “iostream.h”int main()int i,a,b,c;for (i =1; i 100; i +)a= i %3;b= i %5;c= i %7;if (a=1 & b=0 & c=5) cout i endl;return 1;实例14 百鸡问题/*/ program14.cpp/*#include “iostream.h”int main()

8、int x,y,z;for (x=1;x20;x+) for y=1;y33;y+) z=100-x-y; if (5*x+y*3+z/3=100&z%3=0) cout”公鸡有”x”只,母鸡有”y”只,小鸡有”z=a & cA & c=Z) n+;cout”总计输入了”n”个英文字符.n”;return 1;实例16 英文字符的统计/*/ program16.cpp/*#include “iostream.h”#iinclude “stdio.h”int main()int k,s,v,n;s=0;n=0;do coutk;if (k100) cout”输入错误!重新输入: 07n “; c

9、ontinue;s+=k;n+; while(1);v=s/n;cout”共输入”n”人,平均成绩是”endl;return 1;实例17 最大值与最小值/*/ program17.cpp/*#include “iostream.h”int max(int,int);int min(int,int);int main()int k,kmax,kmin;kmax=0;kmin=0;docoutk;if (9999=k)break;kmax=max(kmax,k);kmin=min(kmin,k);while(1);cout”最大值为”kmaxendl;cout”最小值为”kminb?a:b);i

10、nt min(int a,int b) return(ab?a:b);实例18 数的阶乘/*/ program18.cpp/*#include “iostream.h”int fac(int);int main()int n;long int N;coutn;N=fac(n);CoutNendl;Return 1;int fac(int n)long int s;if (n=1) return 1;s=n*fac(n-1);return s;实例19 菲波那契数列/*/ program19.cpp/*#include “iostream.h”int f(int);int main()int n

11、,s,I;coutn;for (I=1;I=n;I+)s=f(i);cout”第”I个月共有”s”对兔子”k; if (k=9999) break; if (k%2=0) ax=k;x+;elseby=k;y+;while (1);for (i=0; ix; i+)coutai” “;cout”n”;for (i=0; ix; i+)coutai” “;return 1;实例23 矩阵转置/*/ program23.cpp/*#include “iostream.h”int main()int a34=1,2,3,4,5,6,7,8,9,10,11,12;int b43;int i,j;for

12、 (i=0; i3; i+) for (j=0;j4;j+) coutaij” “;cout”n”;cout”n”;for (i=0; i3; i+) for (j=0;j4;j+) bj i=aij;for (i=0; i4; i+)for (j=0;j3;j+) cout (sign=n) n=-n;do *str+=n%10+0; while (n/=10)0;if (sign0) *str+=-;*str=n;reverse(strtmp);void resverse(char *str)char *t;int c;t=str+(strlen(str)-1;for (t;strt;st

13、r+,t)c=*str;str=t;*t=c; void main()int n;char str40;coutn;itostr(str,n);cout”转换为字符串: ”strendl;实例29 学生成绩统计/*/ program29.cpp/*#include “iostream.h”typedef struct studentchar name8;int chin;int eng;int math; STUD;void main()STUD a20;int i,j,ch,en,ma;char f=Y;ch=0;en=0;ma=0;i=0;While (f=Y|f=y) coutsi.na

14、me;coutsi.chin;coutsi.eng;coutsi.math; coutf;i+;for (j=0;ji;j+) ch=ch+sj.chin; ma=ma+sj.math; en=en+sj.eng;cout”语文平均成绩为”ch/iendl;cout”数学平均成绩为”ma/iendl;cout”英语平均成绩为”en/iendl;第三章实例34 温度转换类的封装/*/ program34.cpp/*#include “iostream.h”class temperapublic:float cels;float change() return (9*cels)/5+32;void

15、 main() tempera c;coutc.cels;cout”转化为华氏温度值为”c.change()” 度.n ”;p99 例子#include “iostream.h”class ADDpublic:static int sta;int num;void add() sta+;num+;cout”sta=”sta”n”;cout”num=”num”n”;int ADD:sta=0;void main()ADD p1,p2;p1.num=0;p2.num=0;p1.add();p2.add();实例35 对象的生存周期/*/ program35.cpp/*#include iostre

16、am.hclass cls_Apublic:cls_A() cout类实例初始化。nn;cls_A()cout类实例被撤销。n;void f()cout调用成员函数。nn;void main()cls_A *p;coutf();delete p;实例36 不同类型数据的比较/*/ program36.cpp/*#include iostream.hint comp(int a,int b) coutb?a:b);float comp(float a,float b) coutb?a:b);double comp(double a, double b) coutb?a:b);void main(

17、) int i1,i2; float f1,f2; double d1,d2;cini1;cini2;coutcomp(i1,i2)f1;cinf2;coutcomp(f1,f2)d1;cind2;coutcomp(d1,d2)endl;实例39 园与园柱()/*/ program39.cpp/*#include “iostream.h”class cyclprivate:float PI;public:float r;cycl() PI=float(3.14);r=0;const float area()return PI*r*r;class cylinder:public cyclpubl

18、ic:float h;cylinder() r=0; h=0;float cubage()return area()*h;void main()cylinder cy;coutcy.r;coutcy.h;cout”园柱体底面积为: ”cy.area()”n”;cout”园柱体体积为: ”cy.cubage()”n”;实例40 园与园柱(2)/*/ program40.cpp/*#include iostream.hclass cyclprivate:float PI;public:float r;cycl() PI=float(3.14); r=0;cycl(float R) PI=float

19、(3.14); r=R;const float area()return PI*r*r;const float girth() return 2*r*PI; ;class cylinder:public cyclpublic:float h;cylinder() h=0;cylinder(float R):cycl (R)cylinder(float R,float H):cycl(R)h=H;const float cubage()return cycl:area()*h;const float area()return girth()*h+(cycl:area()*2;void main(

20、)cylinder cy1,cy2(5),cy3(5,10);coutcy1.r;coutcy1.h;cout园柱体1的表面积为: cy1.area()n;cout园柱体1的体积为: cy1.cubage()nn;coutcy2.h;cout园柱体2的表面积为: cy2.area()n;cout园柱体2的体积为: cy2.cubage()nn;cout园柱体3的底面积为: cy3.cycl:area()n;cout园柱体3的表面积为: cy3.area()n;cout园柱体3的体积为: cy3.cubage()nn;实例41 部门职员信息/*/ program41.cpp/*#include

21、“iostream.h”class departmentpublic:int num;char design10;class employeepublic:int num;char name10;class empl_dept:public department,public employeepublic:void input()coutemployee:num;cinname;cindepartment:num;cindesign;void print() cout”职员编号:”employee:num” 姓名:”name; cout” 部门编号:”department:num” 部门名称”

22、TextOut(x,y, “在窗口上画一个圆形”);x=r.right/2;y=r.bottom/2;pDC-Ellipse(x-100,y-100,x+100,y+100);实例49 应用程序的创建与撤销 (P162163)int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)/添加下面这一行语句,显示对话框MessageBox(“准备建立应用程序”, “创建程序”,MB_OK|MB_ICONEXCLAMATION);If (CFrameWnd:OnCreate(lpCreateStruct)= -1) return 1;void CMa

23、inFrame:OnClose( )/TODO:Add your message handler code here and/or call defaultMessageBox(“准备销毁程序窗口”, “撤销窗口”,MB_OK|MB_ICONEXCLAMATION);CFrameWnd:OnClose( );实例50 键盘字符的输入与显示 (P164166)CProgram50Doc:CProgram50Doc( )/TODO: add one-time construction code hereText=”;void CProgram50View:OnChar(UNIT nChar,UIN

24、T nRepCnt, UINT nFlags)/TODO:Add your message handler code here and/or call defaultCProgram50Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);if (nChar=b) pDoc-Text.Delete(pDoc-Text.GetLength( )-1,1);else pDoc-Text+=nChar;Invalidate( );Cview:OnChar(nChar, nRepCnt,nFlags);void CProgram50View:OnDraw(CDC *p

25、DC)CProgram50Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add draw code for native data hereCRect r;GetClientRect(&r);pDC-DrawText(pDoc-Text,r,DT_WORDBREAK|DT_CENTER);实例51 区域探测 (P168171)void CProgram51View:OnLButtonUp(UINT nFlags,CPoint point)/TODO: Add your message handler code here and/or ca

26、ll defaultCRect r;GetClientRect(&r);char *s;if (point.xr.right/2) if (point.yr.bottom/2)s=”你现在位于第四区”;elses=”你现在位于第一区”;else if (point.yr.bottom/2)s=”你现在位于第三区”;else s=”你现在位于第二区”;MessageBox(s,”位置提示”,MB_OK|MB_ICONEXCLAMATION);CView:OnLButtonUp(nFlags,point);void CProgram51View:OnDraw(CDC *pDC) CProgram5

27、1Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add draw code for native data herepDC-TextOut(mPoint.x,mPoint.y,pDoc-Text);void CProgram51View:OnMouseMove(UINT nFlags,CPoint point)/TODO: Add your message handler code here and/or call defaultCProgram51Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);p

28、Doc-Text=IntToCStr(point.x)+”,”+IntToCStr(point.y);mPoint.x=point.x+20;mPoint.y=point.y;Invalidate( );CView:OnMouseMove(nFlags,point);Cstring CProgram51View:IntToCStr(int Num)int i;CString strnum=”;char c;while (Num0)i=Num%10;c=0;c=c+i;strnum+=(char)c;Num=Num/10;strnum.MakeReverse( );return strnum;实

29、例52 滚动字幕 (P172173)CProgram52View:CProgram52View()/TO: add construction code herems=0;x=0;CProgram52View:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CView:OnCreate(lpCreateStruct)=-1)/TODO: Add your specialized creation code hereSetTimer(1,10,NULL);return 0;void CProgram52View:OnTimer(UNIT nIDEvent)/T

30、ODO: Add your message handler code and/or call default/InvalidateRect(NULL,ture);/UpdateWindow();ms+=10;x=x+1;Invalidate();CView:OnTimer(10);void CProgram52View:OnDraw(CDC *pDC)/TODO: add draw code for native data hereCRect r;GetClientRect(&r);CString s;s.Format(“字幕已运行%d毫秒”,ms);if (xr.right-r.left)x

31、=0;pDC-TextOut(x,r.bottom/2,”Visual C+ 6.0 “);pDC-TextOut(x,r.bottom/2+20,s);实例53园的移动轨迹 (P175179)void CProgram53View:OnDraw(CDC *pDC)CProgram53Doc*pDoc=GetDocument();ASSERT VALID(pDoc);/TODO: add draw code for native data hereDrawEps();void CPprogram53View:OnInitialUpdate()CScrollView:OnInitialUpdat

32、e();CSize sizeTotal;/TODO: calculate the total size of this viewsizeTotal.cx=1000;sizeTotal.cy=800;SetScrollSizes(MM_TEXT,sizeTotal);void CProgram53View:DrawEps()CDC *pDC=GetDC();CRect r;GetClientRect(&r);int x=0,y=0;x=r.right/2;y=r.bottom/2;pDC-Ellipse(x-50,y-50,x+50,y+50);void CProgram53View:OnHSc

33、roll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO: Add your message handler code here and/or call defaultDrawEps();CScrollView:OnHScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnVScroll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO: Add your message handler code here and/or call d

34、efaultDrawEps();CScrollView:OnVScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnKeyDown(UNIT nChar, UINT nRepCnt,UINT nFlags)/TODO: Add your message handler code and/or call defaultswitch(nChar)case VK_NUMPAD7:OnVScroll(SB_LINEUP,0,NULL);OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD1:OnVSc

35、roll(SB_LINEDOWN,0,NULL);OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD8:OnVScroll(SB_LINEUP,0,NULL);break;case VK_NUMPAD2:OnVScroll(SB_LINEDOWN,0,NULL);break;case VK_NUMPAD6:OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD4:OnHScroll(SB_LINELEFT,0,NULL);break;case VK_NUMPAD9:OnVScroll(SB_LIN

36、EUP,0,NULL);OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD3:OnVScroll(SB_LINEDOWN,0,NULL);OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_NUMPAD5:Invalidate();break;CScrollView:OnKeyDown(nChar,nRepCnt,nFlags);实例54 应用程序的生存周期 (P180182)int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)MessageBox(

37、“收到WM_CREATE消息! “,”提示信息”,MB_OK);If (CFrameWnd:OnCreate(lpCreateStruct)=-1)return 1;void CMainFrame:OnClose()/TODO:Add your message handler code here and/or call defaultMessageBox(“收到WM_CLOSE消息! “,”提示信息”,MB_OK);CFrameWnd:OnClose();void CMainFrame:OnDestroy()CFrameWnd:OnDestroy();/TODO: Add your messa

38、ge handler code hereMessageBox(“收到WM_DESTROY消息! “,”提示信息”,MB_OK);BOOL CMainFrame:OnQueryEndSession()if (!CFrameWnd:OnQueryEndSession()return FALSE;/TODO: Add your specialized query end session code hereMessageBox(“收到WM_QUERYENDSESSION消息! “,”提示信息”,MB_OK);return TURE;void CMainFrame:PostNcDestroy()/TOD

39、O:Add your spcialized code here and/or call the base classMessageBox(“正在执行PostNcDestroy函数! “,”提示信息”,MB_OK);CFrameWnd:PostNcDestroy();BOOL CMainFrame:DestroyWindow()/TODO:Add your spcialized code here and/or call the base classMessageBox(“正在执行DestroyWindow函数! “,”提示信息”,MB_OK);return CFrameWnd:DestroyW

40、indow();实例56 电子秒表 (P192198)void CProgram56Dlg:OnClose()/TODO:Add your control notification handler code hereCDialog:OnCancel();void CProgram56Dlg:OnButstart()/TODO: Add your control notification handler code herem_iMSEL=0;SetTimer(1,10,NULL);m_tStar=Ctime:GetCurentTime();void CProgram56Dlg:OnButend(

41、)/TODO:Add your control notification handler code hereKillTimer(1);void CProgram56Dlg:OnTimer(UNIT nIDEvent)/TODO: Add your message handler code and/or call defaultCTimeSpan tInterval;CTime tCurrentTime;int ih,im,is;tCurrentTime=CTime:GetCurentTime();ih=tCurrentTime.GetHour();im=tCurrentTime.GetMinu

42、te();is=tCurrentTime.GetSecond();m_strCurr.Format(“现在时间是%d点%d分%d秒”ih,im,is);m_iMSEL+=1;tInterval=tCurrentTime-m_tStar;ih=tInterval.GetHours();im=tInterval.GetMinutes();is=tInterval.GetSeconds();if (m_iMSEL99) m_iMSEL=0;m_strClock.Format(“%d:%d:%d.%d”,ih,im,is,m_iMSEL);UpdateData(FALSE);CDialog:OnTimer(nIDEvent);全部结束

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!