Visual C++实例.doc

上传人:1595****071 文档编号:33826971 上传时间:2022-08-12 格式:DOC 页数:20 大小:88.50KB
返回 下载 相关 举报
Visual C++实例.doc_第1页
第1页 / 共20页
Visual C++实例.doc_第2页
第2页 / 共20页
点击查看更多>>
资源描述

《Visual C++实例.doc》由会员分享,可在线阅读,更多相关《Visual C++实例.doc(20页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、如有侵权,请联系网站删除,仅供学习与交流Visual C+实例【精品文档】第 20 页第一章实例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

2、str20;coutstr;coutstr”, Welcome to 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#in

3、clude “iostream.h”int main()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 yea

4、r;int f1,f2,f3;coutyear;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

5、”欢迎您, Joe.n”;else 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”程序终

6、止! 07n”;break;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()

7、const 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()int x

8、,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 “; continue;s

9、+=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);int min(int a,

10、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,s,I;coutn;for (I=1;I

11、=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 (i=0; i3; i+) for (j=0;j4

12、;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;str+,t)c=*str;str=t;*t=c;voi

13、d 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.name;coutsi.chin;coutsi.eng;couts

14、i.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 main() tempera c;coutc.cels;cout”转

15、化为华氏温度值为”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 iostream.hclass cls_Apublic:cls_A() cout类实例初始

16、化。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() int i1,i2; float f1,f2; double d1,d2;cini

17、1;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 cyclpublic:float h;cylinder() r=0; h=0;float cubage()re

18、turn 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(3.14); r=R;const float area()return PI*r*r;const f

19、loat 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()cylinder cy1,cy2(5),cy3(5,10);coutcy1.r;coutcy1.h;co

20、ut园柱体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 “iostream.h”class departmentpublic:int num;char design10;

21、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” 部门名称”TextOut(x,y, “在窗口上画一个圆形”);x=r.right/2;y=r.bottom/2;pDC-El

22、lipse(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 CMainFrame:OnClose( )/TODO:Add your message handler code her

23、e 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,UINT nRepCnt, UINT nFlags)/TODO:Add your message handler cod

24、e 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 *pDC)CProgram50Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);

25、/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 call defaultCRect r;GetClientRect(&r);char *s;if (point.xr.

26、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) CProgram51Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add d

27、raw 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);pDoc-Text=IntToCStr(point.x)+”,”+IntToCStr(point.y);mPoint

28、.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;实例52 滚动字幕 (P172173)CProgram52View:CProgram52View()/TO: add

29、 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)/TODO: Add your message handler code and/or call default/In

30、validateRect(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=0;pDC-TextOut(x,r.bottom/2,”Visual C+ 6.0 “);pDC-TextOut

31、(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:OnInitialUpdate();CSize sizeTotal;/TODO: calculate the total size of th

32、is 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:OnHScroll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO:

33、 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 defaultDrawEps();CScrollView:OnVScroll(nSBCode,nPos,pScrol

34、lBar);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:OnVScroll(SB_LINEDOWN,0,NULL);OnHScroll(SB_LINELEFT,0,NULL);br

35、eak;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_LINEUP,0,NULL);OnHScroll(SB_LINERIGHT,0,NULL);break;case VK_

36、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(“收到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 message handler code hereMessageBox

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com