Visual C實(shí)例_第1頁
Visual C實(shí)例_第2頁
Visual C實(shí)例_第3頁
Visual C實(shí)例_第4頁
Visual C實(shí)例_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第一章實(shí)例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;實(shí)例2 Welcome to Win32!/*/ program2.cpp/*#include “stdafx.h”#include “iostream.h”int main(int argc,char *argv)char str20;cout<<”Please e

2、nter your name:”;cin>>str;cout<<str<<”, Welcome to Win32!n”;return 0;實(shí)例4 程序調(diào)試/*/ program4.cpp/*#include “stdafx.h”include “iostream.h”int ain()int main()int max,a,b,c;cout<<”依次輸入三個(gè)整數(shù),本程序?qū)?duì)其比較大小,并輸出最大的數(shù).n”;cout<<”第一個(gè)數(shù)”;cin>>a;cout<<”第二個(gè)數(shù)”;cin>>b;cout<

3、;<”第三個(gè)數(shù)”;cin>>c;if (a>b)max=a;else max=b;if (c>max)max=c;cut<<”最大的數(shù)是: “<<max<<endl;return 0;第二章實(shí)例5 進(jìn)入C+的密碼/*/ program5.cpp/*#include “iostream.h”int main()int key;cout<<”請(qǐng)輸入密碼: “;cin>>key;if (key=1234)cout<”密碼正確! “<<endl;cout<<”歡迎進(jìn)入C+ !”<

4、;<endl;return 1;實(shí)例6 有提示音的密碼程序/*/ program6.cpp/*#include “iostream.h”int main()int key;cout<<”請(qǐng)輸入密碼: ”;cin>>key;if (key=0)cout<”密碼正確! “<”n”;cout<<”歡迎進(jìn)入C+ !”<<”n”;cout<<”07”<<”n”;return 1;實(shí)例7 閏年的計(jì)算/*/ program7.cpp/*#include “iostream.h”int main()int year;in

5、t f1,f2,f3;cout<<”請(qǐng)輸入年份: ”;cin>>year;f1=(int)(year/4)*4=year;f2=(int)(year/100)*100=year;f3=(int)(year/400)*400=year;if (f1&&(!f2)|f2&&f3) cout<<year<<”年是閏年. n”;elsecout<<year<<”年不是閏年. n”;return 1;實(shí)例9 多用戶密碼程序/*/ program9.cpp/*#include “iostream.h”i

6、nt main()int key;cout<<”請(qǐng)輸入密碼: “;cin>>key;if (key=123) cout<<”歡迎您, Wizard.n”;else if (key=246)cout<<”歡迎您, Joe.n”;else if (key=999)cout<<”歡迎您, David.n” else cout<<”密碼錯(cuò)誤!n”<<”程序終止! 07n”;return 1;實(shí)例10 另一種多用戶密碼程序/*/ program10.cpp/*#include “iostream.h”int main()

7、int key;cout<<”請(qǐng)輸入密碼: “;cin>>key;switch(key) case 123:cout<<”歡迎您, Wizard.n”;break;case 246:cout<<”歡迎您, Joe.n”;break;case 999: cout<<”歡迎您, David.n”;break;default:cout<<”密碼錯(cuò)誤!n”<<”程序終止! 07n”;break;return 1;實(shí)例11 成績分段統(tǒng)計(jì)/*/ program11.cpp/*#include “iostream.h”int

8、 main()int cj;cout<<”請(qǐng)輸入分?jǐn)?shù): ”;cin>cj;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;實(shí)例12 連續(xù)數(shù)字的求和/*/ program12.cpp/*#include “iostream.h”int main()cons

9、t int MAX=100;int n,s;s=0;n=1;while (n<=MAX)s=s+n;n+;cout<<”1100的總和為 ”<<s<<”n”;return 1;實(shí)例13 韓信點(diǎn)兵/*/ 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;ret

10、urn 1;實(shí)例14 百雞問題/*/ program14.cpp/*#include “iostream.h”int main()int x,y,z;for (x=1;x<20;x+) for y=1;y<33;y+) z=100-x-y; if (5*x+y*3+z/3=100&&z%3=0) cout<<”公雞有”<<x<<”只,母雞有”<<y<<”只,小雞有”<<z<<”只.n”;return 1;實(shí)例15 英文字符的統(tǒng)計(jì)/*/ program15.cpp/*#include

11、“iostream.h”#iinclude “stdio.h”int main()int n=0;char c;for (;)c=getchar();if (c=n)break;if (c>=a && c<=z|c>A && c<=Z) n+;cout<<”總計(jì)輸入了”<<n<<”個(gè)英文字符.n”;return 1;實(shí)例16 英文字符的統(tǒng)計(jì)/*/ program16.cpp/*#include “iostream.h”#iinclude “stdio.h”int main()int k,s,v,n;s=

12、0;n=0;do cout<<”請(qǐng)輸入成績: ”; cin>>k;if (k<0|k>100) cout<<”輸入錯(cuò)誤!重新輸入: 07n “; continue;s+=k;n+; while(1);v=s/n;cout<<”共輸入”<<n<<”人,平均成績是”<<endl;return 1;實(shí)例17 最大值與最小值/*/ program17.cpp/*#include “iostream.h”int max(int,int);int min(int,int);int main()int k,kma

13、x,kmin;kmax=0;kmin=0;docout<<”請(qǐng)輸入數(shù)據(jù): “;cin>>k;if (9999=k)break;kmax=max(kmax,k);kmin=min(kmin,k);while(1);cout<<”最大值為”<<kmax<<endl;cout<<”最小值為”<<kmin<<endl;return 1;int max(int a,int b) return(a>b?a:b);int min(int a,int b) return(a<b?a:b);實(shí)例18 數(shù)的

14、階乘/*/ program18.cpp/*#include “iostream.h”int fac(int);int main()int n;long int N;cout<<”請(qǐng)輸入一個(gè)整數(shù): “;cin>>n;N=fac(n);Cout<<N<<endl;Return 1;int fac(int n)long int s;if (n=1) return 1;s=n*fac(n-1);return s;實(shí)例19 菲波那契數(shù)列/*/ program19.cpp/*#include “iostream.h”int f(int);int main()

15、int n,s,I;cout<<”請(qǐng)輸入月數(shù): “;cin>>n;for (I=1;I<=n;I+)s=f(i);cout<<”第”<<I<<個(gè)月共有”<<s<<”對(duì)兔子”<<endl;return;int f(int n)int s;if (n=1|n=2) return 1;s=f(n-1)+f(n-2);return s;實(shí)例21 奇偶有別/*/ program21.cpp/*#include “iostream.h”int main()const int MAX=100;int aMA

16、X;int bMAX;int i,k,x,y;x=0;y=0;do cin>>k; if (k=9999) break; if (k%2=0) ax=k;x+;elseby=k;y+;while (1);for (i=0; i<x; i+)cout<<ai<<” “;cout<<”n”;for (i=0; i<x; i+)cout<<ai<<” “;return 1;實(shí)例23 矩陣轉(zhuǎn)置/*/ program23.cpp/*#include “iostream.h”int main()int a34=1,2,3,

17、4,5,6,7,8,9,10,11,12;int b43;int i,j;for (i=0; i<3; i+) for (j=0;j<4;j+) cout<<aij<<” “;cout<<”n”;cout<<”n”;for (i=0; i<3; i+) for (j=0;j<4;j+) bj i=aij;for (i=0; i<4; i+)for (j=0;j<3;j+) cout <<”n”return 1;實(shí)例27 整數(shù)轉(zhuǎn)換為字符串/*/ program27.cpp/*#include “ios

18、tream.h”#include “string.h”void itostr(char *,int);void reverse(char *);void itostr(char *str,int n)int sign;char *strcmp=str;if (0>(sign=n) n=-n;do *str+=n%10+0; while (n/=10)>0;if (sign<0) *str+=-;*str=n;reverse(strtmp);void resverse(char *str)char *t;int c;t=str+(strlen(str)-1;for (t;str

19、<t;str+,t)c=*str;str=t;*t=c; void main()int n;char str40;cout<<”輸入數(shù)字:”;cin>>n;itostr(str,n);cout<<”轉(zhuǎn)換為字符串: ”<<str<<endl;實(shí)例29 學(xué)生成績統(tǒng)計(jì)/*/ program29.cpp/*#include “iostream.h”typedef struct studentchar name8;int chin;int eng;int math; STUD;void main()STUD a20;int i,j,ch,

20、en,ma;char f=Y;ch=0;en=0;ma=0;i=0;While (f=Y|f=y) cout<<”姓名:”; cin>>;cout<<”語文:”; cin>>si.chin;cout<<”英語:”; cin>>si.eng;cout<<”數(shù)學(xué):”; cin>>si.math; cout<<”繼續(xù)輸入? (Y/N) “; cin>>f;i+;for (j=0;j<i;j+) ch=ch+sj.chin; ma=ma+sj.math; en=e

21、n+sj.eng;cout<<”語文平均成績?yōu)椤?lt;<ch/i<<endl;cout<<”數(shù)學(xué)平均成績?yōu)椤?lt;<ma/i<<endl;cout<<”英語平均成績?yōu)椤?lt;<en/i<<endl;第三章實(shí)例34 溫度轉(zhuǎn)換類的封裝/*/ program34.cpp/*#include “iostream.h”class temperapublic:float cels;float change() return (9*cels)/5+32;void main() tempera c;cout<

22、<”輸入攝氏溫度值: “;cin>>c.cels;cout<<”轉(zhuǎn)化為華氏溫度值為”<<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.nu

23、m=0;p2.num=0;p1.add();p2.add();實(shí)例35 對(duì)象的生存周期/*/ program35.cpp/*#include "iostream.h"class cls_Apublic:cls_A() cout<<"類實(shí)例初始化。nn"cls_A()cout<<"類實(shí)例被撤銷。n"void f()cout<<"調(diào)用成員函數(shù)。nn"void main()cls_A *p;cout<<"測試開始!nn"p=new cls_A;p->

24、;f();delete p;實(shí)例36 不同類型數(shù)據(jù)的比較/*/ program36.cpp/*#include "iostream.h"int comp(int a,int b) cout<<"int 類型數(shù)據(jù)比較。最大值為"return(a>b?a:b);float comp(float a,float b) cout<<"float 類型數(shù)據(jù)比較。最大值為"return(a>b?a:b);double comp(double a, double b) cout<<" dou

25、ble 類型數(shù)據(jù)比較。最大值為:"return(a>b?a:b);void main() int i1,i2; float f1,f2; double d1,d2;cin>>i1;cin>>i2;cout<<comp(i1,i2)<<endl;cin>>f1;cin>>f2;cout<<comp(f1,f2)<<endl;cin>>d1;cin>>d2;cout<<comp(d1,d2)<<endl;實(shí)例39 園與園柱()/*/ pro

26、gram39.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()return area()*h;void main()cylinder cy;cout<<”輸入園柱體半徑 r:”;cin>>cy.r;cout<<

27、”輸入園柱體高 h:”;cin>>cy.h;cout<<”園柱體底面積為: ”<<cy.area()<<”n”;cout<<”園柱體體積為: ”<<cy.cubage()<<”n”;實(shí)例40 園與園柱(2)/*/ program40.cpp/*#include "iostream.h"class cyclprivate:float PI;public:float r;cycl() PI=float(3.14); r=0;cycl(float R) PI=float(3.14); r=R;co

28、nst 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()cylinder cy1,

29、cy2(5),cy3(5,10);cout<<"輸入園柱體1的半徑 r:"cin>>cy1.r;cout<<"輸入園柱體1的高 h:"cin>>cy1.h;cout<<"園柱體1的表面積為: "<<cy1.area()<<"n"cout<<"園柱體1的體積為: "<<cy1.cubage()<<"nn"cout<<"輸入園柱體2的高 h

30、:"cin>>cy2.h;cout<<"園柱體2的表面積為: "<<cy2.area()<<"n"cout<<"園柱體2的體積為: "<<cy2.cubage()<<"nn"cout<<"園柱體3的底面積為: "<<cy3.cycl:area()<<"n"cout<<"園柱體3的表面積為: "<<cy3.

31、area()<<"n"cout<<"園柱體3的體積為: "<<cy3.cubage()<<"nn"實(shí)例41 部門職員信息/*/ program41.cpp/*#include “iostream.h”class departmentpublic:int num;char design10;class employeepublic:int num;char name10;class empl_dept:public department,public employeepublic:void

32、 input()cout<<”依次輸入職員編號(hào)、姓名、部門編號(hào)、部門名稱:n”;cin>>employee:num;cin>>name;cin>>department:num;cin>>design;void print() cout<<”職員編號(hào):”<<employee:num<<” 姓名:”<<name; cout<<” 部門編號(hào):”<<department:num<<” 部門名稱”<<design;void main()empl_de

33、pt z;z.input();z.print();第四章實(shí)例48 窗口輸出文字與圖形 (P152153)void CProgram48View:OnDraw(CDC *pDC)CProgram48Doc *pDoc=GetDocument( );ASSERT_VALID(pDoc);/TODO: add code for native data here/以下為新增的代碼CRect r;GetClientRect(&r);int x=0, y=0;pDC->TextOut(x,y, “在窗口上畫一個(gè)圓形”);x=r.right/2;y=r.bottom/2;pDC->Ell

34、ipse(x-100,y-100,x+100,y+100);實(shí)例49 應(yīng)用程序的創(chuàng)建與撤銷 (P162163)int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)/添加下面這一行語句,顯示對(duì)話框MessageBox(“準(zhǔn)備建立應(yīng)用程序”, “創(chuàng)建程序”,MB_OK|MB_ICONEXCLAMATION);If (CFrameWnd:OnCreate(lpCreateStruct)= -1) return 1;void CMainFrame:OnClose( )/TODO:Add your message handler code here

35、 and/or call defaultMessageBox(“準(zhǔn)備銷毀程序窗口”, “撤銷窗口”,MB_OK|MB_ICONEXCLAMATION);CFrameWnd:OnClose( );實(shí)例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 code

36、 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_V

37、ALID(pDoc);/TODO: add draw code for native data hereCRect r;GetClientRect(&r);pDC->DrawText(pDoc->Text,r,DT_WORDBREAK|DT_CENTER);實(shí)例51 區(qū)域探測 (P168171)void CProgram51View:OnLButtonUp(UINT nFlags,CPoint point)/TODO: Add your message handler code here and/or call defaultCRect r;GetClientRect(&a

38、mp;r);char *s;if (point.x>r.right/2) if (point.y>r.bottom/2)s=”你現(xiàn)在位于第四區(qū)”;elses=”你現(xiàn)在位于第一區(qū)”;else if (point.y>r.bottom/2)s=”你現(xiàn)在位于第三區(qū)”;else s=”你現(xiàn)在位于第二區(qū)”;MessageBox(s,”位置提示”,MB_OK|MB_ICONEXCLAMATION);CView:OnLButtonUp(nFlags,point);void CProgram51View:OnDraw(CDC *pDC) CProgram51Doc *pDoc=GetDocu

39、ment( );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);pDoc->Te

40、xt=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 (Num>0)i=Num%10;c=0;c=c+i;strnum+=(char)c;Num=Num/10;strnum.MakeReverse( );return strnum;實(shí)例5

41、2 滾動(dòng)字幕 (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)/TOD

42、O: 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(“字幕已運(yùn)行%d毫秒”,ms);if (x>r.right-r.

43、left)x=0;pDC->TextOut(x,r.bottom/2,”Visual C+ 6.0 “);pDC->TextOut(x,r.bottom/2+20,s);實(shí)例53園的移動(dòng)軌跡 (P175179)void CProgram53View:OnDraw(CDC *pDC)CProgram53Doc*pDoc=GetDocument();ASSERT VALID(pDoc);/TODO: add draw code for native data hereDrawEps();void CPprogram53View:OnInitialUpdate()CScrollView:

44、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,y+50);voi

45、d CProgram53View:OnHScroll(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 c

46、ode here and/or call defaultDrawEps();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

47、;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;case VK_NU

48、MPAD9: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);實(shí)例54 應(yīng)用程序的生存周期 (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消息! “,”

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論