




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
C++簡樸程序經(jīng)典案例【案例2-1】設(shè)計(jì)一種編寫僅包括C++程序基本構(gòu)成元素的程序/*
//注釋行開始ThisisthefirstC++program.
Designedbyzrf*/
//注釋行結(jié)束#include<iostream>
//包括頭文獻(xiàn)usingnamespacestd;
//打開命名空間std//Thisisthemainfunction
//單行注釋語句intmain(void)
//主函數(shù),程序入口{
//塊作用域開始intage;
//申明一種變量age=20;
//賦值語句cout<<"Theageis:\n";
//輸出一種字符串cout<<age<<endl;
//輸出變量中的值return0;
//主函數(shù)返回0}
//塊作用域結(jié)束
【案例2-2】計(jì)算圓的周長和面積——C++語言中常量、變量#include<iostream>usingnamespacestd;intmain(){
constfloatPI=3.1415926;
//float型常量floatr=2.0;
//用float型常量初始化變量cout<<"r="<<r<<endl;
//輸出圓的半徑floatlength;
//float型變量申明length=2*PI*r;
//計(jì)算圓的周長cout<<"Length="<<length<<endl;
//輸出圓的周長floatarea=PI*r*r;
//計(jì)算圓的面積cout<<"Area="<<area<<endl;
//輸出圓的面積return0;}【案例2-3】整數(shù)的簡樸運(yùn)算——除法、求余運(yùn)算法和增量減量運(yùn)算符#include<iostream>usingnamespacestd;intmain(){
intx,y;x=10;
y=3;cout<<x<<"/"<<y<<"is"<<x/y
//整數(shù)的除法操作<<"withx%yis"<<x%y<<endl;
//整數(shù)的取余操作x++;
--y;
//使用增量減量運(yùn)算符cout<<x<<"/"<<y<<"is"<<x/y<<"\n"
//整數(shù)的除法操作<<x<<"%"<<y<<"is"<<x%y<<endl;
//整數(shù)的取余操作return0;}【案例2-4】多重計(jì)數(shù)器——前置和后置自增運(yùn)算符#include<iostream>
usingnamespacestd;
intmain()
{
intiCount=1;
iCount=(iCount++)+(iCount++)+(iCount++);
//后置++cout<<"Thefirst
iCount="<<iCount<<endl;iCount=1;
iCount=(++iCount)+(++iCount)+(++iCount);
//前置++cout<<"ThesecondiCount="<<iCount<<endl;iCount=1;
iCount=-iCount++;
//后置++cout<<"Thethird
iCount="<<iCount<<endl;iCount=1;
iCount=-++iCount;
//前置++cout<<"Thefourth
iCount="<<iCount<<endl;return0;
}【案例2-5】對整數(shù)“10”和“20”進(jìn)行位運(yùn)算——位運(yùn)算的應(yīng)用#include<iostream>usingnamespacestd;intmain()
{
cout<<"20&10="<<(20&10)<<endl;
//按位與運(yùn)算cout<<"20^10="<<(20^10)<<endl;
//按位異或運(yùn)算cout<<"20|10="<<(20|10)<<endl;
//按位或運(yùn)算cout<<"~20="<<(~20)<<endl;
//按位取反運(yùn)算cout<<"20<<3="<<(20<<3)<<endl;
//左移位運(yùn)算cout<<"-20<<3="<<(-20<<3)<<endl;
//左移位運(yùn)算cout<<"20>>3="<<(20>>3)<<endl;
//右移位運(yùn)算cout<<"-20>>3="<<(-20>>3)<<endl;
//右移位運(yùn)算return0;}【案例2-6】實(shí)現(xiàn)邏輯“異或”運(yùn)算——邏輯運(yùn)算應(yīng)用#include<iostream>usingnamespacestd;intmain(){
boolp,q;p=true;
q=true;cout<<p<<"XOR"<<q<<"is"<<((p||q)&&!(p&&q))<<"\n";//輸出異或成果p=false;
q=true;cout<<p<<"XOR"<<q<<"is"<<((p||q)&&!(p&&q))<<"\n";//輸出異或成果p=true;
q=false;cout<<p<<"XOR"<<q<<"is"<<((p||q)&&!(p&&q))<<"\n";//輸出異或成果p=false;
q=false;cout<<p<<"XOR"<<q<<"is"<<((p||q)&&!(p&&q))<<"\n";//輸出異或成果return0;}【案例2-7】高效篩選器——用條件運(yùn)算符“?”構(gòu)建條件體現(xiàn)式#include<iostream>usingnamespacestd;intmain()
{
intiNum1=1,iNum2,iMax;cout<<"Pleaseinputtwointegers:\n";
cin>>iNum1>>iNum2;iMax=iNum1>iNum2?iNum1:iNum2;
//使用條件運(yùn)算符構(gòu)建條件體現(xiàn)式cout<<"Themaxintegeris:"<<iMax<<endl;return0;}【案例2-8】“多計(jì)算與單提取”功能的實(shí)現(xiàn)——逗號(hào)體現(xiàn)式#include<iostream>usingnamespacestd;intmain()
{
intVal1,Val2,Val3,Left,Midd,Righ;Left=10;
Midd=20;
Righ=30;Val1=(Left++,--Midd,Righ++);
//使用逗號(hào)體現(xiàn)式Val2=(Righ++,Left++,--Midd);
//使用逗號(hào)體現(xiàn)式Val3=(--Midd,Righ++,Left++);
//使用逗號(hào)體現(xiàn)式cout<<"Val1=\t"<<Val1<<"\nVal2=\t"<<Val2<<"\nVal3=\t"<<Val3<<endl;return0;}【案例2-9】高效的算術(shù)運(yùn)算符——復(fù)合賦值運(yùn)算符#include<iostream>usingnamespacestd;intmain(){
intn=20;
cout<<"n="<<n<<endl;n+=8;
cout<<"Aftern+=8,n="<<n<<endl;
//使用復(fù)合的賦值運(yùn)算符+=n-=6;
cout<<"Aftern-=6,n="<<n<<endl;
//使用復(fù)合的賦值運(yùn)算符-=n*=1;
cout<<"Aftern*=1,n="<<n<<endl;
//使用復(fù)合的賦值運(yùn)算符*=n/=4;
cout<<"Aftern/=4,n="<<n<<endl;
//使用復(fù)合的賦值運(yùn)算符/=n%=3;
cout<<"Aftern%=3,n="<<n<<endl;
//使用復(fù)合的賦值運(yùn)算符%=return0;}【案例2-10】計(jì)算不一樣數(shù)據(jù)類型的存儲(chǔ)容量——sizeof運(yùn)算符#include<iostream>usingnamespacestd;intmain(){
cout<<"Thesizeofanintis:\t\t"<<sizeof(int)<<"bytes.\n";cout<<"Thesizeofashortintis:\t"<<sizeof(short)<<"bytes.\n";cout<<"Thesizeofalongintis:\t"<<sizeof(long)<<"bytes.\n";cout<<"Thesizeofacharis:\t\t"<<sizeof(char)<<"bytes.\n";cout<<"Thesizeofawchar_tis:\t"<<sizeof(wchar_t)<<"bytes.\n";cout<<"Thesizeofafloatis:\t\t"<<sizeof(float)<<"bytes.\n";cout<<"Thesizeofadoubleis:\t"<<sizeof(double)<<"bytes.\n";return0;}【案例2-11】巧妙獲取整數(shù)部分——double和int數(shù)據(jù)類型的轉(zhuǎn)換#include<iostream>usingnamespacestd;intmain(){
intnn=10,mm;doublexx=4.741,yy;cout<<"nn*xx="<<nn*xx<<endl;
//體現(xiàn)式類型轉(zhuǎn)換mm=xx;
yy=nn;
//賦值類型轉(zhuǎn)換cout<<"mm="<<mm<<endl<<"yy="<<yy<<endl;cout<<"int(xx)="<<int(xx)<<endl<<"(int)xx="<<(int)xx<<endl;
//強(qiáng)制類型轉(zhuǎn)換cout<<"int(1.412+xx)="<<int(1.412+xx)<<endl;
//強(qiáng)制類型轉(zhuǎn)換cout<<"(int)1.412+xx="<<(int)1.412+xx<<endl;
//強(qiáng)制類型轉(zhuǎn)換return0;}【案例2-12】將分?jǐn)?shù)轉(zhuǎn)換為小數(shù)——強(qiáng)制類型轉(zhuǎn)換#include<iostream>usingnamespacestd;intmain(){
for(inti=1;i<=5;++i)cout<<i<<"/3is:"<<(float)i/3<<endl;
//強(qiáng)制類型轉(zhuǎn)換return0;}【案例2-13】安全的除法計(jì)算器#include<iostream>usingnamespacestd;intmain(){
inta,b;cout<<"Enternumerator:";
cin>>a;cout<<"Enterdenominator:";
cin>>b;if(b)cout<<"DivideResultis:"<<a/b<<'\n';
//排除除數(shù)為零的狀況elsecout<<"Dividebyzero!\n";return0;}【案例2-14】猜數(shù)游戲——嵌套的if條件語句#include<iostream>#include<cstdlib>usingnamespacestd;intmain(){
intMagNum,GueNum;
MagNum=rand();
//產(chǎn)生隨機(jī)數(shù)cout<<"EntertheGuessnumber:";
cin>>GueNum;if(GueNum==MagNum){
//if語句塊起始位置cout<<"*ItisRight*\n"<<MagNum<<"istheMagessnumber.\n";}
//if語句塊結(jié)束位置else{
//else語句塊起始位置cout<<"Sorry,you'rewrong."<<endl;if(GueNum>MagNum)
cout<<"Guessednumberistoohigh.\n";else
cout<<"Guessednumberistoolow.\n";}
//else語句塊結(jié)束位置return0;}【案例2-15】根據(jù)輸入月份輸出從年初到本月底的天數(shù)——不帶break的switch#include<iostream>usingnamespacestd;intmain(){
intyear,month,days=0;cout<<"Inputyearandmonth:";
cin>>year>>month;switch(month)
//每個(gè)case分支均沒有break語句{
case12:days+=31;case11:days+=30;case10:days+=31;case
9:days+=30;case
8:days+=31;case
7:days+=31;case
6:days+=30;case
5:days+=31;case
4:days+=30;case
3:days+=31;case
2:
//判斷與否為閏年if(year%4==0&&year%100!=0||year%400==0)
days+=29;else
days+=28;case
1:days+=31;}if(days==0)
cout<<"Wrongmonth"<<endl;else
cout<<"Totaldaysis:"<<days<<endl;return0;}【案例2-16】計(jì)算數(shù)的階乘——do-while循環(huán)語句#include<iostream>usingnamespacestd;intmain(){
longlimits;cout<<"Enterapositiveinteger:";
cin>>limits;cout<<"Factorialnumbersof"<<0<<"is"<<1<<endl;cout<<"Factorialnumbersof"<<1<<"is"<<1<<endl;longfac=1,i=1;do
//使用do-while循環(huán){
fac*=++i;
cout<<"Factorialnumbersof"<<i<<"is"<<fac<<endl;}while(fac<limits);return0;}【案例2-17】計(jì)算數(shù)的階乘——for循環(huán)#include<iostream>usingnamespacestd;intmain(){
longlimits;cout<<"Enterapositiveinteger:";
cin>>limits;cout<<"Factorialnumbersof"<<0<<"is"<<1<<endl;cout<<"Factorialnumbersof"<<1<<"is"<<1<<endl;longfac=1;for(inti=2;fac<=limits;i++)
//使用for循環(huán){
fac*=i;cout<<"Factorialnumbersof"<<i<<"is"<<fac<<endl;}return0;}【案例2-18】篩選素?cái)?shù)——步長為2的for循環(huán)#include<iostream>#include<cstdlib>usingnamespacestd;intmain(){
longn;cout<<"Enterapositiveinteger:";
cin>>n;if(n<2)
cout<<n<<"isnotprime."<<endl;elseif(n<4)
cout<<n<<"isprime."<<endl;elseif(n%2==0)
cout<<n<<"=2*"<<n/2<<endl;else{
for(inti=3;i<=n/2;i+=2)
//步長為2if(n%i==0)
{cout<<n<<"="<<i<<"*"<<n/i<<endl;
exit(0);}cout<<n<<"isprime."<<endl;}return0;}【案例2-19】輸出1~20之間的偶數(shù)——continue語句#include<iostream>usingnamespacestd;intmain(){
cout<<"Theevennumbersareasfollows:"<<endl;for(inti=0;i<=20;i++){
if(i%2)continue;
//根據(jù)條件使用continue結(jié)束本次循環(huán)cout<<i<<'';}return0;}【案例2-20】記錄輸入整數(shù)的個(gè)數(shù)并求和——exit()函數(shù)#include<iostream>#include<cstdlib>usingnamespacestd;intmain(){
intsum=0,num=0,m;cout<<"Pleaseinputintegers(0:end):"<<endl;do{cin>>m;
num++;
sum+=m;if(m==0){cout<<"Enterednumbers:"<<num<<"integers.\n";cout<<"Thesumis:"<<sum<<endl;exit(0);
//使用exit()函數(shù)終止程序}}while(1);return0;}【案例2-21】“剪刀、石頭、布”游戲——枚舉類型#include<iostream>usingnamespacestd;enumChoice{ROCK,CLOTH,SCISS};
//申明枚舉類型ChoiceenumWinner{Play1,Play2,Tie};
//申明枚舉類型Winnerintmain(){
intn;
Choicecho1,cho2;
Winnerwinner;cout<<"Chooserock(0),cloth(1),orSciss(2):"<<endl;cout<<"PlayerNo.1:";
cin>>n;
cho1=Choice(n);cout<<"PlayerNo.2:";
cin>>n;
cho2=Choice(n);if(cho1==cho2)winner=Tie;elseif(cho1==ROCK)if(cho2==CLOTH)
winner=Play2;else
winner=Play1;elseif(cho1==CLOTH)if(cho2==SCISS)
winner=Play2;else
winner=Play1;elseif(cho2==ROCK)
winner=Play2;else
winner=Play1;if(winner==Tie)
cout<<"\tTied!\n";elseif(winner==Play1)
cout<<"\tPlayerNo.1wins."<<endl;else
cout<<"\tPlayerNo.2wins."<<endl;return0;}【案例2-22】簡樸的學(xué)生信息類型——構(gòu)造體#include<iostream>#include<iomanip>usingnamespacestd;structstudent
//學(xué)生信息構(gòu)造體{
intnum;charname[20];chargender;intage;}stu1={1001,"ZhangSan",'M',19};intmain(){
studentstu2={1002,"LiSi",'M',20};
//申明構(gòu)造體變量并初始化studentstu3={1003,"WangHong",'F',22};
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 全國自學(xué)考試現(xiàn)代設(shè)計(jì)方法自考試題及答案
- 5G技術(shù)實(shí)時(shí)監(jiān)控應(yīng)用-洞察與解讀
- 《園林植物造景設(shè)計(jì)》網(wǎng)上作業(yè)題及答案
- 學(xué)習(xí)行為數(shù)據(jù)分析-第6篇-洞察與解讀
- 2025年事業(yè)單位招聘考試旅游類專業(yè)綜合能力測試真題模擬詳解
- 2025廣西柳州城市職業(yè)學(xué)院招聘專任教師、輔導(dǎo)員38名考前自測高頻考點(diǎn)模擬試題及一套答案詳解
- 邊緣計(jì)算下的數(shù)據(jù)存儲(chǔ)方案-洞察與解讀
- 關(guān)境數(shù)據(jù)融合策略-洞察與解讀
- 衡水文綜中考試卷及答案
- 家校溝通平臺(tái)構(gòu)建-洞察與解讀
- DB15∕T 3968-2025 一鍵報(bào)警信息接入公安機(jī)關(guān)技術(shù)規(guī)范
- DB45∕T 2883-2024 健康體檢機(jī)構(gòu)護(hù)理質(zhì)量管理規(guī)范
- 普洱茶教學(xué)課件
- 肺癌教學(xué)課件
- 2025年廣告設(shè)計(jì)與制作考試題及答案
- 徽派建筑速寫課件
- 華為公司采購部管理制度
- 物料平衡檢查管理制度
- 2025年陜西高考物理試卷真題及答案詳解(山西寧夏青海適用)
- CJ/T 251-2007銅分集水器
- T/CSWSL 020-2020肉羊用菌酶協(xié)同發(fā)酵飼料
評論
0/150
提交評論