问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.epp,请将堆栈类的定义补充完整。使程序的输出结果为:Theelementofstackare:4321注意:请勿修改主函数main和其他函数中的任何内容,只在横线处编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#include<iostream>usingnamespacestd;constintSize=5;classStack;C1assItem{public://********found********Item(constint&val):________{}//构造函数对item进行初始化private:intitem;Item*next;friendclassStack;};classStack{public:Stack():top(NULL){)~Stack();intPop();voidPush(constint&);private:Item*top;};Stack::~Stack(){Item*P=top,*q;while(p!=NULL){q=P->next;//********found********________;//释放p所指向的节点p=q;}}intStack::Pop(){Item*temp;intret;//********found********________;//使temp指向栈顶节点ret=top->item;top=top->next;deletetemp;returnret;}voidStack::Push(constint&val){Item*temp=newItem(val);//********found********________;//使新节点的next指针指向栈顶数据top=temp;}intmain(){Stacks;for(inti=1;i<Size;i++)s.Push(i);cout<<"Theelementofstackare:";for(i=1;i<Size;i++)cout<<s.Pop()<<'\t';return0;}
问答题请使用VC6或使用[答题]菜单打开proj2下的工程proj2,该工程中包含一个程序文件main.cPP,其中有坐标点类point、线段类Line和三角形类Triangle的定义,还有main函数的定义。程序中两点间距离的计算是按公式实现的,三角形面积的计算是按公式实现的,其中。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:Side1:9.43398Side2:5Side3:8area:20注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>#include<cmath>usingnamespacestd;classPoint{//坐标点类public:constdoublex,y;Point(doublex=0.0,doubley=0.0):x(x),y(y){}//**********found**********doubledistanceTo(______)const{//到指定点的距离returnsqrt(x-P.x)*x-P.x)+(y-P.y)*(y-P.y));}};classLine{//线段类public:constPointp1,p2;//线段的两个端点//**********found**********Line(Pointp1,Pointp2):______{}doublelength()const{returnp1.distanceTo(p2);}//线段的长度};classTriangle{//角形类public:constPointp1,p2,p3;//三角形的三个顶点//**********found**********Triangle(______):p1(p1),p2(p2),p3(p3){}doublelength1()const{//边p1,p2的长度returnLine(p1,p2).length();doublelength2()const{//边p2,p3的长度returnLine(p2,p3).length();}doublelength3()const{//边p3,p1的长度returnLine(p3,p1).length();}doublearea()const{//三角形面积//**********found**********doubles=______;returnsqrt(s*(s-length1())*(s-length2())*(s-length3()));}};intmain(){Triangler(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0));cout<<"Side1:"<<r.length1()<<endl;cout<<"Side2:"<<r.length2()<<endl;cout<<"Side3:"<<r.length3()<<endl;cout<<"area:"<<r.area()<<endl;return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分的程序。函数func(intA[NUM],int n)实现的功能是将数组的内容进行一次重新排序。排序的方法是:给定11,则下标为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<<end1; return 0;}
问答题使用VC++6.0打开
下的源程序文件1.cpp,但该程序运行有问题,请改正main()函数中的错误,使该程序的输出结果正确。
程序输出:
8
8
注意:错误的语句在/********error********/的下面。修改该语句即可,其他的语句不能修改。
试题程序:
#include<iostream.h>
class C0
{
public:
/********error********/
static int n=0;
C0()
{
n++;
}
~C0()
{
n--;
}
};
/********error********/
int C0::n;
void main()
{
C0 obj;
C0 obj2[7];
C0*c=NULL;
c=
/********error********/
cout<<c.n<<endl;
cout<<C0::n<<endl;
}
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Clock(“时钟”)的定义和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Initial times are
0 d:0 h:0 m:59 s
After one second times are
0 d:0 h:1 m:0 S
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Clock
{
public:
Clock(unsigned long i = 0);
void set(unsigned long i = 0);
void print() const;
void tick(); //时间前进一秒
Clock operator ++();
private:
unsigned long total_sec, seconds, minutes, hours, days;
};
Clock::Clock(unsigned long i)
: total_sec(i), seconds(i % 60),
minutes((i/60) % 60),
hours((i/3600) % 24),
days(i/86400) {}
void Clock::set (unsigned long i)
{
total_sec = i;
seconds = i % 60;
minutes = (i/60) % 60;
hours = (i/3600) % 60;
days = i/86400;
}
// ERROR *******found*******
void Clock::print()
{
cout << days << "d:" << hours << "h:"
<< minutes << "m:" << seconds << "s" << endl;
}
void Clock::tick ()
{
// ERROR *******found*******
set (total_sec ++);
}
Clock Clock::operator ++()
{
tick();
// ERROR *******found*******
return this;
}
int main()
{
Clock ck(59);
cout << "Initial times are" << endl;
ck.print();
++ ck;
cout << "After one second times are" << endl;
ck.print();
return 0;
}
问答题改错题
使用VC6打开考生文件夹下的工程test17_1,此工程包含一个源程序文件test17_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Vehicle message
Car message
Vehicle message
源程序文件test17_1.cpp清单如下:
#include
class vehicle
{
int wheels;
float weight;
public:
/***************** found *****************/
void message(void){ cout>"Car message/n";}
};
class truck:public vehicle
{
int passenger_load;
float payload;
public:
int passengers(void){return passenger_load;}
};
void main()
{
vehicle * unicycle;
unicycle=new vehicle;
/***************** found *****************/
unicycle.message();
delete unicycle;
unicycle=new car;
unicycle->message();
delete unicycle;
unicycle=new truck;
unicycle->message();
delete unicycle;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。该工程中包含一个程序文件main.cpp,其中有类Quadritic、类Root及主函数main的定义。一个Quadritic对象表示一个ax2+bx+c的一元二次多项式。一个Root对象用于表示方程ax2+bx+c=0的一组根,它的数据成员num_of_roots有3种可能的值,即0、1和2,分别表示根的3种情况:无实根、有两个相同的实根和有两个不同的实根。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为(注:输出中的X^2表示x2):
3X^2+4x+5=0.0 无实根
4.5X^2+6X+2=0.0有两个相同的实根:-0.666667和-0.666667
1.5X^2+2X-3=0.0有两个不同的实根:0.896805和-2.23014
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream> #include<iomanip>
#include<cmath> using namespace std; class
Root{ //一元二次方程的根 public: const double
x1; //第一个根 const double x2; //第二个根
const int num of roots; //不同根的数量:0、1或2
//创建一个“无实根”的Root对象 Root(): x1(0.0), x2(0.0),
num_of_roots(0){) //创建一个“有两个相同的实根”的Root对象
Root(double root) //********found********
:______{} //创建一个“有两个不同的实根”的Root对象 Root (double
root1, double root2): x1(root1), x2(root2), num_of_roots(2){}
void show()const{ //显示根的信息 cout<<"/t/t";
switch(num_of_roots){ case 0:
//********found******** ______ case
1: cout<<"有两个相同的实根:"<<x1<<"和"<<x2; break;
default: cout<<"有两个不同的实根:"<<x1<<"和"<<x2;break;
} } }; class Quadratic
{//二次多项式 public: const double a, b, c;
//分别表示二次项、一次项和常数项等3个系数 Quadratic(double a, double b, double
c) //构造函数 //*********found*********
"______{} Quadratic(Quadratic } Quadratic
sub(Quadratic X)const{ //求两个多项式的差
//*********found********* ______ }
double value(double x)const{ //求二次多项式的值
return a*x*x+b*x+c; } Root
root()const{//求一元二次方程的根 double delta=b*b-4*a*c;
//计算判别式 if(delta<0.0) return Root();
if(deita==0.0) return Root(-b/(2*a)); double
sq=sqrt(delta); return
Root((-b+sq)/(2*a),(-b-sq)/(2*a)); } void
show()const{//显示多项式
cout<<endl<<a<<"X^2"<<show-pos<<b<<"x"<<c<<noshowpos;
} void showFunction(){ //显示一元二次方程
show(); cout<<"=0.0"; }
}; int main(){ Quadratic q1(3.0, 4.0,
5.0), q2(4.5, 6.0, 2.0), q3(q2. sub(q1));
q1.showFunction(); q1.root().show();
q2.showFunction(); q2.root().show();
q3.showFunction(); q3.root().show();
cout<<endl; return 0; }
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使输入某年某月某日,可判断这一天是这一年的第几天。程序分析:以3月5日为例,应该先把前两个月的天数加起来,然后再加上5天即本年的第几天(特殊情况:闰年输入月份大于3时需考虑多加一天)。注意:只能补充函数func(int year,int month,int day),请勿改动其他部分的内容。#include<iostream.h>int func(int year,int month,int day){}void main(){cout<<func(2007,2,1)<<endl;cout<<func(2007,i0,i0)<<endl;cout<<func(2008,8,12)<<endl;return;}
问答题使用VC6打开考生文件夹下的工程test5_3。此工程包含一个源程序文件test5_3.cpp,其中定义了一个类Pn,用于求n!。请按要求完成下列操作,将程序补充完整。 (1)定义私有数据成员value(存放n)和fact(存放n!),它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。 (2)完成默认构造函数Fn的定义,使Fn对象的数据成员value值为i,fact值为1。请在注释“//**2**”之后添加适当的语句。 (3)完成函数Cal的定义,该函数计算value的阶乘,并将运算结果保存在fact中,要求使用while循环实现。请在注释“//**3**”之后添加适当的语句。 (4)将主函数补充完整,直接调用类的成员函数,在界面上输出value的阶乘值。请在注释“//**4**”之后添加适当的语句。 注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件test5_3.cpp清单如下: #include<iostream.h> class Fn //**1** public: Fn(int i); void Cal(); void disp(); ; Fn::Fn(int i) //**2** void Fn::Cai() //**3** fact*=i--; void Fn::disp() cout<<value<<"!="<<fact<<endl; void main() int value; cout<<"Enter the value:"; cin>>value; Fn A(value); //**4**
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。(1)完成默认构造函数TestClass的定义,使得TestClass对象的类型为int,默认值为a=0,b=0,c=0,请在注释//********1********后添加适当的语句。(2)定义类的私有成员变量,X、Y、Z类型为int,请在注释//********2********后添加适当的语句。(3)定义类TestClass的数据成员count声明为静态整数型数据成员,请在注释//********3********后添加适当的语句。(4)在构造函数中实现用count表示类TestClass被实现对象的次数。请在注释//********4********后添加适当的语句。本程序的输出结果为:The point is(1,1,1)There are 3 point objectsThe point is(1,2,3)There are 3 point objectsThe point is(0,0,0)There are 3 point objects注意:除在指定的位置添加语句之外,请不要改动程序的其他部分。#include<iostream>using namespace std;class TestClass{public://********1********{X=a;Y=b;Z=c;//********4********}void Display(){cout<<"The point is("<<X<<','<<Y<<','<<Z<<')'<<endl;cout<<"There are"<<count<<"point objects."<<endl;}private://********2********//********3********};int TestClass::count=0;int main(){TestClass p1(1,1,1),p2(1,2,3),p3(0,0,0);p1.Display();p2.Display();p3.Display();return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为: CMyObject,in the construtor ~CMyObject,in the destrutor 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>class CMyObject{ //********error******** //********error******** CMyObject{cout<<"CMyObject,in the constrUtor"<<endl;} ~CMyObject(){cout<<"~CMyObject,in the destrutor"<<endl;}};Void main(){ CMyObject obj1;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp。本程序要求输入一字符串,然后将其中的小写字母改变成大写字母,字符可能输入多行,当输入字符“?”时,默认字符输入结束,最后把结果输出到界面。 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream.h>void main()( int i=0; char str[120]; cout<<”Enter a string endwith'?'."<<end1; //********error******** cin.getline(str,120); //********error******** while(Str) { //********error******** if(str[i]>='a') Str[i] =Str[i] -'a'+'A'; i++; ) cout<<str<<end1; return;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错误,请改正程序中的错误,使得程序输出:
1,2,3,4,5,
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在********error********的下面。
1 #include
2 class TestClass
3 ∥********error********
5 TestClass(int i)
6 {
7 m—i=i,
8 )
9 void print()
10 {
11 cout<<++m—i<<‘,’
12 }
13 public:
14 int m—i ;
15 };
16 int main()
17 {
18 ∥********error********
19 int i(),
20 Test TestClass data(0);
21 while(i>5)
22 {
23 ∥********error********
24 i++;
25 data.print();
26 )
27 cout<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数sum(int n)计算在n范围内,能被7和11整除的所有整数的和(包括n在内)。
注意:不能修改程序的其他部分,只能补充sum()函数。
#include
double sum(int n)
{
}
void main()
{
cout<
问答题使用VC6打开
下的源程序文件modi3.cpp。此程序的运行结果为:
In CDerive"s display().b=1
In CDerive2"s display().b=2
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)定义函数display()为无值型纯虚函数。请在注释//********1********之后添加适当的语句。
(2)建立类CDerive的构造函数,请在注释//********2********之后添加适当的语句。
(3)完成类CDerive2成员函数diaplay()的定义。请在注释//********3********之后添加适当的语句。
(4)定义类Derive1的对象指针d1,类CDerive2的对象指针d2。其初始化值分别为1和2。请在注释//********4********之后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的稗序代码。
#include <iostream>
using namespace std;
class CBase
{
public:
CBase(int i){b=i;}
//********1********
protected:
int b;
};
class CDerive:public CBase
{
public:
void display()
{
cout
"In CDerive "s display()."
" b= "
b
endl;
}
};
class CDerive2: public CBase
{
public:
CDerive2(int i):CBase(i) {}
//********3********
};
void func(CBase *obj)
{
obj->display();
}
void main()
{
//********4********
func (d1);
func (d2);
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个operator+函数。程序的正确输出应该是: 两个数据表: 1,2,3,4,5,6 3,4,5,6,7,8 两个数据表之和: 4,6,8,10,12,14 要求: 补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //DataList.h #include<iostream> using wpace std; class DataListf//数据表类 int len; doubled; public: DataList(int len,double data[]=NULL); DataList(DataList&data); int length()const{return len;} double getElement(int i)const{return d[i];} DataList operator+(const DataList&list)const;//两个数据表求和 void show()const;//显示数据表 }; void writeToFile(char*,const DataList&); //main.clyp #include"DataList.h" Datalist::DatRl.ist(int len,double data[]):len(1en){ d=new double[len]; for(int i=0;i<len;i++) d[i]=(data==NULL?0.0:data[i]); } DataList::DataList(DataList&data):len(data.1en){ d=new double[len]; for(int i=0;i<len;i++) d[i]=data.d[i]; } DataList DataList::operator+(const DataList&list)const{//两个数据表求和 double*dd=new double[list.1ength()]; //********333******** //********666******** return DataList(list.length(),dd); } void DataList::show()const{//显示数据表 for(int i=0;i<len-1;i++) cout<<d[i]<<","; cout<<d[len-1]<<endl; } int main(){ double s1[]={1,2,3,4,5,6}; double s2[]={3,4,5,6,7,8}; DataList listl(6,s1),list2(6,s2);//定义两个数据表对象 cout<<"两个数据表:"<<endl; list1.show(); list2.show(); cout<<endl<<"两个数据表之和:"<<endl; (1istl+list2).show(); writeToFile("",list1+list2); return 0; }
问答题请编写一个函数void fun(char ss[]),该函数将字符串ss翻转,如ss为“123abc”则翻转后为“cba321”。注意:用数组方式及for循环来实现该函数。 注意:部分源程序已存在文件test16_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。 文件test16_2.cpp 的内容如下: #include<iostream.h> #include<string.h> void fun (char ss[]); void main ( ) char s[80]; cout<< "请输入字符串: "; cin>>s; fun(s); cout<< "逆序后的字符串: "<< s<<end1; void fun(char ss[])
问答题请编写一个函数char*change(char instr[]),将输入字符串中的所有小写字母转换为大写字母输出。要求使用for循环实现。如输入jinfeiteng,则输出结果是JINFEITENG。
注意:部分源程序已存在文件test21_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数change的花括号中填写若干语句。
文件test21_2.cpp的内容如下:
char*change(char instr[]);
#include"iostream.h"
void main()
{
char instr[50];
char *outstr;
cout<<"Input a string:"<<endl;
cin>>instr;
outstr=change(instr);
cout<<"Over graded string:"<<endl;
cout<<outstr<<endl;
}
char*change(char instr[])
{
}
问答题改错题
使用VC6打开考生文件夹下的工程kt6_1,此工程包含一个源程序文件kt6_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor2
Constructor1
i=0
i=10
Destructor
源程序文件kt6_1.cpp清单如下:
#include
classCSample
{
inti;
public:
CSample(){coutdisp();
/**********found**********/
b->disp();
}
问答题使用VC6打开下的源程序文件modi1.cpp,但该程序运行有问题,请改正main函数中的错误,使程序的输出结果是:nember=1nember=10nember=100注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>classTestClass{public:TestClass(inti){nember=i;}voidSetNember(intm){nember=m;}intGetNember()const{returnnember;}voidPrint()const{cout"nember="nemberendl;}private:intnember;};voidmain(){//********error********TestClassobj1;obj1.Print();TestClassobj2(3);//********error********obj1.nember=10;//********error********TestClass.SetNember(100);obj1.Print();obj2.Print();}
