![VisualC++實例_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-3/18/b71d071c-2163-4480-a33d-862677b79783/b71d071c-2163-4480-a33d-862677b797831.gif)
![VisualC++實例_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-3/18/b71d071c-2163-4480-a33d-862677b79783/b71d071c-2163-4480-a33d-862677b797832.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、-WORD格式-可編輯-第一章實例 1 Hello, Visual C+*/ programl.cpp*#include “ iostream.h ” #include “ stdio.h ” int main()); printf( “ Hello, World!n ” coutvv ” 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;c
2、outvv ” Please enter your name: cinstr;coutvvstrvv ” , Welcome to Win32!n return 0;實例4程序調試*/ program4.cpp*#include “ stdafx.h ” include “ iostream.h ” int ain() int main() int max,a,b,c;coutvv依次輸入三個整數,本程序將對其比較大小,并輸出最大的數.n ” ;coutvv ”第一個數”;cina;coutvv ”第二個數”;cinb;coutvv ”第三個數”;cinc;if (ab)max=a;else
3、max=b;if (cmax)max=c;cutvv ”最大的數是:“ vvmaxvvendl;return 0;第二章實例5進入C+勺密碼 */ program5.cpp*#include “ iostream.hint main() int key;coutvv ”請輸入密碼:“;cinkey;if (key=1234)coutkey;if (key= O)coutv ”密碼正確!“ v” n ” ;coutvv ”歡迎進入 C+ ! ” vv” n ”coutvv ” 07 ” vv” n ” ; return 1;實例7閏年的計算*/ program7.cpp*#include “ i
4、ostream.h ”int main()int year; int f1,f2,f3;coutvv ”請輸入年份:”cinyear;f1=(int)(year/4)*4=year; f2=(int)(year/100)*100=year; f3=(int)(year/400)*400=year;if (f1&(!f2)|f2&f3) coutvvyearvv ”年是閏年 .n else coutvvyearvv ”年不是閏年 .n return 1;實例9多用戶密碼程序/*/ program9.cpp/*#include “ iostream.hint main()int key;coutv
5、v ”請輸入密碼:cinkey;if (key=123) coutvv ” 歡迎您,Wizard.n else if (key=246)coutvv ” 歡迎您,Joe.n ” ;else if (key=999) coutvv ” 歡迎您,David.n ” elsecoutvv ”密碼錯誤!n ” key;switch(key)case 123:coutvv ” 歡迎您,Wizard.nbreak;case 246:coutvv ” 歡迎您,Joe.nbreak;case 999:coutvv ” 歡迎您,David.nbreak;default:coutvv ”密碼錯誤!n ” vv”程
6、序終止! 07n” ;break; return 1;實例11成績分段統(tǒng)計*/ program11.cpp*#include “ iostream.hint main()int cj;coutvv ”請輸入分數:cincj;coutvvswitch(cj)case 90:” 90100 有 12 人.ncoutvvcase 80:” 8090 有 25 人.ncoutvvcase 70:” 7080 有 17 人.ncoutvvcase 60:” 6070 有 5 人.n”coutvv” default:” 60分以下有7人.nreturn 1;12連續(xù)數字的求和實例*/ program12.
7、cpp*#include “ iostream.h ”int main() const int MAX=100; int n,s;s=0;n=1;while (nv=MAX) s=s+n; n+; coutvv ” 1100 的總和為 ” vvsvv” n ” return 1;實例13韓信點兵*/ program13.cpp*#include “ iostream.h ”int main() int i,a,b,c;for (i =1; i v100; i +)a= i %3;b= i %5;c= i %7;if (a=1 & b=0 & c=5)coutvv i vvendl; retur
8、n 1;實例14百雞問題*/ program14.cpp*#include “ iostream.h ”int main()int x,y,z;for (x=1;x20;x+)for y=1;y= a & c A & ck;if (kv0|k100)coutvv ”輸入錯誤!重新輸入:07ncontinue;s+=k;n+; while(1); v=s/n;coutvv ”共輸入”vvnvv” 人,平均成績是” vvendl; return 1;實例17最大值與最小值*/ program17.cpp*#include “ iostream.h ”int max(int,int);int min
9、(int,int);int main()int k,kmax,kmin;kmax=0;kmin=0;docoutvv ”請輸入數據:“;cink;if (9999=k)break;kmax=max(kmax,k); kmin=min(kmin,k);while(1);coutvv ”最大值為” vvkmaxvvendl;coutvv ”最小值為” vvkminvvendl;return 1;int max(int a,int b)return(ab?a:b);int min(int a,int b)return(an;N=fac(n);CoutvvNvvendl;Return 1;int fa
10、c(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;coutvv ”請輸入月數:“;cinn;for (l=1;lv=n;l+) s=f(i);vvendl;coutvv ”第” vv|vv個月共有” vvsvv”對兔子”return;int f(int n)int s;if (n=1|n=2)return 1;s=f(n-1)+f(n-2);return s;實
11、例21奇偶有別/ program21.cpp*#include “ iostream.hint main()const int MAX=100;int aMAX;int bMAX; int i,k,x,y;x=0; y=0;do cink;if (k=9999) break;if (k%2=0) ax=k;x+; else by=k;y+; while (1);for (i=0; ix; i+)coutaivv ”coutvv ” n ” ;for (i=0; ix; i+)coutaivv ”return 1;實例23矩陣轉置*/ program23.cpp*#include “ iostr
12、eam.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;j+)coutaijvv”coutvv ” n ” ;coutvv ” n ” ;for (i=0; i(sig n=n)n=-n;do*str+=n%10+ O while (n/=10)0;if (sign0) *str+=- ;*str= n ; reverse(strtmp);void resverse(char *str) char *t;int c;t=str+(strlen(str)-
13、1;for (t;strn;itostr(str,n);coutvv ” 轉換為字符串 :” vvstrvvendl;實例29學生成績統(tǒng)計*/ 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)coutvv ”姓名:”;;coutvv ”語文:”;cin
14、si.chin;coutvv ”英語:cin si.e ng;coutvv ”數學:”;cinsi.math;coutvv”繼續(xù)輸入? (Y/N)“ ;cinf;i+;for (j=O;jc.cels;coutvv轉化為華氏溫度值為”vvc.change()vv度.n”;p99例子#include “iostream.h ”class ADD public: static int sta; int num; void add() sta+; num+;coutvv sta= vvstavvn ;coutvv num= vvnumvv n ;;int ADD:sta=0;void main()A
15、DD p1,p2;p1.num=0;p2.num=0;p1.add();p2.add();實例35對象的生存周期*/ program35.cpp*#include iostream.hclass cls_Apublic: cls_A() coutvv類實例初始化。nn;cls_A()coutvv類實例被撤銷。n; void f()coutvv調用成員函數。nn;;void main()cls_A *p;coutvv測試開始!nn;p=new cls_A;p-f(); delete p;實例36不同類型數據的比較*/ program36.cpp*#include iostream.h int c
16、omp(int a,int b) coutvvint類型數據比較。最大值為;return(ab?a:b); float comp(float a,float b)coutb?a:b); double comp(double a, double b) coutvv double類型數據比較。最大值為:return(ab?a:b);void main()int i1,i2;float f1,f2;double d1,d2;cini1;cini2;coutvvcomp(i1,i2)vvendl;cinf1;cinf2;coutvvcomp(f1,f2)vvendl;cind1;cind2;coutv
17、vcomp(d1,d2)vvendl;實例39園與園柱(1)*/ program39.cpp*#include “ iostream.hclass cyclprivate:float PI;public:float r;cycl()Pl=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() return area()*h;;void main()cylinder cy;coutvv ”輸入園柱體半徑r:
18、”;cincy.r;coutvv ”輸入園柱體高 h: ”;cincy.h;coutvv ”園柱體底面積為:” vvcy.area()vv ” n”;coutvv ”園柱體體積為:” vvcy.cubage()vv ” n”;實例40園與園柱(2)*/ program40.cpp*#include iostream.hclass cyclprivate:float PI;public:float r;cycl()Pl=float(3.14);r=0;cycl(float R)Pl=float(3.14);r=R; const float area() return Pl*r*r; const
19、float girth() return 2*r*PI;class cylinder:public cycl public: 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);coutvv輸入園柱體1的半徑r:
20、;cincy1.r;coutvv輸入園柱體1的高h:;cincy1.h;coutvv園柱體 1 的表面積為:vvcy1.area()vvn;coutvv園柱體 1 的體積為: vvcy1.cubage()vvnn; coutvv輸入園柱體2的高h:;cincy2.h;coutvv園柱體 2 的表面積為:vvcy2.area()vvn;coutvv園柱體 2 的體積為: vvcy2.cubage()vvnn;coutvv園柱體3的底面積為:vvcy3.cycl:area()vvn;coutvv園柱體 3 的表面積為:vvcy3.area()vvn;coutvv園柱體 3 的體積為: vvcy3.
21、cubage()vvnn;實例41部門職員信息*/ program41.cpp*#include“ iostream.hclass departmentpublic:int num;char design10;class employeepublic:int num;char name10;; class empl_dept:public department,public employee public:void input()coutvv ”依次輸入職員編號、姓名、部門編號、部門名稱n”;cinemployee:num;cinname;cindepartment:num;cindesign
22、;void print()coutvv ” 職員編號:” vvemployee:numvv” 姓名:” vvname;coutvv ”咅部門編號:” vvdepartment:numvv ” 咅部門名稱” vvdesign;void main()empl_dept z;z.input();乙 print();第四章實例48窗口輸出文字與圖形(P152153)void CProgram48View:OnDraw(CDC *pDC) CProgram48Doc *pDoc=GetDocument();ASSERT_VALID(pDoc);/TODO: add code for native dat
23、a here/以下為新增的代碼CRect r;GetClientRect(&r);int x=0, y=0;pDC-TextOut(x,y,“在窗口上畫一個圓形”);x=r.right/2;y=r.bottom/2;pDC-Ellipse(x-100,y-100,x+100,y+100);實例49應用程序的創(chuàng)建與撤銷(P162163)int CMainFrame:OnCreate(LPCREATESTRUCTIpCreateStruct)/添加下面這一行語句,顯示對話框MessageBox( “準備建立應用程序”,“創(chuàng)建程序”,MB_OK|MBCONEXCLAMATION);If (CFram
24、eWnd:OnCreate(IpCreateStruct)= -1)return 1;void CMainFrame:OnClose()TODO:Add your message handler code here and/or call defaultMessageBox( “準備銷毀程序窗口”,“撤銷窗口”,MB_OK|MBCONEXCLAMATION);CFrameWnd:OnClose();實例50鍵盤字符的輸入與顯示(P164166)CProgram50Doc:CProgram50Doc()/TODO: add one-time construction code hereText=
25、 ” ;void CProgram50View:OnChar(UNIT nChar,UINT 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);elsepDoc-Text+=nChar;Invalidate();Cview:OnChar(nChar, nRepCnt
26、,nFlags);void CProgram50View:OnDraw(CDC *pDC)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區(qū)域探測(P168171)void CProgram51View:OnLButtonUp(UINT nFlags,CPointpoint)/TODO:Add you
27、r message handler code here and/or call defaultCRect r;GetClientRect(&r);char *s;if (point.xr.right/2)if (point.yr.bottom/2)s=”你現在位于第四區(qū)”;elses=”你現在位于第一區(qū)”;elseif (point.yr.bottom/2)s=”你現在位于第三區(qū)”;elses= ”你現在位于第二區(qū)”;MessageBox(s, ” 位置提示”,MB_OK|MBCONEXCLAMATION);CView:OnLButtonUp(nFlags,point);void CProgr
28、am51View:OnDraw(CDC *pDC)CProgram51Doc *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,CPointpoint)/TODO: Add your message handler code here and/or calldefaultCProgram51Doc *pDoc=GetD
29、ocument();ASSERT_VALID(pDoc);pDoc-Text=lntToCStr(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;s
30、trnum.MakeReverse();return strnum;實例52滾動字幕(P172173)CProgram52View:CProgram52View()/TO: add construction code herems=0;x=0;CProgram52View:OnCreate(LPCREATESTRUCTIpCreateStruct)if (CView:OnCreate(lpCreateStruct)=-1)/TODO: Add your specialized creation code hereSetTimer(1,10,NULL);return 0;void CProgra
31、m52View:OnTimer(UNIT nIDEvent)/TODO: Add your message handler code and/or calldefaultlnvalidateRect(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( “字幕已運行
32、 d毫秒”,ms);if (xr.right-r.left)x=0;pDC-TextOut(x,r.bottom/2, ” Visual C+ 6.0“);pDC-TextOut(x,r.bottom/2+2O,s);實例53園的移動軌跡 (P175179)void CProgram53View:OnDraw(CDC *pDC)CProgram53Doc*pDoc=GetDocument();ASSERT VALID(pDoc);/TODO: add draw code for native data hereDrawEps();void CPprogram53View:OnlnitialUp
33、date()CScrollView:OnInitialUpdate();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
34、,y+50);void CProgram53View:OnHScroll(UNIT nSBCode,UNITnPos,CScrollBar *pScrollBar)/TODO: Add your message handler code here and/or calldefaultDrawEps();CScrollView:OnHScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnVScroll(UNIT nSBCode,UNIT nPos,CScrollBar *pScrollBar)/TODO: Add your message h
35、andler code here and/or calldefaultDrawEps();CScrollView:OnVScroll(nSBCode,nPos,pScrollBar);void CProgram53View:OnKeyDown(UNIT nChar, UINTnRepCnt,UINT nFlags)/TODO: Add your message handler code and/or calldefaultswitch(nChar)case VK_NUMPAD7: OnVScroll(SB_LINEUP,0,NULL);OnHScroll(SB_LINELEFT,0,NULL)
36、;break;case VK_NUMPAD1: OnVScroll(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;c
37、ase VK_NUMPAD9:OnVScroll(SB_LINEUP,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(LPCREATE
38、STRUCT IpCreateStruct)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_CLOS消息!“,” 提示信息” ,MB_OK);CFrameWnd:OnClose();void CMainFrame:OnDestroy()CFrameWnd:OnDestroy();/TODO: Add your message handler code hereMessageBox( “收到 WM_DESTROY息!“, ” 提示信息” ,MB_OK);BOOL CMainFrame:OnQueryEndSession()if (!CFrameWnd:OnQueryEndSession()return FALSE;/TODO: Add your specialized query end session codehereMessageBox( “收至U WM_
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 對技術研發(fā)產品進行在次加工的合同范本(3篇)
- 2024-2025學年河南省青桐鳴大聯考高一上學期12月月考歷史試卷
- 2025年雙方共同簽署的離婚協議
- 2025年個人購置豪華花園房合同范文
- 2025年九臺市報社資源共享合作協議
- 2025年炊具掛盤項目立項申請報告模板
- 2025年策劃合作賬戶管理解除協議書范本
- 2025年配藥中心項目提案報告模稿
- 2025年供應商合作關系協議文本
- 2025年中國近距離運輸合同規(guī)定
- GB/T 19868.2-2005基于焊接經驗的工藝評定
- 第一章:智能控制概論-課件
- 蘭州牛肉面攻略課件
- 機房巡檢記錄表
- 警燈、警報器使用證申請表
- (中職)電梯維護與保養(yǎng)項目九 電梯曳引系統(tǒng)的維護與保養(yǎng)教學課件
- 中國科學院率先行動計劃組織實施方案
- 園林規(guī)劃設計16_任務三-交通廣場綠地設計
- 節(jié)制閘工程施工組織設計方案
- 《新媒體廣告設計》—教學教案
- 2022版義務教育(物理)課程標準(含2022年修訂和新增部分)
評論
0/150
提交評論