C++高級程序設計:C++作業(練習題)(1-9章)_第1頁
C++高級程序設計:C++作業(練習題)(1-9章)_第2頁
C++高級程序設計:C++作業(練習題)(1-9章)_第3頁
C++高級程序設計:C++作業(練習題)(1-9章)_第4頁
C++高級程序設計:C++作業(練習題)(1-9章)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

C++練習題(第1章■第9章)編寫被調函數,求四個數的平均值,在主函數中調用該函數求四個數的平均值。testl.cpp*x.(全局范圍)~-〃testl.cpp:編寫被調函數,求四個數的平均值//在主函數中調用該函數求四個數的平均值。#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編寫一個通過重載求兩個數中較小數的函數,分別求兩個實數和兩個整數中的較小數。test2.cpp*X(全局范圍)日〃test2.cpp:定義控制臺應用程序的入口點。|//編寫一個通過重載求兩個數中較小數的函數,分別求兩個實數和兩個整數中的較小數#include"stdafx.h"#include<iostream>usingnamespacestd;intconpare(inta,intb);〃比較兩個整數floatcompare(floata,floatb): //比較兩個實數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???青按任意鍵繼續...編寫一個程序,將求兩個實數中的較小值的函數放在一個頭文件中,在源程序文件中包含該頭文件,并實現輸入3個實數,求出最小值。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???請按任意鍵繼續...有兩個一維數組(元素個數不超過1()0個)中的元素已按升序排列,編寫一個函數將兩個數組中的元素歸并成一個數組,其屮的元素仍然按升序排列。日〃test4.cpp:定義控制臺應用程序的入口點。//有兩個一維數組(元素個數不超過100個)中的元素已按升序排列//編寫一個函數將兩個數組中的元素歸并成一個數組,其中的元素仍然按升序排列。#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]://原始數組1for(inti=0;i<m;i++)cin?a[i]:cout?endl;cout?"Howmanynumbersinthesecondarray:intn;cin?n;cout?"Enterthesecondarray:"?endl;int?b=newint[n];//原始數組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++)//數組合并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;〃粹故內存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.一篇文章有若干行,以空行作為輸入結束的條件。統計一篇文章中的單詞the(不管大小寫,單詞the是由空格隔開的)的個數。6.編寫程序,輸入若干學生的學號、姓名和考試分數。該程序能輸岀每個學生的學號、姓名和相應的考試成績,同時能查找并輸出最高分的學生的學號、姓名和考試分數。要求使用結構體數組。testb.cppX圍)二//test6.cpp:定義控制臺應用程序的X口點。〃偏寫程序,輸入若干學生的學號、姓名和考試分數。//該程序能輸出每個學生的學號、姓名和相應的考試成績,//同時能查找并輸出最高分的學生的學號、姓名和肴試分數。要求使用結構體數組。#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];//假設學生數少于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.在主函數中輸入任意三個整數存入變量a、b、c中,調用函數swap(inl*,int*,血*)實現將三個變量值進行交換(要求函數的參數為三個變量a、b、c的地址),達到從小到大排序的目的,即變量a中的值是初始三個值中的最小者,變量c中的值是初始三個值中的最大者,最后輸出a、b、c三個變量的值。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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論