问答题使用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打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。sum(int n1计算所有n的因子之和(不包括1和自身)。
注意:不能修改程序的其他部分,只能补充sum
()函数。
{}include
int sum(int n)
{
}
void main()
{
cout<
问答题请使用菜单命令或直接用VC6打开
下的工程proj2,实现一个自定义的字符串类MyString,重载取下标操作符([]),使类的对象能够通过下标取字符串的某位元素。如果下标越界,则返回第—个元素的值。
请在程序中//**********found**********之下一行的横线处填写适当的代码,并删除横线,使程序完整、正确。
输出结果为:
s
t
#include<iostream>
#include<cstring>
using nmnespace std;
class MyString
{
private:
char*str;
public:
MyString(char*s)
{
//**********found**********
str=new______;
strcpy(str, s);
}
~MyString()
{
//**********found**********
______;
}
char
};
char
//**********found**********
return______;
}
int main()
{
MyString test("test string");
cout<<test[5]<<endl;
cout<<test[13]<<endl;
return 0;
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义的MyString类是一个用于表示字符串的类。假设字符串由英文单词组成,单词与单词之间使用一个空格作为分隔符。成员函数wordCount的功能是计算组成字符串的单词的个数。例如,字符串“dog”由1个单词组成;字符串“thequickbrownfoxjumpsoverthelazydog”由9个单词组成。请编写成员函数wordCount。在main函数中给出了一组测试数据,此时程序应显示:读取输入文件…STR1=1STR2=9要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。//mystring.h#include<iostream>#include<string.h>usingnamespacestd;classMyString{public:MyString(constchar*s){str=newchar[strlen(S)+1];strcpy(str,S);};—MyString(){delete[]str;)intwordCount()const;private:char*str;};voidwriteToFile(char。,int);//main.cpp#include<fstream>#include”mystring.h”intMyString::wordCount()const{//********333********//********666********}intmain(){charinname[128],pathname[80];strcpy(pathname,"");sprintf(inname,"in.dat",pathname);cout<<"读取输入文件…\n\n";ifstreaminfile(inname);if(infile.fail()){cerr<<"打开输入文件失败!";exit(1);}charbuf[4096];infile.getline(buf,4096);MyStringstr1("dog"),str2("thequickbrownfoxjumpsoverthelazydog"),str3(buf);str1.wordCount();cout<<"STR1="<<str1.wordCount()<<end1;cout<<"STR2="<<str2.wordCount()<<end1<<end1;writeToFile(pathname,str3.wordCount());return0;}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数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 &p)const{//**********found**********______________}void show(){cout<<endl:cout<<name<<"<<(is_male?"男":"女")<<"<<"出生日期:"<<birth_date.getYear()<<"年"//显示出生年//**********found**********______________//显示出生月<<birth_date.getDay()<<"日";//显示出生日}};void sortByName(Person ps[],int size){//将人员数组按姓名排列为升序for(int i=0;i<size-1;i++){//采用选择排序算法int m=i;for(int j=i+1;j<size;j++)if(ps[j].compareName(ps[m])<0)m=j;if(m>i){Person p=ps[m];ps[m]=ps[i];ps[i]=p;}}}int main(){Person staff[]={Person("张三",true,Date(1978,4,20)),Person("王五",false,Date(1965,8,3)),Person("杨六",false,Date(1965,9,5)),Person("李四",true,Date(1973,5,30))};const int size=sizeof(staff)/sizeof(staff[0]);int i;cout<<endl<<"按姓名排序";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;return0;}
问答题改错题
使用VC6打开考生文件夹下的工程test10_1,此工程包含一个源程序文件test10_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果为:
class Base
class D1
class D2
class D3
f in Base
源程序文件test10_1.cpp清单如下:
#include
class Base
{
public:
Base(){cout<<"class Base"<
问答题简单应用题
请编写一个函数void swap(int *x,int *y),用来交换两个数的值。
注意:部分源程序已存在文件test14_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数swap的花括号中填写若干语句。
文件test14_2.cpp的内容如下:
#include
void swap(int *x,int *y);
void main()
{
int a=1,b=3;
swap(
cout<<"a="<
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,但该程序运行时有错,请改正main()函数中的错误,使程序的输出结果如下:
Constructor.
Default constructor.
Area is 12
Area is 0
Area is 12
注意:错误的语句在∥********error********的下面,修改该语句即可。
#include
class CRectangle
{
private:
double length,width;
public:
CRectangle()
{
coutlength=1;
this一>width=w;
}
void GetArea()
{
cout<<“Area is”<
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。请完成函数fun(char*str,char ch),本函数采用二分法,在己按字母次序从小到大排序的字符数组str中,查找字符ch,若ch在数组中,函数返回字符ch在数组中的下标,否则返回一1。 二分法查找的思想是:初始查找区间的下界为0,上界为len一1,查找区间的中后,k=(下界+上界)/2;若list[k]等于ch,查找成功;若list[k]>ch,则新的查找区间的下界不变,上界改为k-1;否则新的查找区间的下界改为k+1,上界不变。在新区间内继续用二分法查找。注意:请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include<iostream.h>int fun(char*Str,char ch){}Void msin(){ char Str[]=(‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’,‘k’,0); char ch; cout<<“请输入一个字符:”<<end1; Cin>>ch;cout<<“输入数字的位置是:”<<fun(Str,ch)<<end1; return;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is:10 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。 //proj1.cpp #include <iostream> using namespace std; class Member //ERROR ********found******** private: Member(int val):value(val) int value; ; class MyClass Member m; public: MyClass(int val) int GetValue () const return_m.value; ; int main () MyClass * obj=new MyClass(10); //ERROR ********found******** 下列i语句输出 obj指向类中的 value值 cout<<"The value is:"<<obj.GetValue ()<<endl; delete obj; return 0;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR *********found**********”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: NUM=0 Value=1 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。 //proj1.cpp #include <iostream> using namespace std; class MyClass int_i; friend void Increment (MyClass public: const int NUM; MyClass(int i=0) NUM=0; _i=i; int GetValue () const return_i; ; //ERROR ********found******** void Increment () f._i++; int main () MyClass obj; MyClass::Increment (obj); cout<<"NUM="<<obj.NUM<<endl<<"Value="<<obj.GetValue()<<endl; return 0;
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程projl。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: Name:Smith Age:21 ID:99999 CourseNum: 12 Record:970 注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include <iostream> using namespace std; class Studentlnfo protected: //ERROR **********found********** char Name; int Age; int ID; int CourseNum; float Record; public: Studentlnfo(char*name,int Age,int ID,int courseNum,float record); //ERROR **********found********** void ~Studentlnfo() float AverageRecord() return Record/CourseNum; void show()const cout<<"Name:"<<Name<<"Age:<<"CourseNum:"<<CourseNum<<"Record:"<<Record<<endl; ; //ERROR **********found********** Studentlnfo Studentlnfo (char*Name,int Age,int ID,int CourseNum,float Record) Name=name; Age=age; this->ID=ID; CourseNum=courseNum; Record=record; int main() Studentlnfo st("Smith",21,99999,12,970); st.show(); 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"<<endl;}};int main(){TestClass1 obj;//********3********//********4********p->fun();return0;}
问答题凡是使用过C语言标准库函数strcpy(char*s1,char*s2)的程序员都知道,使用该函数时有一个安全隐患,即当指针s1所指向的空间不能容纳字符串s2的内容时,将发生内存错误。类Stung的Strcpy成员函数能进行简单的动态内存管理,其内存管理策略为:①若已有空间能容纳新字符串,则直接进行字符串复制;②若已有空间不够时,将重新申请一块内存空间(能容纳下新字符串),并将新字符串内容复制到新申请的空间中,释放原字符串空间。
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp。其中定义了类String和用于测试该类的主函数main,且成员函数Strepy的部分实现代码也已在该文件中给出,请在标有注释“//TODO:”的行中添加适当的代码,将这个函数补充完整,以实现其功能。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
// proj2.cpp
#include <iostream>
using namespace std;
class String {
private:
int size; //缓冲区大小
char * buf; //缓冲
public:
String(int bufsize);
void Strcpy(char * s); //将字符串
s复制到buf中
void Print() const;
~String()
{ if (buf != NULL) delete [] buf; }
};
String::String (int bufsize)
{
size = bufsize;
bur = new char[size];
* buf = '/0';
}
void String::Strcpy (char * s)
{
char * p,* q;
int fen = strlen(s);
if (len+1 > size) { //缓冲区空间不够,须安排更大空间
size = len+1;
p = q = new char[ size];
//********** found**********
while((* q=* s) !=0) {______}
//TODO: 添加代码将字符串s复制到字符指针q中
delete [] buf;
buf = p;
}
else {
//**********found**********
for(p=buf; ______; p++, s++);
//TODO: 添加代码将字符串s复制到buf中
}
}
void String::Print() const
{
cout << size << '/t' << buf <<endl;
}
int main ()
{
char s[100];
String str (32);
cin.getline(s, 99);
str.Strcpy(s);
str.Print ();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错误,请改正程序中的错误,使得程序输出: 10 TestClass1 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。 #include<iostream> class TestClass1 { public: TestClass1(){ }; //********error******** private: virtual~TestClass1() { using namespace std;cout<<''TestClass1''<<endl; };}; iass TestClass2:public TestClass1{public: //********error******** explicit TestClass2(int i) { m__i=i; }; TestClass2& operator ()(int i) { this->m__i=i; } void print() {//********error******** cout<<m__i<<endl; }private: int m__i;};void fun(TestClass2 C1){ C1.print();}int main(){ fun(10); return 0;}
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码。函数num(char* str)实现返回字符串中非数字的个数。
例如:abc123abc45
返回输出:6
将函数num()补充完整。
注意:请勿改动主函数。
#include <iostream.h>
int num(char* str)
{
}
int main()
{
char str[1024];
cout
"please input a string:"
endl;
cin.getline(str, 1024);
cout
"char number is "
num (str)
endl;
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp,其中定义了类baseA、priAl和priA2,其中priAl类由baseA类公有派生,priA2类由priAl类公有派生。上述三个类的定义并不完整,请按要求完成下列操作,将程序补充完整。
程序输出结果如下:
10
5
10 5 7
(1)定义类baseA的构造函数,该构造函数有一个整型的参数m,在构造函数中请将m赋值给数据成员a。请在注释“∥********1********”之后添加适当的语句。
(2)定义类priA1的构造函数,该构造函数有两个整型的参数m和11,在构造函数中请将m赋值给数据成员b,将n作为基类baseA构造函数的参数值传入。请在注释“∥********2********”之后添加适当的语句。
(3)定义类priA2的构造函数,该构造函数有三个整型的参数m,n和k,在构造函数中请将m赋值给数据成员c,将n和k分别赋值给基类priAl构造函数的参数m和n。请在注释“∥********3********”之后添加适当的语句。
(4)完成类priA2的成员函数show的定义,该函数调用基类成员函数,输出基类私有成员a和b及类priA2自身的数据成员C的值,上述三个值在输出时以空格隔开。请在注释“∥********4********”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#lnclude
C1ass baseA
{
int a;
public:
∥********1********
int geta(){return a ;}
};
class priAl:publ ic baseA
{
int b;
public:
∥********2********
int getb(){return b;}
};
class priA2:public priAl
{
int C;
public:
∥********3********
void show()
{
∥********4********
}
};
void main()
{
priA2 a(7,5,10);
cout<
问答题请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,补充编劓C++程序proj3.cpp,其功能是读取文本文件in.dat中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符序列反转,并输出到文件out.dat中。文件in.dat的长度不大于1000字节。要求:补充编制的内容写在“//**********333**********”与“//**********66666**********”两行之间。实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。不得修改程序的其他部分。注意:程序最后已将结果输出到文件out.dat中,输出函数writeToFile已经给出并且调用。//proj3.cpp执nclude<iostream>#include<fstream>#include<cstring>using namespace std;class doc{private:char *str;//文本字符串首地址int length;//文本字符个数public://构造函数,读取文件内容,用于初始化新对象,filename是文件名字符串首地址doc(char*filename);void reverse();//将字符序列反转~doc();void writeToFile(char *filename);};doc::doc(char *filename){ifstream myFile(filename);int len=1001,tmp;str=new char[len];length=0;while((tmp=myFile.get())!=EOF){str[length++]=tmp;}str[length]='\0';myFile.close();}void doc::reverse(){//将数组str中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个//字符交换……//**********333**********//**********666**********}doc::~doc(){delete[]str;}void doc::writeToFile(char *filename){ofstream outFile(filename);outFile<<str;outFile.close();}void main(){doc myDoe("in.dat");myDoc.reverse();myDoc.writeToFile("out.dat");}
问答题改错题
使用VC6打开考生文件夹下的工程test1_1,此工程包含一个源程序文件test1_1.cpp,但该程序运行有问题,请改正main函数中的错误,使该程序的输出结果如下:
Constructor called.
Default constructor called.
Area is 6
Area is 0
Area is 6
源程序文件test1_1.cpp清单如下:
#include
class RectAngle
{
private:
double ledge,sedge;
public:
RectAngle()
{
cout<<"Default constructor called./n";
}
RectAngle(double l,double s)
{
ledge=l;sedge=s;
cout<<"Constructor called./n";
}
void Set(double l,double s)
{
ledge=l;sedge=s;
}
void Area()
{
cout<<"Area is "<
问答题使用VC++6.0打开考生文件夹下的源程序文件1.cpp,该程序运行时有错误,请改正程序中的错误,本题的功能是从键盘中输入字符串str,然后输出字符串str中的字符个数。 注意:错误的语句在/********error********/的下面,修改该语句即可。其他的语句不能修改。 试题程序: #include<iostream> int main() /********error********/ cout<<"please input a string:"<<end1; /********error********/ namespace std; char str[256]; cin.getline(str,256); cout<<strlen(str)<<end1; return 0;
