![C++經(jīng)典程序代碼_第1頁](http://file4.renrendoc.com/view/4b8aa753d0a0d329a11f71086712aff0/4b8aa753d0a0d329a11f71086712aff01.gif)
![C++經(jīng)典程序代碼_第2頁](http://file4.renrendoc.com/view/4b8aa753d0a0d329a11f71086712aff0/4b8aa753d0a0d329a11f71086712aff02.gif)
![C++經(jīng)典程序代碼_第3頁](http://file4.renrendoc.com/view/4b8aa753d0a0d329a11f71086712aff0/4b8aa753d0a0d329a11f71086712aff03.gif)
![C++經(jīng)典程序代碼_第4頁](http://file4.renrendoc.com/view/4b8aa753d0a0d329a11f71086712aff0/4b8aa753d0a0d329a11f71086712aff04.gif)
![C++經(jīng)典程序代碼_第5頁](http://file4.renrendoc.com/view/4b8aa753d0a0d329a11f71086712aff0/4b8aa753d0a0d329a11f71086712aff05.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
C++經(jīng)典程序代碼
〃根據(jù)半徑計(jì)算圓的周長和面積
#include<iostream.h>
constfloatPI=3.1416;〃聲明常量(只讀變量)PI為3.1416
floatfCir_L(float);〃聲明自定義函數(shù)fCir_L()的原型
floatfCir_S(float);〃聲明自定義函數(shù)fCir_S()的原型
〃以下是main。函數(shù)
main()
(
floatr,l,s;〃聲明3個變量
cout?nr=H;〃顯示字符串
cin?r;〃鍵盤輸入
l=fCir_L(r);〃計(jì)算圓的周長,賦值給變量1
s=fCir_S(r);〃計(jì)算圓的面積,賦值給變量s
cout?nl=H?l;〃顯示計(jì)算結(jié)果
cout?H\ns="?s;
〃定義計(jì)算圓的周長的函數(shù)fCir_L()
floatfCir_L(floatx)
(
floatz=-1.0;〃聲明局部變量
if(x>=0.0)〃如果參數(shù)大于0,則計(jì)算圓的周長
z=2*0I*x;
return(z);〃返回函數(shù)值
〃定義計(jì)算圓的面積的函數(shù)fCir_S()
floatfCir_S(floatx)
(
floatz=-1.0;〃聲明局部變量
if(x>=0.0)〃如果參數(shù)大于0,則計(jì)算圓的面積
Z=PI*X*X;
return(z);〃返回函數(shù)值
)
/*Program:P1-2.CPP
Writtenby:Hap
Datewritten:02:11:10
*/
#include<iostream.h>
voidmain(void)
(
doublesl,s2,s3;
sl=1.5;/*對變量si賦值*/
cout?Msl=H?s1?endl;
/*對變量s2賦值*/s2=2.5;
cout?Hs2=H?s2?endl;
s3=/*對變量s3賦值*/3.5;
cout?,,s3="?s3?endl;
cout?*,sl4-s2+s3=*,?sl4-s2+s3?endl;〃計(jì)算并顯示
〃計(jì)算并顯示cout?"sl+s2+s3="?sl+s2+s3?endl;
#include<iostream.h>
main()
(
doubler=1.0;
cout?"r="?r?endl;
double1;
1=2*3.1416*r;〃計(jì)算圓的周長,賦值給變量1
cout?"l=H?l?endl;〃顯示圓的周長
doubles=3.1416*r*r;〃計(jì)算圓的面積,賦值給變量s
cout?,,s=H?s?endl;〃顯示圓的面積
cout?nr=,*;〃顯示提示輸入的信息
cin?r;〃鍵盤輸入
1=2*3,1416*r;〃計(jì)算圓的周長,賦值給變量1
cout?nl=H?l?endl;〃顯示圓的周長
s=3.1416*r*r;
cout?"s="?s?endl;〃顯示圓的面積
}
#include<iostream.h>〃包含iostream.h頭文件
voidmain()
〃輸出字符常量、變量和字符串
charcl=W;
coutvv'W';
cout?cl?endl;
cout?"Thisisatest.H?endl;
cout?n"?endl;
〃輸出整型常量、變量和表達(dá)式
intn=100;
cout?10;
cout?n;
cout?2*n?endl;〃輸出整型表達(dá)式
cout?°"?endl;
〃輸出浮點(diǎn)型常量、變量和表達(dá)式
doublepi=3.1415926,r=10.0,s=pi*r*r;
cout?pi?endl;
cout?r;
cout?s;
cout?2*r*pi?endl;〃輸出浮點(diǎn)型表達(dá)式
cout?M"?endl;
〃一個cout可以輸出多項(xiàng)數(shù)據(jù)
cout?,W,?n"?cl?endl;
cout?"Thisisatest."?endl;
cout?npi=H?pi?Hr=n?r?ns=H?s?endl;
#include<iostream.h>〃包含iostream.h頭文件
main()
(〃輸入輸出字符
charc;
cin?c;
cout?"c="?c?endl;
〃輸入輸出整型數(shù)據(jù)
intn;
cin?n;
cout?nn=K?n?endl;
〃輸入輸出浮點(diǎn)型數(shù)據(jù)
doublex;
cin?x;
cout?Hx=H?x?endl;
〃輸入提示
cout?Mn=";
cin?n;
cout<<"n="?n?endl;
〃多項(xiàng)輸入
cout?Hcnx"?endl;
cin?c?n?x;
cout?nc=,,?c<<Mn=H?n?"x=H?x?endl;
#include<iostream.h>〃包含iostream.h頭文件
main()
,〃聲明整型變量
inta,b;
〃從鍵盤上為整型變量賦值
cout?na=°;
cin?a;
cout?nb=";
cin?b;
〃整型數(shù)的算術(shù)運(yùn)算
cout?a?"4-n?b?n="?a+b?endl;
cout?a?H-"?b?H=n?a-b?endl;
cout?a?H*,,?b?,,="?a*b?endl;
cout?a?'7H?b?H=H?a/b?endl;
cout?a?"%"?b?H=H?a%b?endl;
〃測試溢出
shortn=32767,m;//n取short類型的最大值
cout<<nn="?n?endl;
m=n+l;〃引起溢出
cout?"n+1=H?m?endl;
#include<iostream.h>〃包含iostream.h頭文件
main()
1〃聲明變量,并初始化
inta=010,b=10,c=0X10;
〃以十進(jìn)制形式顯示數(shù)據(jù)
cout?HDEC:";
cout?na=H?a;
cout?"b="?b;
cout?nc=H?c?endl;
〃以八進(jìn)制形式顯示數(shù)據(jù)
cout?"OCT:";
cout?oct;〃指定八進(jìn)制輸出
cout?"a="?a;
cout?"b="?b;
cout?nc=n?c?endl;
〃以十六進(jìn)制形式顯示數(shù)據(jù)
cout?HHEX:n;
cout?hex;〃指定十六進(jìn)制輸出
cout?Ma=n?a;
cout?nb="?b;
cout?"c=H?c?endl;
〃八、十和十六進(jìn)制數(shù)混合運(yùn)算并輸出
cout?"a+b+c=";
cout?dec;〃恢復(fù)十進(jìn)制輸出
cout?a+b+c?endl;
〃測試八、十和十六進(jìn)制輸入
cout?nDEC:a=H;cin?a;
cout?"OCT:b=n;cin?b;
cout?nHEX:a=M;cin?c;
cout?HDEC:K?dec?endl;〃指定十進(jìn)制輸出
cout?Ha="?a?endl;
cout?Hb=M?b?endl;
cout?,,c=,,?c?endl;
#include<iostream.h>〃包含沁stream.h頭文件
#include<iomanip.h>//iomanip.h頭文件包含setprecision。的定義
main()
(〃float型變量的聲明、輸入、計(jì)算和輸出
floatfx,fy;
cout?nfx=,f;
cin?fx;
cout?"fy=n;
cin?fy;
cout?fx?H4-H?fy?H=H?fx4-fy?endl;
cout?fx?"""<<fy?n=n?fx-fy?endl;
cout?fx?K*,,?fy?,,=,,?fx*fy?endl;
cout?fx?n/n?fy?H="?fx/fy?endl?endl;
//cout?fx?n%n?fy?"=n?fx%fy?endl;Error!
“double型變量的聲明、輸入、計(jì)算和輸出
floatdx,dy;
cout?Hdx=";
cin?dx;
cout?Hdy=M;
cin?dy;
cout?dx?n+n?dy?n="?dx4-dy?endl;
cout?dx?,,-n?dy?,,=,,?dx-dy?endl;
cout?dx?,,*"?dy?H=,,?dx*dy?endl;
cout?dx?,7H?dy?*,=',?dx/dy?endl?endl;
//cout?fx?,,%M?fy?',=',?fx%fy?endl;Error!
〃測試float和double類型數(shù)據(jù)的有效位
fx=10.0;fy=6.0;
floatfz=fx/fy;
dx=10.0;dy=6.0;
doubledz=dx/dy;
cout?nfz=H;
cout?setprecision(20)?fx?'7"?fy?n="?fz?endl;
cout?"dz=";
cout?setprecision(20)?dx?,7"?dy?,,=H?dz?endl?endl;;
〃float型溢出
floatx=3.5el4;
cout?"x="?x?endl;
cout?Hx*x=n?x*x?endl;
cout<v"x*x*x="<vx*x*x<<endl;
#include<iostream.h>〃包含iostream.h頭文件
main()
〃字符類型變量的聲明
charcl='A';
charc2;
〃字符數(shù)據(jù)的運(yùn)算及輸出
c2=cl+32;
cout?Mc1=,f?c1?endl;
cout?nc2=n?c2?endl;
〃輸出字符及ASCH碼
cout?cl?n:H?int(cl)?endl;
cout?c2?":"?int(c2)?endl;
cout?,$,?":',?int(,$,)?endl;
〃輸入字符
cout?nclc2H?endl;
cin?cl?c2;
cout?"cl=,,?cl?nc2="?c2?endl;
#include<iostream.h>〃包含iostream.h頭文件
main()
(
charcl=>\a\TAB=,\t';
〃陣鈴一聲
cout?cl?endl;
〃使用水平制表符
cout?1<<TAB?2?TAB?3?TAB?4?endl;
〃使用雙引號
cout?nHesaid\HThankyou\n."?endl;
〃使用回車換行
cout?"abc\n"?"def'?'\n';
)
#include<iostream.h>〃包含iostream.h頭文件
main()
(〃聲明bool變量,并初始化
boolflag1=false,flag2=true;
〃輸出布爾常量和變量
cout?Hfalse:H?false?endl;
cout?ntrue:M?true?endl;
cout?"flagl=n?flagl?endl;
cout?nflag2=n?flag2?endl;
〃布爾變量的賦值和輸出
intx=l;
flagl=x>0;〃存放關(guān)系運(yùn)算結(jié)果
cout?"flagl="?flagl?endl;
flag2=flagl;//bool類型變量相互賦值
cout?,,flag2=,,?flag2?endl;
〃布爾變量超界處理
flagl=100;
cout?"flagl="?flagl?endl;
flag2=-100;
cout?Hflag2="?flag2?endl;
)「
#include<iostream.h>
constdoublePI=3.1416;〃聲明常量(const變量)PI為3.1416
main()
(〃聲明3個變量
doubler,I,s;
〃輸入圓的半徑
cout?"r=n;
cin?r;
〃計(jì)算圓的周長
l=2*PI*r;
cout?"1=H?l?endl;
〃計(jì)算圓的面積
s=PI*r*r;
cout?"s=',?s?endl;
#include<iostream.h>
main()
{〃定義枚舉類型,并指定其枚舉元素的值
enumcolor{
RED=3,
YELL0W=6,
BLUE=9
};
〃聲明枚舉變量a和b,并為枚舉變量a賦初值
enumcolora=RED;
colorb;〃合法,與C語言不同
//輸出枚舉常量
cout?"RED="?RED?endl;
cout?"YELLOW="?YELLOW?endl;
cout?"BLUE="?BLUE?endl;
〃枚舉變量的賦值和輸出
b=a;
a=BLUE;
cout?Ha="?a?endl;
cout?Hb="?b?endl;
//a=100;錯誤!
//a=6也錯誤!
〃枚舉變量的關(guān)系運(yùn)算
b=BLUE;//枚舉變量的賦值運(yùn)算
cout?"a<b="?(a<b)?endl;
}
#include<iostream.h>
constdoublePI=3.1416;〃聲明常量(const變量)PI為3.1416
main()
(
〃聲明3個變量
doubler=3,l,s;
〃計(jì)算圓的周長
l=2*PI*r;
cout?nl=H?l?endl;
〃計(jì)算圓的面積
s=PI*r*r;
cout?"s=H?s?endl;
〃驗(yàn)證賦值誤差
intil,is;
il=l;
is=s;
cout?Hil="?il?endl;
cout?His=H?is?endl;
)
#include<iostream.h>
main()
{
〃變量聲明
charc;
doublex,y;
〃測試自增
cout?H++EandE++:H?endl;
c='B';
cout?nc=n?+4-c?endl;〃輸出c=C
c='B,;
cout?"c="?c++?endl;〃輸出c=B
x=15*
y=5+++x;〃加號后的空格不能少
cout?ny=n?y?endl;〃輸出y=7.5
x=1.5;
y=5+x++;
cout?ny=M?y?endl;〃輸出y=6.5
cout?Mn?endl;
〃測試自減
cout?n—EandE—:M?endl;
c=B;
cout?nc=H?-c?endl;〃輸出c=A
c='B';
cout?,,c=',?c—?endl;〃輸出c=B
x=1.5;
y=5+—x;
cout?"y="?y?endl;〃輸出y=5.5
x=1.5;
y=5+x—;
cout?ny="?y?endl;〃輸出y=6.5
#include<iostream.h>
main()
(
inta=3,b=2;
〃輸出關(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?nflag="?flag;
}.
#include<iostream.h>
main()
(
floata=3.5,b=2.1,c=0;
cout?"a=M?a?"b="?b?Hc="?c?endl;
〃與運(yùn)算
cout<<"a&&b="<<(a&&b)<<endl;〃輸出1
cout<<"a&&c="<<(a&&c)<<endl;〃輸出0
〃或運(yùn)算
cout<<"allb="<<(allb)<<endl;〃輸出1
cout<<"allc="<<(allc)<<endl;〃輸出1
〃非運(yùn)算
cout<<"!a="<<!a<<endk<"!c="<<!c<<endl;〃輸出01
〃關(guān)系運(yùn)算和邏輯運(yùn)算
boolflag=a>=0&&a<=5;〃變量a在[0,5]區(qū)間內(nèi)
cout?"a=>0&&a<=5="?flag<<endl;〃輸出1
〃算術(shù)運(yùn)算、關(guān)系運(yùn)算和邏輯運(yùn)算
cout<<"a+5>2*b+2lla<b+3="<<(a+5>2*b+2lla<b+3)<<endl;〃輸出1
}
#include<iostream.h>
main()
'〃按位與運(yùn)算
cout?"24&12="?(24&12)?endl;
〃按位異或運(yùn)算
cout?"24A12="?(24A12)?endl;
〃按位或運(yùn)算
cout?"24ll2="?(24ll2)?endl;
〃按位取反運(yùn)算
cout?"~24="?(~24)?endl;
〃左移位運(yùn)算
cout?"5?3="?(5?3)?endl;
cout?"-5?3="?(-5?3)?endl;
〃右移位運(yùn)算
cout<<H5?3=n?(5?3)?endl;
cout?n-5?3="?(-5?3)?endl;
#include<iostream.h>
main()
inta=l,b=l,c=3;
〃顯示a,b,c的值
cout?,,a=,,?a?Hb=n?b?"c=H?c?endl;
〃計(jì)算顯示(l)b+=a+2*c%5;的結(jié)果
b+=a+2*c%5;〃相當(dāng)于表達(dá)式語句b=b+(a+2*c%5);
cout?M(l)b=n?b?endl;
〃計(jì)算顯示(2)avv=c-2*b;的結(jié)果
a=1,b=1,c=3;
a?=c-2*b;//相當(dāng)于表達(dá)式語句a=a?(c-2*b);
cout?n(2)a=H?a?endl;
〃計(jì)算顯示(3)a*=b=c=3;的結(jié)果
a=l,b=l,c=3;
a*=b=c=3;〃相當(dāng)于語句組c=3;b=c;a=a*b;
cout?"(3)a=n?a?nb=H?b?"c=H?c?endl;
〃計(jì)算顯示(4)a+=b+=c;的結(jié)果
a=l,b=l,c=3;
a+=b+=c;〃相當(dāng)于語句組b=b+c;a=a+b;
cout?M(4)a=n?a?nb=H?b?"c=n?c?endl;
〃計(jì)算顯示(5)a-=b=++c+2;的結(jié)果
a=l,b=l,c=3;
a-=b=++c+2;〃相當(dāng)于語句組++c;b=b+c+2;a=a-b;
cout?M(5)a=,,?a?nb=n?b?"c=n?c?endl;
)
#include<iostream.h>
main()
(
〃用sizeof計(jì)算各類種常量的字節(jié)長度
cout?,,sizeof(,$,)=,,?sizeof(,$,)?endl;
cout?Hsizeof(1)=H?sizeof(1)?endl;
cout?nsizeof(1.5)=n?sizeof(l.5)?endl;
cout?,,sizeof(\,'Good!\H)=',?sizeof(nGood!")?endl;
〃用sizeof計(jì)算各類型變量的字節(jié)長度
inti=100;
charc='A';
floatx=3.1416;
doublep=0.1;
cout?"sizeof(i)=n?sizeof(i)?endl;
cout?nsizeof(c)=H?sizeof(c)?endl;
cout?nsizeof(x)=n?sizeof(x)?endl;
cout?"sizeof(p)=n?sizeof(p)?endl;
〃用sizeof計(jì)算表達(dá)式的字節(jié)長度
cout?"sizeof(x+1.732)="?sizeof(x+l.732)?endl;
〃用sizeof計(jì)算各類型的字節(jié)長度
cout?"sizeof(char)=H?sizeof(char)?endl;
cout?Hsizeof(int)=H?sizeof(int)?endl;
cout?Hsizeof(float)=H?sizeof(float)?endl;
cout?"sizeof(double)=M?sizeof(double)?endl;
〃用sizeof計(jì)算數(shù)組的字節(jié)長度
charstr[]="Thisisatest.";
inta[10J;
doublexy[10j;
cout?"sizeof(str)="?sizeof(str)?endl;
cout?Hsizeof(a)=n?sizeof(a)?endl;
cout?nsizeof(xy)=H?sizeof(xy)?endl;
〃用sizeof計(jì)算自定義類型的長度
structst{
shortnum;
floatmath_grade;
floatChinese_grade;
floatsum_grade;
);
ststudent1;
cout?"sizeof(st)=H?sizeofi(st)?endl;
cout?nsizeof(studentl)=H?sizeof(studentl)?endl;
)
#include<iostream.h>
main()
(〃聲明變量語句中使用順序運(yùn)算
intx,y;
〃計(jì)算中使用順序運(yùn)算
x=50;
y=(x=x-5,x/5);
cout?"x="?x?endl;
cout?,,y=,,?y?endl;
)
#include<iostream.h>
main()
(
〃測試表達(dá)式類型的轉(zhuǎn)換
intn=100,m;
doublex=3.791,y;
cout?Hn*x=n?n*x?endl;
〃賦值類型轉(zhuǎn)換
m=x;
y=n;
cout?nm=H?m?endl;
cout?ny=n<<y?endl;
〃強(qiáng)制類型轉(zhuǎn)換
cout?nint(x)=H?int(x)?endl;
cout?H(int)x=H?(int)x?endl;
cout?"int(1.732+x)="?int(1.732+x)?endl;
cout?n(int)1.732+x=n?(int)1.723+x?endl;
cout?ndouble(l00)=n?double(l00)?endl;
}.
#include<iostream.h>
main()
{
floata,b,s;
cout?nab"?endl;
cin?a?b;〃利用cin從鍵盤上為變量a,b賦值
s=a;
if(a<b){
s=b;〃if語句中只有這一個語句,可省略花括號
)
s=s*s;〃變量s中保存a,b中較大的一個數(shù)的平方
cout?Ms=n?s;
#include<iostream.h>
main()
(
intx,y;
cout?Mx=";
cin?x;
if(x<=0){〃滿足條件執(zhí)行
y=2*x;
cout?ny=n?y;〃輸出結(jié)果
)
else{〃不滿足條件執(zhí)行
y=x*x;
cout?Hy=H?y;〃輸出結(jié)果
)
)
#include<iostream.h>
main()
inta,b,c;
intsmallest;
cout?nabc',?endl;
cin?a?b?c;
if(a<=b)〃外層條件語句
if(a<=c)〃內(nèi)層條件語句
smallest=a;
else
smallest=c;
)
else
{「
if(b<=c)〃內(nèi)層條件語句
smallest=b;
else
smallest=c;
)
cout?,,Smallest=',?smallest?endl;
#include<iostream.h>
main()
(
intscore;
〃從鍵盤上輸入分?jǐn)?shù)
cout?Mscore=";
cin?score;
〃用帶elseif的條件語句判斷處理
if(score<0IIscore>100)
cout?"Thescoreisoutofrange!H?endl;
).
elseif(score>=90)
cout?HYourgradeisaA.M?endl;
elseif(score>=80)
cout?"YourgradeisaB.M?endl;
elseif(score>=70)
cout?HYourgradeisaC."?endl;
elseif(score>=60)
cout?HYourgradeisaD."?endl;
else
cout?HYourgradeisaE.H?endl;
)
#include<iostream.h>
main()
(
intn;
cout?nn=";
cin?n;
if(n>=0&&n<=100&&n%2==0)
cout?"n=H?n?endl;
else
cout?HThe"?n?"isoutofrange!H?endl;
#include<iostream.h>
main()
(
inta,b,Max;
〃輸入數(shù)據(jù)
cout?"a=n;
cin?a;
cout?nb=";
cin?b;
〃找出較大值
Max=a>b?a:b;
cout?HMax=M?Max?endl;
#include<iostream.h>
main()
(
inta,b;
〃輸入數(shù)據(jù)
cout?"a=n;
cin?a;
cout?Hb=";
cin?b;
〃除法判斷
if(b!=0&&a%b==0){
cout?b?Hdividesn?a?endl;
cout?Ha/b=H?a/b?endl;
)
else
cout?b?HdoesnotdivideM?a?endl;
#include<iostream.h>
main()
(
//x,y為操作數(shù),c為運(yùn)算符
intx,y,z;
charcl;
cin?x?cl?y;//cl
〃多路選擇語句選擇不同表達(dá)式計(jì)算語句
switch(cl){
case,4-,:cout?x?,,+',?y?',=',?x+y?endl;
break;
case,-,:cout?x?',-',?y?,,=n?x-y?endl;
break;
case,*,:cout?x?',*,,?y?H=',?x*y?endl;
break;
case7':cout?x?,7"?y?,,=H?x/y?endl;
break;
case,%,:cout?x?"%"?y?H=H?x%y?endl;
break.
default:cout?HWrong!”vvendl;〃當(dāng)不符合上述情況Fl寸執(zhí)行本子句
#include<iostream.h>
floatx=365.5;〃聲明全局變量
main(){
intx=l,y=2;
doublew=x+y;
(
doublex=1.414,y=1.732,z=3.14;
cout?Hinner:x="?x?endl;
cout?Hinner:y=H?y?endl;
cout?Hinner:z=n?z?endl;
cout?nouter:w=H?w?endl;
cout?°::x=H?::x?endl;〃訪問重名的全局變量
)
cout?"outer:x=n?x?endl;
cout?,,outer:y=,,?y?endl;
cout?nouter:w=H?w?endl;
〃coutvv"inner:z="vvz<vendl;無效
cout?H::x=:x?endl;〃訪問重名的全局變量
}.
#include<iostream.h>
main(){
〃顯示1,2,3...10
for(inti=l;i<=10;i++)
cout?i?"n;
cout?endl;
〃顯示10,9,8…1
for(intj=10;j>=1;j—)
cout?j?Mn;
cout?endl;
〃顯示1,3,5...9
for(intk=1;k<=10;k=k+2)
cout?k?H
cout?endl;
〃顯示ABC...Z
fbr(charc=,A,;c<=,Z,;c++)
cout?c;
cout?endl;
〃顯示0,0.1,0.2...1.0
for(floatx=0;x<=1.0;x=x+0.1)
cout?x?H
cout?endl;
〃顯示001,0.2…1.0
for(floatx1=0;x1<=1.0+0.l/2;xl=x1+0.1)
cout?xl?HM;
cout?endl;
〃計(jì)算s=l+2+3...+100
ints=0;
for(intn=1;n<=100;n++)
s=s+n;
cout?ns=M?s?endl;
).
#include<iostream.h>
main()
'〃計(jì)算s=l+2+3…+100
ints=0,n=l;
while(n<=100){
s=s+n;
n++;
)
cout?ns=H?s?endl;
〃累加鍵盤輸入的數(shù)據(jù)
doublex,sum=0.0;
cout?"x=n;
cin?x;
while(x!=0){
sum+=x;
cout?"x=n;
cin?x;
)
cout?nsum=n?sum?endl;
)
#include<iostream.h>
main()
{〃計(jì)算s=1+2+3…+100
ints=0,n=0;
do{
n++;
s+=n;
}while(n<100);
cout?"s="?s?endl;
〃累加鍵盤輸入的數(shù)據(jù)
doublex,sum=0.0;
do{
cout?Mx=n;
cin?x;
sum+=x;
}while(x!=0);
cout?nsum=H?sum?endl;
)
#include<iostream.h>
main()
(
〃計(jì)算和打印打印乘法九九表
fbr(inti=l;i<=9;i++){
cout?i;
for(intj=l;j<=9;j++)
cout?endl;
#include<iostream.h>
main()
(
intx,sum=0;
〃定義標(biāo)號Ll
LI:cout?nx=n;
cin?x;
if(x==-l)
gotoL2;〃無條件轉(zhuǎn)移語句,轉(zhuǎn)至UL2語句處
else
sum+=x;
gotoLl;〃無條件轉(zhuǎn)移語句,轉(zhuǎn)到L1語句處
〃定義標(biāo)號L2
L2:cout?nsum=n?sum?endl;
#include<iostream.h>
main()
(〃累加鍵盤輸入的數(shù)據(jù)
doublex,sum=0.0;
while(l){
cout?',x=n;
cin?x;
if(x<=0)break;
sum+=x;
)
cout?"sum=n?sum?endl;
#include<iostream.h>
main()
(
inti;
for(i=l;i<=20;i++)
if(i%3==0)〃能被3整除的整數(shù),返回進(jìn)行下次循環(huán)
continue;
cout?i?Hn;
)
cout?endl;
}
#include<iostream.h>
main()
(〃聲明數(shù)組和變量
inta[5],i,sum;
doubleavg;
〃從鍵盤上循環(huán)為數(shù)組賦值
for(i=0;i<5;i++){
cout?"a["?i<<"]=";
cin?a[i];
)
〃直接顯示數(shù)組元素
cout?a[0]?a[lJ?a[2]?a[3]?a[4]?endl;
〃利用for循環(huán)顯示數(shù)組各元素的值
for(i=0;i<5;i++)
cout?a[i]?n
cout?endl;
〃計(jì)算數(shù)組元素之和,并顯示計(jì)算結(jié)果
sum=a[0]+a[1]+a[2]4-a[3]+a[4];
cout?Hsum=,,?sum?endl;
〃利用循環(huán)計(jì)算數(shù)組的累加和
for(sum=0,i=0;i<5;i++)
sum+=a[i];
〃顯示累加和及平均值
cout?Hsum=n?sum?endl;
avg=sum/5.0;
cout?navg=H?avg?endl;
)一
#include<iostream.h>
main()
(
inti,max,index,a[5];
〃從鍵盤上為數(shù)組賦值
for(i=0;i<=4;i++)
(
cout?"a["?i?"]=";
cin?a[i];
)
//利用循環(huán)遍歷數(shù)組,找出最大值的元素及其下標(biāo)
max=a[0J;
for(i=0;i<=4;i++)
(
if(max<a[ij)
{
max=a[i];
index=i;
cout?n\nMax=H?max?Hindex="?index;
)
#include<iostream.h>
#definesize5
main()
(〃聲明變量
inti,j;
floatt,a[size];
〃從牌盤上為期組賦值
for(i=0;i<size;i++)
(
cout?"a["?i?"l=";
cin?a[i];
)
〃對數(shù)組按從小到大順序排序
for(i=O;i<size-l;i++)
for(j=i+l;j<size;j++)
if(a[i]>a[j])
(
t=a[i];
a[i]=a[j];
a[j]=t;
)
〃顯示排序結(jié)果
for(i=0;i<size;i++)
cout?a[i]?"°;
cout?endl;
〃輸入要查找的數(shù)據(jù)
intvalue;
intfound;〃找到為1,否則為0
intlow,high,mid;
for(i=l;i<=3;i++){
cout?nvalue=";
cin?value;
〃二分法杳找數(shù)組a
found=0;
low=0;
high=size-l;
while(low<=high)
{-
mid=(high+low)/2;
if(a[midj==value)
(
found=l;
break;
)
if(a[midj<value)
low=mid+1;
else
high=mid-l;
if(found)
cout?HThevalufoundat:a[H?mid?nJ=H?a[mid]?endl;
else
cout?"The"?value?"isnotfound!"?endl;
#include<iostream.h>
main()
(
〃聲明變量
inti,j;
floatt,a[5];
〃從鍵盤上為數(shù)組賦值
for(i=0;i<=4;i++)
(
cout?"a["?i<<"]=";
cin?a[i];
〃對數(shù)組按從大到小順序排序
for(i=0;i<=3;i++)
for(j=i+l;j<=4;j++)
if(a[i]<=aUJ)
(
t=a[i];
a[ij=a[j];
afj]=t;
〃顯示排序結(jié)果
for(i=0;i<=4;i++)
cout?a[i]?"
)
#include<iostream.h>
main()
,〃聲明二維數(shù)組及變量
inta[2][3],i,j;
〃從鍵盤上為數(shù)組a賦值
for(i=0;i<2;i++)
for(j=O;j<3;j++)
(
cout?"a["?i?"]["?j?"]=";
cin?a[i][jj;
)
〃顯示數(shù)組a
for(i=0;i<2;i++){
for(j=O;j<3;j++)
(
cout?a[i]UJ?"
}
cout?endl;
〃找出該數(shù)組的最大元素及其下標(biāo)
inth,l,Max=a[O][OJ;
for(i=0;i<2;i++){
for(j=O;j<3;j++)
(
if(Max<ari][j]){
Max=a[iJ[jJ;
h=i;
l=j;
cout?"Max:"?"a["?h?"]["?l?"]="?a[h][l]?endl;
}
#include<iostream.h>
main()
(
〃聲明字符數(shù)組和變量
charstr[6J;
inti;
〃從鍵盤上輸入字符串
cout?Mstr=";
cin?str;
cout?str?endl;
〃按數(shù)組和下標(biāo)變量兩種方式顯示字符數(shù)組
cout?str?endl;
for(i=0;i<6;i++)
cout?str[i];
cout?endl;
〃字符串反向輸出
for(i=5;i>=0;i—)
cout?str[i];
cout?endl;
〃將字符數(shù)組變成大寫字母后輸出
for(i=0;i<=5;i++)
str[i]-=32;〃小寫字母轉(zhuǎn)換成大寫字母
cout?str?endl;〃顯小字符串
)
#include<iostream.h>
main()
(〃聲明變量和指針變量
inta,b,c,*ip;
〃指針變量ip指向變量a
a=100;
ip=&a;〃使指針變量ip指向變量a
cout?na=M?a?endl;
coutvv"*ip="vv*ipvvendl;
cout?Hip="?ip?endl;
〃指針變量ip指向變量b
ip=&b;//使指針變量ip指向變量b
b=200;
cout?"b="?b?endl;
cout?"*ip="?*ip?endl;
cout?Hip=H?ip?endl;
〃指針變量ip指向變量c
ip=&c;〃使指針變量ip指向變量b
*ip=a+b;
cout?nc=n?c?endl;
cout?***ip=',?*ip?endl;
cout?"ip=n?ip?endl;
}
#include<iostream.h>
main()
(
〃聲明數(shù)組、變量和指針變量
inta[2][3],i,j;
int*ip;
〃從鍵盤上為數(shù)組a賦值
for(i=0;i<2;i++)〃為數(shù)組a賦值
for(j=O;j<3;j++)
(
cout?"a["?i?"]["?j<<"]=";
cin?a[i][jj;
〃利用下標(biāo)變量顯示數(shù)組a
for(i=0;i<2;i++){
for(j=O;j<3;j++)
(
cout?a[i][j]?"'
)
cout?endl;
〃利用指針變量顯示數(shù)組a
ip=&afO]rO];
for(i=0;i<2;i++){
forQ=O;j<3;j++)
(
cout?"a["?i?"]["?j?"J=";
cout?ip?"
cout?*ip?endl;
ip++;
)
)
)
#include<iostream.h>
main()
1〃聲明數(shù)組、變量和指針變量
inta[]={12,3,4,5,6};
int*ipl,*ip2;
〃測試指針的賦值運(yùn)算
ipl=a;
ip2=ipl;
cout?n*ip1=n?(*ip1)?endl;
cout?*,*ip2=*,?(*ip2)?endl;
〃測試指針的自增自減運(yùn)算和組合運(yùn)算
ipl++;
ip2+=4;
cout?H*ip1=n?(*ip1)?endl;
cout?n*ip2=n?(*ip2)?endl;
〃測試.指的變量之間的關(guān)系運(yùn)算
intn=ip2>ipl;
cout?"ip2>ipl=H?n?endl;
cout?Hip2!=NULL=H?(ip2!=NULL)?endl;
/侑針變量之間的減法
n=ip2-ipl;
cout?nip2-ipl=H?n?endl;
)
#include<iostream.h>
main()
〃聲明字符型數(shù)組和指針變量
charstr[10];
char*strip=str;
〃輸入輸出
cout?"str=n;
cin?str;〃用字符數(shù)組輸入字符串
cout?,,str=H?str?endl;
cout?nstrip=H?strip?endl;
cout?nstrip=n;
cin?strip;〃用字符指針變量輸入字符串
cout<<"str="?str?endl;
cout?Hstrip=n?strip?endl;
〃利用指針變量改變其指向字符串的內(nèi)容
*(strip+2)=T;
cout?,,str=H?str?endl;
cout?"strip="?strip?endl;
〃利態(tài)為字符型指針變量分配內(nèi)存
strip=newchar(lOO);
cout?nstrip=n;
cin?strip;〃用字符指針變量輸入字符串
cout?"str=H?str?endl;
cout?,,strip=,,?strip?endl;
}.
#include<iostream.h>
main()
(
//聲明用于存放運(yùn)動員號碼的數(shù)組
inth[J={1001,1002,1003,1004);
//聲明用于存放運(yùn)動員成績的數(shù)組
floatx[J={12.3,13.1,11.9,12.1);
〃聲明用于存放運(yùn)動姓名的字符型指針數(shù)組
char*p[l={"Wanghua","Zhangjian","Liwei","Huaming"};
//i,j,it是用做循環(huán)控制變量和精時(shí)變量
int
//ft用做暫存變量
floatft;
//pt為字符型指針變量用做暫存指針變量
char*pt;
〃用選擇法對數(shù)組X進(jìn)行排序,并相應(yīng)調(diào)整數(shù)組h和p中的數(shù)據(jù)
for(i=0;i<=3;i++)
for(j=i+l;j<=3;j++)
if(x[i]>=x[jj){
ft=x[i],x[i]=x[j],xfj]=ft;
it=h[i],h[i]=h[j],h[j]=it;
pt=p[iJ,p[iJ=p[j],pLjJ=pt;
〃以下打印排序結(jié)果
for(i=0;i<=3;i++)
cout?h[iJ?M,"?p[iJ?M,H?x[iJ?endl;
)
#include<iostream.h>
main()
(
〃聲明指針數(shù)組
char*colors[]={"Red","Blue","Yellow","Green");
〃指向指針的指針變量
char**pt;
〃通過指向指針的變量訪問其指向的內(nèi)容
pt=colors;
for(inti=0;i<=3;i++){
cout?Hpt=M?pt?endl;
cout?M*pt=H?*pt?endl;
cout?,,**pt=,,?**pt?endl;
pt++;
)
)
#include<iostream.h>
main()
{〃定義結(jié)構(gòu)類型
structbooks
(
chartitle[20];
charauthor[15];
intpages;
floatprice;
};
〃聲明結(jié)構(gòu)變量
structbooksZbk={"VC++","Zhang",295,35.5};
booksWbk;
〃對結(jié)構(gòu)變量的輸出
cout?"Zbk:"vvend1;
cout?Zbk.title?endl;
cout?Zbk.author?endl;
cout?Zbk.pages?endl;
cout?Zbk.price?endl;
cout?Hn?endl;
〃對結(jié)構(gòu)成員的運(yùn)算
Zbk.pages+=10;
Zbk.price+=0.5;
cout?"Zbk.pages="?Zbk.pages?endl;
cout?nZbk.price=H?Zbk.price?endl;
cout?MM?endl;
〃對結(jié)構(gòu)變量的輸入輸出
cout?HWbk.title='*;
cin?Wbk.title;
cout?nWbk.author=";
cin?Wbk.author;
cout?HWbk.pages=";
cin?Wbk.pages;
cout?"Wbk.price=";
cin?Wbk.price;
cout?HWbk:"vvendl;
cout?Wbk.title?endl;
cout?Wbk.author?endl;
cout?Wbk.pages?endl;
cout?Wbk.price?endl;
cout?n"?endl;
〃結(jié)構(gòu)變量之間的相互賦值
bookstemp;
temp=Wbk;
cout?Htemp:"?endl;
cout?temp.title?endl;
cout?temp.author?endl;
cout?temp.pages?endl;
cout?temp.price?endl;
#include<iostream.h>
main()
(
inti;
〃定義結(jié)構(gòu)類型
structstudent{
intnum;
charname[10];
floatmaths;
floatphysics;
floatchemistry;
doubletotal;
);
//聲明結(jié)構(gòu)數(shù)組st
s
溫馨提示
- 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湘教版數(shù)學(xué)八年級下冊4.5《一次函數(shù)的應(yīng)用》聽評課記錄3
- 湘教版九年級數(shù)學(xué)下冊2.6弧長與扇形面積第1課時(shí)弧長聽評課記錄
- 八年級上冊道德與法治第一單元 走進(jìn)社會生活則 復(fù)習(xí)聽課評課記錄
- 蘇科版數(shù)學(xué)八年級下冊《9.1 圖形的旋轉(zhuǎn)》聽評課記錄2
- 蘇教版小學(xué)五年級上冊數(shù)學(xué)口算練習(xí)題
- 出國勞務(wù)派遣合同范本
- IT程序員保密協(xié)議書范本
- 深圳經(jīng)濟(jì)特區(qū)房產(chǎn)抵押貸款協(xié)議書范本
- 全國事業(yè)單位聘用合同范本
- 鄉(xiāng)村振興戰(zhàn)略合作合同范本
- 《霍爾效應(yīng)測量磁場》課件
- 《統(tǒng)計(jì)分析與SPSS的應(yīng)用(第7版)》課件全套 第1-12章 SPSS統(tǒng)計(jì)分析軟件概述
- 黑龍江省哈爾濱市2022-2023學(xué)年八年級上學(xué)期期末數(shù)學(xué)試題(含答案)
- 《瘋狂動物城》全本臺詞中英文對照
- 中專數(shù)學(xué)(基礎(chǔ)模塊)上冊課件
- 智慧農(nóng)業(yè)整體解決方案
- 總經(jīng)理權(quán)責(zé)授權(quán)書
- 高考作文復(fù)習(xí)任務(wù)驅(qū)動型作文的審題立意課件73張
- 家具廠規(guī)章制度
- 三查四定管理制度(參考模板)
- 《體育與健康教學(xué)改革指導(dǎo)綱要》的時(shí)代意義、內(nèi)容特征和踐行路徑兼論新時(shí)代學(xué)校體育的走向
評論
0/150
提交評論