问答题使用VC6打开
下的源程序文件modi1.cpp,该程序运行时有错误,请改正其中的错误,使程序正常运行,并且输出以下结果:
(4,5)
7,8
(4,8)
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
class CObj0
{
public:
CObj0(int i,int j)
{
x=i;
y=j;
}
//******error******
virtual void move(int a;int b)
{
x+=a;
y+=b;
}
void print()
{
cout
" ("
x
","
y
")"
endl;
}
public:
int x,y;
};
class CObj1: public CObj0
{
public:
//******error******
CObj1(int i,int j,int k):(i,j)
{
m=k;
n=l;
}
void print()
{
cout
m
","
n
endl;
}
void func()
{
move (3,5);
}
void display()
{
//******error******
print();
}
private:
int m,n;
};
void main()
{
CObj0 obj (4,5);
obj.print();
CObj1 obj1(1,3,7,8);
obj1.func();
obj1.print();
obj1.display();
}
问答题使用VC6打开
下的源程序文件modi1.cpp,该程序运行时有错误,请改正程序中的错误,使得程序输出:
number1=4.8 number2=93.6 number3=0.9
PRODUCT=404.352
注意:错误的语句在//******error******的下面。修改该语句即可。其他的语句不能修改。
#include <iostream>
using namespace std;
int main()
{
double number1=4.8, number2=93.6, number3=0.9;
//******error******
product=number1*number2*n
umber3;
//******error******
cout
"number1="
number1<"/t"
"number2="
number2
"/t";
//******error******
cout
"number3="
number3
"/t";
cout
"PRODUCT="
product
endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。此程序的运行结果为:
In CDerive’S display().b=1
In CDerive2’S display().b=2
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义函数display()为无值型纯虚函数。请在注释∥********1********之后添加适当的语句。
(2)建立类CDerive的构造函数,请在注释 ∥********2********之后添加适当的语句。
(3)完成类CDerive2成员函数diaplay0的定义。请在注释 ∥********3********之后添加适当的语句。
(4)定义类Derivel的对象指针dl,类CDerive2的对象指针d2。其初始化值分别为1和2。请在注释 ∥********4********之后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include
using namespace std;
class CBase
{
public:
CBase(int i){b=i ;}
∥********1********
protected:
int b;
;;
class CDerive:publ ic CBase
{
public:
∥********2********
void display()
{
coutdisplay();
}
void main()
{
∥********4********
func(d1);
func(d2);
}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,此工程中声明的Array是一个表示数组的类。一个Array对象可以包含多个整型元素。Array的成员说明如下:
成员函数add用于向数组的末尾添加一个元素;
成员函数get用于获取数组中指定位置的元素;
数据成员a表示实际用于存储数据的整型数组;
数据成员size表示数组的容量,数组中的元素个数最多不能超过size;
数据成员num表示当前数组中的元素个数。
SortedArray是Array的派生类,表示有序数组。SortedArray重新定义了Array中的add函数,以确保有序数组中的元素始终按照升序排列。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
10,9,8,7,6,5,4,3,2,1,
1,2,3,4,5,6,7,8,9,10,
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class Array {
public:
Array(unsigned int s)
{
size = s;
num= 0;
a = new int[s];
}
virtual ~Array() {delete[] a;}
virtual void add(int e)
{
if (num< size) {
// **********found**********
______
num ++;
}
}
int get(unsigned int i) const
{
if (i < size)
return a[i];
return 0;
}
protected:
int * a;
unsigned int size, num;
};
class SortedArray : public Array {
public:
// **********found**********
SortedArray(unsigned int s)
:______{}
virtual void add(int e)
{
if (num >= size)
return;
int i =0, j;
while (i <num) {
if (e < a[i]) {
for (j =num; j > i; j--) {
// **********found**********
______;
}
// **********found**********
______;
break;
}
i++;
}
if (i == num)
a[i] = e;
num ++;
}
};
void fun(Array
for (i = 10; i >= 1; i--) {
a.add (i);
}
for (i = 0; i < 10; i++) {
cout << a.get(i) << ", ";
}
cout << endl;
}
int main()
{
Array a(10);
fun(a);
SortedArray sa(10);
fun(sa);
return 0;
}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中定义了BaseX类(X进制整数类),其中成员函数print输出该整数指定数制的表示形式。使用展转相除法,可从低位到高位逐位求出所需要的表示形式,例如,求十制数231的四进制表示形式,方法如下:231/4=57……357/4=14……114/4=3……2结果是:3213在main函数中给出了一组测试数据,正确的输出是:2001注意:只需在//**********333**********和//**********666**********,之间填入所编写的若干语句,不要改动程序中的其他内容。#include<iostream>#include<cmath>#include”BaseX.h”using namespace std;BaseX::BaseX(int n)(thiS一>num=n;}BaseX::一BaseX(){)void BaseX::print(int x){ //输出整数num的X进制表示形式int arr[1 00]; //存放转换结果,arr[0]存放最低位int index=0; //当前数组arr中元素的个数//**********333**********//**********666**********for(int i=、index一1;i>=0;i—一){ //从高位到低位输出所需表示形式cout<<arr[i];}cout<<endl; } int main(){BaseX b(12 9);b.print(4);writeToFile(”C:\\test\、.-),return 0; } //proj3\BaseX.h class BaseX{int num;public:BaseX(int n);一BaseX();void print《int x); }; void writeToFile(const char*path);
问答题请使用VC6或使用【答题】菜单打开
prog3下的工程prog3,其中包含了类Polynomial(“多项式”)的定义。
形如5x
4
+3.4x
2
-7x+2的代数式称为多项式,其中的5为4次项系数,3.4为2次项系数,-7为1次项系数,2为0次项(常数项)系数。此例缺3次项,意味着3次项系数为0,即省略了0x
3
。在Polynomial中,多项式的各个系数存储在一个名为coef的数组中。例如,对于上面的多项式,保存在coef[0]、coef[1]...coef[4]中的系数依次为:2.0、-7.0、3.4、0.0、5.0,也即对于i次项,其系数就保存在coef[i]中。成员函数getValue计算多项式的值,多项式中x的值是由参数指定的。
请补充完成文件:Polynomial.cpp中成员函数getValue的定义。此程序的正确输出结果应为:
Value of p1 when x=2.0:59.8
Value of p2 when x=3.0:226.8
注意:只在函数getValue的“// *******333*******”和“// *******666*******”之间填入若干语句,不要改动程序中的其他内容。
//Polynomiac.h
#include <iostream>
using namespace std;
class Polynomial{ //“多项式”类
public:
Polynomial (double coef[], int hum):coef (new double[num]), num_of_terms(num) {
for (int i = 0; i < num_of_terms; i ++)
this -> coef[i] = coef[i];
}
~Polynomial() {delete[] coef;}
//返回指定次数项的系数
double getCoefficient (int power) const {return coef[power];}
//返回在x等于指定值时多项式的值
double getValue (double x) const;
private:
//系数数组,coef[0]为0次项(常数项)系数,coef[1]为1次项系数,coef[2]为2次项(平方项)系数,余类推。
double * coef;
int num_of_terms;
};
void writeToFile(const char * path);
// Polymomial.cpp
#include "Polynomial.h"
double Polynomial::getValue (double x) const {
//多项式的值value为各次项的累加和
double value = coef[0];
// ********333********
// ********666********
return value;
}
//main.cpp
#include "Polynomial.h"
int main() {
double p1[] = {5.0, 3.4, -4.0, 8.0}, p2[] = {0.0, -5.4, 0.0, 3.0, 2.0};
Polynomial poly1 (p1, sizeof(p1)/sizeof (double)), poly2 (p2,sizeof(p2)/sizeof (double));
cout << "Value of p1 when x = 2.0:" << poly1.getValue(2.0) << endl;
cout << "Value of p2 when x = 3.0:" << poly2.getValue(3.0) << endl;
writeToFile(" ");
return 0;
}
问答题请编写一个函数maxofarray(atype*p,int count),该函数从一个数组中找出其中的最大元素,并且数组中可以存放多种数据类型的元素。 注意:部分源程序己存在文件test42_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数maxofarray的花括号中填写若干语句。 文件test42_2.cpp清单如下: #include<iostream.h> #include<string.h> #include<conio.h> template<class atype> void maxofarray(atype* p,int count) void main () int len=5; char *p1; cout<<"the char type array and it's length is 5:/n"; cout<<"the array element is a b c d e/n"; p1=new char[len]; for (int i=0;i<len;i++) p1[i]='a'+i; maxofarray(p1,len);
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分的程序。函数func(int A[NUM],int n)实现的功能是将数组的内容进行一次重新排序。排序的方法是:给定n,则下标为i的数字与下标为n-i的数字交换。从0开始,交换N/2次,则最后的内容为排序后的结果。如果:A[8]={1,2,3,4,5,6,7,8},n=6,则结果:A[8]={7,6,5,4,3,2,1,8}。 注意:不能修改其他代码。#include<iostream.h>#define NUM 8void func(int A[NUM],int n){}int main(){ int A[NUM]={1,2,3,4,5,6,7,8); func(A,6); for(int i=0;i<sizeof(A)/Sizeof(int);i++) { cout<<A[i]<<‘’; } cout<<endl; return 0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmjl下的工程pmjl,此工程中含有一个源程序文件pmjl.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:Theextensionis:CPP注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>usingnamespacestd;classNyClass{char*p;public:NyClass(charc){p=newchar;*p=c;}//ERROR********Eound********NyClass(constNyClasscopy){p=newchar;*p=*(copy.p);}//ERROR********found********下列析构函数用于释放字符指针~NyClass(){freep;)NyClass&operator=(constNyClass&rhs){if(this=&rhs)return*this;*P=*(rhs.p);//ERROR********found********returnthis;}charGetChar()const{return*p;}};intmain(){NyClassobjl('C'),obj2('P');NyClassobj3(obj1);obj3=obj2;cout<<"Theextensionis:"<<obj1.GetChar()<<obj2.GetChar()<<obj3.GetChar()<<end1;return0;}
问答题编写函数fun(),它的功能是利用以下所示的简单迭代方法求方程cos(x)-x=0的一个实根。 Xn+1=COS(Xn) 迭代步骤如下: (1)取x1初值为0.0。 (2)x0=x1,把x1的值赋给x0。 (3)x1=cos(x0),求出一个新的x1。 (4)若x0-x1的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2)。 (5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。 程序输出结果Root=0.739085。 注意:部分源程序已存在文件test6_2.cpp中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 文件test6_2的内容如下: #include<conio.h> #include<math.h> #include<iostream.h> float fun() void main() cout<<"Root="<<fun()<<endl;
问答题请使用VC6或使用[答题]菜单打开考生文件夹projl下的工程proj1,其中有枚举DOGCOLOR、狗类Dog和主函数man的定义。程序中位于每个“//ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: There is a white dog named Hoho. There is a black dog named Haha. There is a motley dog named Hihi. 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include <iostream> using namespace std; enum DOGCOLOR BLACK,WHITE,YELLOW,BROWN,PIEBALD,OTHER; class Dog //狗类 DOGCOLOR color; char name[20]; static int count; public: Dog(char name[],DOGCOLOR color) strcpy(this->name,name); strcpy(this->color,color); DOGCOLOR getColor () constreturncolor; const char* getName () const return * name; const char * getColorString ()const switch (color) case BLACK:return"black"; case WHITE:return"white"; case YELLOW:return"yellow"; case BROWN:return"brown"; case PIEBALD:return"piebald"; return"motley"; void show () const cout<<"There is a "<<getColorString()<<"dog named"<<name<<'.'<<endl; ; int main () //ERROR **********found********** Dog dog1("Hoho",WHITE),dog2("Haha",BLACK);dog3("Hihi",OTHER); dog1.show(); dog2.show(); dog3.show(); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并且使程序输出的结果为:
a=7,b=0
a=8,b=10
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class cobj
{
public:
∥********error********
friend void func(CObj
};
void func(CObj
func(obj 1,8,1 0);
obj 1.display();
}
问答题简单应用题
请编写一个函数 int sum(int n),该函数完成1+2+3+…+n的运算,并返回运算结果,其中n>0。注意:请使用递归算法实现该函数。
注意:部分源程序已存在文件test11_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。
文件test11_2.cpp的内容如下:
#include
int sum(int n)
{
}
void main()
{
int n;
cout>n;
int result=sum(n);
cout<<"结果为:"<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程包含有一个源程序文件proj2.cpp,其中定义了Stack类和ArrayStack类。 Stack是一个用于表示数据结构“栈”的类,栈中的元素是字符型数据。Stack为抽象类,它只定义了栈的用户接口,如下所示: 公有成员函数 功能 push 入栈:在栈顶位置添加一个元素 pop 退栈:取出并返回栈顶元素 ArrayStack是Stack的派生类,它实现了Stack定义的接口。ArrayStack内部使用动态分配的字符数组作为栈元素的存储空间。数据成员maxSize表示的是栈的最大容量,top用于记录栈顶的位置。成员函数push和pop分别实现具体的入栈和退栈操作。 请在程序中的横线处填写适当的代码,然后删除横线,以实现上述功能。此程序的正确输出结果应为: a,b,c c,b,a 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 //proj2.cpp #include <iostream> using namespace std; class Stack public: virtual void push (char c)=0; virtual char pop()=0; ; class ArrayStack:public Stack char*p; int maxSize; int top; public: ArrayStack(int s) top=0; maxSize=s; //********found******** p=______; ~ArrayStack() //********found******** ______; void push (char c) if (top==maxSize) cerr<<"Overflow! /n"; return; //********found******** ______; top++; char pop () if (top==0) cerr<<"Underflow! /n"; return'/0'; top--; //********found******** ______; ; void f (Stack cout<<ch[0]<<","<<ch[1]<<","<<ch[2]<<endl; sRef.push(ch[0]);sRef.push(ch[1]);sRef.push(ch[2]); cout<<sRef.pop()<<endl; int main () ArrayStack as(10); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)声明类objAl,请在注释∥********1********后添加适当的语句。
(2)为类0bjA0增加友元函数rune(),请在注释∥********2********后添力口适当的语句。
(3)为类objAl增加友元函数func(),请在注释∥********3********后添加适当的语句。
(4)函数rune0返回objAl对象中的变量和0bjA0的静态变量的乘积,请在注释∥********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
{}include
∥********1********
class objA0
{
private:
static int m A0 ;
∥********2********
};
int objA0::m—A0=1 0;
class objAl
{
private:
int m A1;
∥********3********
public:
objAl(int i)
{
m A1=i;
}
};
int func(ohjAl
cout<
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示雇员的Employee类,但类Employee的定义并不完整。请按要求完成下列操作,将类CEmployee的定义补充完成。 (1)定义私有数据成员name、street、city、zipcode和age分别用于表示姓名、街道、城市、邮编、年龄,除年龄是整型外其余都是char型的数据。请在注释//********1********之后添加适当的语句。 (2)完成默认构造函数CEmployee的定义,使其把参数传递给私有数据成员name、street、city、zipcode和age。请在注释//********2********之后添加适当的语句。 (3)完成成员函数alterName(char*newName)的定义。请在注释//********3********之后添加适当的语句。 (4)完成成员函数IsEqual(char*ename)的定义,实现当name相等时返回真,否则为假的功能,请在注释//********4********之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>#include<string.h>#defiRe MAXLEN 20 class CEmployee {private: //********1******** int age;public: CEmployee (char*newName,char *newstreet,char *newCt,char*newZp,int newAge); void alterName(char*newName); Void display(); bool IsEqual(char*ename); }; CEmployee::CEmployee (char *newName,char *newstreet,char *newCt,char*newZp,int newAge) { //********2******** age=newAge; } void CEmployee::alterName(char *newName) { //********3******** } bool CEmployee::IsEqual (char *ename) { //********4******** } void CEmployee::display() { cout << name << " " <<street<<" "; cout << city << " "<<zipcode<<" "<<age<<endl;}void main(void){ CEmployee employee[4]。 { CEmployee("李伟","兴荣路213号","兰州","413412”,21), CEmployee("张星","南山街157号","贵州","534670",30), CEmployee("赵曦","北大街108号","深圳","412440",43), CEmployee("刘兰","南大街330号","北京","670893",17), }; for(int i=0;i<4;i++) employee[i].display(); cout<<"\n修改\"张星\"的名字为\"刘新\"\n"<<endl; for(int j=0;j<4;j++) { if(employee[j].IsEqual("张星")) { employee[j].alterName("刘新"); employee[j].display(); break; } }}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错误,请改正其中的错误,使程序正确运行。并且使程序输出的结果为: OK 注意:错误的语句在//*****error******的下面,修改该语句即可。#include<iostream.h>Class CBase{public: CBase() { a=b=0; }private: int a,b;};class CDerivel:public CBase{public: CDerive1() { } //*****error****** virtual void func();};class CDerive2:public CDerive1{public: CDerive2() { a=0; b=0; } void func() { //*****error****** cout<<"OK"<end1; }private: int a; int b;};void main(){ CDerive2 obj; //*****error****** CDerive1 *p =(CDerivel*)obj; p->func();}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:Name:Smith Age:21 ID:99999 CourseNum:12 Record:970 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>USing namespace std;clasS StudentInfo{protected://ERROR**********found********** char Name; int Age; int ID; int CourseNum; float Record;public: StudentInfo(char*name,int Age,int ID,int courseNum,float record);//ERROR**********found********** void—StudentInfo(){) float AVerageRecord(){ return Record/CourseNum; } void show()const{ cout<<”Name:”<<Name<<”Age:”<<Age<<”ID:”<<ID <<”CourseNum:”<<CourseNum<<”Record:”<<Record<<endl; } }; //ERROR**********found********** StudentInfo StudentInfo(char * Name f int Age ,int ID r int CourseNum, float Record) { Name=name; Age=age; thiS一>ID=ID; CourseNum=courSeNum; Record=record; } int main() { StudentInfo st(”Smith”,21,99999,12,97 0); st.show(); return 0; }
问答题请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intArray重载了“=”运算符。程序中给出了一个测试数据文件input,不超过300个的整数。程序的输出是: 10 11 13 16 20 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //intArray.h class intArray private: int*array; int length; public: intArray (char*filename); intArray (); intArray ~intArray (); void show (); ; void writeToFile (const char*path); //main.cpp #include<iostream> #include<fstream> #include<cstring> #include "intArray.h" using namespace std; intArray::intArray () length=10; array=new int[length]; intArray::intArray(char*filename) ifstream myFile(filename); array=new int[300]; length=0; while (myFile>>array[length++]) length--; myFile.close(); intArray length=src.length; array=new int[length]; return*this; intArray::~intArray () delete [] array; void intArray::show () int step =0; for(int i=0;i<length;i=i+step) cout<<array[i]<<endl; step++; void main () intArray*arrayP=new intArray("input.dat"); intArray myArray; myArray=*arrayP; (*arrayP).show(); delete arrayP; writeToFile(" ");
问答题使用VC++6.0打开
下的源程序文件3.cpp。其中定义的类不完整,按要求完成下列操作,将类的定义补充完整。
(1)利用define定义常量TRUE为1,定义常量FALSE为0,请在注释1后添加适当的语句。
(2)在类A2前增加A1的声明,请在注释2后添加适当的语句。
(3)在类C1中声明友元函数bool func(A2
bool m_A2;
friend bool func(A2
public:
A2()
{
m_A2=FALSE;
}
public;
void setA2(bool n)
{
m_A2=n;
}
};
class A1
{
private;
bool m_A1;
//********3*********
public;
A1()
{
m_A1=TRUE;
}
public;
void setA1(bool n)
{
m_A1=n;
}
};
bool func(A2
A1 obj1;
cout<<func(obj0,obj1)<<endl;
obj0.setA2(TRUE);
obj1.setA1(TRUE);
cout<<func(obj0,obj1)<<endl;
return 0;
}
