问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程含有一个源程序文件pmj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is 10 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。//proj1.cpp#include<iostream>using namespace std;class MyClass{ 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<<endl; return 0; }
问答题使用VC6打开考生文件夹proj2下的工程proj2,其中有元素类Element和队列类Queue的定义。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的输出结果应为:3850507注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。#include<iostream>#include<cmath>#include<cstdliti>using nameespace std:#define MaxLength100class Element{//“元素”类public:int n;Element(int i=0):n(i){}};class Queue{//“队列”类Element*element;//指向存储元素的数组的指针int tail;//队尾元素的下标public:Queue():element(new Element[100]),tail(-1){}~Queue(){delete[]element;}void push(Element ele);//在队列尾端添加一个元素Element pop();//在队列首端删除一个元素,返回被删元素Element front()const{return element[0];}//返回队首元素,但不从队列中删除该元素//**********found**********int size()const{return(______________);}//返回元素个数void show()const;//显示集合中所有元素};void Queue::push(Element ele){if(tail==MaxLength-1)return;//空间满,不做任何处理//**********found**********____________________________;}Element Queue::pop(){if(size()==0)exit(1);//队列空,不做任何处理Element tmp=element[0];for(int i=0;i<tail;i++)element[i]=element[i+1];//**********found**********____________________________;return tmp;}void Queue::show()const{//**********found**********for(_____________________)cout<<element[i].n<<' ';cout<<endl:}int main(){Queue q;q.push(3);q.push(8);q.push(5);q.push(0);q.show();q.pop();q.poP();q.push(7);q.show();return0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹prog3下的工程prog3,其中包含了类TaxCalculator(“个税计算器”)和主函数main的定义。创建“个税计算器”需要接收税率表信息和起征额信息。在main函数中,通过两个数组创建了如下的税率表:利用这个税率表创建“个税计算器”时,假定起征额为2000元(即不超过2000元的所得不征收个人所得税)。请补充完成计算应纳个人所得税额的成员函数getTaxPayable,其中的参数income为月收入。此程序的正确输出结果应为:月收入为800元时应缴纳个人所得税0元月收入为1800元时应缴纳个人所得税0元月收入为2800元时应缴纳个人所得税55元月收入为3800元时应缴纳个人所得税155元月收入为4800元时应缴纳个人所得税295元月收入为5800元时应缴纳个人所得税455元注意:只能在函数getTaxPayable中的“//**********333**********”和“//**********666**********”之间填入若干语句,不要改动程序中的其他内容。//TaxCalculator.h#include#includeusingnamespacestd;clasSTaxCalculator{public:TaxCalculator(doublethe_limits[],doubletherates[],intthelength,doublethe_threshold):lowerlimits(newdouble[thelength]),rates(newdouble[the—length]),list_len(the_length),thresh-old(thethreshold){for(inti=0;i=0){//********333********//********666********一一i;}returntaxpayable;}voidTaxCalculator::showTaxPayable(doubleincome)const{cout
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。该工程中包含程序文件main.cpp,其中有类CDate(“日期”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
原日期:2005-9-25
更新后的日期:2006-4-1
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cstdlib>
using namespace std;
class CDate //日期类
{
// ERROR *********found*********
protected:
Cmate() {};
CDate(int d, int m, int y)
{
// ERROR *********found*********
SetDate(int day=d, int month=m, int year = y);
};
void Display(); //显示日期
void SetDate (int day, int month,int year)
//设置日期
{ m_nDay=day; m_nMonth =month; m_nYear = year; }
private:
int m_nDay; //日
int m_nMonth; //月
int m_nYear; //年
};
void Cmate::Display() //显示日期
{
// ERROR *********found*********
cout << m_nDay << "-" << m_nMonth << "-" << m_nYear;
cout << endl;
}
int main ()
{
CDate d (25, 9, 2005);
//调用构造函数初始化日期
cout << "原日期:";
d. Display ();
d. SetDate(1, 4, 2006);
//调用成员函数重新设置日期
cout << "更新后的日期:";
d. Display();
return 0;
}
问答题使用VC++6.0打开下的源程序文件1.cpp,该程序运行时有错误,请改正错误,使程序正常运行,并且要求最后一个catch必须抛出执行的任何异常。
程序异常,输出信息为
error
0
ERROR
注意:不要改动main函数,不能增加或删除行,也不能更改程序的结构,错误的语句在//******error******的下面。
试题程序:
#include<iostream.h>
int main()
{
try
{
throw("error");
}
//********error********
catch(char s)
{
cout<<s<<endl;
}
try
{
throw((int)0);
}
//********error********
catch()
{
cout<<i<<endl;
}
try
{
throw(0);
throw("error");
}
//********error********
catch()
{
cout<<"ERROR"<<endl;
}
return 0;
}
问答题使用VC++6.0打开
下的源程序文件1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,其输出的结果为
30
130
注意:错误的语句在//******error******的下面,修改该语句即可。
试题程序:
#include<iostream.h>
int a=10;
class TC
{
public:
TC()
{
a=b=0;
}
void display()
{
//******error******
cout<<a<<b<endl;
}
void func(int a)
{
//******error******
a+=a;
}
void func2()
{
//******error******
a+=a;
}
private:
int a,b;
};
void main()
{
TC obj;
obj.func(3);
obj.display();
obj.func2();
obj.display();
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)重载运算符int,请在注释∥********1********后添加适当的语句。
(2)重载运算符“
using namespace std;
C1ass TestClass
{
public:
∥********1********
{
cout<<”int”<
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (1)定义类的私有变量a、b,请在注释//********1********后添加适当的语句。 (2)完成类的构造函数,对变量a、b赋值,缺省两个变量值为1,在注释//********2********之后添加语句。 (3)完成打印函数display(),打印出“a*b=c”格式的一个乘法算式,在注释//********3********之后添加语句。 (4)补充主函数,使得打印9×9的乘法口诀。在注释//********4********后添加语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>class CMyFormula{private: //********1********public: //********2******** { this->a=a; this->b=b; } void set(int x,int y) { a=x; b=y; } void display() { //********3******** }};int main(){ CMyFormula formular(1,1), for(int i=1;i<=9;i++) { //********4******** for(int j=1; j++) { formular.set(j,i), formular.display();cout<<" "; } cout<<end1; } return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义类的公有数据成员函数No、Name[32],其中No是int型的数据,Name[32]为字符型。请在注释//********1********后添加适当的语句。 (2)完成函数set的定义,分别设置No和Name的值,请在注释//********2********后添加适当的语句。 (3)完成函数print(),请在注释//********3********后添加适当的语句,打印的输出如下: No=111 Name=Garfield (4)加缺省构造函数,设置No为0,Name为空,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class TestClass{public: //********1******** //********2******** { No=no; for(int i=0;strName[i]!=0;i++) { Name[i]=strName[i]; Name[i+1]=0; } } void print() { //********3******** } Testelass() { //********4******** }};int main(){ TestClass stu; stu.Set(111,"Garfield"); stu.print(); return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。完成fun()函数,其功能是:求出M行N列二维数组每行元素中的最小值,并计算它们的和值。和值通过形参传回主函数输出。
注意:不能修改程序的其他部分,只能修改fun()函数。
#include
#define M 2
#define N 4
void fun(int a[M][N],int *sum)
{
}
void main()
{
int x[M][N]={7,6,5,2,4,2,8,3};
int s;
fun(x,&s);
cout<
问答题请使用VC6或使用【答题】菜单打开
proj1下的工程proj1。其中有线段类Line的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
End point 1 = (1,8),End point 2 = (5,2),length = 7.2111。
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Line;
double length(Line);
class Line{ //线段类
double x1,y1; //线段端点1
double x2,y2; //线段端点2
public:
// ERROR *******found*******
Line (double x1, double y1, double x2, double y2) const {
this -> x1 = x1;
this -> y1 = y1;
this -> x2 = x2;
this -> y2 = y2;
}
double getX1() const {return x1;}
double getY1() const {return y1;}
double getX2() const {return x2;}
double getY2() const {return y2;}
void show() const {
cout << "End point 1 = ("<< x1 << "," << y1;
cout << "), End point 2 = ("<< x2 << "," << y2;
// ERROR *******found*******
cout << "), length = " << length (this)
<< "。" << endl;
}
};
double length (Line i) {
// ERROR *******found*******
return sqrt ((1.x1 - 1.x2) * (1.x1 - 1.x2) + (1.y1 - 1.y2) * (1.y1 - 1.y2));
}
int main() {
Line r1(1.0,8.0,5.0,2.0);
r1.show();
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeT。File.obj。补充完成重载赋值运算符函数,完成深复制功能。屏幕上输出的正确结果应该是:Hello!Happy new year!要求:补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。注意:程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。//proj3.h#include<iostream>#include<iomanip>using namespace std;class MiniString{public:friend ostream &operator<<(ostream &output,const MiniString &s)//重载流插入运算符{output<<s.sPtr;return output;}friend istream &operator>>(istream &input,MiniString &s)//重载流提取运算符{char temp[100];//用于输入的临时数组temp[0]='\0';//初始为空字符串input>>setw(100)>>temp;int inLen=strlen(temp);//输入字符串长度if(inLen!=0){s.length=inLen;//赋长度if(s.sPtr!=0)delete[]s.sPtr;//避免内存泄漏s.sPtr=new char[S.length+1];stcpy(s.sPtr,temp);//如果s不是空指针,则复制内容}elses.sPtr[0]='\0';//如果s是空指针,则为空字符串return input;}void modString(const char * string2)//更改字符串内容{if(string2!=0)//如果string2不是空指针,则复制内容{if(strlen(string2)!=length){length=strlen(stringy2);delete[]sPtr;sPtr=new char[length+1];//分配内存}strcpy(sPtr,strng2);}else sPtr[0]='\0';//如果string2是空指针,则为空字符串}MiniString& operator=(const MiniString &otherString);MiniString(const char * s=" "):length((s!=0)?strlen(s):0)//构造函数{sPtr=0:if(length!=0)setString(s);}~MiniStfing()//析构函数{delete[]sPtr;}private:int length;//字符串长度char *sPtr;//指向字符串起始位置void setString(const char * string2)//辅助函数{sPtr=new char[strlen(string2)+1];//分配内存if(string2!=0)strcpy(sPtr,string2);//如果string2不是空指针,则复制内容elsesPtr[0]='\0';//如果string2是空指针,则为空字符串}};//proj3.cpp#include<iostream>#include<iomanip>using namespace std;#include"proj3.h"MiniString& MiniString::operator=(const MiniString &otherString){//重载赋值运算符函数。提示:可以调用辅助函数setString//**********333**********//**********666**********}int main(){MiniString str1("Hello!"),str2;void writeToFile(const char*);str2=str1;//使用重载的赋值运算符str2.modString("Happy new year!");cout<<str1<<'\n';cout<<str2<<'\n':writeToFile(" ");return0;}
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下:
Constructor.
Default constructor.
Areais 12
Area is 0
Area is 12
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class CRectangle
{
private:
double length l width;
public:
CRectangle()
{
coutlength=1;
this一>width=w;
}
void GetArea()
{
cout<<”Area is”<
问答题使用VC6打开考生文件夹proj2下的工程proj2。其中在编辑窗口内显示的主程序文件中定义有类XBase和XDerived,以及主函数main。程序文本中位于每行"//**********found**********下面的一行内有一处或多处下划线标记,请在每个下划线标记处填写合适的内容,并删除掉下划线标记。经修改后运行程序,得到的输出结果为:s=25#include<iostream>using namespace std;class XBase{ private: int mem1,mem2; public: //**********found********** XBase(int m1=0,int m2=0):meml(m1),_________{} //**********found********** virtual int__________};//**********found**********class XDerived:__________{ private: int mere3; public: XDerived():XBase(),mere3(0){} //**********found********** XDerived(int ml,int m2,int m3):__________{} int sum(){return XBase:slim()+mem3;}};void main(){ XDerived b(3,4,5); XBase a(6,7),*pb=&b; int S=pb->sum()+a.sum(); cout<<"s="<<s<<endl:}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说日月和代码,完成空出部分程序。函数fun(int*arr,int n)的功能是将指定的有序数组压缩成各元素互不相同的有序数组,即相同数只保留一个,多余的被删除:并返回互不相同的元素的个数。 注意:不能修改程序的其他部分,只能修改fun()函数,#include<iostream>int fun(int*a,int n){}void msin(){ int A[]={6,6,5,4,4,3,3,2,1}; Int j =fun(A,s i zeof(A)/si zeof(int)); for(int i=0 ; i<j ;i++) { std::cout<<A[i]<<‘ ’; } std::cout<<s td::endl; return;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和C代码,完成空出部分的程序,实现函数fun(char*s1,char*s2)将字符串s2连接在字符串s1之后,构成一个首指针为s1的字符串。注意:不能修改函数的其他部分。#include<iostream.h>#define MAXLINE1000void fun(char*s1,char*s2){}void main(){char s1[NAXLINE];char s2[256];cout<"please input a string:"<<endl;cin.getline(s1,NAXLINE);Cout<<"please input otherstring:"<<endl;cin.getline(s2,256);fun(s1,s2);cout<<s1<<endl;return;}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中声明了一个单向链表类sList。sList的成员函数Prepend的功能是在链表头部加入一个新的元素。请编写成员函数Prepend。在main函数中给出了一组测试数据,此时程序的输出应为:
B->A->
###
A->
###
A->
###
exiting inner block
exiting outer block
注意:只在函数Prepend的“// *******333*******”和“// *******666*******”之间填入若干语句,不要改动程序中的其他内容。
//SList.h
struct sListItem {
char data;
sListItem * next;
};
class sList {
public:
sList(): h(0) {}
//0表示空链表
~sList();
void Prepend (char c);
//在链表前端加入元素
void Del();
//删除链表首元素
sListItem * First() const {return h;}
//返回链表首元素
void Print() const;
//打印链表内容
void Release();
//销毁链表
private:
sListItem * h;
//链表头
};
void writeToFile (const char *);
//main.cpp
#include <iostream>
#include "sList.h"
using namespace std;
sList:: ~sList()
{
Release();
}
void sList::Prepend (char c)
{
// *******333*******
// *******666*******
}
void sList::Del()
{
sListItem * temp = h;
h = h -> next;
delete temp;
}
void sList::Print() const
{
sListItem * temp = h;
while (temp != 0) //判断是否到达链表尾部
{
cout << temp -> data << "->";
temp = temp -> next;
}
cout << "/n###" << endl;
}
void sList::Release()
{
while (h != 0)
Del();
}
int main()
{
sList * ptr;
{
sList obj;
obj.Prepend("A");
obj.Prepend("B");
obj.Print();
obj.Del();
obj.Print();
ptr =
ptr -> Print();
cout << "exiting inner block" << endl;
}
cout << "exiting outer block" << endl;
writeToFile(" ");
return 0;
}
问答题改错题
使用VC6打开考生文件夹下的工程test7_1,此工程包含一个源程序文件test7_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor1
Constructor1
Constructor1
Destructor
Constructor2
Destructor
x=0
x=5
Destructor
Destructor
源程序文件test1_1.cpp清单如下:
#include
class B
{
int x;
public:
B(){x=0;cout<<"Constructor1"<
问答题使用VC6打开考生文件夹下的源程序文件modi2.epp。请完成函数fun(char*s),该函数完成以下功能: (1)把S中的大写字母转换成小写字母,把其中的小写字母转换成大写字母。并且在函数中调用写函数WriteFile()将结果输出到modi2.txt文件中。 例如:s=''helloTEST'',则结果为:s=''HELLOtest'' (2)完成函数WriteFile(char*s),把字符串输入文件中。 提示:打开文件使用的第二参数为ios 注意:不要改动main函数,不得增行或行,也不得更改程序的结构。#include<iostream>#include<fstream>#include<cmath>using narnespace std;void WriteFile(char*s){}void fun(ehar*s){}void ClearFile(){ orstream out1; out1.open(''modi2.txt''); out1.close(); } intmain() { ClearFile(); char s[1024]; tout<<''please input a string:''<<endl; cin.getline(s,1024); fun(s); return.0; }
问答题请使用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****”。//proj 2.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&sRef){char ch[]={’a’,’b’,’C’);cout <<ch[0] <<”, ” <<ch[1] <<”,”<<ch[2]<<endl;sRef.push(ch[0]);sRef.push(ch[1]);sRef.push(ch[2]); cout<<sRef.pop()<<”,”; cout<<sRef.pop()<<”,”; cout<<sRef.pop()<<endl;}int main(){ ArrayStack as(10); f(as); return 0;}
