使用VC6打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(intn)返回1,2,3,…,n的和。其中n大于0。
程序要求使用递归实现上述功能。
注意:不能修改程序的其他部分,只能补充sum函数。
试题程序:
#include
#include
intsum(intn)
{
}
voidmain()
{
cout$amp;
endl;
return;
}
程序要求使用递归实现上述功能。
注意:不能修改程序的其他部分,只能补充sum函数。
试题程序:
#include
#include
intsum(intn)
{
}
voidmain()
{
cout$amp;
endl;
return;
}
2
源程序文件test36_1.cpp清单如下:
include<iostream.h>
class amount;
class coins
{
/***************** found *****************/
enum units {penny, nickel, dime, quarter, half_dollar}
/***************** found *****************/
class amount;
};
class amount
{
/***************** found *****************/
coins:units money;
public:
void setm();
int germ();
};
void amount::setm()
{
money = coins::dime;
}
int amount::getm()
{
return money;
}
int main ()
{
amount ob;
ob.setm();
cout << ob.getm()<<end1;
return 0;
}
n=2
n=5
n=7
源程序文件test13_1清单如下:
include<iostream.h>
class Sample
{
int n;
public:
/*************** found ************+***/
Sample()
Sample(int i){n=i;}
/***************** found ********+**********/
void add(Sample s1,Sample s2)
/***************** found ****************/
{
this.n=s1->n+s2.n;
}
void disp(){cout<<"n="<<n<<endl;}
};
void main()
{
Sample s1(2),s2(5),s3;
s3.add(&s1,s2);
s1.disp();
s2.disp();
s3.disp();
}
2 is a factor of 10
源程序文件test38_1.cpp清单如下:
include <iostream.h>
class myclass
{
/***************** found *****************/
int n,d
public:
myclass(int i, int j){ n=i; d=j; }
friend int factor(myclass ob);
};
/***************** found *****************/
int myclass::factor(myclass ob)
{
/***************** found *****************/
if(ob.n%ob.d)
return 1;
else
return 0;
}
void main()
{
myclass ob1(10,2),ob2(13,3);
if(factor(ob1))
cout<<"2 is a factor of 10\n";
if(factor(ob2))
cout<<"3 is not a factor of 13\n";
}
函数中的错误,使该程序的输出结果为:
Values are: 1,2 and 3
源程序文件test26_1.cpp清单如下;
include <iostream.h>
class CommonBase
{
public:
int x;
};
/*****************found*****************/
class DeriveCommonA::public CommonBase
{
public:
int y;
};
class DeriveCommonB:public CommonBase
{
public:
int z;
};
/*****************found*****************/
class Overlapping:public DeriveCommonA; public DeriveCommonB
{
public:
void Display()
{
cout<<"Values are: "<<DeriveCommonA::x<<", "<<y<<" and "<<z<<end1;
}
};
int main ()
{
Overlapping ov;
/*****************found*****************/
ov.x=1;
ov.y=2;
ov.z=3;
ov.Display();
return 0;
}
400
40
源程序文件test35_1.cpp清单如下:
include <iostream.h>
class Student
{
public:
Student(int xx){x=xx;}
virtual float calcTuition();
/***************** found *****************/
private:
int x;
};
float Student::calcTuition()
{
return float(x'x);
}
/***************** found *****************/
class GraduateStudent::public Student
{
public:
/***************** found *****************/
GraduateStudent(int xx) ::Student(xx){}
virtual float calcTuition();
};
float GraduateStudent::calcTuition()
{
return float(x*2);
}
void main()
{
Student s(20);
GraduateStudent gs(20);
cout<<s.calcTuition()<<end1;
cout<<gs.calcTuition()<<end1;
}
5
5
源程序文件test28_1.cpp清单如下:
include <iostream.h>
class A{
public:
/***************** found *****************
static int n=1;
A(){n++;};
~A() (n--;};
};
/***************** found *****************
int n=0;
int main() {
A a;
A b[3];
A *c=new A;
c=&a;
/***************** found *****************
cout<<c.n<<end1;
cout<<A::n<<end1;
return 0;
}
使用VC6打开考生文件夹下的工程test18_1,此工程包含一个源程序文件(est18_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Enter x and y:
-4 -5
xoffset=1
yoffset=1
angle=45
radius=1.41421
源程序文件test18_1.cpp 清单如下:
include <iostream.h>
include <math.h>
class point
{
public:
void set(double ix,double iy)
{
x=ix;
y=iy;
}
double xoffset()
{
return x;
}
double yoffset()
{
return y;
}
double angle ()
{
return (180/3.14159)*atan2(y,x);
}
/**************** found *******************/
inline radius ()
{
return sqrt(x*x+y*y);
}
protected:
double x;
double y;
};
void main()
{
point p;
double x,y;
cout<<"Enter x and y:\n";
cin>>x>>y;
p.set(x,y);
/**************** found *******************/
p.x+=5;
p.y+=6;
/**************** found *******************/
cout<<"xoffset="<<p.xoffset()<<end1;
cout<<"yoffset="<<p.yoffset()<<end1;
cout<<"angle="<<p.angle()<<end1;
cout<<"radius="<<p.radius()<<end1;
The first result is 5
The second result is 2
The third result is 2
The fourth result is 6
源程序文件test40_1.cpp清单如下:
include <iostream.h>
int subtraction (int a, int b)
{
int r;
r=a-b;
/***************** found ************************/
return &r;
}
int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is "<< z << '\n';
cout << "The second result is "<< subtraction(7,subtraction (7,2)) << '\n';
/**************** found *************************/
cout << "The third result is "<< subtraction (&x,&y) << '\n';
/***************** found ************************/
z= 4 + *subtraction (x,y);
cout << "The fourth result is "<< z << '\n';
return 0;
}
源程序文件RevMain3.cpp清单如下:
//RevMain3.cpp
include<iostream>
using namespace std;
class MyClass{
public:
/* * * * * * * * *found * * * * * * + * * */
void MyClass(int a){ value=a;}
int Max(int x,int y)
{
if(x>y)
return x>y?x:y;
else
return y>value?y:value;
}
/* * * * * * * * *found * * * * * * * * * */
~MyClass(int a)
{
value=a;
}
private:
int value;
};
int main()
{
MyClass my(10);
cout<<my.Max(20,30)<<end1;
return 0;
}
x=20
源程序文件RevMain4.cpp的清单如下:
//RevMain4.cpp
include<iostream>
using namespace std;
class MyClass
{
public:
MyClass(int a)
{
x=a;
}
void setX(int a)
{
x=a;
}
void print()
{
cout<<"x="<<x;
}
private:
int x;
};
int main()
{
const MyClass obj (10);
/ ************found**************/
obj.setX(20);
/ ************found**************/
obj.print();
return 0;
}
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!