admin 管理员组文章数量: 887021
2023年12月17日发(作者:mani词根)
编程题和读程序写结果复习题
一、读程序写结果
1、(数组、控制语句与指针)
#include
using namespace std;
void main ()
{
int a [6],*p,i;
p=&a[2];
for(i=0;i<6;i++)
a[i]=i;
for(i=0;i<4;i++,p++)
cout<<*p;
2、(构造函数重载)
#include
using namespace std;
class MyClass
{
public:
MyClass();
MyClass(int);
~MyClass();
void Display();
protected:
int number;
};
MyClass::MyClass()
{
number=0;
cout<<"Constructing normallyn";
}
MyClass::MyClass(int m)
{
number=m;
cout<<"Constructing with a number:"< bdsfid="98" p="">
}
void MyClass::Display()
{
cout<<"Display
}
MyClass::~MyClass()
{
cout<<"Destructingn";
}
void main()
{
MyClass obj1;
MyClass obj2(20);
y( );
y( );
}
3、
#include(二维数组)
#include
using namespace std;
void main(){
int b[][5]={{11,12,13,14,15},
a number :"< bdsfid="103" p="">
{21,22,23,24,25},
{31,32,33,34,35}};
int(*pb)[5];
pb=b;
for(int i=0;i<3;i++,pb++)
{
for(int j=0;j<5;j++)
cout<<*(*pb+j)<<”,”;
cout<
}
}
4、(数组作为函数参数)
#include
#include
using namespace std;
void fun(char s[],int k);
void main(void)
{ char a[]="abcde";
int n ;
n=strlen(a);
fun(a,n);
}
void fun(char s[],int k)
{ int x,y;
char c;
x=0;
for(y=k-1;x
{
c=s[y];s[y]=s[x];s[x]=c;
x++;
}
}
5、(继承关系中构造函数的调用次序)
#include
using namespace std;
class base
{
int n;
public:
base(int a)
{
cout<<"constructing base class"< p="">
n=a;
cout<<"n="<
}
~base(){cout<<"destructing base classes"<<="">
class sub:public base
{
int m;
public:
sub(int a,int b):base(a)
{
cout<<"constructing sub class"< p="">
m=b;
cout<<"m="< p="">
}
bdsfid="190"
~sub(){cout<<"destructing sub class"<<="">
void main()
{
sub s(3,4);
}
6、(静态局部变量)
#include
using namespace std;
void test();
void main()
{
for(int i=0;i<4;i++)
test();
}
void test()
{
int i=0;
static int j=0;
i++; j++;
cout<
p="">
}
7、(静态数据成员)
#include
using namespace std;
class sample
{
int n;
static int sum;
public:
bdsfid="214"
sample(int x){n=x;}
void add(){sum+=n;}
void disp()
{
cout<<"n="< p="">
}
};
int sample::sum=0;
void main()
{ sample a(2),b(3),c(4);
(); ();
(); ();
(); ();
();
();
}
8、(全局变量和局部变量)
#include
using namespace std;
int a=5;
void main() {
int a=10,b=20;
{ int a=0,b=0;
for(int i=1; i<=3; i++)
{ a+=i; b+=a; }
}
cout <<::a<<' '<
p="">
}
9、(常量、数组与指针)
#include
using namespace std;
const int m=10;
void main()
{ int a[m],av=0;
int *p=a;
for(int i=0;i<10;i++)
a[i]=2*i;
av=av+*p;
av=av/m;
cout<<"av="< p="">
}
10、(引用)
#include
using namespace std;
void f(int &a)
{ cout << "a = " << a << endl;
a = 5;
cout << "a = " << a << endl;} void main()
{
int x = 47;
f(x);
cout << "x = " << x << endl;
}
11、(虚函数)
#include
bdsfid="273"
using namespace std;
class Col
{ public:
Col() { }
~Col() { }
virtual void f()
{ cout<<"Col class f "<<="" p="" void="">
{cout<<"Col class g "<<="">
class Red: public Col
{ public:
~Red() { }
void f(){cout<<"Red class fn";}
void g(){cout<<"Red class gn";} };
void main()
{ Col c;
Red r;
Col *cp;
cp=&c;
cp->f();
cp->g();
cp=&r;
cp->f();
cp->g();
}
12、(引用)
#include
int a[]={1,2,3,4,5};
int &index(int);
void main(){
for(int i=1;i<5;i++)
index(0)+=index(i);
cout< p="">
}
int &index(int i){
return a[i];
}
13、(拷贝构造函数)
#include
using namespace std;
class Test
{
public:
Test(int temp)
{
p1=temp;
cout<<"The constructor is called!"< p="">
}
Test(Test&s){
cout<<"The
}
protected:
int p1;
};
Test f(Test f){
return f;
}
void main()
copy constructor is called!"< bdsfid="346" p="">
bdsfid="326"
{
Test a(99);
Test b=a;
Test c=f(b);
}
二、编程题
1.递归与非递归实现阶乘
#include
int f(int n)
{
if(n==1) return 1;
else return n*f(n-1);
}
void main()
{
long sum=0;
for(int i=1;i<=5;i++)
sum+=f(i);
cout<<"1!+2!+...+5!="< p="">
}
非递归
#include
int f(int n)
{
int m=1;
for(int j=1;j<=n;j++)
m=m*j;
return m;
}
bdsfid="375"
void main()
{
long sum=0;
for(int i=1;i<=5;i++)
sum+=f(i);
cout<<"1!+2!+...+5!="< p="">
}
2.请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height (高)。要求用成员函数实现以下功能:
1)由键盘分别输入3个长方体的长、宽、高;
2)计算长方体的体积;
3)计算长方体的表面积;
#include
//using namespace std;
class Box{
int l;
int w,h;
int v;
int s;
Box (int ll,int ww,int hh){l=ll;w=ww;h=hh;}
int volume(){
v=l*w*h;
return v;
}
int area(){
s=2*(w*h+h*l+w*l);
return s;
}
void print(){
bdsfid="393"
cout<
cout<
}
};
void main(){
int m,n,k;
cin>>m>>n>>k;
Box t(m,n,k);
();
();
();
}
3. 编写一个类:point,包含三个坐标x,y,z。这个类包括:构造函数,拷贝构造函数,析构函数,print函数,重载运算符-。在主函数中,定义对象tp1,tp2,tp3=tp2-tp1,并各个对象相应坐标。
#include
using namespace std;
class point
{
float x,y,z;
public:
point(){}
point(float i,float j,float k)
{
x=i;y=j;z=k;
}
point(point &h){
x=h.x;y=h.y;z=h.z;
};
~point(){cout<<"destructing is called"< bdsfid="448" p="">
void
print(){cout<<"x="< bdsfid="450" p="">
point operator-(point &p){
point temp;
temp.x=x-p.x;
temp.y=y-p.y;
temp.z=z-p.z;
return temp;}
};
void main()
{
point p1(1.0,2.0,3.0);
point p2(2.0,3.0,4.0);
point p3(p2);
point p4=p3-p1;
();
}
(向量) X=(x1,x2,x3) and Y=(y1,y2,y3), and the rule of
their +,-,*,++
is defined as the following:
X+Y=(x1+y1, x2+y2, x3+y3)
X-Y=(x1-y1, x2-y2, x3-y3)
X*Y=(x1*y1, x2*y2, x3*y3)
X++ (x1++,x2++,x3++)
Create the Vector class Vector, it include three data member,
the function member should include:
(1) constructor that initialize the data member.
(2) An overloaded operator + that can add two vectors.
(3) An overloaded operator - that can sub two vectors.
(4) An overloaded operator * that can multiply two vectors.
(5) a show function that can display the vector
And then make the operator++ function as a friend function
of Vector class. Write the main program, create two vector object,
one is (1,2,3) , the other is (4,5,6),then add them and multiply
them respectively.
#include
using namespace std;
class Vector{
int x,y,z;
public:
Vector(int xx=0,int yy=0,int zz=0):x(xx),y(yy),z(zz){
cout<<"The constructor is called!"< p="">
}
Vector operator+(Vector &v){
Vector temp;
temp.x=x+v.x;
temp.y=y+v.y;
temp.z=z+v.z;
return temp;
}
Vector operator-(Vector &v){
Vector temp;
temp.x=x-v.x;
temp.y=y-v.y;
temp.z=z-v.z;
return temp;
}
Vector operator*(Vector &v){
Vector temp;
temp.x=x*v.x;
temp.y=y*v.y;
temp.z=z*v.z;
return temp;
}
void print(){
cout< p="">
}
friend Vector operator++(Vector&v);
};
Vector operator++(Vector&v){
v.x++;
v.y++;
v.z++;
return v;
}
void main(){
Vector v1 (1,2,3),v2(4,5,6),v3;
v3=v1+v2;
();
v3=v1*v2;
();
cout<
}
5. #include
using namespace std;
class Shape
bdsfid="512"
{
public:
virtual double Area()=0;
};
class Triagle:public Shape
{
public:
Triagle(double h,double w){H=h,W=w;}
double Area()
{
return H*W*0.5;
}
private:
double H,W;
};
class Rectangle:public Shape
{
public:
Rectangle(double h,double w)
{
H=h;W=w;
}
double Area(){return H*W;}
private:
double H,W;
};
class Circle:public Shape
{
public:
Circle(double r){radius=r;}
double Area()
{
return radius*radius*3.1415;
}
private:
double radius;
};
void main(){
shape*v;
Triagle t(3.2,3.5);
v=&t;
v->area();
Rectangle r(3.2,3.5);
v=&r;
v->area();
Circle c(4.5);
v=&c;
v->area();
}
6.求100~200之间不能被3也不能被7整除的数,并依次输出
#include
using namespace std;
void main(){
for(int i=100;i<=200;i++){
if(i%3!=0&&i%7!=0)
cout<
}
}
7.编写函数分别求两个数的最大公约数和最小公倍数,并在主函数中分别调用这两个函数
#include
using namespace std;
int hcf(int x,int y){
while(x!=y){
if(x>y)
x=x-y;
else
y=y-x;
}
return y;
}
int icd(int x,int y){
return x*y/hcf(x,y);
}
void main(){
int a,b;
cin>>a>>b;
cout<<"最大公约数为:"< p="" }<="" 最小公倍数为:"<
版权声明:本文标题:编程题和读程序写结果复习题 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1702753125h429419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论