C++语言程序设计
1.(32.0分)
编写一个程序求和,从1加2的平方加2的立方加...,一直加到2的n次方。
n的值从键盘输入。
2.(36.0分) 编写一个程序求一元二次方程的解。 讨论下述情形:
(1)a=0,不是二次方程。 (2)b^2-4ac=0,有两个相等实根。 (3) b^2-4ac>0,有两个不等实根。 (4) b^2-4ac<0,有两个复根(表示成x+yi,x-yi)。
3.(32.0分)
编一程序,求出所有各位数字的立方和等于1099的三位数。
1.(34.0分) 定义一个Person类,它的每个对象表示一个人。数据成员必须包含姓名、出生年份、死亡年份,一个默认的构造函数,一析构函数,读取数据的成员函数,一个print()成员函数显示所有数据。
2.(32.0分)
定义一个Point类来处理三维点points(x,y,z).该类有一默认的constructor,一copy constructor, 一negate()成员函数将point的x,y和z值各乘-1, 一norm()成员函数返回该点到原点(0,0,0)的距离,一个print()成员函数显示x,y,和z的值。
3.(34.0分)
定义一个Shape抽象类,由它派生出Rectanglr和Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( ) 函数计算对象的周长。
1.(30.0分) 4. 以下scat函数将字符串str2连接到字符串strl之后。运行时, 若输入:Li Ming,回车 Good morning!回车 则输出:Li Ming,Good morning!
请填空完成程序。
#include "iostream.h"
#include”stdio.h”
scat( char *strl, char *str2)
{ while(*strl !=___10_____) strl++; while( *strl++ =___11______); /*将str2连接到strl的后而*/
}
main()
{ char a,b;
gets( a ); //从键盘输入一字符串放入a,
gets( b ); //字符串中可包含空格
scat( a, b );
cout<} 5. 以下程序是用来输出如下图形:
#
*#*
#*#*#
*#*#*#* #*#*#*#*#
#include"iostream.h"main(){ int i, j; for( i=1; i<=5; i++ ) { for( j=1; j<=5-i; j++ ) cout<<" " for( j=1; j<=2*i-1; j++ ) if (__12____ )cout<< "*" ; else cout<< "#" ; cout<< "\n" }} 6. 以下程序是用来输入5个整数,并存放在数组中,找出最大数与最小数所在的下标位置,并把二者对调,然后输出调整后的5个数。#include"iostream.h"main(){ int a, t, i, maxi, mini; for( i=0; i<5; i++) cin>> a; mini=maxi=___13____; for( i=1; i<5; i++) { if (___14____) mini=i; if( a>a ) ___15____; } cout<< "最小数的位置是:"<< mini<<”\n”; cout<< "最大数的位置是:"<< maxi<<”\n”; t=a; ____16______;
a=t;
cout<< "调整后数的数为:"
for( i=0; i<5; i++ ) cout<cout<<"\n"}
2.(30.0分) 7.计算下列分段函数,X由键盘输入。 0 (x<=-10.0) y= x (-10.010.0)#include"stdio.h"main(){ float x,y; if(x<=-10.0)y=0; else if ( 17 )y=x; else y=2*x-3; printf(“%f\n”,x);} 9.invert()函数的功能是将一个字符串str的内容倒序存放例如: 字符串str原内容为:abcde,函数调用后变为:edcba。
#include
#include
void invert (char str[ ]) {int i,j, k; j=_____18_____; for(i=0; i19______; } } void main() { char test[]="abcde" invert (test); cout<}9. .下列程序打印出1000以内的所有“完全数”。“完全数”是指一个正整数,其所有小于该数的因子之和等于该数本身。例如:6=1+2+3,又如:28=1+2+4+7+14。#include“iostream.h”#include”iomanip.h”voidmain(){int i,j,s; for (j=2; j<=1000; j++) { s=0; for (i=1; i20____)s+=i; if (___21____) cout< 10.在下面程序的横线处填上适当的语句,使该程序执行结果为10。
#include
class MyClass
{
public: ___22____ //为x置值 ___23____ //取x值
private: int x;
};
void main()
{ MyClass my(10);
cout << my.GetNum() << endl;
}
3.(40.0分) 1 完成下面的类定义。
class MyClass
{
public: MyClass( ) { x = 0; } ___1 ___ int GetNum(______2____ my);
private: int x;
};
int GetNum(____3____ my)
{ return my.x;
}
2下面的类定义了拷贝构造函数,请完成该类的定义和实现。
class MyClass
{
public:
MyClass(int xx = 0, int yy = 0) {X = xx; Y = yy;}
____4_______; //拷贝构造函数
private: int X, Y;
};
myClass::______5______ //拷贝构造函数的实现
{
X=____6______;
_____7 ______; } 3. 以下程序显示如下所示的矩阵,矩阵中每个元素形成的规律是:右上三角阵(含 对角线)元素值为1,其它元素值为:行下标—列下标+1。 1 1 1 1 1 2 1 1 1 1 3 2 1 1 1 4 3 2 1 1
5 4 3 2 1
#include”iostream.h”#include”iomanip.h”main(){inti,j, a; for( i=0; i<=4; i++ ) for(j=0; j<5; j++ ) if(___8____)a = 1; else __9___=i-j+l; for(i=0; i<5; i++) { for(j=0; j<5; j++) cout<
我的答案:
下载了几个学期了,都满分,支持!
页:
[1]