




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C++練習(xí)題(第1章■第9章)編寫被調(diào)函數(shù),求四個(gè)數(shù)的平均值,在主函數(shù)中調(diào)用該函數(shù)求四個(gè)數(shù)的平均值。testl.cpp*x.(全局范圍)~-〃testl.cpp:編寫被調(diào)函數(shù),求四個(gè)數(shù)的平均值//在主函數(shù)中調(diào)用該函數(shù)求四個(gè)數(shù)的平均值。#include"stdafx.h"#include<iostream>usingnamespacestd;doubleaverage(double*num):Hint_tmain(intargc,_TCHAR*argv[]){doublenum[4]:Icharch;cout?"StartthiscalculateYesorNo?(Y/N)":cin?ch;while(ch=='y'||ch=='Y'){.cout?Enterfournumber::for(inti=0;i<4;i++)cin?nwn[i]:cout?"Theaverageis:"〈〈average(num)?endl:cout?"Doyouwantcalculateagain?(Y/N)“:cin?ch;cout?*Bye!*?endl;return0;-doubleaverage(double*num)|{doublesum=0;for(inti=0;i<4;i++)sum+=num[i]:returnsum/4:C:\Windows\system32\cmd.exe編寫一個(gè)通過重載求兩個(gè)數(shù)中較小數(shù)的函數(shù),分別求兩個(gè)實(shí)數(shù)和兩個(gè)整數(shù)中的較小數(shù)。test2.cpp*X(全局范圍)日〃test2.cpp:定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。|//編寫一個(gè)通過重載求兩個(gè)數(shù)中較小數(shù)的函數(shù),分別求兩個(gè)實(shí)數(shù)和兩個(gè)整數(shù)中的較小數(shù)#include"stdafx.h"#include<iostream>usingnamespacestd;intconpare(inta,intb);〃比較兩個(gè)整數(shù)floatcompare(floata,floatb): //比較兩個(gè)實(shí)數(shù)Sint_tmain(intargc,_TCHAR?argv[])floata,b;cout?"Starttocompare?(Y/N)“:charch;cin?ch;while(ch='y'||ch=='Y*){cout?"Entertwonumbertocompare:"?endl;cin?a?b;//if((a-(int)a)==0&&b-(int)b=0)cout《Thesmallnumberis:"?compare(a,b)?endl?endl;cout?"Compareagain?(Y/N)":cin?ch;}cout?"Done!?!w?endl;return0;intcompare(inta,intb)(return(a<b?a:b):3floatcompare(floata,floatb)return(a<b?a:b):100%
C:\Windows\system32\cmd.exetarttocompare?<Y/N>yhtertwonunbertocompare:.35.7845hesnailnunberis:2.3Compareagain?<Y/N>yintertwonunbertocompare:"hesmallnunbei*is:2Compareagain?<Y/N〉n)one???青按任意鍵繼續(xù)...編寫一個(gè)程序,將求兩個(gè)實(shí)數(shù)中的較小值的函數(shù)放在一個(gè)頭文件中,在源程序文件中包含該頭文件,并實(shí)現(xiàn)輸入3個(gè)實(shí)數(shù),求出最小值。stdafx.h smaller.h#include<iostream>#include"smaller.hwusingnamespacestd;-int_tmain(intargc,_TCHAR*argv[])floata,b,c;compare?(Y/N)":ch=='Y!compare?(Y/N)":ch=='Y!cin?ch;while(ch=='y,||cout?"Enterthreerealnumber:w?endl:cin?a?b?c;cout?"Theminimumnumberis:“?(smaller(a,b)<c?smaller(a,b):c)?endl;cout?"Compareagain?(Y/N)”:cin?ch;cout?"Done!!!"?endl;return0;stdafx.hsmaller.hXtest3.cppstdafx.hsmaller.hXtest3.cpp(錄范圍)日floatsmaller(floata,floatb)return(a<b?a:b);}|
C:\Wmdows\system32\cmd.exeStai*tthiscompare?<?/N>yEnterthreerealnumber:123Fheminimumnumberis:1Compareagain?<V/N>yEnterthreerealnumber:1.23545.267450.45643Fheminimumnumberis:0.45643Compareagain?<V/N>nDone???請(qǐng)按任意鍵繼續(xù)...有兩個(gè)一維數(shù)組(元素個(gè)數(shù)不超過1()0個(gè))中的元素已按升序排列,編寫一個(gè)函數(shù)將兩個(gè)數(shù)組中的元素歸并成一個(gè)數(shù)組,其屮的元素仍然按升序排列。日〃test4.cpp:定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。//有兩個(gè)一維數(shù)組(元素個(gè)數(shù)不超過100個(gè))中的元素已按升序排列//編寫一個(gè)函數(shù)將兩個(gè)數(shù)組中的元素歸并成一個(gè)數(shù)組,其中的元素仍然按升序排列。#include"stdafx.h"#include<iostreaj?>usingnamespacestda[],a[],intx,inta[],intx,inta[],intx,intintbubble_sort(intintselect_sort(intintinsert_sort(inty);//冒泡法排序y);//迭擇法排序y);//插入法排序到聲明I□int_tmain(intargc,_TCHAR*argv[]){cout?"Howmanynumbersinthefirstarray:intm.cin?m.cout?"Enterthefirstarray:"?endl:int?a=newint[m]://原始數(shù)組1for(inti=0;i<m;i++)cin?a[i]:cout?endl;cout?"Howmanynumbersinthesecondarray:intn;cin?n;cout?"Enterthesecondarray:"?endl;int?b=newint[n];//原始數(shù)組2for(intj=0;j<n;j++)cin?b[j];cout?endl;intsum=ni-hn;int*c=newint[sum];//生成的教組intt,k:for(k=0;k<m;k++)//數(shù)組合并c[k]=a[k]:for(k;k<sum;k++)c[k]=b[k-m];cout?"Thenewarrayis:w<<endl:for(t=0;t<sum;t++)cout?c[t]?cout?endl?endl:inttimeb(0),bubble_sort(c,select_sort(c,insert_sort(c.times(0),timei(0): //inttimeb(0),bubble_sort(c,select_sort(c,insert_sort(c.sum,timeb)://冒泡法排序sum,times)://迭擇法排序sum,timei);//插入法排序cout?"Theprogrameffienthignestmethodis:if(timeb<times&&timeb<timei)cout?"bubblesort!*?endl;else{if(times<timeb&×<timei)cout?"selectsort!*?endl;elsecout?"insertsort!"?endl;delete[]a;〃粹故內(nèi)存delete[]b;delete[]c;return0;
田I聲明+int_tmain(intargc,_TCHAR*argv[])|(...}~|-intbubble.sort(intc[],intsum,inttineb)//-intinttemp:for(inti=0:i<sum-l:i++){for(intj=0.j<sum-l-i;j++)(if(c[j]>c[j+l])(temp=c[j]:c[j]=c[j+l];c[j+l]=temp;}++timeb:cout?"Afterbubblesortarrayis:*?endl;for(inti=0;i<su?;i++)cout?c[i]?w,\cout?endl:cout?"Bubblesortmethodneedrun"?timeb?"times*:cout?endl<<endl:returntimeb.+'int++'int+int{…}{...}Iselect_sort(intc[],insert_sort(intc[],intswn,inttimes)intsum,inttimei)?LWJ*int_tmain(intargc,_TCHAR*argv[])|(...+intbubble.sort(intc[],intsum,inttimeb)|{...}~|Sintselect_sort(intc[],intsum,inttimes)//迭擇法排序(intk,temp;for(inti=0;i<sm-1;i++)(k=i;for(intj=i+l:j<sum;j++){ if(c[j]<c[k])k=j;++times;}if(kl=i){temp=c[k];c[k]=c[i]:c[i]=temp;}cout?"Afterselectsortarrayis:"?endl;for(inti=0;i<sum;i++)cout?c[i]?*,\cout?endl;cout?"Selectsortmethodneedrun"?times??times";cout?endl<<endl;returntimes;Sintinsert_sort(intc[],intsum,inttimei){...}jSint_tmain(intargc._TCHAR*argv[])l{...}1+intbubble.sort(intc[].timeb)Sintselect_sort(intc[J,times)□intlinsert_sort(intc[].timei)iintj,p;for(inti=l:i<sum;i++)//插入法排序( P=c[i];for(j=i-l;j>=0Wp<c[j]:j~){ c[j+l]=c[j];++timei: }c[j+l]=p;cout?"Afterinsertsortarrayis:"?endl:for(inti=0;i<sum;i++)cout?c[i]?w,\cout?endl;cout?"Insertsortmethodneedrun"?timei?"times";cout?endl<<endl:returntimei:5.一篇文章有若干行,以空行作為輸入結(jié)束的條件。統(tǒng)計(jì)一篇文章中的單詞the(不管大小寫,單詞the是由空格隔開的)的個(gè)數(shù)。6.編寫程序,輸入若干學(xué)生的學(xué)號(hào)、姓名和考試分?jǐn)?shù)。該程序能輸岀每個(gè)學(xué)生的學(xué)號(hào)、姓名和相應(yīng)的考試成績(jī),同時(shí)能查找并輸出最高分的學(xué)生的學(xué)號(hào)、姓名和考試分?jǐn)?shù)。要求使用結(jié)構(gòu)體數(shù)組。testb.cppX圍)二//test6.cpp:定義控制臺(tái)應(yīng)用程序的X口點(diǎn)?!ㄆ珜懗绦?,輸入若干學(xué)生的學(xué)號(hào)、姓名和考試分?jǐn)?shù)。//該程序能輸出每個(gè)學(xué)生的學(xué)號(hào)、姓名和相應(yīng)的考試成績(jī),//同時(shí)能查找并輸出最高分的學(xué)生的學(xué)號(hào)、姓名和肴試分?jǐn)?shù)。要求使用結(jié)構(gòu)體數(shù)組。#include"stdafx.h*#include<iostream>#include〈string〉usingnamespacestd;L-structinfo{intID;charname[20]:intscore;};-int_t>ain(intargc,_TCHAR*argv[]){intnu?:cout?"Howmanystudent'sinformationneedtoinput:cin?num:infostd[100];//假設(shè)學(xué)生數(shù)少于100for(inti=0;i<nu*;i++){ID:":name:?:exaBinationscore:cout?i+1?ID:":name:?:exaBinationscore:cout?"Enterthestudent*scin?std[i].name;cout?"Enterthestudent*scin?std[i].score;cout?endl;|cout?"List:*?endl;for(intj=0;j<num:j++){cout?std[j].ID?*“:cout?std[j].name?w":cout?std[j].score?**?endl:inttemp=std[0].score:intx;for(intk=1:k<num;k++)if(temp<std[k].score){temp=std[k].score:x=k: }cout?"Thescorehigheststudent'sis:-?endl;cout?std[x].ID?""?std[x].name?w"?std[x].score<<endl:return0:
7.在主函數(shù)中輸入任意三個(gè)整數(shù)存入變量a、b、c中,調(diào)用函數(shù)swap(inl*,int*,血*)實(shí)現(xiàn)將三個(gè)變量值進(jìn)行交換(要求函數(shù)的參數(shù)為三個(gè)變量a、b、c的地址),達(dá)到從小到大排序的目的,即變量a中的值是初始三個(gè)值中的最小者,變量c中的值是初始三個(gè)值中的最大者,最后輸出a、b、c三個(gè)變量的值。test3.cptest7.cppXI(全局范圍)i#include"stdafx.h"#include<iostream>usingnamespacestd:voidswap(int*,int*,int*):test3.cptest7.cppXI(全局范圍)i#
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)廢棄物資源化-第17篇-洞察及研究
- 非線性波浪相互作用機(jī)制-洞察闡釋
- 蔬菜加工副產(chǎn)物的結(jié)構(gòu)與性能研究-洞察闡釋
- 人造草坪的聲學(xué)與結(jié)構(gòu)協(xié)同優(yōu)化研究-洞察闡釋
- 物聯(lián)網(wǎng)返回鍵用戶體驗(yàn)優(yōu)化-洞察闡釋
- 藝術(shù)教育中的跨文化視角與創(chuàng)新教學(xué)策略-洞察闡釋
- 運(yùn)維大數(shù)據(jù)平臺(tái)構(gòu)建-洞察闡釋
- 容器化框架應(yīng)用-洞察闡釋
- 暗物質(zhì)引力波探測(cè)-洞察闡釋
- 【關(guān)于某PLC控制的爬壁機(jī)器人系統(tǒng)硬件和軟件設(shè)計(jì)案例4500字】
- GB/T 5249-2013可滲透性燒結(jié)金屬材料氣泡試驗(yàn)孔徑的測(cè)定
- GB/T 28267.3-2015鋼絲繩芯輸送帶第3部分:井下用輸送帶的特殊安全要求
- GB/T 15684-2015谷物碾磨制品脂肪酸值的測(cè)定
- 百靈達(dá)x32數(shù)字調(diào)音臺(tái)說明書簡(jiǎn)體中文
- GA/T 947.2-2015單警執(zhí)法視音頻記錄系統(tǒng)第2部分:執(zhí)法記錄儀
- 噴霧干燥器課程設(shè)計(jì)終稿
- API-650-1鋼制焊接石油儲(chǔ)罐
- 英威騰GD變頻器調(diào)試說明
- 倉(cāng)庫(kù)作業(yè)指導(dǎo)書
- 冰雪奇緣臺(tái)詞中英文對(duì)照完整版(常用)
- 施工現(xiàn)場(chǎng)安全管理處罰規(guī)定
評(píng)論
0/150
提交評(píng)論