




已閱讀5頁,還剩13頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
3.下列程序計算1000以內能被3整除的自然數(shù)之和,請完成程序。 #include void main() int x=1, sum; 1 while(1) if( 2 )break; if( 3 )sum+=x; x+; coutsumendl; 1. 1 #include 2 break; 3 jtemp 2. 1 a1000 3x%3=0 四、程序填空題 1. 下面的函數(shù)fun未使用中間變量實現(xiàn)對兩個數(shù)的交換,請完成下列函數(shù)的定義。 void fun(int &x, int &y) x+=y; y= 1 ; 2 ; 2.下面的函數(shù)bubble()是對整數(shù)數(shù)組a按升序排序的冒泡算法,其中,參數(shù)a存儲將被排序的數(shù)據(jù),size是數(shù)組a中存儲的元素數(shù)目,請完成該函數(shù)。 void bubble(int a, int size) 1 ; for(int p=1; 2 ; p+) for(int i=0; 3 ; i+) if(aiai+1) temp=ai; 4 ; 5 ; 3. 下面的函數(shù)Sort()用于對整數(shù)數(shù)組array按升序排序的選擇排序算法,其中參數(shù)n表示array數(shù)組中存儲的數(shù)組元素數(shù)。例如,假設數(shù)組array中有10個元素,選擇排序就是:先將10個數(shù)中的最小數(shù)與a0對換;再將a1到a9中的最小數(shù)與a1對換,.,直到排序完成。請完成該函數(shù)。 void Sort( int array, int n) int k; 1 ; for(int i=0; in-1; i+) 2 ; for(int j= 3 ; jn; j+) if(arrayjarrayk)k=j; t=arrayk; 4 ; 5 ; 4. 以下程序的功能是求三角函數(shù)sinx的近似值,其計算精度為0.000001。已知求sinx近似值的計算公式為: )!12()1(!7!5!31sin)12(1753.nxxxxxxnn.其中,x的值為弧度。 當輸入的x 值為度數(shù)時,將度數(shù)轉化為弧度的公式為: 1801415926.3xy.#include#include double sin(double x,double eps) double term,sum,y; int n=1; y=x*x; term= 1 ; sum=0; while(fabs(term)=eps) sum += 2 ; n+; term = term*y/ 3 ; term *= -1; return 4 ; void main(void) double x,y; coutx; while(x360)x -= 360; y= 5 ; cout角度為x的sin值是sin(y,1e-6)n; 1. 1x-y 2x=x-y或x-=y 2. 1int temp 2psize 3isize-p 4ai=ai+1 5ai+1=temp 3. 1int t; 2k=i 3i+1 4arrayk=arrayi 5arrayi=t 4. 1x 2term 3(2*n-1)*(2*n-2) 4sum 53.1415926*x/180 四、程序填空題 1. 統(tǒng)計字符串中英文字母個數(shù)的程序。 #include using namespace std; int count (char str); void main() char s180; coutEnter a line:; 1 ; coutcount=count(s1)=a & stri=A & stri=Z) 2 ; return 3 ; 2.如果矩陣A乘以B得到C,則必須滿足如下的規(guī)則: 矩陣A的列數(shù)等于矩陣B的行數(shù); 矩陣A的行數(shù)等于矩陣C的行數(shù); 矩陣B的列數(shù)等于矩陣C的列數(shù); 矩陣相乘的乘法公式為: .nkkjikijbac1下面的函數(shù)MultiMatrix()用于求解整數(shù)矩陣的乘積,其中參數(shù)a、b和c分別表示存儲乘數(shù)、被乘數(shù)以及乘積結果的二維數(shù)組,arow和acol、brow和bcol以及crow和ccol分別表示矩陣a的行數(shù)和列數(shù)、矩陣b的行數(shù)和列數(shù)以及矩陣c的行數(shù)和列數(shù),且該函數(shù)被調用時的實參滿足:acol=4、bcol=5及ccol=5。MultiMatrix()函數(shù)當提供的矩陣不滿足矩陣相乘的條件時該函數(shù)返回1,否則返回0,請完成該函數(shù)。 int MultiMatrix(int a4, int arow, int acol, int b5, int brow, int bcol, int c5, int crow, int ccol) if(acol!=brow)return 1; if( 1 )return 1; if( 2 )return 1; for(int i=0; icrow; i+) for(int j=0; 3 ; j+) 4 ; for(int n=0; 5 ; n+) cij+=ain*bnj; return 0; 3. 下面的函數(shù)fun未使用中間變量實現(xiàn)對兩個數(shù)的交換,請完成下列函數(shù)的定義。插入排序是通過把數(shù)組中的元素插入到適當位置來進行排序的。插入排序的步驟為: (1)將數(shù)組中的頭兩個元素按排序順序排列 (2)把下一個元素(第3個元素)插入到其對應于已排序元素的排序位置 (3)對于數(shù)組中的每個元素重復(2),即把第4個元素插入到適當文職,然后是第5個,等等,直到所有元素都插入排序完成 下面的程序利用了插入排序函數(shù)isort()進行排序,并在主函數(shù)中將排序前和排序后的數(shù)組元素打印,請將程序補充完整。 #include void isort(int a,int size) /*a為被排序數(shù)組,size為a中包含的元素個數(shù)*/ int inserter,index; for(int i=1;i=0 & 2 ) aindex+1=aindex; index-; aindex+1= 3 ; void main() int array=55,2,6,4,32,12,9,73,26,37; int len= 4 ; for(int i=0;ilen;i+) coutarrayi,; coutendl; isort(array, 5 ); for(int i=0;ilen;i+) coutarrayi,; couts1 2 num+; 3 num 2. 1crow!=arow 2ccol!=bcol 3jccol 4cij=0 5nacol 3. 1size 2inserteraindex 3inserter 4sizeof(array)/sizeof(int) 5len 五、程序填空題 1. 下面程序要利用指針變量作為形參實現(xiàn)兩個變量的值互換,請在下面的下劃線處填入正確的程序代碼,完成程序功能。 #include using namespace std; int main() void swap(int *,int *); int i=5,j=8; 1 ; couti jendl; return 0; void swap(int *p1,int *p2) int temp; temp=*p1; 2 ; 3 ; 2. 下面的函數(shù)Fun將一個整數(shù)字符串轉換為一個整數(shù)。 # include # include using namespace std; int Fun (char *str) int num, digital, len; 1 ; len=strlen(str); while (*str!=NULL) digital=*str-0; for (int i=0; 2 ; i+) 3 ; len-; str+; num+=digital; return num; 3. 以下程序求二維數(shù)組的最大值及其行列下標并打印。 #include #include using namespace std; void find( int a34, int *maxi, int *maxj ) int i, j; *maxi=0; *maxj=0; for (i=0; i3; i+) for (j=0; j4; j+) if (aij 1 a*maxi*maxj) *maxi=i, *maxj=j; void main() int a34=3,8,9,5,0,-1,1,-2,3,7,6,3, maxp, minp, i, j; find( 2 ); coutmax= 3 endl; couti=maxpendl; coutj=minpendl; 4. 下面程序要利用形參實現(xiàn)兩個變量的值互換,請在下面的下劃線處填入正確的程序代碼,完成程序功能。 #include using namespace std; int main() void refswap(int &,int &); int a=15,b=18; 1 ; /實參是變量的地址 couta bendl ; /i和j的值已互換 getchar(); return 0; void refswap(int &a,int &b) int temp; temp=a; 2 ; 3 ; 5. 下面的Max函數(shù)用于求給定矩陣a中的最大元素值,以及其所在的行號和列號。 #include using namespace std; void Max(int *a,int m,int n) if (m=0 | n=0) return; int i,j,row=0,column=0,max; max=*a; for (i=0;im;i+) for (j=0;jn;j+) if ( 1 ) max= 2 ; row=i; column=j; coutmax=max,row=row,column=columnendl; int main() int a34=5,12,23,56,19,28,37,46,-12,128,6,8;Max( 3 , 3, 4); getchar(); return 0; 1.1 swap(&i,&j) 2 *p1=*p2 3 *p2=temp 2.1 num=0 2 i 2 a,&maxp,&minp 3 amaxpminp 4.1 refswap(a,b) 2 a=b 3 b=temp 5.1 *(a+i*n+j)max 2 *(a+i*n+j) 3 &a00 五、程序填空題 1. 假設學生鏈表中的結點結構及含義定義如下: struct Student long number; /學號 Student *next; /指向下一個結點的指針 ; 下面的函數(shù)Delete()是從鏈表中將指定學號的學生結點刪除,它有兩個參數(shù):head是學生鏈表的鏈首指針,number是被刪除結點的學生學號,請完成該函數(shù)。 void Delete(Student *head, long number) Student *p; if( 1 ) return; if(head-number = number) 2 ; head=head-next; delete p; return; for(Student *s=head; s-next; 3 ) if( 4 ) p=s-next; s-next=p-next; delete p; 5 ; coutnumber” is not found!”endl; 2. 下面的函數(shù)rotate()實現(xiàn)將二維數(shù)組m參數(shù)表示的方陣進行順時針旋轉90。例如,它將數(shù)組 11 22 33 44 55 66 77 88 99 變?yōu)椋?77 44 11 88 55 22 99 66 33 請將程序補充完整。 1 int SIZE=4; 2 int MatrixSIZESIZE; void rotate(Matrix m) 3 ; for(int i=0; iSIZE;i+) for(int j=0; jSIZE;j+) tempij=m 4 i; for(int i=0; iSIZE; i+) for(int j=0; jSIZE;j+) mij= 5 ; 1.1 swap(&i,&j) 2 *p1=*p2 3 *p2=temp 2.1 num=0 2 i 2 a,&maxp,&minp 3 amaxpminp 4.1 refswap(a,b) 2 a=b 3 b=temp 5.1 *(a+i*n+j)max 2 *(a+i*n+j) 3 &a00 五、程序填空題 1. 下面的C+程序中,定義了一個描述時間的類Time,請在下劃線處填入正確的程序代碼。 #include using namespace std; class Time public: void set_time(); void 1 ; private: int hour; int minute; int 2 ; ; void Time:set_time() cinhour; cin 3 ; cinsec; void Time:show_time() couthour:minute:secendl; 2. 以下是一個采用類結構的方式求n!的程序,請?zhí)羁胀瓿沙绦颉?#include using namespace std; class Factorial private: int n,fact; public: Factorial(int); void Calculate(); void Display(); ; Factorial:Factorial(int val) n=val; 1 ; void Factorial:Calculate () int i=n; while(i1) 2 ; void Factorial:Display () coutn!=factendl; void main() int n; coutn; 3 ; A.Calculate (); A.Display (); 3. #include using namespace std; class MArray 1 : /填寫訪問屬性 void set_value(); /輸入10個整數(shù)并存放在數(shù)組array中 void max_value(); /求數(shù)組中的最大值 void show_value() coutmax=max; private: int array10; int max; ; void MArray:set_value() int i; for (i=0;i10;i+) 2 ; void MArray:max_value() int i; max=array0; for (i=1;imax) 3 int main() MArray arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); 4. 使類完整 1 A int *a, n; public: A():a(0),n(0) A(int nn) 2 /用nn初始化n 3 /用a指向長度為n的動態(tài)數(shù)組空間 ; 5. 下面的C+程序定義了一個找出整型數(shù)組中元素的最大值的類Array_M,請在下劃線處填入正確的程序代碼。 class Array_M public: void set_value(); void 1 ; void show_value(); private: int arrayd20; int max; ; void Array_M:set_value() int i; for (i=0;iarraydi; void Array_M:max_value() int i; max=arrayd0; for(i=1;imax) max= 2 ; void Array_M: 3 coutMax=max; 6. 以下是Box類的定義,其中height是靜態(tài)數(shù)據(jù)成員。要求程序輸出為: 10 10 3000 源程序如下: #include using namespace std; class Box int width, length; public: Box(int,int); int volume(); 1 ; /將height定義為靜態(tài)整形數(shù)據(jù)成員 ; 2 /在類體外定義構造函數(shù),寬度width和 /長度length的缺省值均為10 width=w;length=len; int Box:volume() return (height*width*length); int Box:height=10; void main() Box a(15,20); couta.heighta=this-b=0;this-x=this-y=0; else this-a=a;this-b=b;this-x=x;this-y=y; int IsSquare() /判斷是否為矩形,若是返回1,否則返回0 if ( 1 ) return 1; else return 0; void Move(int xx,int yy) /按左上角將矩形移動到(xx,yy)位置 2 ; 3 ; ; 1. 1 show_time() 2 sec 3 minute 2.1 fact=1 2fact*=i- 3Factorial A(n) 3. 1public 2cinarrayi 3max=arrayi; 4. 1class 2n=nn; 3a=new intn; 5. 1max_value() 2arraydi 3show_value() 6. 1static int height 2Box:Box(int w=10, int len=10) 3coutBox:heightendl 7.1fabs(a-x)=fabs(b-y) 2 x+=xx-a; 3 y+=yy-b; 五、程序填空題 1. 下面函數(shù)聲明類comp,用友元函數(shù)重載運算符+。 1 comp public: int real,imag; comp(int r=0,int i=0)real=r;imag=i; friend comp operator +(comp &,comp &); ; comp 2 int r,i; 3 ; i=x.imag+y.imag; return comp(r,i); 2. 聲明復數(shù)的類complex,用友元函數(shù)重載運算符-。 class complex public: int real, imag; complex(int r=0, int i=0) real=r; imag=i; 1 complex operator -(complex &,complex &); ; complex operator -(complex &a, complex &b) int r=a.real -b.real; int i= 2 ; return 3 ; 3. #include using namespace std; lass point private: float x,y; public: point(float xx=0, float yy=0) x=xx; y=yy; point() bool operator=(point &); bool operator!=(point &); point operator+=(point &); point operator-=(point &); float get_x() return x; float get_y() return y; ; bool point:operator=(point &p) if ( 1 ) return 1; else return 0; bool point :operator!=(point &p) if ( x!=p.get_x() & y!=p.get_y() ) return 1; else return 0; point point:operator+=(point &p) this-x+=p.get_x(); this-y+=p.get_y(); return 2 ; point point:operator-=(point &p) this-x-=p.get_x(); 3 return *this; void main() point p1(1,2), p2(3,4), p3(5,6); coutp1=p2? (p1=p2)endl; coutp1=p2? (p1!=p2)endl; p3+=p1; coutp3+=p1,p3: p3.get_x (),p3.get_y()endl; p3-=p1; coutp3+=p1,p3: p3.get_x (),p3.get_y()endl; 運行結果為: p1=p2? 0 p1!=p2? 1 p3+=p1,p3: 6,8 p3+=p1,p3: 5,6 4. 下面的Time類重載運算符+,使之滿60秒進一分鐘,此時秒又從0開始計時,請在下劃線處填入正確的程序代碼。 #include using namespace std; class Time public: Time() minute=0;sec=0; Time(int m,int s):minute(m),sec(s) 1 ; void display() coutminute:sec=60) sec= 2 ; minute= 3 ; return *this; 1.1class 2operator +(comp &x, comp &y) 3r=x.real+y.real; 2.1 friend 2a.imag-b.imag 3complex(r,i) 3.1x=p.get_x() & y=p.get_y() 2(*this) 3this-y-=p.get_y(); 4.1Time operator+() 2sec-60 3minute+1 五、程序填空題 1. #include using namespace std; class vehicle protected: int size; int speed; public: void setSpeed(int s) speed=s; 1 getSpeedLevel() return speed/10; ; class car :public vehicle public: int getSpeedLevel() return speed/5; ; class truck :public vehicle public: int getSpeedLevel() return speed/15; ; int maxSpeedLevel(vehicle 2 , vehicle 3 ) if (v1.getSpeedLevel()v2.getSpeedLevel() return 1; else return 2; void main() truck t; car c; t.setSpeed(130); c.setSpeed(60); coutmaxSpeedLevel(t,c)endl; /此結果輸出為2 2. A為抽象類,輸出為: this is class B printing this is class C printing 源程序為: #include using namespace std; class A public: 1 ; ; class B :public A public: void printMe() 2 this is class B printingendl; ; class C :public B void printMe()coutthis is class C printingendl; ; void print( 3 ) a.printMe(); void main() B b; C c; print(b); print(c); 3. 下面程序的輸出結果為0,56,56。 #include using namespace std; class base public:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學美術學科培訓
- ICU護理學習文獻匯報
- 電梯安全知識教育
- 建筑企業(yè)質量安全月培訓
- 海關監(jiān)管體系課件
- 個人舞蹈教室租賃合同模板
- 罐頭食品HACCP體系評估與優(yōu)化合同
- 企業(yè)股權收購撤銷及利益分配合同
- 餐飲行業(yè)食品安全事故處理協(xié)議
- 知名餐飲品牌總經(jīng)理任職及品牌推廣合同
- 國開學習網(wǎng)山東開大《行政復議法》形成性考核1-3答案
- (2024)湖北省公務員考試《行測》真題及答案解析
- 公益性公墓建設實施方案(3篇)
- 2023年貴州貴州賴茅酒業(yè)有限公司招聘考試真題
- 合并財務報表格式(2019版)
- 心臟射頻消融術
- 《商務郵件禮儀》課件
- 《配電自動化系統(tǒng)》課件
- 創(chuàng)業(yè)基礎理論與實務(寧波財經(jīng)學院)知到智慧樹章節(jié)答案
- 《某飛機場物業(yè)管理服務方案》
- 《讓子彈飛》電影賞析
評論
0/150
提交評論