问答题请使用VC6或使用【答题】菜单打开考生文件夹pmjl下的工程proj1。此工程中包括类Date(“13期”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:2006-1-12005-12-312005-1-312006-1-1注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。#include<iostream>usingnamespaeestd;clasSDate{public:Date(inty=2006,intm=1,intd=1)//ERROR"/*********found*********:year=y,month=m,day=d{}//ERROR*********found*********Date(constDated){this->year=d.year;this->month=d.month;this->day=d.day;}voidprint()const{cout<<year<<'一'<<month<<'一'<<day<<end1;}private://ERROR*********found*********intyear(2006),month(1),day(1);};intmain(){Datedl,d2(2005,12,31),d3;d1.print:();d2.print:();c13=d2;c12=d1;d1=d3;d1.print();d2.print();return0;}
问答题请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,补充编制C++程序proj3.cpp,其功能是读取文本文件in.dat中的全部内容,将文本存放到doc类的对象myDoc中。然后分别统计26个英文字母在文本中出现的次数,统计时不区分字母大小写。最后将统计结果输出到文件out.dat中。文件in.dat长度不大于1000字节。
要求:
补充编制的内容写在“//********333********”与“//********666********”之间。实现分别统计26个英文字母在文本中出现的次数,并将统计结果在屏幕上输出。统计时不区分字母大小写,输出不限格式。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数writeToFile已经给出并且调用。
// proj3.cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class doc
{
private:
char * str; //文本字符串首地址
int counter[26]; //用于存入26个字母的出现次数
int length; //文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象。filename是文件名字符串首地址。
doc(char * filename);
void count(); //统计26个英文字母在文本中出现的次数,统计时不区分大小写。
~doc();
void writeToFile(char * filename);
};
doc::doc(char * filename)
{
ifstreammyFile(filename);
int len=1001, tmp;
str=new char[men];
length=0;
while((tmp=myFile.get())!=EOF)
{
str[length++] =tmp;
}
str[length] ='/0';
myFile.close();
for(int i=0; i<26; i++)
counter[i] =0;
}
//************* 333***********
//************* 666***********
doc::~doc()
{
delete [] str;
}
void doc::writeToFile ( char * filename)
{
ofstream outFile(filename);
for(int i=0; i<26; i ++)
outFile << counter[i] << endl;
outFile.close();
}
void main ()
{
doc mymoc ("");
myDoc.count();
myDoc.writeToFile("");
}
问答题使用VC++6.0打开
下的源程序文件2.cpp。阅读下列函数说明和代码,补充空出的代码。函数fun(doubleA[5][5])返回二维数组中大于本行平均数的所有数之和。
注意:部分代码已经实现,请勿改动main函数代码。
试题程序:
#include<iostream.h>
double fun(double A[5][5])
{
}
void main()
{
double A[5][5]=
{
{4431.3,45.56,5697.65,768.678,122.45},
{444.4,34.545,5667.656,2548.678,1562.45},
{537.3,655.5456,5677.65,7168.678,1242.45},
{134.3,454.5486,5637.65,7687.678,1322.45},
{189.3,445.5786,5677.65,7528.678,142.45}
};
cout<<fun(A)<<endl;
return;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,通过把类Distance定义为类Point的友元类,来实现计算两点之间距离的功能。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)把类CDistance定义为类CPoint的友元类。请在注释∥********1********之后添加适当的语句。 (2)定义类CPoint的构造函数,完成给私有数据成员x和y的赋值,并且两个参数的缺省值都为0。请在注释∥********2********之后添加适当的语句。 (3)完成类CDistance的成员函数length(Pointfloat CDistance::length(CPoint&pPoint,CPoint&qPoint){ ∥********3********}int main(){ CPoint mPoint(10,10),nPoint(40,50); mPoint.display(); nPoint.display(); CDistance len; cout<<len.1ength(mPoint,nPoint)<<endl; return 0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计总页数:299 已把“C++语言程序设计”翻到第50页 已把“C++语言程序设计”翻到第51页 已把“C++语言程序设计”翻到第52页 已把“C++语言程序设计”翻到第5l页 已把书合上。 当前页:0 注意:只修改每个“//ERROR****found*****”下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class Book{ char*title; int num_pages;//页数 int cur page;//当前打开页面的页码,0表示书未打开public://ERROR**********found********** Book(const char*theTitle,int pages)num_pages(pages) { title=new char[strlen(theTitie)+1]; strcpy(title,theTitle); cout<<endl<<”书名:”<<title <<”总页数:”<<num pages; } 一Book(){delete[]title;} bool isClosed()const{return cur_page==0;}//书合上时返回true,否则返回false bool isOpen()const{return!isClosed();)//书打开时返回true,否则返回false int numOfPages()const{return num—pages;} //返回书的页数 int currentPage()const{return cur_page;, //返回打开页面的页码 //ERROR**********found********** void openAtPage(int page no)const{ //把书翻到指定页 cout<<endl; if(page_no<1 || page_no>num_pages){ cout<<”无法翻到第”<<cur—page<<”页。”; close(); } else{ cur_page=page_no; cout<<”已把“<<title<<”翻到第”<<cur_page<<”页”; } } void openAtPrevPage(){openAtPage(cur_page一1);)//把书翻到上一页 void openAtNextPage(){openAtPage(cur_page+1);}//把书翻到下一页 void close(){ //把书合上 cout<<end1; if(isClosed()) cout<<”书是合上的。”; else{//ERROR**********found********** num_pages=0; cout<<”已把书合上。”; } cout<<end1; }};int main(){Book book(”C++语言程序设计”,299);book.openAtPage(50);book.openAtNextPage();book.openAtNextPage();book.openAtPreVPage();book.close();cout<<”当前页:”<<book.current.Page()<<endl; return 0;}
问答题请编写一个函数comm(int n,int k),该函数将用递归算法计算从n个人中选择k个人组成一个委员会的不同组合数,由n个人里选k个人的组合数=由(n-1)个人里选k个人的组合数+由(n-1)个人里选(k-1)个人的组合数。 注意:部分源程序已存在文件test41_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数comm的花括号中填写若干语句。 源程序文件test41-2.cpp清单如下: #include<iostream.h> int comm(int n, int k) void main ( ) int n=7, k=3; cout<<"n=7,k=3"<<endl; cout<<comm(n,k)<<endl;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Constructor
The value is 10
Destructor
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int x) :value(x) {cout <<"Constructor"<<endl; }
// ERROR ********** found**********
void ~MyClass()
{ cout <<"Destructor"<<endl; }
void Print() const;
private:
// ERROR ********** found**********
int value=0;
};
// ERROR ********** found**********
voidMyClass::Print()
{
cout <<"The value is" <<value <<endl;
}
int main()
{
MyClass object(10);
object.Print();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成以下两个函数:
(1)fun1(int n);求出n的阶乘,必须使用递归调用。
(2)fun2(int n);求出n的阶乘,不能使用递归调用。
如果n小于1则返回0。
注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
∥必须使用递归
int funl(int n)
{
};
∥不能使用递归
int fun2(int n)
{
}
void main()
{
int i;
cout>i;
cout<<“输入数字的阶乘是:”<<
fun1(i)<
问答题使用VC6打开考生文件夹proj1下的工程proj1,其中有“沙发”类Sofa和主函数main的定义。程序中位于每个//ERROR**********found**********下的语句行有错误.请加以更正。更正后程序的输出应该是:座位数:3颜色:红色注意:只能修改每个//ERROR**********found**********下的那一行,不要改动程序中的其他内容。#include<iostream>using namespace std;class Sofa{//“沙发”类int seats;//座位数char color[10];//颜色public://ERROR**********found**********Sofa(int s,const char*co){//ERROR**********found**********if(co=NULLlcolor[0]='\0';elsestrcpy(color,co);}//EllROR**********found**********const char*getSeats()(!onst{return seats;}coust char*getColor()const{return color;}};int main(){Sofa sara(3);tout<<"座位数:"<<sara.getSeats()<<endl;tout<<"颜色:"<<safa.getColor()<<endl;retturn0;}
问答题请编写一个函数fun(),它的功能是计算并输出给定整数n的所有因子(不包括1与自身)之和(规定n的值不大于1000)。例如:输入n的值为856,则输出为sum=763。注意:部分源程序已存在文件test33_2.cpp中。请勿修改主函数main和其他函数中的任何内容,仅在函数count的花括号中填写若干语句。文件test33_2.cpp清单如下: #include<stdio. h> #include<iostream. h> int fun(int n) void main ( ) int n, sum; cout<<"Input n:"<<end1; cin>>n; sum=fun (n); cout<<" sum= /n"<<sum<<end1;
问答题使用VC++6.0打开
下的源程序文件2.cpp。请完成函数fun(int x),该函数的功能是将x的值转换成二进制数输出到屏幕,并且在函数中调用写函数WriteFile()将结果输出到2.txt文件中。
例如:x=6,6的二进制数为110,则输出到屏幕的数为110。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
void WriteFile(char *str)
{
ofstream out1;
out1.open("2.txt",ios_base::binary|ios_base::app);
for(int i=0; str[i] != 0; i++)
out1.put( str[i]);
out1.close();
}
void fun(int x)
{
}
void ClearFile()
{
ofstream out1;
out1.open("2.txt");
out1.close();
}
int main()
{
ClearFile();
fun(13);
return 0;
}
问答题请使用VC6或使用【答题】菜单打开
proj3下的工程proj3,其中声明了SortedList类,是一个用于表示有序数据表的类。其成员函数insert的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个insert函数。程序的正确输出应为:
插入前:
1,2,4,5,7,8,10
插入6和3后:
1,2,3,4,5,6,7,8,10
要求:
补充编制的内容写在“// ********333********”与“// ********666********”之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//SortedList.h
#include <iostream>
using namespace std;
class SortedList { //有序数据表类
int len;
double * d;
public:
SortedList (int len, double data[] = NULL);
~SortedList() {delete [] d;}
int length() const {return len;} //有序数据表长度(即元素的个数)
double getElement (int i) const {return d[i];}
void insert (double data);
void show() const; //显示有序数据表
};
void writeToFile (char *, const SortedList
//main.cpp
#include "SortedList.h"
SortedList::SortedList (int len, double data[]):len(len){
d = new double[len];
for(int k = 0; k < len; k++)
d[k] = (data == NULL? 0.0:data[k]);
for(int i = 0; i < len-1; i++) {
int m=i;
for(int j = i; j < len; j ++)
if(d[j] < d[m]) m=j;
if (m > i) {
double t = d[m];
d[m] = d[i];
d[i] = t;
}
}
}
void SortedList::insert (double data) {
// ********333********
// ********666********
}
void SortedList::show() const { //显示有序数据表
for(int i = 0; i < len - 1; i++)
cout << d[i] << ",";
cout << d[len - 1] << endl;
}
int main() {
double s[] = {5,8,1,2,10,4,7};
SortedList list(7,s);
cout << "插入前:" << endl;
list.show();
list.insert(6.0);
list.insert(3.0);
cout << "插入6和3后:" << endl;
list.show();
writeToFile(" ", list);
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
书名:C++语句程序设计总页数:299
已把“c++语言程序设计”翻到第50页
已把“c++语言程序设计”翻到第51页
已把“c++语言程序设计”翻到第52页
已把“c++语言程序设计”翻到第51页
已把书合上。
当前页:0
注意:只修改每个“//ERROR ***found***”下的那一行,不要改动程序中的其他内容。
#include
using namespace std;
class Book{
char*title;
int num—pages;//页数
int cur_page;//当前打开页面的页码,0表示书未打开
public:
//ERROR***********found***********
Book(const char*theTitle,int pages)num_pages(pages)
{
title=new char[strlen(theTitle)+1];
strepy(title,theTitle);
couthum_pages){
tOUt<<”无法翻到第”<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使程序正常运行,并使程序输出的结果为:
TestClass2
TestClass3
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构, 错误的语句在∥********error********的下面。
#include
Class TestClass1
{
public:
virtual Void fun()=0;
};
class TestClass2:public TestClassl
{
∥********error********
int fun()
{
using namespace std;
coutfun();
obj 2.fun();
return;
}
问答题简单应用
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,此工程包含程序文件main.cpp,其中有类Animal("动物")、类Dog("狗")、类Cat("猫")的定义和主函数main的定义。请在程序中"//****found****"下的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
Dog named Lucky speaks Woof
Cat named Daisy speaks Miaow
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动"//****found****"。
#include
using namespace std;
class Animal
{
public:
Animal(char* str="Animal")
{
//**********found**********
name = new ___________________;
strcpy(name,str);
}
virtual ~Animal() { delete[] name; }
char* getName() const { return name; }
virtual char* getType() const { return "Animal"; }
virtual char* getVoice() const { return "Voice"; }
private:
char* name;
};
class Dog : public Animal {
public:
Dog(char* str) : Animal(str) { }
char* getType() const { return "Dog"; }
//**********found**********
char* getVoice() const { ___________________ }
};
class Cat : public Animal {
public:
//**********found**********
Cat(char* str) : ______________ { }
char* getType() const { return "Cat"; }
char* getVoice() const { return "Miaow"; }
};
void type(Animal
}
//**********found**********
void speak(___________________)
{
cout<<" speaks "<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。函数fun(int n)的功能是实现对一个整数的加密,具体的加密方法是:一个四位长的整数,每位数字加上3然后除以9的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换,然后返回得到的密码。
程序输出:
7654
2108
注意:不能修改其他部分的代码。
#include
#include
int fun(int n)
{
}
int main()
{
int i=1234;
cout<
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sonByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为: 按姓名排序 排序前 张三 男 出生日期:1978年4月20日 王五 女 出生日期:1965年8月3日 杨六 女 出生日期:1965年9月5日 李四 男 出生日期:1973年5月30日 排序后: 李四 男 出生日期:1973年5月30日 王五 女 出生日期:1965年8月3日 杨六 女 出生日期:1965年9月5日 张三 男 出生日期:1978年4月20日 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>using namespace std;class Date{ //日期类int year,month,day;//年、月、日public: Date(int year,int month,int day):year(year),month(month),day(day){) int getYear()const{return year;} int getMonth()const{return month;) int getDay()const{return day;)};class Person{//人员类char name[14],//姓名bool is male;//性别,为true时表示男性Date birth date;//出生日期public: Person(char*name,bool is male,Date birth_date)//**********found********** :______________ { strcpy(this->name,name); } const char*getName()const{return name;) bool isMale()const{return is male;} Date getBirthdate()const{ return birth date;) //利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于 int compareName(const Person cout<<endl<<"排序前:"; for(i=0;i<size;i++)staff[i].show(); sortByName(staff,size); cout<<endl<<endl<<”排序后:”; for(i=0;i<size;i++)staff[i].show(); cout<<endl; return 0;}
问答题请使用VC6或使用[答题]菜单打开
proj1下的工程proj1,该工程含有一个源程序文件prw1. 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或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包括类Point、函数fun和主函数main。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
The point is(0,1)
The point is(3,5)
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Point {
public:
// ERROR *********found*********
Point(int x = 0, int y)
: x_(x), y_(y) {}
// ERROR ********* found*********
void move(int xOff, int yOff) const
{
x_+ = xOff;
y_+ = yOff;
}
void print() const
{
cout << "The point is (" << x_<<','<< y_<< ')'<< endl;
}
private:
int x_, y_;
};
void fun (Point* p)
{
//ERROR ********* found*********
p.print ();
}
int main ()
{
Point p1, p2 (2, 1);
p1.print ();
p2.move (1, 4);
fun (
return 0;
}
