c++程序填空.docx_第1页
c++程序填空.docx_第2页
c++程序填空.docx_第3页
c++程序填空.docx_第4页
c++程序填空.docx_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

3.下列程序计算1000以内能被3整除的自然数之和,请完成程序。 #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. 下面的函数fun未使用中间变量实现对两个数的交换,请完成下列函数的定义。 void fun(int &x, int &y) x+=y; y= 1 ; 2 ; 2.下面的函数bubble()是对整数数组a按升序排序的冒泡算法,其中,参数a存储将被排序的数据,size是数组a中存储的元素数目,请完成该函数。 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. 下面的函数Sort()用于对整数数组array按升序排序的选择排序算法,其中参数n表示array数组中存储的数组元素数。例如,假设数组array中有10个元素,选择排序就是:先将10个数中的最小数与a0对换;再将a1到a9中的最小数与a1对换,.,直到排序完成。请完成该函数。 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. 以下程序的功能是求三角函数sinx的近似值,其计算精度为0.000001。已知求sinx近似值的计算公式为: )!12()1(!7!5!31sin)12(1753.nxxxxxxnn.其中,x的值为弧度。 当输入的x 值为度数时,将度数转化为弧度的公式为: 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. 统计字符串中英文字母个数的程序。 #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,则必须满足如下的规则: 矩阵A的列数等于矩阵B的行数; 矩阵A的行数等于矩阵C的行数; 矩阵B的列数等于矩阵C的列数; 矩阵相乘的乘法公式为: .nkkjikijbac1下面的函数MultiMatrix()用于求解整数矩阵的乘积,其中参数a、b和c分别表示存储乘数、被乘数以及乘积结果的二维数组,arow和acol、brow和bcol以及crow和ccol分别表示矩阵a的行数和列数、矩阵b的行数和列数以及矩阵c的行数和列数,且该函数被调用时的实参满足:acol=4、bcol=5及ccol=5。MultiMatrix()函数当提供的矩阵不满足矩阵相乘的条件时该函数返回1,否则返回0,请完成该函数。 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. 下面的函数fun未使用中间变量实现对两个数的交换,请完成下列函数的定义。插入排序是通过把数组中的元素插入到适当位置来进行排序的。插入排序的步骤为: (1)将数组中的头两个元素按排序顺序排列 (2)把下一个元素(第3个元素)插入到其对应于已排序元素的排序位置 (3)对于数组中的每个元素重复(2),即把第4个元素插入到适当文职,然后是第5个,等等,直到所有元素都插入排序完成 下面的程序利用了插入排序函数isort()进行排序,并在主函数中将排序前和排序后的数组元素打印,请将程序补充完整。 #include void isort(int a,int size) /*a为被排序数组,size为a中包含的元素个数*/ 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. 下面程序要利用指针变量作为形参实现两个变量的值互换,请在下面的下划线处填入正确的程序代码,完成程序功能。 #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. 下面的函数Fun将一个整数字符串转换为一个整数。 # 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. 以下程序求二维数组的最大值及其行列下标并打印。 #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. 下面程序要利用形参实现两个变量的值互换,请在下面的下划线处填入正确的程序代码,完成程序功能。 #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函数用于求给定矩阵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; /指向下一个结点的指针 ; 下面的函数Delete()是从链表中将指定学号的学生结点删除,它有两个参数:head是学生链表的链首指针,number是被删除结点的学生学号,请完成该函数。 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. 下面的函数rotate()实现将二维数组m参数表示的方阵进行顺时针旋转90。例如,它将数组 11 22 33 44 55 66 77 88 99 变为: 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!的程序,请填空完成程序。 #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个整数并存放在数组array中 void max_value(); /求数组中的最大值 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的动态数组空间 ; 5. 下面的C+程序定义了一个找出整型数组中元素的最大值的类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是静态数据成员。要求程序输出为: 10 10 3000 源程序如下: #include using namespace std; class Box int width, length; public: Box(int,int); int volume(); 1 ; /将height定义为静态整形数据成员 ; 2 /在类体外定义构造函数,宽度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. 下面函数声明类comp,用友元函数重载运算符+。 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. 声明复数的类complex,用友元函数重载运算符-。 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论