问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请实现函数fun(double b[],int len)的如下功能:
(1)b[]是一个数组,长度为len;
(2)b[0]=0,b[1]=1;
(3)b[i+2]=b[i]+b[i+1];
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
void fun(double b[],int len)
{
}
void main()
{
double b[128];
fun(b,128);
for(int i=0;i<128;i++)
{
std::cout<
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。完成函数ToUpper(char* des,char* str),该函数实现把str字符串中小写字符转换成大写字符,并存发在des中。
例如:str=“aBcdrFGHijK”;
则:des=“ABCDEFGHIJK”;
注意:不能修改程序的其他部分,只能补充ToUpper ()函数。
#include <iostream.h>
#define MAXLEN 1024
void ToUpper(char* des,char* str)
{
}
void main()
{
char dest [MAXLEN];
char* str="aBcdrFGHijK";
ToUpper(dest,str);
cout
dest
endl;
return;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件pmj2.cpp,其中定义了CharShape类、Triangle类和Rectangle类。 CharShape是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用于显示不同字符图形的相同操作接口。Triangle和Rectangle是Char'Shape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,用于实现各自的显示操作。程序的正确输出结果应为: * *** ***** ******* ######### ######### ######### 请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。 (1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。 (2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。 (3)为类外函数fun添加合适的形参。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#include<iostream>using namespace std;class CharShape{public: CharShape(char ch):一ch(ch){); virtual void Show()=0;protected: char ch;//组成图形的字符 }; class Triangle:public CharShape{ public: Triangle(char ch,int r):Char—Shape(ch),一rows(r){) void Show();private: int rows;//行数 }; class Rectangle:publ ic CharShape{ public: Rectangle(char ch,int r,int c):CharShape(ch),_rows(r),_cols(c){) voio Show();private: int rows,cols;//行数和列数 }; void Triangle::Show() //输出字符组成的三角形 { for(int i=1;i<= rows;i++){//********found********for(int j=1;j<=_______;j++) cout << ch; cout<<endl; } } void Rectangle::Show() //输出字符组成的矩形 { //********found********for(int i=1;i<=________;i++} {//********found******** for(int j=1;j<=_______;j++) cout<< ch;cout<<endl; }}//********found******** 为fun函数添加形参VOid fun(_________){CS.Show();)int main(){Triangle tri(’*’,4);Rectangle rect(’#’,3,8);fun(tri);fun(rect);return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能: (1)定义纯虚类TestClass0,包含纯虚函数fun(),请在注释//********1********后添加适当的语句。 (2)完成类TestClass1,以公有继承TestClass0,请在注释//********2********后添加适当的语句。 (3)定义TestClass0对象指针p,请在注释//********3********后添加适当的语句。 (4)把TestClass1的对象obj的指针赋给TestClass0指针P,请在注释//********4********后添加适当的语句。 注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream.h>Class TestClass0{public: //********1********};//********2********class TestClass1{public: void fun() { cout<<"fun"<<end1; }};int main(){ TestClass1 obj; //********3********//********4******** p->fun()j return 0;}
问答题请使用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 O 0 1 0 0 O 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;//矩阵基础类,一个抽象类class MatrixBase{ int rows,cols;public: MatrixBase(int rows, int cols):rows(rows),cols(cols){) int getRows()const {return rows;} //矩阵行数 int getCols()const{return cols;} //矩阵列数 virtual double getElement(int r,int C)const=0;//取第i个元素的值 void show()const{//分行显示矩阵中所有元素 for(int i=0;i<rows;i++){ cout<<endl; for(int j=0;j<cols;j++) //**********found**WW****** cout<<_____________<<””; } } } //矩阵类 class Matrix:public MatrixBase{ double*val;public://**********found********** Matrix(int rOWS,int cols,double m[]=NULL):________{//**********found**********val=________;for(int i=0;i<rows*cols;i++) val[i]=(m==NULL?0.0:m[i]); } 一Matrix(){delete[]val;) double getElement(int r,int C)const{return val[r*getCols()+C];)};//单位阵(主对角线元素都是1,其余元素都是0的方阵)类class UnitMatrix:public MatrixBase{public: UnitMatrix(int rows):MatrixBase(rows,rows){)//单位阵行数列数相同. double getElement(int r,int C)const{//********** found**********if(________)return 1.0;return 0.0; } }; int main(){ MatrixBase*m; double d[][5]={{1,2,3,4,5),{2,3,4,5,6),{3,4,5,6,7)}; m=new Matrix(3,5,(double*)d); m->show(); delete m; cout<<endl;m=new UnitMatrix(6);m->show();delete m;return 0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Constructor called of 10
The value is 10
Destructor called of 10
注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int i)
{
value = i;
cout << "Constructor called of"<< value << endl;
}
// ERROR **********found**********
void Print()
{ cout << "The value is" << value <<endl; }
// ERROR **********found**********
void ~MyClass()
{ cout << "Destructor called of" << value << endl; }
private:
// ERROR **********found**********
int value = 0;
};
int main()
{
const MyClass obj (10);
obj.Print();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序的执行结果为:注意:错误的语句在//******error******的下面,修改该语句即可。#include#includevoidmain(){inti,j,k;for(i=5;i>=1;i--){//******error******for(j=1;J0;k++)cout
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。完成函数fun(int a[],int n),实现递归函数fun(int a[],int n)的返回值是数组a[]的前n个元素之和。 注意:不能修改程序的其他部分,只能修改fun()函数。#include<iostream.h>int fun(int a[],int n){}Void main(){ int A[]={1,2,3,4}; cout<<fun(A,sizeof(A)/izeof(int))<<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打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char*des,char*st0实现的功能是,如果字符串stI"里面有空格或者不可打印字符,则全部去掉,将转化后的字符串放在字符串des里,然后返回该字符串。
注意:不能修改程序的其他部分,只能补充convert 0函数。
#include
#include
#define MAXLEN 1 024
char*convert(char*des,char*
str)
{
}
void main()
{
char dest[MAXLEN];
char*str:” Ab cDeF hJ K”;
cout<
问答题使用VC6打开考生文件夹下的工程test13_1,此工程包含一个源程序文件test13_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:
n=2
n=5
n=7
源程序文件test13_1清单如下:
#include<iostream.h>
class Sample
{
int n;
public:
/*************** found ************+***/
Sample()
Sample(int i){n=i;}
/***************** found ********+**********/
void add(Sample s1,Sample s2)
/***************** found ****************/
{
this.n=s1->n+s2.n;
}
void disp(){cout<<"n="<<n<<endl;}
};
void main()
{
Sample s1(2),s2(5),s3;
s3.add(&s1,s2);
s1.disp();
s2.disp();
s3.disp();
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*data),此函数的功能是,找出字符串数组中最小的ASCⅡ值,如果有相同变量,则输出最后一个所在的位置;如果字符串为空,则返回一1;或者不存在时也返回一1。
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。计算数字如果第一个字母最小,则返回0。依次增加。
#include
int fun(char*data)
{
}
Void msin()
{
char str[1024];
cout<<“请输入一行英文字符串:\n”;
cin.getline(str,1024);
cout<<”最小的字母出现在距离头部”<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数fun(int*arr,int n)的功能是将指定的有序数组压缩成各元素互不相同的有序数组,即相同数只保留一个,多余的被删除。并返回互不相同的元素的个数。 注意:不能修改程序的其他部分,只能修改fun()函数。#include<i0stream>int fun(int*a,int n){}Void main(){ int A[]=(6,6,5,4,4,3,3,2,1); int j =fun(A,sizeof(A)/sizeof(int)); for(int i=0;i<j;i++) { std::cout<<A[i]<<' '; } Std::cout<<Std::endl; return;}
问答题使用VC6打开考生文件夹下的proj1工程目录内的proj1.dsw文件,其中在编辑窗口内显示的主程序文件中定义有Xabe类和主函数main。在程序文本中位于每行"//ERROR**********found**********下面的一行有错误,请加以更正。 更正后程序的输出为:57 注意:只允许修改每个”//ERROR**********found**********下面的一行语句,不允许改动程序中的其他任何内容。#include<iostream>using namespace std;class Xabe{ int*a;int n; public: Xabc(int aa[],int nn):n(nn){ a=Hew int[n]; for(int i=0;i<n;i++) //ERROR**********found********** aa[i]=a[i]; } inl GetA(int i)const{return a[i];} int SumA(int n); ~Xabc(){delete[]a;}};int Xabc::SumA(int n){ int s=0; for(int j=0;j<n;j++)s+=a[j]; return s;}int main(){ int a[6]={2,5,8,3,6,9}; Xabe x(a,6); a[3]=19; int d=0; for(int i=0;i<6;i++) //ERROR**********found********** d+=x.a[i]; //ERROR**********found********** int f=SumA(5); cout<<d+f<<endl: return 0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应为:
Thp value is 5
The value is 10
There are 2 objects.
There are 1 objects.
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int value)
{
// ERROR ********** found**********
this.value = value;
count ++;
}
// ERROR ********** found**********
void ~MyClass()
{
count--;
}
static int getCount () { return count; }
int getValue() { return value; }
private:
int value;
static int count;
};
// ERROR ********** found**********
static int MyClass::count = 0;
int main()
{
MyClass* p = new MyClass(5);
MyClass* q = newMyClass(10);
cout << "The value is" << p ->getValue() << endl;
cout << "The value is" << q ->getValue() << endl;
cout << "There are" << MyClass::getCount() << " objects." << endl;
delete p;
cout << "There are" <, MyClass::getCount() << "objects." << endl;
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,其中定义了MyString类。MyString是一个用于表示字符串的类,其构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中;成员函数reverse的功能是对字符串进行反转操作,例如,字符串“ABCDE”经过反转操作后,会变为“EDCBA”;成员函数print的作用是将字符串输出到屏幕上。请在横线处填写适当的代码并删除横线,以实现MyString类的功能。此程序的正确输出结果应为:Beforereverse:abcde%Afterreverse:Chagfed注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。//proj2.cpp#include<iostream>usingnamespacestd;classMyString{public:MyString(constchar*s){//**********found**********mstr=newchar[________];strcpy(m_str,s);}~MyString(){//**********found**********________;?}voidreverse(){intn:strlen(m_str);for(inti=0;i<n/2;++i){inttmp=m_str[i];//**********found**********mstr[i]=________;//**********found**********________; }}voidprint(){cout<<mstr<<end1;}//其他成员…private:char*mstr;};intmain(intargc,char*argv[]){MyStringstr1(”abc”),str2("defg");cout<<"Beforereverse:\n";str1.print();str2.print();str1.reverse();str2.reverse();cout<<"Afterreverse:\n";str1.print();str2.print();return0;}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2 3 4 5 27 28 31 66 75
2 3 4 5 6 27 28 3l 66 75
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//IntegorSet.h
#ifndef INTEGERSET
#define INTEGERSET
#include <iostream>
using namespace std;
const int MAXELEMENTS = 100;
//集合做多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
public:
IntegerSet(): counter(0){}
//创建一个空集合
IntegerSet (int data[], int size);
//利用数组提的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元算
int getCount() const {return counter;}
//返回集合中元素的个数
int getElement (int i) const {return elem[i];} //返回集合中指定的元素
void show() const;
};
void WriteToFile(char *);
#endif
//main.cpp
#include "IntegerSet.h"
#include <iomanip>
IntegerSet:: IntegerSet (int data[], int size): counter(0) {
for(int i = 0; i < size; i++)
add (data[i]);
}
void IntegerSet::add (int element) {
int j;
//从后往前寻找第一个小于等于element的元素
for(j = counter; j > 0; j--)
if (element >= elem[j - 1])
break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j > 0)
if (element == elem[j - 1])
return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for (int k = counter; k > j; k--)
elem[ k] = elem[k - 1];
elem[j] = element;
//将element插入到该位置
counter ++; //计数器加1
}
void IntegerSet::remove (int element) {
// ********333********
// ********666********
}
void IntegerSet::show() const {
for(int i = 0; i < getCount (); i++)
cout << setw(4) << getElement(i);
cout << endl;
}
int main() {
int d[] = {5,28,2,4,5,3,2,75,27,66,31};
IntegerSet s(d, 11); s.show();
s.add(6); s.show();
s.add(19); s.show();
s.remove(2); s.show();
s.add(4); s.show();
WriteToFile(" ");
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码。函数num(char *str)实现返回字符串中非数字的个数。
例如:abc123abc45
返回输出:6
将函数num()补充完整。
注意:请勿改动主函数。
#include
int num(char *str)
{
}
int main()
{
char str[1024];
cout<<"please input a strfng:
"<
问答题请使用VC6或使用[答题]菜单打开
proj1下的工程proj1,该工程中包含程序文件main. cpp,其中有类Door(“门”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
打开503号门…门是锁着的,打不开。
打开503号门的锁…锁开了。
打开503号门...门打开了。
打开503号门...门是开着的,无须再开门。
锁上503号门…先关门…门锁上了。
注意:只修改每个“//ERROR *********found*********”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Door {
int num;//门号
bool closed;//true表示门关着
bool locked;//true表示门锁着
public:
Door(int num) {
//ERROR *********found*********
num=this->num;
closed=locked=true;
}
bool isClosed()const {return closed;}
//门关着时返回true, 否则返回false
bool isOpened()const {return ! closed;}
//门开着时返回true, 否则返回false
bool isLocked()const {return locked;}
//门锁看时返回true, 否则返回false
bool isUnlocked()const {return ! locked;}
//门未锁时返回true, 否则返回false
void open() {//开门
cout<<endl<<"打开"<<num<<"号门...“;
//ERROR *********found*********
if(closed)
cout<<"门是开着的,无须再开门。";
else if(locked)
cout<<"门是锁着的,打不开。";
else {
closed=false;
cout<<"门打开了。";
}
void close() {//关门
cout<<endl<<"关上"<<num<<"号门…";
if(closed)
cout<<"门是关着的,无须再关门。";
else {
closed=tree;
cout<<"门关上了。";
}
}
//ERROR *********found*********
void lock()const{ //锁门
cout<<endl<<"锁上"<<num<<"号门…";
if(locked)
cout<<"门是锁着的,无须再锁门。";
else{
if(! closed) {
cout<<"先关门…";
closed=true;
}
locked=true;
cout<<"门锁上了。";
}
}
void unlock() {//开锁
cout<<endl<<"开"<<num<<"号门的锁…";
if(! locked)
cout<<"门没有上锁,无须再开锁。";
else {
locked=false;
cout<<"锁开了。";
}
}
};
int main() {
Door door(503);
door. open();
door. unlock();
door. open();
door. open();
door. lock();
return 0;
}
问答题使用VC++6.0打开
下的源程序文件2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数sum(int A[NUM][NUM],int n)的功能是计算矩阵中所有非质数数字的和。
提示:函数isPrime(int n)的功能是判定当前数字是否为质数,如果是,则返回true。
注意:不能修改程序的其他部分,只能修改sum函数。
试题程序:
#include<iostream.h>
#include<cmath>
#define NUM 50
int A[NUM][NUM]=
{
{1,3,5,7,9},
{11,13,15,17,19},
{21,23,25,27,29},
{31,33,35,37,39},
{41,43,45,47,49}
};
bool isPrime(int n)
{
if(n==1)
return false;
if(n==2)
return true;
for(int i=2;i<n/2;i++)
{
if(n%i==0)
return false;
}
return true;
}
int sum(int A[NUM][NUM],int n)
{
}
int main()
{
cout<<sum(A,5)<<endl;
return 0;
}
