问答题请编写两个函数int sum_of_powers(int k,int n),powers(int m,int n),求1~6的k次方的和,sum_of_powers中参数k和n分别表示k次方和所求数列中最大的一个自然数,最后返回所求值,powers中参数m和n分别表示m为底数n为指数,最后返回所求值。要求使用for循环和函数嵌套(int sum_of_powers中调用powers)实现算法。输出结果如下: sum of 4 powers of intergers from 1 to 6=2275 注意:部分源程序已存在文件test25_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数sum_of_powers和powers的花括号中填写若干语句。 文件test25_2.cpp的内容如下: #include<iostream.h> const int k(4); const int n(6); int sum_of_powers(int k,int n),powers(int m,int n); void main() cout<<"sum of "<<k<<" powers Of intergers from 1 to "<<n<<"="; cout<<sum_of_powers(k,n)<<endl; int sum_of_powers(int k,int n) int powers(int m, int n)
问答题下列程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(inta[][N],intm),该函数的功能是使数组右上半三角元素中的值乘以m。例如,若m的值为2,a数组中的值为:则返回主程序后a数组中的值应为:注意:部分源程序在文件PROG1.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。试题程序:
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2 3 4 5 27 28 31 66 75
2 3 4 5 6 27 28 3l 66 75
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//IntegorSet.h
#ifndef INTEGERSET
#define INTEGERSET
#include <iostream>
using namespace std;
const int MAXELEMENTS = 100;
//集合做多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
public:
IntegerSet(): counter(0){}
//创建一个空集合
IntegerSet (int data[], int size);
//利用数组提的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元算
int getCount() const {return counter;}
//返回集合中元素的个数
int getElement (int i) const {return elem[i];} //返回集合中指定的元素
void show() const;
};
void WriteToFile(char *);
#endif
//main.cpp
#include "IntegerSet.h"
#include <iomanip>
IntegerSet:: IntegerSet (int data[], int size): counter(0) {
for(int i = 0; i < size; i++)
add (data[i]);
}
void IntegerSet::add (int element) {
int j;
//从后往前寻找第一个小于等于element的元素
for(j = counter; j > 0; j--)
if (element >= elem[j - 1])
break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j > 0)
if (element == elem[j - 1])
return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for (int k = counter; k > j; k--)
elem[ k] = elem[k - 1];
elem[j] = element;
//将element插入到该位置
counter ++; //计数器加1
}
void IntegerSet::remove (int element) {
// ********333********
// ********666********
}
void IntegerSet::show() const {
for(int i = 0; i < getCount (); i++)
cout << setw(4) << getElement(i);
cout << endl;
}
int main() {
int d[] = {5,28,2,4,5,3,2,75,27,66,31};
IntegerSet s(d, 11); s.show();
s.add(6); s.show();
s.add(19); s.show();
s.remove(2); s.show();
s.add(4); s.show();
WriteToFile(" ");
return 0;
}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。完成函数fun(char *s1,char *s2)的空出部分。函数fun(char *s1,char *s2)的功能是将在字符串s1中下标为偶数的字符,紧随其后重复出现一次,放在一个新串s2中,s2中字符按原字符串中字符的顺序排列。(注意0为偶数) 例如:当s1中的字符串为“abcdef”时,s2中的字符串应为“aaccee”。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include<iostream.h> void fun(char *s1,char *s2) void main() char sl[100],s2[l00]; cout<<"Please enter string string:"<<end1; cin.getline(s1,100); fun(s1,s2); cout<<"The result is:"<<s2<<end1; return;
问答题改错题
使用VC6打开考生文件夹下的工程test4_1,此工程包含一个源程序文件test4_1.cpp,但该程序在类的定义中存在问题,请改正类定义中的错误,使程序的输出结果如下:
a=3 b=13
a=5 b=13
注意:请勿修改主函数main中的任何内容。
源程序文件test4_1.cpp清单如下:
#include
class T
{
public:
/**********found**********/
T(int x){a=x; b+=x;}
/**********found**********/
void display(T c)
{ cout<<"a="<
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码。函数MergeAndSort (int s[],int e[],int a[],int m,int n)实现将两个数组合并。这两个数组已经有序,按照由小到大的顺序排列。
例如:
e[]={1,3,5,6},m是数组e的长度,即为4。
a[]={2,4,5,7,11,13},n是数组a的长度,即为6。
则执行的结果为:s[]={1,2,3,4,5,6,7,11,13}
补充函数fun(int s[],int e[],int a[],int m,int n),使之实现上述要求。
注意:请勿改动主函数。
#include <iostream. h>
void MergeAndSort (int s[], int e[],int a[],int m, int n)
{
}
int main ()
{
int data[20], i;
int a[] = {1,3,5,6};
int b[]={2,4,5,7,11,13};
cout
"a[]=";
for (i=0; i<4; i++)
cout
a[i]
",";
cout
endl;
cout
"b[]=";
for (i=0; i<6; i++)
cout
b[i]
",";
cout
endl;
MergeAndSort (data,a,b,4,6);
cout
"s[]=";
for (i=0; i<9; i++)
cout
data[i]
",";
cout
endl;
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中声明了SortedList类,是一个用于表示有序数据表的类。其成员函数insert的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个insert函数。程序的正确输出应为:
插入前:
1,2,4,5,7,8,10
插入6和3后:
1,2,3,4,5,6,7,8,10
要求:
补充编制的内容写在“// ********333********”与“// ********666********”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//SortedList.h
#include <iostream>
using namespace std;
class SortedList { //有序数据表类
int len;
double * d;
public:
SortedList (int len, double data[] = NULL);
~SortedList() {delete [] d;}
int length() const {return len;} //有序数据表长度(即元素的个数)
double getElement (int i) const {return d[i];}
void insert (double data);
void show() const; //显示有序数据表
};
void writeToFile (char *, const SortedList
//main.cpp
#include "SortedList.h"
SortedList::SortedList (int len, double data[]):len(len){
d = new double[len];
for(int k = 0; k < len; k++)
d[k] = (data == NULL? 0.0:data[k]);
for(int i = 0; i < len-1; i++) {
int m=i;
for(int j = i; j < len; j ++)
if(d[j] < d[m]) m=j;
if (m > i) {
double t = d[m];
d[m] = d[i];
d[i] = t;
}
}
}
void SortedList::insert (double data) {
// ********333********
// ********666********
}
void SortedList::show() const { //显示有序数据表
for(int i = 0; i < len - 1; i++)
cout << d[i] << ",";
cout << d[len - 1] << endl;
}
int main() {
double s[] = {5,8,1,2,10,4,7};
SortedList list(7,s);
cout << "插入前:" << endl;
list.show();
list.insert(6.0);
list.insert(3.0);
cout << "插入6和3后:" << endl;
list.show();
writeToFile(" ", list);
return 0;
}
问答题使用VC6打开
下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
10
6
30
2
2
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
class CMyClass
{
private:
int number;
int Add(int i)
{
return number+=i;
}
int Sub(int i)
{
return number-=i;
}
int Mul(int i)
{
return number*=i;
}
int Div(int i)
{
if (i!=0)
{
return number/=i;
}
else
return number;
}
//******error******
typedef int (FUNC) (int);
//******error******
FUNC func[];
public:
CMyClass ()
{
func[0]=CMyClass::Add;
func[1]=CMyClass::Sub;
func[2]=CMyClass::Mul;
func[3]=CMyClass::Div;
number=0;
}
int CallFunction(int i,int j)
{
//******error******
return (func[i]) (j);
}
};
void main ()
{
CMyClass myobj;
cout
myobj.CallFunction (0,10)
endl;
cout
myobj.CallFunction (1, 4)
endl;
cout
myobj.CallFunction (2,5)
endl;
cout
myobj.CallFunction (3,15)
endl;
cout
myobj.CallFunction (3,0)
endl;
}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类Rectangle和Circle的声明,二者分别是计算面积的函数GetArea()和计算对象周长的函数GetPerim()。程序中位于每个//************found************下的语句行有错,请加以改正。改正后程序的输出应该是: The area of the Circle is 78.5 The perimeter of the Circle is 31.4 The area of the Rectangle is 24 The perimeter of the Rectangle is 20 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动“//************found************”。 //源程序 #include<iostream> using namespace std; class Shape public: Shape() ~Shape() //************found************ ______float GetArea()=0; //************found************ ______float GetPerim()=0; ; class Circle: public Shape public: Circle(float radius):itsRadius(radius) ~Circle() float CetArea() return 3.14 *itsRadius *itsRadius; float CetPerim()return 6.28 *itsRadius; private: float itsRadius: ; class Rectangle: public Shape public: //************found************ Rectangle(float len, float width):______; ~Rectangle(); virtual float GetArea() return itsLength *itsWidth; float GetPerim()return 2*itsLength+2*itsWidth; virtual float GetLength() return itsLength; virtual float GetWidth()return itsWidth; private: float itsWidth; float itsLength; ; int main() //************found************ sp=new Circle(5); cout<<"The area of the Circle is"<<sp->GetArea()<<endl; cout<<"The perimeter of the Circle is"<<sp->GetPerim()<<endl; delete sp; sp=new Rectangle(4,6); cout<<"The area of the Rectangle is"<<sp->GetArea()<<endl; cout<<"The perimeter of the Rectangle is"<<sp->GetPerim()<<endl; delete sp; return 0:
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char*des,char*str)实现的功能是,如果字符串str里面有空格或者不可打印字符,则全部去掉,将转化后的字符串放在字符串des里,然后返回该字符串。
注意:不能修改程序的其他部分,只能补充convert()函数。
#include
#include
#define MAXLEN 1024
char*convert(char*des,char*
str)
{
}
void main()
{
char dest[MAXLEN];
char*str="AbcDeFhJK";
cout<
问答题用VC6打开考生文件夹下的源程序文件modi3.cpp,这个程序完成输出到屏幕一些特定的信息,但工程有错误或者不完整,请完成以下功能: (1)初始化m_Num2的值为j,请在注释//********1********后添加适当的语句。 (2)补充全局函数fun()使之能够调用类TestClass的成员变量,请在注释//********2********后添加适当的语句。 (3)初始化静态变量,请在注释//********3********后添加适当的语句。 (4)修改注释//********4********后的语句。 使得程序输出以下内容: m-Num1=1 m_Num2=2 m_Num3=1 m_Num1=4 m_Num2=2 m_Num3=7 注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream.h>class TestClass{public: //********1********TestClass(int i,int j) { m_Num1=i; m_Num3+=i; } void Print() { cout<<"m_Num1 = "<<m_Num1<<end1; cout<<"m_Num2 = "<<m Num2<<end1; cout<<"m_Num3 = "<<m Num3<<end1; ) void Add(int i) { m_Num3+:i; } private: int m_Num1; const int m_Num2, Static int m_Num3; //********2******** }; //********3******** void fun() { TestClass Num(1,2); Num.m Num1=4; //********4******** Num.Add(); Num.Print(); ) void main() { TestClass Num(1,2); Num.Print(); fun(); return; }
问答题使用VC6打开考生文件夹下的工程RevProj8。此工程包含一个源程序文件 RevMain8.cpp。在该文件中,函数resort的功能是:能在一个数列中,对从指定位置开始的几位数,按相反顺序重新排列,并在主函数中输出新的序列。 请改正程序中的错误,使它能得到正确结果。 注意,不要改动main函数,不得删行或增行,也不得更改程序的结构。 源程序文件RevMain8.cpp中的程序清单如下: //RevMain8.cpp #include <instream> using namespace std; void resort(int arr[],int where,int amount); int main () int number [20] ,where, arrount, i; cout<<"Input 20 numbers/n"; for (i=0; i<20; i++) cin>>number [i]; cout<<"How many do you want to sort: "; cin>>arrount; cout<<"/n where do you want to start: "; cin>>where; cout<<"old array as follow:/n"; for (i=0; i<20; i++) cout<<nmuber [i] <<" "; resort (number,where, arrount); cout<<"/n resorted array as follow:/n"; for (i=0; i<20; i++) cout<<number [i] <<" "; cout<<end1; return 0; void resort(int array[],int where, int amount) int *pi, *p2, temp; p1= p2= /* * * * *FOUND * * * * */ for (;p1< ) /* * * * *FOUND * * * * */ *p1=*p2; *p2=*p1; return;
问答题改错题
使用VC6打开考生文件夹下的工程kt7_1,此工程包含一个源程序文件kt7_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor1
Constructor1
Constructor1
Destructor
Constructor2
Destructor
x=0
x=5
Destructor
Destructor
源程序文件kt21_1.cpp清单如下:
#include
classB
{intx;
public:
B(){x=0;cout
B(inti){x=i;cout
~B(){cout
/**********found**********/
~B(inti){cout
voidprint(){cout
voidmain()
{B*ptr;
ptr=newB[2];
/**********found**********/
ptr[0]=B(0);
ptr[1]=B(5);
/**********found**********/
for(inti=0;i<2;)
ptr[i].print();
delete[]ptr;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程pmj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示: 读取输入文件… 一反转前一 STR1=ABCDEF STR2=ABCDEFG 一反转后一 STR1=FEDCBA STR2=GFEDCBA 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。//mgsering.h#include<iostream>#include<cstring>using namespace std;cl ass MyString{public: MyString(const char*S) { str=new char[strlen(s) +1]; strcpy(str,s); } 一NyString(){delete[]st;r;) void EeveEse(); friend ostream return os; }private: char*str; }; void writeToFile(char*,const NyString cout<<”…反转后…/n”; cout<<”STR1=:”<<str1 <<end1; cout<<”STR2=”<<Str2<<endl<<end1; writeToFile(pathname,str3); return 0;}
问答题请使用”答题”菜单或使用VC6打开考生文件夹pmjI下的工程pmjl,程序中位于每个//ERROR**********found**********水下的语句行有错误,请加以更正,不得修改程序的其他部分。更正后程序的输出应该是:ATme注意:只能修改每个//ERROR**********found**********下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class Choice{private:const char answer,public://ERROR**********found**********Choice(char C){answer=C;)一Choice(){)void check(char x){cout<<x<<endl;//ERROR**********found**********if(answer=x)cout<<“True”<<endl;elsecout<<“False”<<end1;}};int main(){//ERROR **********found**********Choice C=new Choice(‘A’);C一>check(’A’);delete C;return 0;}
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(cha* des,char* str)实现的功能是:
(1)如果字符串最后面有空格,则全部删除;
(2)转换后的数据放到des,并且指针作为返回值返回。
注意:不能修改程序的其他部分,只能补充convert0函数。
#include <iostream.h>
#include <ctype.h>
#define MAXLEN 1024
char* convert(char* des,char* str)
{
}
void main()
{
char dest[MAXLEN];
char *string="abc def";
cout
string
"<--"
endl;
cout
convert(dest, string)
"<--"
endl;
return;
}
问答题请编写一个函数fun(int x,int n),该函数返回x的n次幂的值,其中x和n都是非负整数。x的n次幂的计算方法是1与x相乘n次,如x的20次幂的计算为1与x相乘20次,
注意:部分源程序已存在文件test30_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。
如输入3和4,输出结果如下:
3 4
81
文件test30_2.cpp清单如下:
#include<iostream.h>
double fun(int x, int n)
{
}
void main ( )
{
int x,n;
cin>>x>>n;
cout<<fun(x,n)<<end1;
}
问答题请使用VC6或使用[答题]菜单打开
proj1下的工程proj1,此工程中含有一个源程序文件proj1. cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Constructor called.
The value is 10
Copy constructor called.
The value is 10
Destructor called.
Destructor called.
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1. cpp
#include<iostream>
using namespace std;
class MyClass {
public:
//ERROR **********found**********
MyClass(int i)
{value=i; cout<<"Constructor called."<<endl;}
//ERROR **********found**********
MyClass(const MyClass P)
{
value=P. value;
cout<<"Copy constructor called."<<endl:
}
void Print()
{cout<<"The value is"<<value<<endl:}
//ERROR **********found**********
void~MyClass()
{cout<<"Destructor called."<<endl;}
private:
int value;
};
int main()
{
MyClass obj1;
obj1. Print();
MyClass obj2(obj1);
obj2. Print();
return 0;
}
问答题综合应用题
使用VC6打开考生文件夹下的工程test4_3。此工程包含一个源程序文件test4_3.cpp,其对一个学校的教师和考生情况进行了描述。由于教师和考生之间有相同的信息,如姓名、年龄等,所以可抽象出一个基类person,考生类student和教师类teacher作为person类的派生类。请按要求完成下列操作,将程序中的类定义补充完整。
(1)定义基类person的私有数据成员name和age,分别用于表示姓名和年龄,name为字符指针,age为int型的数据。请在注释"//**1**"之后添加适当的语句。
(2)完成基类person的重载构造函数person(char *n,int a)的定义,把数据成员name,age分别初始化为参数n,a的值,请在注释"//**2**"之后添加适当的语句。
(3)根据主函数main中对s1的声明,定义派生类student的构造函数。在注释"//**3**"之后添加适当的语句。
(4)完成派生类teacher成员函数void setinf(char *n,int a,char *d,char *l)的定义,其功能为记录教师的姓名、年龄、院系与职称信息。请在注释"//**4**"之后添加适当的语句。
输出结果如下:
刘雷(45)destructor
蒋军(23)destructor
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test4_3.cpp清单如下:
#include
class person
{
//**1**
public:
person(){}
person(char *n,int a)
{
//**2**
}
void setname(char *n){name=n;}
void setage(int a){age=a;}
~person(){cout<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,请编写一个函数int Invert(char*str),其作用是将一个表示整数的字符串转换为相应整数。
注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,也不要删除或移动“//****found****”。
//proj2.cpp
#include <iostream>
#include <cstring>
using namespace std;
int Invert(char * str)
{
//********** found***********
______;
while(* str!='/0')
{
//********** found***********
int digital =
num=num* 10 +digital;
//********** found***********
______
}
return num;
}
int main()
{
char * str=new char[10];
cout <<"Please input the integerstring:";
cin >> str;
cout <<Invert(str) <<endl;
return 0;
}
