C++經(jīng)典代碼大全_第1頁
C++經(jīng)典代碼大全_第2頁
C++經(jīng)典代碼大全_第3頁
C++經(jīng)典代碼大全_第4頁
C++經(jīng)典代碼大全_第5頁
已閱讀5頁,還剩45頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、根據(jù)半徑計算圓的周長和面積#include<iostream.h>constfloatPI=3.1416;/聲明常量(只讀變量)PI為3.1416floatfCir_L(float);聲明自定義函數(shù)fCir_L()的原型floatfCir_S(float);聲明自定義函數(shù)fCir_S()的原型以下是main()函數(shù)main()floatr,l,s;/聲明3個變量cout<<"R="顯示字符串cin>>r;/鍵盤輸入l=fCir_L(r);計算圓的周長,賦值給變量ls=fCir_S(r);計算圓的面積,賦值給變量scout<<&

2、quot;l="<<l;顯示計算結(jié)果cout<<"ns="<<s;定義計算圓的周長的函數(shù)fCir_L()floatfCir_L(floatx)floatz=-1.0;聲明局部變量if(x>=0.0)如果參數(shù)大于0,則計算圓的周長z=2*PI*x;return(z);/返回函數(shù)值定義計算圓的面積的函數(shù)fCir_S()floatfCir_S(floatx)floatz=-1.0;聲明局部變量if(x>=0.0)如果參數(shù)大于0,則計算圓的面積z=PI*x*x;return(z);返回函數(shù)值/*Program:P1-2.CP

3、PWrittenby:HapDatewritten:02:11:10*/#include<iostream.h>voidmain(void)doubles1,s2,s3;s1=1.5;/*對變量s1賦值*/cout<<"s1="<<s1<<endl;/*對變量s2賦值*/s2=2.5;cout<<"s2="<<s2<<endl;s3=/*對變量s3賦值*/3.5;cout<<"s3="<<s3<<endl;cout&

4、lt;<"s1+s2+s3="<<s1+s2+s3<<endl;計算并顯示計算并顯示cout<<"s1+s2+s3="<<s1+s2+s3<<endl;#include<iostream.h>main()doubler=1.0;cout<<"r="<<r<<endl;doublel;l=2*3.1416*r;cout<<"l="<<l<<endl;doubles=3.

5、1416*r*r;cout<<"s="<<s<<endl;計算圓的周長,顯示圓的周長計算圓的面積,顯示圓的面積賦值給變量賦值給變量lscout<<"R="cin>>r;l=2*3.1416*r;cout<<"l="<<l<<endl;s=3.1416*r*r;cout<<"s="<<s<<endl;顯示提示輸入的信息/鍵盤輸入計算圓的周長,賦值給變量顯示圓的周長顯示圓的面積l#incl

6、ude<iostream.h>包含iostream.h頭文件voidmain()/輸出字符常量、變量和字符串charc1='A'cout<<'W;cout<<c1<<endl;cout<<"Thisisatest."<<endl;cout<<""<<endl;輸出整型常量、變量和表達(dá)式intn=100;cout<<10;cout<<n;cout<<2*n<<endl;/輸出整型表達(dá)式cou

7、t<<""<<endl;輸出浮點型常量、變量和表達(dá)式doublepi=3.1415926,r=10.0,s=pi*r*r;cout<<pi<<endl;cout<<r;cout<<s;cout<<2*r*pi<<endl;/輸出浮點型表達(dá)式cout<<""<<endl;一個cout可以輸出多項數(shù)據(jù)cout<<'W<<""<<c1<<endl;cout<&l

8、t;"Thisisatest."<<endl;cout<<"pi="<<pi<<"r="<<r<<"s="<<s<<endl;#include<iostream.h>包含iostream.h頭文件main()輸入輸出字符charc;cin>>c;cout<<"c="<<c<<endl;輸入輸出整型數(shù)據(jù)intn;cin>>n;cou

9、t<<"n="<<n<<endl;輸入輸出浮點型數(shù)據(jù)doublex;cin>>x;cout<<"x="<<x<<endl;/B入提示cout<<"n="cin>>n;cout<<"n="<<n<<endl;多項輸入cout<<"c n x"<<endl;cin>>c>>n>>x;cout<

10、<"c="<<c<<" n="<<n<<" x="<<x<<endl;)#include <iostream.h>包含 iostream.h 頭文件main()聲明整型變量int a,b;從鍵盤上為整型變量賦值cout<<"a="cin>>a;cout<<"b="cin>>b;整型數(shù)的算術(shù)運算cout<<a<<"+"

11、<<b<<"="<<a+b<<endl;cout<<a<<"-"<<b<<"="<<a-b<<endl;cout<<a<<"*"<<b<<"="<<a*b<<endl;cout<<a<<"/"<<b<<"="<&l

12、t;a/b<<endl;cout<<a<<"%"<<b<<"="<<a%b<<endl;測試溢出short n=32767,m;/n 取 short 類型的最大值cout<<"n="<<n<<endl;m=n+1; 引起溢出cout<<"n+1="<<m<<endl;)#include <iostream.h>包含 iostream.h 頭文件mai

13、n()聲明變量,并初始化int a=010,b=10,c=0X10;以十進(jìn)制形式顯示數(shù)據(jù)cout<<"DEC:"cout<<" a="<<a;cout<<" b="<<b;cout<<" c="<<c<<endl;以八進(jìn)制形式顯示數(shù)據(jù)cout<<"OCT:"cout<<oct;指定八進(jìn)制輸出cout<<" a="<<a;cout<

14、;<" b="<<b;cout<<" c="<<c<<endl;以十六進(jìn)制形式顯示數(shù)據(jù)cout<<"HEX:"cout<<hex;指定十六進(jìn)制輸出cout<<" a="<<a;cout<<" b="<<b;cout<<"c="<<c<<endl;/八、十和十六進(jìn)制數(shù)混合運算并輸出cout<<"a

15、+b+c="cout<<dec;恢復(fù)十進(jìn)制輸出cout<<a+b+c<<endl;/測試八、十和十六進(jìn)制輸入cout<<"DEC:a="cin>>a;cout<<"OCT:b="cin>>b;cout<<"HEX:a="cin>>c;cout<<"DEC:"<<dec<<endl;指定十進(jìn)制輸出cout<<"a="<<a

16、<<endl;cout<<"b="<<b<<endl;cout<<"c="<<c<<endl;)#include<iostream.h>包含iostream.h頭文件#include<iomanip.h>/iomanip.h頭文件包含setprecision()的main()/float型變量的聲明、輸入、計算和輸出floatfx,fy;cout<<"fx="cin>>fx;cout<<&qu

17、ot;fy="cin>>fy;cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl;cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl;cout<<fx<<"*"<<fy<<"="<<fx*fy<<end

18、l;cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl;cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;Error!/double型變量的聲明、輸入、計算和輸出floatdx,dy;cout<<"dx="cin>>dx;cout<<"dy=&

19、quot;cin>>dy;cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl;cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl;cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;cout&

20、lt;<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl;cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl;Error!/測tfloat和double類型數(shù)據(jù)的有效位fx=10.0;fy=6.0;floatfz=fx/fy;dx=10.0;dy=6.0;doubledz=dx/dy;cout<<"fz=

21、"cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl;cout<<"dz="cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;/float型溢出floatx=3.5e14;cout&l

22、t;<"x="<<x<<endl;cout<<"x*x="<<x*x<<endl;/布爾變量超界處理flag1=100;cout<<"flag1="<<flag1<<endl;flag2=-100;cout<<"flag2="<<flag2<<endl;)#include <iostream.h>const double PI=3.1416; 聲明常量(const 變

23、量)PI 為 3.1416 main() (聲明3個變量double r,l,s;輸入圓的半徑cout<<"r="cin>>r;/計算圓的周長l=2*PI*r;cout<<"l="<<l<<endl;/枚舉變量的賦值cout<<"x*x*x="<<x*x*x<<endl;)#include<iostream.h>包含iostream.h頭文件main()(字符類型變量的聲明charc1='A'charc2;字符數(shù)

24、據(jù)的運算及輸出c2=c1+32;cout<<"c1="<<c1<<endl;cout<<"c2="<<c2<<endl;/B出字符及ASCII碼cout<<c1<<":"<<int(c1)<<endl;cout<<c2<<":"<<int(c2)<<endl;cout<<'$'<<":"&l

25、t;<int('$')<<endl;/B入字符cout<<"c1c2"<<endl;cin>>c1>>c2;cout<<"c1="<<c1<<"c2="<<c2<<endl;)#include<iostream.h>包含iostream.h頭文件main()(charc1='a',TAB='t'陣鈴一聲cout<<c1<<end

26、l;使用水平制表符cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;使用雙引號cout<<"Hesaid"Thankyou"."<<endl;使用回車換行cout<<"abcn"<<"def"<<'n')#include<iostream.h>包含iostream.h頭文件main()(/聲明bool變量,并

27、初始化boolflag1=false,flag2=true;/酶出布爾常量和變量cout<<"false:"<<false<<endl;cout<<"true:"<<true<<endl;cout<<"flag1="<<flag1<<endl;cout<<"flag2="<<flag2<<endl;布爾變量的賦值和輸出intx=1;flag1=x>0;存放關(guān)系運算結(jié)果

28、cout<<"flag1="<<flag1<<endl;flag2=flag1;/bool類型變量相互賦值cout<<"flag2="<<flag2<<endl;/計算圓的面積s=PI*r*r;cout<<"s="<<s<<endl;#include<iostream.h>main()(/定義枚舉類型,并指定其枚舉元素的值enumcolorRED=3,YELLOW=6,BLUE=9);聲明枚舉變量a和b,并為枚舉變量

29、a賦初值enumcolora=RED;colorb;/合法,與C語言不同/輸出枚舉常量cout<<"RED="<<RED<<endl;cout<<"YELLOW="<<YELLOW<<endl;cout<<"BLUE="<<BLUE<<endl;枚舉變量的賦值和輸出b=a;a=BLUE;cout<<"a="<<a<<endl;cout<<"b=&quo

30、t;<<b<<endl;/a=100;錯誤!/a=6也錯誤!枚舉變量的關(guān)系運算b=BLUE;運算cout<<"a<b="<<(a<b)<<endl;)聲明常量(const變量)PI為3.1416constdoublePI=3.1416;main()(/聲明3個變量doubler=3,l,s;計算圓的周長l=2*PI*r;cout<<"l="<<l<<endl;計算圓的面積s=PI*r*r;cout<<"s="<

31、<s<<endl;驗證賦值誤差intil,is;il=l;is=s;cout<<"il="<<il<<endl;cout<<"is="<<is<<endl;#include<iostream.h>main()(變量聲明charc;doublex,y;測試自增cout<<"+EandE+:"<<endl;c='B'cout<<"c="<<+c<&l

32、t;endl;/輸出c=Cc='B'cout<<"c="<<c+<<endl;/輸出c=Bx=1.5;y=5+x;加號后的空格不能少cout<<"y="<<y<<endl;/輸出y=7.5x=1.5;y=5+x+;cout<<"y="<<y<<endl;/輸出y=6.5cout<<""<<endl;測試自減cout<<"-EandE-:"

33、<<endl;c='B'cout<<"c="<<-c<<endl;/輸出c=Ac='B'cout<<"c="<<c-<<endl;/輸出c=Bx=1.5;y=5+-x;cout<<"y="<<y<<endl;/輸出y=5.5x=1.5;y=5+x-;cout<<"y="<<y<<endl;/輸出y=6.5#include<i

34、ostream.h>main()(輸出關(guān)系表達(dá)式cout<<a<b<<endl;cout<<(a<b)<<(a>b)<<(a>=b)<<(a=b)<<(a!=b)<<endl;boolflag=2*a<b+10;cout<<"flag="<<flag;#include<iostream.h>main()(floata=3.5,b=2.1,c=0;cout<<"a="<&l

35、t;a<<"b="<<b<<"c="<<c<<endl;/與運算cout<<"a&&b="<<(a&&b)<<endl;/輸出1cout<<"a&&c="<<(a&&c)<<endl;輸出0或運算cout<<"a|b="<<(a|b)<<endl;/輸出1cout&l

36、t;<"a|c="<<(a|c)<<endl;/輸出1/非運算cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;/輸出01關(guān)系運算和邏輯運算boolflag=a>=0&&a<=5;/變量a在0,5區(qū)間內(nèi)cout<<"a=>0&&a<=5="<<flag<<endl;/輸出1算術(shù)運算、關(guān)系運算和邏輯

37、運算cout<<"a+5>2*b+2|a<b+3="<<(a+5>2*b+2|a<b+3)<<endl;/輸出1#include<iostream.h>main()(按位與運算cout<v"24&12="vv(24&12)vvendl;按位異或運算coutvv"24A12="vv(24A12)vvendl;按位或運算cout<v"24|12="vv(24|12)vvendl;按位取反運算cout<<&qu

38、ot;24="<<(24)<<endl;左移位運算cout<<"5<<3="<<(5<<3)<<endl;cout<<"-5<<3="<<(-5<<3)<<endl;右移位運算cout<v"5>>3="vv(5>>3)v<endl;cout<v"-5>>3="vv(-5>>3)vvendl;#inc

39、lude<iostream.h>main()(inta=1,b=1,c=3;/顯示a,b,c的值cout<<"a="<<a<<"b="<<b<<"c="<<c<<endl;inta=3,b=2;/計算顯示(1)b+=a+2*c%5;的結(jié)果b+=a+2*c%5;相當(dāng)于表達(dá)式語句b=b+(a+2*c%5);cout<<"(1)b="<<b<<endl;計算顯示a<<=c-2*b

40、;的結(jié)果a=1,b=1,c=3;a<<=c-2*b;/相當(dāng)于表達(dá)式語句a=a<<(c-2*b);cout<<"(2)a="<<a<<endl;計算顯示a*=b=c=3;的結(jié)果a=1,b=1,c=3;a*=b=c=3;相當(dāng)于語句組c=3;b=c;a=a*b;cout<<"(3)a="<<a<<"b="<<b<<"c="<<c<<endl;計算顯示a+=b+=c;的結(jié)果a=1,

41、b=1,c=3;a+=b+=c;相當(dāng)于語句組b=b+c;a=a+b;cout<<"(4)a="<<a<<"b="<<b<<"c="<<c<<endl;計算顯示(5)a-=b=+c+2;的結(jié)果a=1,b=1,c=3;a-=b=+c+2;相當(dāng)于語句組+c;b=b+c+2;a=a-b;cout<<"(5)a="<<a<<"b="<<b<<"c=&qu

42、ot;<<c<<endl;#include<iostream.h>main()用sizeof計算各類種常量的字節(jié)長度cout<<"sizeof('$')="<<sizeof('$')<<endl;cout<<"sizeof(1)="<<sizeof(1)<<endl;cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl;cout<

43、<"sizeof("Good!")="<<sizeof("Good!")<<endl;用sizeof計算各類型變量的字節(jié)長度inti=100;charc='A'floatx=3.1416;doublep=0.1;cout<<"sizeof(i)="<<sizeof(i)<<endl;cout<<"sizeof(c)="<<sizeof(c)<<endl;cout<<&

44、quot;sizeof(x)="<<sizeof(x)<<endl;cout<<"sizeof(p)="<<sizeof(p)<<endl;用sizeof計算表達(dá)式的字節(jié)長度cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl;用sizeof計算各類型的字節(jié)長度cout<<"sizeof(char)="<<sizeof(char)<<endl;cout&

45、lt;<"sizeof(int)="<<sizeof(int)<<endl;cout<<"sizeof(float)="<<sizeof(float)<<endl;cout<<"sizeof(double)="<<sizeof(double)<<endl;用sizeof計算數(shù)組的字節(jié)長度charstr尸'Thisisatest."inta10;doublexy10;cout<<"sizeof(s

46、tr)="<<sizeof(str)<<endl;cout<<"sizeof(a)="<<sizeof(a)<<endl;cout<<"sizeof(xy)="<<sizeof(xy)<<endl;用sizeof計算自定義類型的長度structstshortnum;floatmath_grade;floatChinese_grade;floatsum_grade;ststudent1;cout<<"sizeof(st)=&quo

47、t;<<sizeof(st)<<endl;cout<<"sizeof(student1)="<<sizeof(student1)<<endl;#include<iostream.h>main()聲明變量語句中使用順序運算intx,y;/計算中使用順序運算x=50;y=(x=x-5,x/5);cout<<"x="<<x<<endl;cout<<"y="<<y<<endl;#include<

48、iostream.h>main()/測試表達(dá)式類型的轉(zhuǎn)換intn=100,m;doublex=3.791,y;cout<<"n*x="<<n*x<<endl;賦值類型轉(zhuǎn)換m=x;y=n;cout<<"m="<<m<<endl;cout<<"y="<<y<<endl;強(qiáng)制類型轉(zhuǎn)換cout<<"int(x)="<<int(x)<<endl;cout<<&quo

49、t;(int)x="<<(int)x<<endl;cout<<"int(1.732+x)="<<int(1.732+x)<<endl;cout<<"(int)1.732+x="<<(int)1.723+x<<endl;cout<<"double(100)="<<double(100)<<endl;#include<iostream.h>main()floata,b,s;cout<

50、;<"ab"<<endl;cin>>a>>b;/利用cin從鍵盤上為變量a,b賦值s=a;if(a<b)s=b;/if語句中只有這一個語句,可省略花括號s=s*s;/變量s中保存a,b中較大的一個數(shù)的平方cout<<"s="<<s;#include<iostream.h>main()(intx,y;cout<<"x="cin>>x;if(x<=0)滿足條件執(zhí)行y=2*x;cout<<"y="

51、<<y;/輸出結(jié)果else/不滿足條件執(zhí)行y=x*x;cout<<"y="<<y;/輸出結(jié)果#include<iostream.h>main()inta,b,c;intsmallest;cout<<"abc"<<endl;cin>>a>>b>>c;if(a<=b)外層條件語句if(a<=c)/內(nèi)層條件語句smallest=a;elsesmallest=c;elseif(b<=c)內(nèi)層條件語句smallest=b;elsesmall

52、est=c;cout<<"Smallest="<<smallest<<endl;#include<iostream.h>main()intscore;從鍵盤上輸入分?jǐn)?shù)cout<<"score="cin>>score;/用帶elseif的條件語句判斷處理if(score<0|score>100)cout<<"Thescoreisoutofrange!"<<endl;elseif(score>=90)cout<<&

53、quot;YourgradeisaA."<<endl;elseif(score>=80)cout<<"YourgradeisaB."<<endl;elseif(score>=70)cout<<"YourgradeisaC."<<endl;elseif(score>=60)cout<<"YourgradeisaD."<<endl;elsecout<<"YourgradeisaE."<<

54、;endl;#include<iostream.h>main()intn;cout<<"n="cin>>n;if(n>=0&&n<=100&&n%2=0)cout<<"n="<<n<<endl;elsecout<<"The"<<n<<"isoutofrange!"<<endl;#include<iostream.h>main()inta,b,

55、Max;輸入數(shù)據(jù)cout<<"a="cin>>a;cout<<"b="cin>>b;找出較大值Max=a>b?a:b;cout<<"Max="<<Max<<endl;#include<iostream.h>main()inta,b;輸入數(shù)據(jù)cout<<"a="cin>>a;cout<<"b="cin>>b;除法判斷if(b!=0&&

56、a%b=0)cout<<b<<"divides"<<a<<endl;cout<<"a/b="<<a/b<<endl;elsecout<<b<<"doesnotdivide"<<a<<endl;#include<iostream.h>main()/x,y為操作數(shù),c為運算符intx,y,z;charc1;cin>>x>>c1>>y;/c1多路選擇語句選擇不同表

57、達(dá)式計算語句switch(cl)case'+':cout«x«"+"«y«"="«x+y«endl;break;case'-':cout«x«"-"«y«"="«x"y«endl;break;case'*':cout«x«"*"«y«"="«x*y&#

58、171;endl;break;case7':cout«x«"/"«y«"="«x/y«endl;break;case'%':cout«x«"%"«y«"="«x%y«endl;break;default:cout«"Wrong!"«endl;當(dāng)不符合上述情況時執(zhí)行本子句)#include<iostream.h>floatx

59、=365.5;聲明全局變量main()intx=1,y=2;doublew=x+y;doublex=1.414,y=1.732,z=3.14;cout«"inner:x="«x«endl;cout«"inner:y="«y«endl;cout«"inner:z="«z«endl;cout«"outer:w="«w«endl;cout«":x="«:x

60、1;endl;訪問重名的全局變量)cout«"outer:x="«x«endl;cout«"outer:y="«y«endl;cout«"outer:w="«w«endl;/cout«"inner:z="«z«endl;無效cout«":x="«:x«endl;訪問重名的全局變量#include<iostream.h>main()/顯示

61、1,2,3.10for(inti=1;i<=10;i+)cout«i«"cout«endl;顯示10,9,8.1for(intj=10;j>=1;j-)cout«j«"cout«endl;顯示1,3,59for(intk=1;k<=10;k=k+2)cout«k«"cout«endl;顯示ABC.Zfor(charc='A'c<='Z'c+)cout«c;cout«endl;/顯示0,0.1,0.21

62、.0for(floatx=0;x<=1.0;x=x+0.1)cout«x«"cout«endl;顯示0,0.1,0.2.1.0for(floatx1=0;x1<=1.0+0.1/2;x1=x1+0.1)cout«x1«"cout«endl;計算s=1+2+3.+100ints=0;for(intn=1;n<=100;n+)s=s+n;cout«"s="«s«endl;#include<iostream.h>main()(計算s=1+2+

63、3.+100ints=0,n=1;while(n<=100)s=s+n;n+;cout<<"s="«s«endl;累加鍵盤輸入的數(shù)據(jù)doublex,sum=0.0;cout«"x="cin»x;while(x!=0)sum+=x;cout«"x="cin»x;)cout«"sum="«sum«endl;)#include<iostream.h>main()(計算s=1+2+3.+100ints=0

64、,n=0;don+;s+=n;while(n<100);cout<<"s="«s«endl;累加鍵盤輸入的數(shù)據(jù)doublex,sum=0.0;docout«"x="cin»x;sum+=x;while(x!=0);cout«"sum="«sum«endl;)main()(計算和打印打印乘法九九表for (int i=1;i<=9;i+) cout<<i;for (int j=1;j<=9;j+)cout<<

65、9;t'<<i<<"*"<<j<<"="<<i*j;cout<<endl; #include<iostream.h> main() int x,sum=0;/定義標(biāo)號L1L1: cout<<"x="cin>>x;if (x=-1)goto L2;無條件轉(zhuǎn)移語句,轉(zhuǎn)到L2語句處elsesum+=x;goto L1;無條件轉(zhuǎn)移語句,轉(zhuǎn)到 L1語句處/定義標(biāo)號L2L2: cout<<"sum="

66、<<sum<<endl;#include<iostream.h> main() /累加鍵盤輸入的數(shù)據(jù)double x,sum=0.0;while(1) cout<<"x="cin>>x;if (x<=0) break;sum+=x;cout<<"sum="<<sum<<endl;#include<iostream.h> main() int i;for (i=1;i<=20;i+)if (i%3=0)/能被3整除的整數(shù),返回進(jìn)行下次循環(huán)

67、continue;cout<<i<<""cout<<endl;#include<iostream.h> main() 聲明數(shù)組和變量int a5,i,sum;doubleavg;/從鍵盤上循環(huán)為數(shù)組賦值for(i=0;i<5;i+)cout<<"a"<<i<<"="cin>>ai;/直接顯示數(shù)組元素cout<<a0<<a1<<a2<<a3<<a4<<endl;/利

68、用for循環(huán)顯示數(shù)組各元素的值for(i=0;i<5;i+)cout<<ai<<""cout<<endl;/計算數(shù)組元素之和,并顯示計算結(jié)果sum=a0+a1+a2+a3+a4;cout<<"sum="<<sum<<endl;/利用循環(huán)計算數(shù)組的累加和for(sum=0,i=0;i<5;i+)sum+=ai;顯示累加和及平均值cout<<"sum="<<sum<<endl;avg=sum/5.0;cout<&l

69、t;"avg="<<avg<<endl;#include<iostream.h>main()inti,max,index,a5;/從鍵盤上為數(shù)組賦值for(i=0;i<=4;i+)cout<<"a"<<i<<"="cin>>ai;/利用循環(huán)遍歷數(shù)組,找出最大值的元素及其下標(biāo)max=a0;for(i=0;i<=4;i+)if(max<ai)max=ai;index=i;cout<<"nMax="<&

70、lt;max<<"index="<<index;#include<iostream.h>#definesize5main()聲明變量inti,j;floatt,asize;從鍵盤上為數(shù)組賦值for(i=0;i<size;i+)(cout<<"a"<<i<<"="cin>>ai;)/對數(shù)組按從小到大順序排序for(i=0;i<size-1;i+)for(j=i+1;j<size;j+)if(ai>aj)(t=ai;ai=aj;aj

71、=t;)顯示排序結(jié)果for(i=0;i<size;i+)cout<<ai<<""cout<<endl;/陶人要查找的數(shù)據(jù)intvalue;intfound;/找到為1,否則為0intlow,high,mid;for(i=1;i<=3;i+)cout<<"value="cin>>value;二分法查找數(shù)組afound=0;low=0;high=size-1;while(low<=high)mid=(high+low)/2;if(amid=value)found=1;break;)

72、if(amid<value)low=mid+1;elsehigh=mid-1;)if(found)cout<<"Thevalufoundat:a"<<mid<<"="<<amid<<endl;elsecout<<"The"<<value<<"isnotfound!"<<endl;)#include<iostream.h>main()聲明變量inti,j;floatt,a5;/從鍵盤上為數(shù)組賦

73、值for(i=0;i<=4;i+)cout<<"a"<<i<<"="cin>>ai;)/對數(shù)組按從大到小順序排序for(i=0;i<=3;i+)for(j=i+1;j<=4;j+)if(ai<=aj)t=ai;ai=aj;aj=t;)顯示排序結(jié)果for(i=0;i<=4;i+)cout<<ai<<"")#include<iostream.h>main()聲明二維數(shù)組及變量inta23,i,j;/從鍵盤上為數(shù)組a賦值for(

74、i=0;i<2;i+)for(j=0;j<3;j+)cout<<"a"<<i<<""<<j<<"="cin>>aij;)顯示數(shù)組afor(i=0;i<2;i+)for(j=0;j<3;j+)cout<<aij<<"")cout<<endl;)找出該數(shù)組的最大元素及其下標(biāo)inth,l,Max=a00;for(i=0;i<2;i+)for(j=0;j<3;j+)if(Max&l

75、t;aij)Max=aij;h=i;l=j;)cout<<"Max:"<<"a"<<h<<""<<l<<"="<<ahl<<endl;#include<iostream.h>main()(聲明字符數(shù)組和變量charstr6;inti;從鍵盤上輸入字符串cout<<"str="cin>>str;cout<<str<<endl;按數(shù)組和下標(biāo)變量兩

76、種方式顯示字符數(shù)組cout<<str<<endl;for(i=0;i<6;i+)cout<<stri;cout<<endl;字符串反向輸出for(i=5;i>=0;i-)cout<<stri;cout<<endl;將字符數(shù)組變成大寫字母后輸出for(i=0;i<=5;i+)stri卜=32;小寫字母轉(zhuǎn)換成大寫字母cout<<str<<endl;顯示字符串#include<iostream.h>main()(聲明變量和指針變量inta,b,c,*ip;指針變量ip指向變量

77、aa=100;ip=&a;使指針變量ip指向變量acout<<"a="<<a<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;指針變量ip指向變量bip=&b;使指針變量ip指向變量bb=200;cout<<"b="<<b<<endl;cout<<"*ip="<&l

78、t;*ip<<endl;cout<<"ip="<<ip<<endl;指針變量ip指向變量cip=&c;使指針變量ip指向變量b*ip=a+b;cout<<"c="<<c<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;#include<iostream.h>main()(聲明數(shù)組、變量和

79、指針變量inta23,i,j;int*ip;/從鍵盤上為數(shù)組a賦值for(i=0;i<2;i+)為數(shù)組a賦值for(j=0;j<3;j+)(cout<<"a"<<i<<""<<j<<"="cin>>aij;/利用下標(biāo)變量顯示數(shù)組afor(i=0;i<2;i+)for(j=0;j<3;j+)cout<<aij<<""cout<<endl;/利用指針變量顯示數(shù)組aip=&a00;f

80、or(i=0;i<2;i+)for(j=0;j<3;j+)cout<<"a"<<i<<""<<j<<"="cout<<ip<<""cout<<*ip<<endl;ip+;#include<iostream.h>main()聲明數(shù)組、變量和指針變量inta=1,2,3,4,5,6;int*ip1,*ip2;/測試指針的賦值運算ip1=a;ip2=ip1;cout<<"

81、;*ip1="<<(*ip1)<<endl;cout<<"*ip2="<<(*ip2)<<endl;/測試指針的自增自減運算和組合運算ip1+;ip2+=4;cout<<"*ip1="<<(*ip1)<<endl;cout<<"*ip2="<<(*ip2)<<endl;/測試指針變量之間的關(guān)系運算intn=ip2>ip1;cout<<"ip2>ip1="

82、;<<n<<endl;cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;指針變量之間的減法n=ip2-ip1;cout<<"ip2-ip1="<<n<<endl;聲明指針數(shù)組char *colors="Red","Blue","Yellow","Green" 指向指針的指針變量 char *pt;/通過指向指針的變量訪問其指向的內(nèi)容 pt=colors;f

83、or (int i=0;i<=3;i+) cout<<"pt="<<pt<<endl; cout<<"*pt="<<*pt<<endl; cout<<"*pt="<<*pt<<endl; pt+; #include<iostream.h> main() /定義結(jié)構(gòu)類型 struct books char title20; char author15; int pages; float price; ;聲明結(jié)構(gòu)變量

溫馨提示

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

最新文檔

評論

0/150

提交評論