问答题使用VC6打开考生文件夹下的工程RevPrroj7。此工程包含一个源程序文件 RevMain7.cpp。在该文件中,函数fun()的功能是:逐个比较a、b两个字符串对应位置中的字符,把ASCII码值大或相等的字符依次存放到c数组中,形成一个新的字符串。例如:若a中的字符串为aBCDeFgH,b中的字符串为ABcd,则c中的字符串为 aBcdeFgH。 请改正程序中的错误,使它能得到正确结果。 注意,不要改动主函数,不得删行或增行,也不得更改程序的结构。 源程序文件RevMain7.cpp中的程序清单如下: //RevMain7.cpp #include<iostream> #include<string> using namespace std; void fun(char *p,char *q,char *c) int k=1; while(*p!=*q) if(*p<*q) c[k]=*q; else c[k]=*p; if(*p) p++; if(*q) q++; int main() char a[10]="aBCDeFgH",b[10]="ABcd",c[80]='/0'; fun(a,b,c); cout<<"The string a is "<<a<<'/n'; cout<<"The string b is "<<b<<'/n'; cout<<"The string c is "<<c<<endl; return 0;
问答题请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程包含一个源程序文件proj3.cpp,其中定义了用于表示二维向量的类MyVector;程序应当显示(6,8)。但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“//** 1** *****found****”的下方是构造函数的定义,它用参数提供的坐标对x和y进行初始化。 (2)在“//**2** ****found****”的下方是减法运算符函数定义中的一条语句。两个二维向量相减生成另一个二维向量:其X坐标等于两向量X坐标之差,其Y坐标等于两向量Y坐标之差。 (3)在“//**3** ****found****”的下方,语句的功能是使变量v3获得新值,它等于向量v1与向量v2之和。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。//proj 3.cpp#include<iostream>using std::ostream;using std::cout;using std::endl;class MyVector{ //表示二维向量的类double X;//x坐标值double y; //Y坐标值public:MyVector(double i=0.0,double j=0.0); //构造函数 MyVector operator+(MyVector j); //重载运算符+ friend MyVector operator一(MyVec—tor i,MyVector j); //重载运算符一 friend ostream //**1** **********found********** ___________(double i,double j):X(i), Y(j){) MyVector MyVector::operator+(MMVecor j){ return MyVector(x+j.X,Y+j.y); } MyVector operator一(MyVector i, MyVector J) {//**2** ***********found***********return MyVector(_______); } ostream&operator<<(ostream&OS, MyVector V){ OS<<’(’<<v.X<<’,’<<v.Y<<’)’;//输出向量v的坐标 return OS; } int main() { MyVector vl(2,3),v2(4,5),v3; //**3** **********found********** v3=__________; cout<<v3<<end1; return 0; }
问答题使用VC6打开下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)基类Person完成打印功能,定义其中的打印函数为虚函数,请在注释//********1********后添加适当的语句。(2)类Man继承于Person,在构造函数中设置性别为1,请在注释//********2********后添加适当的语句。(3)类Woman继承于Person,在构造函数中设置性别为0,请在注释//********3********后添加适当的语句。(4)实现函数fun(),用于传递不同的类型,打印出性别,请在注释//********4********后添加适当的语句。输出的内容如下:Sex=ManSex=Woman注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>C1assPerson{public://********1********voidprint(){};intsex;};classMan:publicPerson{public:Man(){//********2********}voidprint(){cout"Man"endl;};};classWoman:publicPerson{public:Woman(){//********3********}voidprint(){cout"Woman"endl;};};//********4********{cout"Sex=";p.print();}intmain(){Manm;fun(m);Womann;fun(n);return0;}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为: 1 0 注意:错误的语句在//********error********的下面,修改该语句即可。#include<iostream.h>struct struct{ union { int a; char c[4]; }; int b; //********error********}Void main(){ struct m; //********error******** m.c[0]=0; m.c[1]=0; m.c[2]=0; //********error******** m.c[3]=1; m.b=m.c[3]; cout<<m.a<<endl<<m.b<<endl;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。请完成以下部分,实现在屏幕上输出为: TestClass3 TestClass2 这个程序需要修改的部分,请按照以下部分实现。(1)类TestClass0不能被实例化,请定义一个纯虚函数print,在注释//********1********后添加适当的语句。(2)类TestClassl私有虚继承类TestClass0,请在注释//********2********后添加适当的语句。(3)类TestClass2公有继承类TestClass0,请在注释//********3********后添加适当的语句。(4)类TestClass3公有继承类TestClass2与TcstClass1,请在注释//********4********后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。{}include<iostream.h>class TestClass0{ //********1******** }; //********2******** class TestClass1: { public: void print() { cout<<”TestClass1”<<end1; }};//********3********class TestClass2:{public: void print() { cout<<"TestClass2"<<end1; }};//********4********Class TestClass3:{public: void print() { cout<<”TestClass3”<<end1; }};void main(){ TestClass3 c3; TestClass2 c2; c3.print(); c2.print(); return;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)完成构造函数的定义,请在注释∥********1********后添加适当的语句。
(2)定义类的友元函数fun(),请在注释∥********2********后添加适当的语句。
(3)定义类的友元函数main(),请在注释∥********3********后添加适当的语句。
(4)补充语句,释放内存。请在注释∥********4********后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include(iostream.h>
C1ass TestClass
{
public:
∥********1********
{
data=d;
next=NULL;
}
void SetNext(TestClass*P)
{
next=P ;
}
private:
int data;
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!=NULL
else
U一>next=V;
t一>next=v;
}
return h;
}
void main()
{
TestClass*h=NULL;
TestClass*q=NULL;
do {
int data;
cout>data;
if(data==0)break;
TestClass*P = new
TestClasS(data);
if(h==NULL)
{
h=q=P;
}
else
{
q一>SetNext(P);
q=P;
}
}while(1);
h=fun(h);
for(; h!=NULL ;)
{
q=h一>next;
coutdata<<””<
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了用于表示日期的类Date,但类Date的定义并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有成员变量year、month、day,分别表示年、月、曰,类型为int。请在注释∥********1********后添加适当的语句。 (2)完成构造函数,分别给year、month、day赋值,请在注释∥********2********后添加适当的语句。 (3)完成重载符号“+=”的定义,请在注释∥********3********后添加适当的语句。 (4)完成print()打印函数,如2008年8月8日到屏幕和文件modi3.txt格式相同,请在注释∥********4********后添加适当的语句。 注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<iostream.h>#include<fstream>#include<iomanip>#include<cmath>using namespace std;void WriteFile(int c){ 0fstream out1; out1.open(“modi 3.txt”,ios base::app); out1<<c<< ‘ ’: out1.close();}void WriteFile(char*str){ ofstream out1; out1.open(“modi3.txt”,ios base::app); outl<<str;out1.close();}void ClearFiie(){ ofstream out1; out1.open(“modi3.txt”);out1.close();}c]ass Date{public: Date(int y,int m,int d) {∥********2******** } void print()const;∥********3******** { month+=m; int i=month/1 2; int j=month%12; if(j==0) { year+=(i一1); month=12; } else { year+=i; month=j; } return*this; }private:∥********1********};void Date::print()const{ ∥********4******** WriteFile(year); WriteFile(“年”); WriteFile(month); WriteFile(“月”); WriteFile(day); WriteFile(“日”);}int msin(){ ClearFile(); Date Oly—day(2 0 0 8,8,8); Oly day+=3; Oly—day.print(); return 0;}
问答题使用VC6打开考生文什夹下的工程test1_3。此工程包含一个test1_3.cpp,其中定义了类circle和column,其中column类由circle类protected派生,但两个类的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类circle的保护数据成员r和area,它们都是double型的数据。请在注释“//**1**”之后添加适当的语句。 (2)添加类circle的无参数的构造函数,使circle对象的r默认值为0,请在汁释“//**2**”之后添加适当的语句。 (3)添加派生类column构造函数的定义,传入的参数为double型的height和radius,并将具分别赋值给数擗成员h和基类的r,请在注释“//**3**”之后添加适当的语句。 (4)完成派生类column的成员函数print的定义,使其以格式“r=_area=_”先输出半径r和底面积area,再以格式“h=_vol=_”输出高h和体积vol的值,请在注释“//**4**”之后添加适当的语句。 输山结果如下: r=1 area=3.1416 h=2 vo1=6.2832 注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。 源程序文件test1_3.cpp清单如下: #include<iostream.h> const double pi=3.1416; class circle protected: //**1** public: //**2** circle(double radius) r=radius; void print() area=r*r*pi; cout<<"r="<<r<<" "<<"area="<<ared<<endl; ; class column: protected circle protected: double h; public: //** 3 ** void print() // ** 4 ** ; void main() column col(1.0,2.0); Col.print();
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中包含主程序文件main.cpp和用户定义的头文件Array.h,整个程序包含有类Array的定义和主函数main的定义。请把主程序文件中的Array类的成员函数Contrary()的定义补充完整,经补充后运行程序,得到的输出结果应该是:
5 8
5,4,3,2,1
0,0,8.4,5.6,4.5,3.4,2.3,1.2
注意:只允许在“// *******333*******”和“// *******666*******”之间填写内容,不允许修改其他任何地方的内容。
//Array.h
#include <iostream>
using namespace std;
template<class Type, int m>
class Array { //数组类
public:
Array(Type b[], int mm) { //构造函数
for(int i=0; i<m; i++)
if(i<mm) a[i]=b[i];
else a[i]=0;
}
void Contrary();
//交换数组a中前后位置对称的元素的值
int Length() const {return m;}
//返回数组长度
Type operator [] (int i) const {
//下标运算符重载为成员函数
if(i<0 || i>=m)
{cout << "下标越界!" << endl; exit(1);}
return a[i];
}
private:
Type a[m];
};
void writeToFile(const char *);
//不用考虑此语句的作用
//main.cpp
#include "Array.h"
//交换数组a中前后位置对称的元素的值
template<class Type, int m>
void Array <Type,m>::Contrary() { //补充函数体
// ********333********
// ********666********
}
int main() {
int s1[5] = {1,2,3,4,5};
double s2[6] = {1.2,2.3,3.4,4.5,5.6,8.4};
Array <int,5> d1 (s1,5);
Array <double,8> d2 (s2,6);
int i;
d1.Contrary(); d2.Contrary();
cout << d1.Length() << "" << d2.Length() << endl;
for(i=0;i<4;i++)
cout << d1[i] << ",";
cout << d1[4] << endl;
for(i=0;i<7;i ++)
cout << d2[i] << ",";
cout << d2[7] << endl;
writeToFile(" ");
//不用考虑此语句的作用
return 0;
}
问答题使用VC++6.0打开
下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。使sum(int n)能计算所有n的因子之和(不包括1和自身)。
注意:不能修改程序的其他部分,只能补充sum函数。
试题程序:
#include<iostream.h>
int sum(int n)
{
}
void main()
{
cout<<sum(10)<<endl;
cout<<sum(200)<<endl;
cout<<sum(400)<<endl;
return;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*s),该函数完成以下功能:
(1)把s中的大写字母转换成小写字母,把其中的小写字母转换成大写字母。并且在函数中调用写函数WriteFile()将结果输出到modi2.txt文件中。
例如:s=“helloTEST”,则结果为:s=“HELLOtest”
(2)完成函数WriteFile(char*s),把字符串输入文件中。
提示: 打开文件使用的第二参数为ios_base::binary Jios—base::app。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
#include
#include
#include
using namespace std;
Void WriteFile(char*s)
{
}
void fun(char*s)
{
}
void ClearFile()
{
ofstream outl ;
outl.open(“modi2.txt”);
out1.Close();
}
int main()
{
C1earFile();
char s[1 02 4];
cout<<“please input a string:”<
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序的执行结果为:1 2 3 4 5 4 3 2 11 2 3 4 3 2 11 2 3 2 11 2 11注意:错误的语句在//******error******的下面,修改该语句即可。#include<iostream.h>#include<iomanip.h>void main(){int i,j,k;for(i=5;i>=1;i--){//********error********for(j=1;j<=i;j++)cout<<" ";//********error********for(k=1;k>=i;k++)cout<<setw(3)<<k;//********error********for(k=i-1;k>0;k++)cout<<setw(3)<<k;cout<<endl;}}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为:CMyObject,in the construtor~CMyObject,in the destrutor注意:错误的语句在//******error******的下面,修改该语句即可。#include<lOStream.n>class CMyObject{//******error******//******error******CMyObject{cout<<"CMyObject,in the construtor"<<endl;}~CMyObject(){cout<<"~CMyObject,in the destrutor"<<endl;}};void main(){CMyObjectobj1;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。完成函数fun(char*d,int a[]),其功能是把S中出现的数字转换成数字存储在a[]中,然后返回转换的个数。
例如:s=“1234abcdef567”:
则:a[]中存储着1234567
返回:7
注意:不能修改程序的其他部分,只能修改fun()函数。
#include
int fun(char*s,int a[])
{
}
int main()
{
int a(1 02 4];
int len=fun(“1234abcdef567”,a);
for(int i=0;i<1en?i++)
{
cout<
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)基类Person完成打印功能,定义其中的打印函数为虚函数,请在注释∥********error********后添加适当的语句。
(2)类Man继承于Person,在构造函数中设置性别为1,请在注释∥********error********后添加适当的语句。
(3)类Woman继承于Person,在构造函数中设置性别为0,请在注释∥********error********后添加适当的语句。
(4)实现函数fun(),用于传递不同的类型,打印出性别,请在注释∥********error********后添加适当的语句。
输出的内容如下:
Sex=Man
Sex=Woman
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
C1ass Person
{
public:
∥********1********
void print(){};
int sex;
};
class Man:public Person
{
public:
Man()
{
∥********2********
}
void print(){
cout<<”Man”<
问答题使用VC6打开
下的源程序文件modi1.cpp,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
1
5
1
注意:错误的语句在//******error******的下面,修改该语句即可。
#include<iostream.h>
//******error******
enum
{
Sun
Mon
Tue
Wed
Thu
Fri
Sat
//******error******
} MyEnum;
struct Struct
{
//******error******
int Fri,
int Sun;
};
void main()
{
int i =Mon;
MyEnum t = Fri;
Struct str1;
str1.Fri =Mon;
cout
i
endl;
cout
t
endl;
cout
str1.Fri
endl;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹prog1下的工程prog1,该工程中包含程序文件main.cpp,其中有Salary(“工资”)类和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应为:
应发合计:3500应扣合计:67.5实发工资:3432.5
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Salary{
public:
Salary(const char * id, double the_base, double the_bonus, double the_tax)
//ERROR **********found**********
:the_base(base), the bonus (bonus), the tax(tax)
{
staff_id=new char[strlen(id)+1];
strcpy(staff_id, id);
}
//ERROR **********found**********
~Salary(){ delete * staff id; }
double getGrossPay() const{ returnbase +bonus; } //返回应发项合计
double getNetPay()const{ return getGrossPay() -tax; } //返回实发工资额
private:
char * staff_id; //职工号
double base; //基本工资
double bonus; //奖金
double tax; //代扣个人所得税
};
int main() {
Salary pay("888888", 3000.0, 500.0, 67.50);
cout <<"应发合计:" << pay.getGrossPay() <<"";
cout <<"应扣合计:" <<pay. getGrossPay() -pay.getNetPay() <<"";
//ERROR **********found**********
cout <<"实发工资:" <<pay::getNetPay() <<endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.epp。阅读下列函数说明和代码,补充空出的代码。函数DecToBin(char*des,int n)的功能是将十进制数据n转换成二进制数据,并将转换结果存放在des中。
如:120的二进制数据为1111000
例:
DecToBin(char*des,1 20);
cout
#define M.AXLEN 1 02 4
void DecToBin(char*des,int n)
{
}
void main()
{
char deS[MAXLEN];
int n=12 0;
DecToBin(des,n);
cout<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正确运行,并且使程序输出的结果为:
CMyObject,in the construtor
CMyObject,in the destrutor
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class CMyObj ect
{
∥********error********
∥********error********
CMyObj ect{cout<<“CMyObj ect,in the construtor”<
问答题综合应用
请使用"答题"菜单或使用VC6打开考生文件夹下的工程proj3,,其中声明了List类,它是一个用于表示整数列表的类。List的成员函数insert的功能是将一个指定的整数插入到列表的指定位置处,原位置处的及其后的所有元素依次向后顺移一个位置。请补充完整成员函数insert。在main函数中给出了一组测试数据,此情况下程序的输出应该是:
5 3 7 9 13 2 6 8 1 0
5 3 7 13 2 6 8 1 0
5 -23 3 7 13 2 6 -19 8 1 0
注意:只需在 //********333******** 和 //********666******** 之间填入所编写的若干语句,不要改动程序中的其他内容。
