问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: Hello 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>void main(){ //********error******** typedef BOOL bool; //********error******** BOOL a=FALSE; int i=0X80000000; //********error******** a=!i; if(a) { cout<<"Hello"<<endl; } return;}
问答题使用VC++6.0打开
下的源程序文件2.cpp。请完成函数fun(int x),该函数的功能是将x的值转换成二进制数输出到屏幕,并且在函数中调用写函数WriteFile()将结果输出到2.txt文件中。
例如:x=6,6的二进制数为110,则输出到屏幕的数为110。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
void WriteFile(char *str)
{
ofstream out1;
out1.open("2.txt",ios_base::binary|ios_base::app);
for(int i=0; str[i] != 0; i++)
out1.put( str[i]);
out1.close();
}
void fun(int x)
{
}
void ClearFile()
{
ofstream out1;
out1.open("2.txt");
out1.close();
}
int main()
{
ClearFile();
fun(13);
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
100
37
32
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
∥********error********
void main(
{
∥********error********
int m=0142;
∥********error********
int n=0X27;
int q=32;
cout<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(int n),求出nXn矩阵的对角线数字的平方和。如果n为奇数,则对角线交叉部位数字只参与一次计算。
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#define MAX N 10
int XX[MAX N][MAX N];
int fun(int n)
{
}
void main()
{
int n;
do
{
cout>n;
if(n=1)
{
break;
}
}while(1);
for(int i=0;i>XX[i][j];
}
}
cout<
问答题请使用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 31 56 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
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter; //用于记录集合中元素个数的计数器
puhlic:
IntegerSet():counter(0){}
//创建一个空集合
IntegerSet(int data[],int size);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remeve(int element);
//删除集合中指定的元素
int getCount()const{return counter;}
//返回集合中元素的个数
int getElement(int i)const{retum elem[i];}//返回集合中指定的元素
void show()const;
};
void WriteToFile(char*);
#endif
//main.cpp
#include”IntegerSet.h”
#include
IntegerSet::IntegerSet(int data[],int size):counter(0){
for(int i=0;i0;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++; //计数器加l
}
void IntegerSet::remove(int element){
//***************333***************
//***************666***************
void IntegerSet::show()const{
for(int i=0;i
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使函数fun()实现以下功能:找出一个整数,它加上100后是一个完全平方数,再加上268又是一个完全平方数,请问该数是多少?
程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后开方,如果开方后的结果满足条件,即是结果。
#include
#include
Void fun()
{
}
int main()
{
fun();
return 0;
}
问答题使用VC++6.0打开
下的源程序文件2.cpp,请实现函数fun(double a[],int len)的如下功能:
(1)a[]是一个数组,长度为len。
(2)a[0]=0,a[1]=1。
(3)a[i+2]=a[i]+a[i+1]。
注意:不能修改函数的其他部分。
试题程序:
#include<iostream>
void fun(double a[],int len)
{
}
void main()
{
double a[20];
fun(a,20);
for(int i=0;i<20;i++)
{
std::cout<<a[i]<<"";
if(i%6==5)
std::cout<<std::endl;
}
return;
}
问答题请使用“答题”菜单或使用VC6打开考生文件夹proj1下的工程proj1。此工程定义了Stop-Watch(秒表)类,用于表示时、分、秒信息,有构造函数StopWatch()、设置时间函数reset()、并且重载了前置和后置++运算符,用于实现增加秒的功能。程序中位于每个//ERROR************found************下的语句行有错误,请加以改正。改正后程序的输出应该是: 00:00:00 00:01:00 注意:只能修改每个//ERROR************found************下的那一行,不要改动程序中的其他内容。 //源程序 #include<iostream> #include<iomanip> using namespace std; class StopWatch //“秒表”类 int hours,minutes,seconds; //小时、分钟、秒 public: StopWatch():hours(0),minutes (0),seconds(0) void reset()hours=minutes=seconds=0; StopWatch operator++(int) //后置++ StopWatch old=*this; ++(*this); return old; //前进1秒 StopWatch& operator++() //前置++ //ERROR************found************ if(seconds++==60) seconds=0;minutes++; if(minutes==60) minutes=0;hours++; //ERROR************found************ return this: friend void show(StopWatch); ; void show(StopWatch watch) cout<<setfill('0'); cout<<setw(2)<<watch.hours<<':'<<setw(2)<<watch.minutes<<':'<<setw(2)<<watch.seconds<<endl; int main() StopWatch sw; show(sw); for (int i=0; i<59; 1++) sw++; //ERROR************found************ show(sw++); return 0:
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了CharShape类、Triangle类和Rectangle类。 CharShape是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用于显示不同字符图形的相同操作接口。Triangle和Rectangle是CharShape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,用于实现各自的显示操作。程序的正确输出结果应为: * *** ***** ******* ######## ######## ######## 请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。 (1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。 (2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。 (3)为类外函数fun添加合适的形参。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 //proj2.cpp #include<iostream> using namespace std; class CharShape public: CharShape (char ch):ch (ch)); virtual void Show()=0; protected: char ch;//组成图形的字符; class Triangle:public CharShape public: Triangle(char ch,int r):Char-Shape(ch),_rows (r) void Show(); private: int rows; //行数 ; class Rectangle: public CharShape public: Rectangle (char ch,int r,int c): CharShape (ch),_rows(r),_cols(c) void Show(); private: int rows,cols;//行数和列数 ); void Triangle::Show() //输出字符组成的三角形 for (int i=1;i<=rows;i++) //********found******** for (int j=1;j<=______;j++) cout<<_ch; cout<<endl; void Rectangle::Show() //输出字符组成的矩形 //********found******** for (int i=1;i<=______;i++) //********found******** for(int j=1;j<=______;j++) cout<<_ch; cout<<endl; //********found********为fun函数添加形参 void fun(______)cs.Show();int main() Triangle tri('*',4); Rectangle rect('#',3,8); fun (tri); fun (rect); return 0;
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
1 2 3 4
注意:模板参数名用T。只修改注释“// ERROR *******found*******”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
//将数组元素个数Size定义为4
// ERROR *******found*******
const int Size;
template <typename T>
class MyClass
{
public:
MyClass (T * p)
{
for(int i=0;i < Size;i ++)
array[i] = p[i];
}
void Print();
private:
T array[Size];
};
template <typename T>
// ERROR *******found*******
void MyClass::Print()
{
for(int i = 0;i <Size;i ++)
cout << array[i] << "/t";
}
int main()
{
int intArray[Size] = {1,2,3,4};
// ERROR *******found*******
MyClass <double> obj (intArray);
obj.Print();
cout << endl;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The value of member objects is 8
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class Member
{
public:
Member(int x) {val =x;}
int GetData() {return val;}
private:
//ERROR ******** found********
int val =0;
};
class MyClass
{
public:
// ERROR ******** found********
MyClass(int x) { data=x; }
void Print()
// ERROR ******** found********
{ cout <<"The value of member object is" <<data.val <<endl;}
private:
Member data;
};
int main()
{
MyClass obj(8);
obj.Print();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示矩形的CRect类,但类CRect的定义并不完整。请按要求完成下列操作,将类CRect的定义补充完成。
(1)定义私有数据成员leftPoint、topPoint、rightPoint、bottomPoint,分别用于表示矩形左上角及右下角的点的坐标,它们都是double型的数据。请在注释∥********1********之后添加适当的语句。
(2)完成默认构造函数CRect的定义,指定缺省实参为0,都是double型的数据。请在注释∥********2********之后添加适当的语句。
(3)定义函数体为空的析构函数。请在注释∥********3********之后添加适当的语句。
(4)在main()函数中定义CRect类的实例rect2,并把rect1 的值赋给rect2。 请在注释∥********4********之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
Class CRect
{
private:
∥********1********
public:
∥********2********
∥********3********
void SetPoints(double,
double,double,double);
void SetLeftPoint(double m){leftPoint=m;)
void SetRightP0int(double m){ rightPoint=m;)
void SetTopPoint(double m){topPoint=m;)
void SetBottomPoint(double m){bottomPoint=m;)
void Di splay();
};
CRect::CRect(double 1,double
t,double r,double b)
{
leftPoint=1;topPoint=t;
rightPoint=r;bottomPoint
=b;
}
void CRect::SetPoints(double
1,double t,double r,double b)
{
leftPoint=1;topPoint=t;
rightPoint=r;bottomPoint
=b;
}
void CRect::misplay()
{
cout<<“left—top point iS
(”<<1eftPoint<<“,”<
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmj2下的工程pmj2,该工程中包含程序文件main.cpp,其中有类Mammal(“哺乳动物”)、类Elephant(“大象”)、类Nome(“老鼠”)的定义和主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述定义。此程序的正确输出结果应为: ELEPHANT MOUSE 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;enum category {ENPTY,ELEPHANT,NOUSE);char*output[]={”ENPTY”,”ELEPHANT”,”NOUSE”};ClasS Nammal{public: Nammal(char*str) {//**********found**********name=new_____strcpy(name,str); } virtual char*WhoAmI()=0; virtual—Nammal(){delete[]name;} void Print(){tout<<WhoAmI()<<end1;)private: char*name;};class Elephant:public Nammal{public://**********found**********Elephant(char*str):________{} char*WhoAmI(){return output[ELE—PHANT];)};class Mouse:public Mammal{public: Mouse(char*str):Mammal(str){)//**********found**********char*WhoAmI(){______} }; int main() { //**********found**********Mammal*pm=new_______(”Huanhuan”); pm->Print(); delete pm; pm=new Mouse(”Micky”); pm->Print(); delete pm; return 0; }
问答题请使用VC6或使用[答题]菜单打开
proj3下的工程文件proj3,此工程中包含一个源程序文件proj3. cpp,补充编制C++程序proj3. cpp,其功能是读取文本文件in. dat中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符序列反转,并输出到文件out. dat中。文件in. dat的长度不大于1000字节。
要求:
补充编制的内容写在“//**********333**********”与“//**********66666**********”两行之间。实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。不得修改程序的其他部分。
注意:程序最后已将结果输出到文件out. dat中,输出函数writeToFile已经给出并且调用。
//proj3. cpp
#include<iostream>
#nclude<fstream>
#include<cstring>
using namespace std;
class doc
{
private:
char*str;//文本字符串首地址
int length;//文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象,filename是文件名字符串首地址
doc(char*filename);
void reverse();//将字符序列反转
~doc();
void writeToFile(char*filename);
};
doc::doc(char*filename)
{
ifstream myFile(filename);
int len=1001, trap;
str=new char[len];
length=0;
while((tmp=myFile. get())!=EOF)
{
str[length++]=tmp;
}
str[length]="/0";
myFile. close();
}
void doc::reverse() {
//将数组str中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个
//字符交换……
//*************333*************
//*************666*************
}
doc::~doc()
{
delete[]str;
}
void doc::writeToFile(char*filename)
{
ofstream outFile(filename);
outFile<<str;
outFile. close();
}
void main()
{
doc myDoc("in. dat");
myDoc. reverse();
myDoc. writeToFile("out. dat");
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写VMArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是: ValArray v1={1,2,3,4,5} ValArray v2={2,2,2,2,2} 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间。不要修改程序的其他部分。 注意: 相关文件包括:main.cpp、ValArray.h。 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//ValArray.h#include<iostream>using namespace std;clas s ValArray{ int*v; int Size;public: ValArray(const int*P,int n):Size(n) { v=new int[size]; for(int i=0;i<Size;i++) v[i]=P[i]; } ValArray(const ValArray for(int i=0;i<size一1;i++) out <<v[i] <<”,”; out<<v[size一1] <<’)’; } }; void writeToFile(const char*); //main.cpp #include”ValArray.h” ValArray::ValArray(const ValArray v1.print(cout); cout<<endl; cout<<”ValArray v2=”; v2.print(cout); cout<<endl; writeToFile(””); return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)类CBase有一个常数变量Number1,在构造函数时对其初始化,请在注释∥********1********后添加适当的语句。
(2)类CPri是CBase的公共继承类,请在注释∥********2********后添加适当的语句。
(3)类CPri构造函数有两个形式参数,第一个是对CBase的初始化,第二个缺省为0,用来对变量Number2进行初始化。请在注释∥********3********后添加适当的语句。
(4)类CPri的函数display()重载于CBas~的打印函数。完成对变量Number2的输出,然后调用基类的打印函数,请在注释∥********4********后添加适当的语句。输出的内容如下:
Number2=12
Number=8
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
C1ass CBase
{
private:
int Number;
public:
∥********1********
CBase(int x)
{}
void display()
{
cout<<"Number=”<
问答题使用VC6打开
下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
Number=8
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
class CMyClass
{
public:
//******error******
void
}
void Set(int m)
{
Number=m;
}
void display()
{
cout
"Number="
Number
endl;
}
private:
int Number;
};
void main()
{
int* p;
//******error******
//******error******
p=t.Get();
*p=8;
t.display();
}
问答题使用VC++6.0打开
下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数IsPalindromes(char * string)实现的功能是判定给定的字符串是否构成回文字符串,如果是则返回1,否则返回0。
例如:abcdcba或者1234321都认为是回文字符串。
如果串为空或一个字母时,均认为是回文字符串。
注意:不能修改程序的其他部分,只能补充IsPalindromes函数。
试题程序:
#include<iostream.h>
#define MAXLEN 1024
bool IsPalindromes(char * string)
{
}
void main()
{
char str[MAXLEN];
cout<<"请输入一行文字"<<endl;
cin.getline(str,MAXLEN);
cout<<IsPalindromes(str)<<endl;
return;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹pmj2下的工程proj2,其中在编辑窗口内显示的主程序文件中定义有类Base和Derived,以及主函数main。程序文本中位于每行 “//****found****”下面的一行内有一处或多处下画线标记,请在每个下画线标记处填写合适的内容,并删除下画线标记。经修改后运行程序,得到的输出应为: sum=55. 注意:只在横线处填写适当的代码,不要改动程序中的其他内容。#include<iostream>using namespace std;ClaSS Base{ public: Base(int m1,int m2){ meta1=m1;mem2=m2; } int SLIm(){return mem1+mem2;) private: int mem1,mem2;//基类的数据成员 }; //派生类Derived从基类Base公有继承 //************* found************** claSS Derived: { public: //构造函数声明 Derived(int m1,int m2,int m3); //sum函数定义,要求返回meml、mem2和mem3之和//************* found**************int sum(){return________+mere3;}private: int mere3; //派生类本身的数据成员 }; //构造函数的类外定义,要求由m1和m2分别初始化mem1和mem2,由m3初始化mem3 //**********found********** _______Derived(int m1,int m2,int m3): //**********found********* ______,mere3(m3){) int main(){ Base a(4,6); Derived b(10,15,20); int sum=a.sum()+b.sum(); cout;<<”sum=”<<sum<<endl; 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,hclas S intArray{private: int*array; int length;public: intArray(char*filename); intArray(); intArrayvoid 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[1ength++]) length一一; myFile.close();}intArray& intArray:: operator =(const intArray&src){if(array!=NULL)delete[]array;length=src.length;array=new int[length];//*************333***********//*************666*********** 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(””); }
