问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3,此工程中包含一个源程序文件p,oj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示矩形的类MyRectan~e;程序应当显示:(0,2)(2,2)(2,0)(0,0)4但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:(1)在“//**1******found****”的下方是构造函数的定义,它用参数提供的左上角和右下角的坐标对up_left和down_fight进行初始化。(2)在“//**2******found****”的下方是成员函数getDownLeft的定义中的一条语句。函数getDownLeft返回用MyPoint对象表示的矩形的左下角。(3)在“//**3******found****”的下方是成员函数area的定义,它返回矩形的面积。注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。//proj3.cpp#include<iostream>usingnamespacestd;classMyPoint{//表示平面坐标系中的点的类doublex;doubley;public:MyPoint(doublex,doubleY){this->x=x;this->y=y;)doublegetX()const{returnx;)doublegetY()const{returnY;}voidshow()const{cout<<'('<<x<<','<<y<<')';)};classMyRectangle{//表示矩形的类MyPointup_left;//矩形的左上角顶点MyPointdown_riqht;//矩形的右下角顶点public:MyRectangle(MyPointupleft,MyPointdownright);MyPointgetUpLeft()const{returnup_1eft;}//返回左上角坐标MyPointgetDownRight()const{returndown_right;//返回右下角坐标MyPointgetUpRight()const;//返回右上角坐标MyPointgetDownLeft()const;//返回左下角坐标doublearea()const;//返回矩形的面积);//**1************found**********MyRectangle::MyRectangle(________):up_left(p1),down_right(p2){)MyPointMyRectangle::getUpRight()const{returnMyPoint(down_right.getX(),up_left.getY());}MyPointMyRectangle::getDownLeft()const{//**2************found**********returnMyPoint(________);}//**3************found**********________area()Iconst{return(getUpLeft().getX()-getDownRight().getX())*(getDownRight().getY()-getUpLeft().getY());}intmain(){MyRectangler(MyPoint(0,2),MyPoint(2,0));r.getUpLeft().show();r.getUpRight().show();r.getDownRight().show();r.getDownLeft().show();cout<<r.area()<<end1;return0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个头文件shape.h,其中包含了类Shape、Point和Triangle的声明;包含程序文件shape.cpp,其中包含了类Triangle的成员函数和其他函数的定义;还包含程序文件proj2.cpp,其中包含测试类Shape、Point和Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为: 此图形是一个抽象图形,周长=0,面积=0 此图形是一个三角形,周长=6.82843,面积=2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 //shape.h class Shape public: virtual double perimeter()const return 0; //返回形状的周长 virtual double area()const return 0;) //返回形状的面积 virtual const char * name()constreturn"抽象图形"; //返回形状的名称 ; class Point //表示平面坐标系中的点的类 double x; double y; public: //**********found********** Point (double x0, double y0):______//用x0、y0初始化数据成员X、Y double getX() constreturn x; double getY() constreturn y; ; class Triangle:public Shape //**********found********** ______; //定义3个私有数据成员 public: Triangle(Point p1,Point p2,Pointp3):point1(p1),point2(p2),point3(p3) double perimeter()const; double area () const; const char* name () constreturn "三角形"; //shape.cpp #include "shape.h" #include <cmath> double length(Point p1,Point p2) return sqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY())); double Triangle::perimeter()const //一个return语句,它利用length函数计算并返回三角形的周长 //**********found********** double Triangle::area()const double s=perimeter () /2.0; return sqrt (s* (s-length(point1,point2))* (s-length(point2,point3))* (s-length(point3,point1))); //proj2.cpp #include"shape.h" #include<iostream> using namespace std; //**********found********** ______ //show函数的函数头(函数体以前的部分) cout<<"此图形是一个"<<shape.name()<<",周长="<<shape.perimeter()<<",面积="<<shape.area()<<endl; int main() Shape S; Triangle tri (Point(0,2),Point(2,0),Point(0,0)); show(s); show(tri); return 0;
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:smallersmallerSillallerlargest注意:只修改注释“//ERROR**********found**********”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>usingnamespacestd;constintSize=4;classMyClass{public:MyClass(intx=0):value(X){}voidSet《intx){value=x;}friendvoidjudge(MyClassaobj);private:intvalue;};//ERROR**********found**********voidMyClass::judge(MyClass&obj){if(obj.value=Size)cout<<"largest"<<end1;elsetout<<"smaller"<<end1;}intmain(){MyClass*ptr=newMyClass[Size];for(inti=0;i<Size;i++){//ERROR**********found**********(ptr+i).Set(i+1);judge(*(ptr+i));}//ERROR**********found**********deleteptr;return0;}
问答题使用VC++6.0打开
下的源程序文件1.cpp,该程序运行时有错误,请改正程序中的错误,使得程序输出:
9,8,7,6,5
注意:错误的语句在/********error********/的下面,修改该语句即可。其他的语句不能修改。
试题程序:
#include<iostream.h>
class TC
{
/********error********/
TC(int i)
{
m_i=i;
}
void print()
{
cout<<--m_i<<",";
}
public:
int m_i;
};
int main()
{
/********error********/
int i();
TC obj(10);
while(i>5)
{
/********error********/
i++;
obj.print();
}
cout<<endl;
return 0;
}
问答题使用VC++6.0打开
下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,输出的结果为
Constructor,i=0,
Destructor
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include<iostream.h>
class TC
{
int i;
public:
TC();
void display();
~TC();
};
//******error******
TC:TC()
{
cout<<"Constructor"<<",";
i=0;
}
//******error******
TC:display()
{
cout<<"i="<<i<<","<<endl;
}
//******error******
TC:TC()
{
cout<<"Destructor"<<endl;
}
void main()
{
TC a;
a.display();
}
问答题使用VC6打开下的源程序文件modi2.cpp。请完成函数fun(char*data),此函数的功能是,找出字符串数组中最小的ASCII值,如果有相同变量,则输出最后一个所在的位置;如果字符串为空,则返回-1;或者不存在时也返回-1。注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回0。依次增加。#include<iostream.h>intfun(char*data){}voidmain(){charstr[1024];cout"请输入一行英文字符串:/n";cin.getline(str,1024);cout"最小的字母出现在距离头部""fun(str)"个字母处"endl;}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。请完成以下两个函数。 (1)fun1(int n)求出n的阶乘,必须使用递归调用。 (2)fun2(int n)求出n的阶乘,不能使用递归调用。如果n小于1则返回0。 注意:不能修改函数的其他部分。 试题程序: #include<iostream.h> //必须使用递归 int fun1(int n) //不能使用递归 int fun2(int n) void main() int i; cout<<"请输入一个整数:"<<end1; cin>>i; cout<<"输入数字的阶乘是:"<<fun1(i)<<end1; cout<<"输入数字的阶乘是:"<<fun2(i)<<end1; return;
问答题请使用【答题】菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明的是一个人员信息类,补充编制程序,使其功能完整。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 TsinghHa。 注意:只能在函数address_change的“//********333*********”和“//**********666********”之间填入若干语句,不要改动程序中的其他内容。 程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//proj3.h#include<iostream>#include<string>using namespace std;class Person{ char name[20]; int age; char* address; public: Person(){age=0;address=0;) void name_change(char*_name);//名字修改函数 void age_change(int_age);//年龄修改函数 void address_change(char*_add);//地址修改函数 void info_display();//人员信息显示 一Person();//析构函数};void writeToFile(const char*path);proj3.cpp#include<iostream>#include<string>#include”proj 3.h”using namespace std;void Person::name change(char*_name){ strcpy(name,_name);}void Person::age_change(int_age){age=_age;}void Person::address_change(char*_add){ if(address!=NULL)delete[]ad-dress;//********333********//********666********}void Person::info_display(){ cout<<name<<’\t’ <<age<<’/t’; if(address!=NULL) cout<<address<<endl; cout<<endl; } Person::一Person(){ if(addresS!=NULL) delete[]address; } void main() { Person p1; p1.name change(”Zhang”); p1.age_change(20); p1.address_change(”Tsinghua Uni-versity”); p1.address_change(”Tsinghua”); p1.info_display(); writeToFile(””); }
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmj1,其中有点类Point和线段类Line和主函数main的定义,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出应为: p1=(8,4)p2=(3,5) 注意:只修改两个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>#include<cmath>using namespace std;clasS Point{ double x,Y;public: Point(double x=0.0,double Y=0.0)//ERROR**********found********** {X=x;Y=Y;) double getX()const(return X;) double getY()const{return y;}//ERROR**********found********** void show()const{cout<<’(’<<X<<’,’<<Y<<’)’)};clasS Line{ Point p1,p2;public: Line(Point pt1,Point pt2) //ERROR**********found********** {pt1=p1;pt2=p2;} Point getP1()const{return p1;} Point getP2()const{return p2;) }; int main(){ Line line(Point(8,4),Point(3,5)); cout<<”p1=”; line.getP1().show(); cout<<”p2=”; line.getP2().show(); cout<<endl; return 0; }
问答题使用VC6打开下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)完成构造函数的定义,请在注释//********1********后添加适当的语句。(2)定义类的友元函数fun(),请在注释//********2********后添加适当的语句。(3)定义类的友元函数main(),请在注释//********3********后添加适当的语句。(4)补充语句,释放内存。请在注释//********4********后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>classTestClass{public://********1********{data=d;next=NULL;}voidSetNext(TestClass*p){next=p;}private:intdata;TestClass*next;//********2********//********3********};TestClass*fun(TestClass*h){TestClass*t,*s,*u,*v;s=h->next;h->next=NULL;while(s!=NULL){for(t=s,v=h;v!=NULLu=v,v=v->next);s=s->next;if(v==h)h=t;elseu->next=v;t->next=v;}returnh;}voidmain(){TestClass*h=NULL;TestClass*q=NULL;do{intdata;cout"pleaseinputanumber,andendwith0"endl;cindata;if(data==0)break;TestClass*p=newTestClass(data);if(h==NULL){h=q=p;}else{q->SetNext(p);q=p;}}while(1);h=fun(h);for(;h!=NULL;){q=h->next;couth->data""endl;deleteh;//********4********}return;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//t+++found母
***”。
#nClude using namespaCe std; //矩阵基础类,一个抽象类
Class MatrixBase{ int rows,Cols; publiC:
MatrixBase(int rows,int Cols):roWS(rows),Cow(Cols){} int getRows()Const{retum rows;}//矩阵行数
int getCols()Const{return Cols;}//矩阵列数
virtual double getElement(int r,int C)Const=0;//取第i个元素的值 void show()Const{//分行显示矩阵中所有元素
for(int i=0;i
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程pl,og3,其中声明了ValArray类,该类在内部维护一个动态分配的int型数组v。ValArr~类的成员函数cycle用于对数组元素进行向左循环移动。调用一次cycle后,数组的第二个元素至最后一个元素都将向左移动一个位置,而最左端的元素将循环移动到最右端位置上。例如,若ValArray表示的数组为{1,2,3,4,5},则第一次调用cycle后,数组变为{2,3,4,5,1},第二次调用cycle后,数组变为{3,4,5,1,2},依次类推。请编写成员函数cycle。在main函数中给出了一组测试数据,此情况下程序的输出应该是:v={1,2,3,4,5}v={2,3,4,5,1}v={3,4,5,l,2}v={4,5,1,2,3}v={5,1,2,3,4}要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFfle已经编译为obj文件,并且在本程序中调用。//VaiArray.h#include<iostream>usingnamespacestd;classVaiArray{int*v;intsize;pubiic:VaiArray(constint*p,intn):size(n){v=newint[size];for(inti=0;i<size;i++)v[i]=P[i];}~VaiArray(){delete[]v;}voidcycle();voidprint(ostream&out)const{out<<'{';for(inti=0;i<Size-1;i++)out<<v[i]<<",";out<<v[size-1]<<'}'}};voidwriteToFile(constchar*);//main.cpp#include"ValArray.h"voidValArray::cycle(){//将数组v中的size个整数依次移动到它的前一个单元,其中第一个整数移到原来最后元素所在单元。//********333********//********666********}intmain(){constinta[]={1,2,3,4,5);ValArrayv(a,5);for(inti=0;i<5;i++){cout<<"v=";v.print(cout);cout<<end1;v.cycle();}writeToFile("");return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数fun(int n)的功能是在n行n列的矩阵中,每行都有最大的数,本程序求这n个最大数中的最小一个,并作为参数返回。
注意:不能修改程序的其他部分,只能修改fun()函数。
#include
#define N 100
int a[N][N];
int fun(int n)
{
}
void main()
{
int n;
cout>n;
for(int i=0;i>a[i][j];
}
cout<<"The min of max numbers is"<
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了Employee类和Manager类。Employee用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager是Employee的公有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Nanager的属性还包括级别(level)。Employee类的成员函数print用于输出雇员的信息;Manager类的成员函数print负责输出经理的信息。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
Name:Sally Smith
Dept:Sales
Level:2
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee (string name, string dept):
//**********found**********
______
{}
virtual void print() const;
string dept () const //返回部门名称
{
//**********found**********
______
}
virtual ~Employee() {}
private:
string name_;
string dept_;
};
class Manager : public Employee {
public:
Manager (string name, string dept, int level):
//**********found**********
______
{}
virtual void print() const;
private:
int level_;
};
voidEmployee::print() const
{
cout << "Name: " << name_<< endl;
cout << "Dept: " << dept_<< endl;
}
voidManager::print() const
{
//********** found**********
______
cout << "Level: " << level_<<endl;
}
int main ()
{
Employee* emp = new Manager("Sally Smith", "Sales", 2);
emp->print();
delete emp;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹prog1下的工程prog1。此工程中包含程序文件main.cpp,其中有类Score(“成绩”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
学号:12345678课程:英语总评成绩:85
注意:只修改每个“//ERROR****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Score {
public:
Score (const char * the_course,const char * the_id, int the_normal,int the_midterm, int the_end_of_term)
: course (the_course), normal (the_normal), midterm (the_midterm), end_of_term(the_end_of_term){
//ERROR **********found**********
strcpy (the_id, student_id) ;
}
const char * getCourse ()const{ return course; } //返回课程名称
//ERROR **********found**********
const char * getID () const { return } //返回学号
int getNormal () const { return normal; } //返回平时成绩
int getMidterm()const{ return midterm; }
int getEndOfTerm () const { returnend of term; } //返回期末考试成绩
int getFinal ()const; //返回总评成绩
private :
const char * course; //课程名称
char student_id[12]; //学号
int normal; //平时成绩
int midterm; //期中考试成绩
int end of term; //期末考试成绩
};
//总评成绩中平时成绩占20%,期中考试占30%,期末考试占50%,最后结果四舍五入为一个整数
// ERROR **********found**********
int getFinal () const {
return normal* 0.2 +midterm* 0.3 +end_of_term* 0.5 +0.5;
}
int main () {
char English[ ] ="英语";
Score score (English, "12345678", 68, 83, 92);
cout <<"学号:" <<score. getID () <<"";
cout <<"课程:" <<score. getCourse()<<"";
cout <<"总评成绩:" <<score.getFihal () <<endl;
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程prog1。其中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
v1=23;v2=42
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class MyClass
{
int v1;
static int v2;
public:
MyClass(int v) : vl (v) {}
int getValue() const { return v1; }
static int getValue(int dummy)
{
// ERROR ********** found**********
return v1;
}
};
// ERROR ********** found**********
int MyClass.v2 = 42;
int main()
{
MyClass obj (23);
// ERROR ********** found**********
int v1 = obj.v1;
int v2 = MyClass::getValue(0);
cout << "v1 = " << v1 << "; v2 = "<< v2 << endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为:Hello Visual C++!Hello Visual C++!!注意:错误的语句在//******error******的下面,修改该语句即可。#include<iostream.h>class CMyObjett{public:CMyObject(){//******error******cout<<"Hello,"VisualC++!"<<endl;}CMyObject(int n){//******error******cout<<"Hello,\nVisual C++'!"<<endl;}//******error******^CMyObject(){}};void main(){CMyObject obj1;CMyObject obj2(0);}
问答题简单应用题
常用字符串函数strcat(s1,s2)可将字符串s2添加到字符串s1的末端,但其使用必须保证字符串s1足够大,以便保存它自己的内容和字符串s2中的内容。请编写一个函数char *append(char *s1,char *s2),其可将字符串s2添加到字符串s1的末端,而且不受s1空间大小的限制。请利用常用字符串函数实现。
常用字符串函数说明:
strcpy(to,form):将form字符串复制到to字符串;
strcat(s1,s2):将字符串s2添加到字符串s1的末端,但必须保证字符串s1足够大;
strlen(s):返回字符串s的长度;
注意:部分源程序已存在文件test4_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数append的花括号中填写若干语句。
输出结果如下:
this is a string.
文件test4_2.cpp的内容如下:
#include
#include
char *append(char *s1,char *s2)
{
}
void main()
{
char *s,*s1,*s2;
s1="this is ";
s2="a string.";
s=append(s1,s2);
cout<
问答题改错题
使用VC6打开考生文件夹下的工程test6_1,此工程包含一个源程序文件test6_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor2
Constructor1
i=0
i=10
Destructor
源程序文件test6_1.cpp清单如下:
#include
class CSample
{
int i;
public:
CSample(){coutdisp();
/**********found**********/
b->disp();
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:The value is10注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostrearn>using namespace std;class NyClass{int value;public://ERROR********found********void MyClass(int val):value(val){}int GetValue()const{return value;}void SetValue(int val);};//ERROR ********found********inline void SetValue(int val){value=val:}int main(){MyClass obj(0);obj.SetValue(10);//ERROR********found********下列语句功能是输出obj的成员value的值cout<<"The value is"<<obj.value<<end1;return0;}