问答题使用VC6打开考生文件夹下的工程MyProj8。此工程包含一个源程序文件MyMain8.cpp,该程序实现栈的入栈和出栈的操作。其中有两个类:一个是节点类node,它包含节点值和指向上一个节点的指针prey;另一个类是栈类stack,它包含栈的头指针top。但类的定义并不完整。 请按要求完成下列操作,将类Sample的定义补充完成: ①定义私有节点值data,它是血型的数据,以及定义一个指向上一个节点的指针prev。请在注释“//* *1* *”之后添加适当的语句。 ②完成构造函数node(int d,node*n)的定义,使得私有成员data和prev分别初始化为d和n。请在注释“//* *2* *”之后添加适当的语句。 ③完成类stack的成员函数push(int i)的类体内的定义。函数push()实现入栈这个操作,即把形参i压入栈中,那么此时应该创建一个新的节点,并让这个节点的prev指针指向栈顶。请在注释“//* *3 * *”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain8.cpp清单如下: //MyMain 8.cpp #include <iostream> using namespace std; class stack; class node private: //* * 1 * * public: node(int d, node *n) //* * 2 * * friend class stack; ; class stack node *top; //栈头 public: stack() top=0; void push(int i) //* * 3 * * int pop() node*t=top; if(top) top=top->prev; int c=t->data; delete t; return c; return 0; ; int main() stack s; s.push(6); s.push(3); s.push(1); return 0;
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为:sizeof(str1)=5sizeof(str2)=10sizeof(str3)=1注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。(1)不能删除assert()语句;(2)只能修改后面的数字。提示:assert()函数如果为假,则会产生一个中断异常。#include<iostream.h>#include<assert.h>voidmain(){char*str1="abc";//********error********assert(sizeof(str1)==3);cout"sizeof(str1)=5"endl;charstr2[10]="ab";//********error********assert(sizeof(str2)==2);cout"sizeof(str2)=10"endl;charstr3=23;//********error********assert(sizeof(str3)==4);cout"sizeof(str3)=1"endl;return;}
问答题请使用VC6或使用【答题】菜单打开
proj2下的工程proj2,该工程中包含程序文件main.cpp,其中有类Mammal(“哺乳动物”)、类Elephant(“大象”)、类Mouse(“老鼠”)的定义和主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述定义。此程序的正确输出结果应为:
ELEPHANT
MOUSE
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
enum category {EMPTY, ELEPHANT, MOUSE};
char * output[] = {"EMPTY", "ELEPHANT", "MOUSE"};
class Mammal
{
public:
Mammal(char * str)
{
// **********found**********
name = new______
strcpy(name,str);
}
virtual char * WhoAmI() = 0;
virtual ~Mammal() {
delete[] name;}
void Print() {cout << WhoAmI() << endl;}
private:
char * name;
};
class Elephant: public Mammal
{
public:
// **********found**********
Elephant(char * str):______{}
chars * WhoAmI() {return output[ELEPHANT];}
};
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或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:ConstructorThevalueis10Destructor注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>usingnamespacestd;classMyClass{public:MyClass(intx):value(x)(cout<<"Constructor"<<end1;)//ERROR**********found**********void~MyClass(){cout<<"Destructor"<<end1;)voidPrint()const;private://ERROR**********found**********intvalue=0;};//ERROR**********found**********voidMyClass::Print(){cout<<"Thevalueis"<<value<<end1;}intmain(){MyClassobject(10);object.print();return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.epp。阅读下列函数说明和代码,补充空出的代码。函数IsPalindromes(cha*string)实现的功能是判定给定的字符串是否构成回文字符串,如果是则返回1,否则返回0。
如:1234554321或者1234321都认为是回文字符串。
如果串为空或一个字母时,均认为是回文字符串。
注意:不能修改程序的其他部分,只能补充
IsPalindromes()函数。
#include
#define MAXLEN 1024
bool IsPalindromes(char*string)
{
}
void main()
{
char str[MAXLEN];
cout<<"请输入—行文字"<
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码。 函数sort(int &m,int &n,int &l)实现将3个整数m,n,1由大到小输出。m最大,l最小。程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果m<n,则将m与n的值进行交换,然后再用m与l进行比较,如果m<l,则将m与l的值进行交换,这样能使m最大,然后再将n与l进行比较,若n<l,则将n与l的值互换,互换后l最小。 将函数sort(int &m,int &n,int &l)补充完整,实现3个数的排序。 注意:请勿改动主函数。 试题程序: #include<iostream.h> vold sort(int &m,int &n,int &l) int main() int x=9; int y=13; int z==3; sort(x,u,z); cout<<x<<','<<y<<','<<z<<end1; return 0;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。其中有类Point(“点”)、Rectangle(“矩形”)和Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x轴的正方向是水平向右的,y轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是:
--圆形----------
圆心=(3,2)
半径=1
面积=3.14159
--外切矩形---------
左上角=(2,1)
右下角=(4,3)
面积=4
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
#include<cmath>
using namespace std;
//平面坐标中的点
//本题坐标系统中,x轴的正方向水平向右,y轴的正方向竖直向下。
class Point{
public:
Point(double x=0.0, double y=0.0): x_(x), y_(y){)
double getX() const {return x; }
double getY() const {return y; }
void setX (double x) {x_=x; }
void setY (double y) {y_=y; }
private:
double x_; //x坐标
double y_; //y坐标
};
//矩形
class Rectangle {
public:
Rectangle(Point p, int w, int h)
:point(p), width(w), height(h)
{}
double area() const//矩形面积
{
return width*height;
}
Point topLeft()const//左上角顶点
{
return point;
}
Point bottomRight()const
//右下角顶点(注:y轴正方向竖直向下)
{
//********found********
return Point(______);
}
private:
Point point; //左上角顶点
double width; //水平边长度
double height; //垂直边长度
};
//圆形
class Circle{
public:
Circle(Point p, double r): center(p), radius(r){}
Rectangle boundingBox()const;
//外切矩形
double area()const//圆形面积
{
//********found********
return PI*______;
}
public:
static const double PI; //圆周率
private:
Point center; //圆心
double radius; //半弪
};
const double Circle::PI=3.14159;
Rectangle Circle::boundingBox
()const
{
//********found********
Point pt(______);
int w, h;
//********found********
w=h=______;
return Rectangle(pt, w, h);
}
int main()
{
Point p(3, 2);
Circle c(p, 1);
cout<<"--圆形-----/n";
cout<<"圆心=(""<<p.getX()<<','<<p.getY() <<")/n";
cout<<"半径="<<1<<endl;
cout<<"面积="<<c.area()<<endl<<endl;
Rectangle bb=c.boundingBox();
Point t1=bb.topLeft();
Point br=bb.bottomRight();
cout<<"--外切矩形-------/n";
cout<<"左上角=("<<t1.getX()<<','<<t1.getY()<<")/n";
cout<<"右下角=("<<br.getX()<<','<<br.getY() <<")/n";
cout<<"面积="<<bb.area()<<endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。通过继承完成输入到屏幕指定的信息; TestClassA TestClassB TestClassC 其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)完成类B虚继承于A,请在注释//********1********后添加适当的语句。 (2)完成类C虚继承于A,请在注释//********2********后添加适当的语句。 (3)完成类D继承于B,C,请在注释//********3********后添加适当的语句。 (4)函数fun通过调用基类的fun,完成所输出的内容,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>class TestClassA{public: void fun(){ cout<<''TestClassA''<<endl; }};//********1********class TestClassB{public: void fun() { cout<<''TestClassB''<<endl; }};//********2********class TestClassC{public: void fun() { cout<<''TestClassC''<<endl; }};//********3********clasS TestClassD{public: void fun() { //********4******** }};void main(){ TestClassD test; test.fun(); return;}
问答题综合应用
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,工程中的IntQueue是以链表方式实现的整数队列,队列中的每个元素是一个整数,保存在一个结点中,并通过结点的链接形成一个链,head和tail分别是指向该链的队首结点和队尾结点的指针。既然是队列,入队元素总是添加在队列尾部,出队元素总是队列的首元素。请补充完成成员函数show。此程序的正确输出结果应为:
刚建立时:
添加8,3后:8 3
添加5后:8 3 5
删除首元素8后:3 5
添加-7后:3 5 -7
删除首元素3后:5 -7
注意:只需在函数purchase的"//********333********"和"//********666********"之间填入若干语句,不要改动程序中的其他内容。
//IntQueue.h
#include
using namespace std;
class Node{ //"结点"类
public:
int data; //元素值:一个整数
Node* next; //指向下一个结点的指针
Node(int intdata):data(intdata),next(NULL){}
};
class IntQueue{ //"整数队列"类
Node *head; //队首指针
Node *tail; //队尾指针
public:
IntQueue():head(NULL),tail(NULL){}
bool isEmpty()const{ return head==NULL; } //空队列时返回true,否则返回false
void enqueue(int intdata); //入队:将元素添加到队列的尾部
bool dequeue(int //出队:删除队首元素,该元素的值通过参数带回
//若出队成功,返回true,否则返回false
void show(ostream //输出队列内容
};
void writeToFile(const char *path);
//IntQueue.cpp
#include "IntQueue.h"
//入队:将元素添加到队列的尾部
void IntQueue::enqueue(int intdata){
if(isEmpty())
head=tail=new Node(intdata);
else
tail=tail->next=new Node(intdata);
}
//出队:删除队首结点,若成功,返回true,否则返回false
bool IntQueue::dequeue(int
intdata=head->data;
Node *p=head;
head=head->next;
delete p;
return true;
}
//输出队列内容
void IntQueue::show(ostream
return;
}
//main.cpp
#include"IntQueue.h"
int main()
{
IntQueue que;
cout<<"刚建立时:"; que.show();
que.enqueue(8);
que.enqueue(3);
cout<<"添加8,3后:"; que.show();
que.enqueue(5);
cout<<"添加5后:"; que.show();
int x;
que.dequeue(x);
cout<<"删除首元素"<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错误,请改正错误,使得程序输出:
Hello
test
注意:不要改动mam函数,不能增行或删行,也不能更改程序的结构,错误的语句在
∥********error********的下面。
#include
∥********error********
template
void fun(T t)
{
std::cout
void fun(bool t)
{
std::cout<<(t?“Hello”:“Hi”)
<
问答题使用VC6打开考生文件夹下的工程test22_3,此工程包含一个源程序文件test22_3.cpp,其中定义了用于表示考生的类 CStudent和它的派生类CGraduateStudent,但它们的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义CStudent的构造函数,函数含参数nm,它是char型的指针数据,请使用字符串函数将类数据成员name初始化。请在注释“//**1**”之后添加适当的语句。 (2)完成类CStudent的成员函数void addscore(double sc)的定义,将类数据成员score加上参数sc,请在注释“//**2**”之后添加适当的语句。 (3)添加类CGraduateStudent的成员函数void addpaper(int d)的定义,将int型的参数d的值加到类的数据成员paper上,请在注释“//**3**”之后添加适当的语句。 (4)调用对象sdt的成员函数addpaper使得该考生的论文数量变为10,将请在注释“//**4**”之后添加适当的语句。 输出结果如下: Student Hanson (Advisor:MichaelPhong) Total paper is 10 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test22_3.cpp清单如下: #include<iostream.h> #include<string.h> class CStudent private: char name[30]; int code; double score; public: CStudent(char* nm) //**1** CStudent() void putname(char* nm) strcpy(name,nm); void putcode(int d)code=d; void putscore(double sc)score=sc; void getname(char* nm)strcpy(nm,name); double getcode()return code;) double getscore()return score; void addscore(double sc) //**2** ; class CGraduateStudent:public CStudent private: char advisor[30]; int paper; public: void putadvisor(char*nm) strcpy(advisor, nm); void getadvisor(char*nm) strcpy(nm, advisor); void putpaper(int d) paper=d; //**3** int getpaper() return paper; ; void main() CGraduateStudent sdt; sdt.putname("Hanson"); sdt.putadvisor("MichaelPhong"); sdt.putpaper(8); //**4** char csname[30]; sdt.getname(csname); char caname[30]; sdt.getadvisor(caname); cout<<"Student"<<csname<<endl; cout<<"(Advisor:"<<caname<<")"<<endl; cout<<"Total paper is"<<sdt.getpaper()<<endl;
问答题使用VC6打开下的源程序文件modi1.cpp。本程序要求输入一字符串,然后将其中的小写字母改变成大写字母,字符可能输入多行,当输入字符“?”时,默认字符输入结束,最后把结果输出到界面。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream.h>voidmain(){inti=0;charstr[120];cout"Enterastringendwith"?"."endl;//********error********cin.getline(str,120);//********error********while(str){//********error********if(str[i]>="a")str[i]=str[i]-"a"+"A";i++;}return;}
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数fim(intN[4])的功能是用4个数字,组成互不相同且无重复数字的三位数,并将满足所有条件的数字输出到屏幕,并且每输出一个三位数字就换一行。
程序分析:可填在百位、十位、个位的数字都是1、2、3、0。组成所有的排列后再去掉不满足条件的排列。如果判定第一个数字是0则也去掉。
#include <iostream.h>
void fun(int N[4])
{
}
int main ()
{
int N[4]={1,2,3,0};
fun (N);
return 0;
}
问答题使用VC6打开下的源程序文件modi.cpp,该程序运行时有错误,请改正其中的错误,使得程序正确运行。程序输出:s1:n=20s2:n=10执行s3.add(s1,s2)s3:n=30s4:n=30注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>classTestClass{intn;public:TestClass(){}TestClass(intm){n=m;}//********error********TestClassadd(TestClasss1,s2){this->n=s1.n+s2.n;//********error********return(this);}voiddisp(){cout"n="nendl;}//********error********}voidmain(){TestClasss1(20),s2(10),s3,s4;cout"s1:";s1.disp();cout"s2:";s2.disp();s4=s3.add(s1,s2);cout"执行s3.add(s1,s2)/ns3:";s3.disp();cout"s4:";s4.disp();}
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
This object is no.1
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass():count(0) {cout <<"This object is";}
// ERROR *******found*******
void Inc() const
{cout <<"no." << ++count << endl;}
private:
// ERROR *******found*******
int count = 0;
};
int main()
{
MyClass * obj = new MyClass;
// ERROR *******found*******
obj.Inc();
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程prog3,其中声明了VaIArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是: VaIArray v1=1,2,3,4,5 VaIArray v2=2,2,2,2,2 要求: 补充编制的内容写在“//*******333*******”与“//********666********”之间。不要修改程序的其他部分。 注意: 相关文件包括:main.cpp、ValArray.h。 程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。 //ValArray.h #include<iostream> using namespace std; class 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 ~ValArray()delete [] v; void setElement(int i,int val) v[i] =val; void print (ostreami<size-1;i++) out<<b[i]<<","; out<<v[size-1]<<''; ; void writeToFile (const char*); //main.cpp #include "ValArray.h" ValArray::ValArray (const ValArray ValArray v1(a,5); ValArray v2(v1); for (int i=0;i<5;i++) v2.setElement(i,2); cout<<"ValArray v1="; v1.print(cout); cout<<endl; cout<<"ValArray v2="; v2.print(cout); cout<<endl; writeToFile(" "); return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)定义私有常量PI,请在注释//********1********后添加适当的语句。(2)完成类的私有常量PI的赋值,以及完成对半径radius的赋值。请在注释//********2********后添加适当的语句。(3)完成计算圆面积的函数GetArea()。请在注释//********3********后添加适当的语句。(4)完成计算圆周长的函数GetGinh()。请在注释//********4********后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>#define CONST_PI3.141592class CCircle{private:double radius;//********1********public://********2********CCircle(int radius)(this->radius=radius;}double GetArea(){//********3********}double GetGirth(){//********4********}void SetRadius(int m){radius=m;}};int main(){CCircle circle(5);cout<<circle.GetArea()<<endl;cout<<circle.GetGirth()<<endl;circle.SetRadius(10);cout<<Circle.GetArea()<<endl;cout<<circle.GetGirth()<<endl;return0;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序输出的结果为: Max is 7 提示:max()数实现找出两个数中的最大值,并作为函数值返回。 注意:错误的语句在 //******error******的下面,修改该语句即可。#include<iostream>using namespace std; //******error******int max(int a,int b){ if(a<b) { int t=a; a=b; b=t; } return b;}int main(){ int m=-3; int n=7; //******error****** max(-3,n); cout<<"Max is"<<m<<end1; return 0;}
问答题使用VC6打开下的源程序文件modi2.cpp。完成函数fun(inta[],intn),实现递归函数fun(inta[],intn)的返回值是数组a[]的前n个元素之和。注意:不能修改程序的其他部分,只能修改fun()函数。#include<iostream.h>intfun(inta[],intn){}voidmain(){intA[]:{1,2,3,4};coutfun(A,sizeof(A)/izeof(int))endl;return;}
问答题使用VC6打开
下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)定义私有常量PI,请在注释//********1********后添加适当的语句。
(2)完成类的私有常量PI的赋值,以及完成对半径radius的赋值。请在注释//********2********后添加适当的语句。
(3)完成计算圆面积的函数GetArea()。请在注释//********3********后添加适当的语句。
(4)完成计算圆周长的函数GetGirth()。请在注释//********4********后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include<iostream.h>
#define CONST_PI 3.141592
class CCircle
{
private:
double radius;
//********1********
public:
//********2********
CCircle(int radius)
{this->radius=radius;
double GetArea()
{
//********3********
}
double GetGirth()
{
//********4********
}
void SetRadius(int m)
{
radius=m;
}
};
int main()
{
CCircle circle(5);
cout
circle.GetArea()
endl;
cout
circle.GetGirth()
endl;
circle. SetRadius(10);
cout
circle. GetArea()
endl;
cout
circle.GetGirth()
endl;
return 0;
}
