问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错误,请改正程序中的错误。本题的功能是:从键盘输入字符串s,然后输出字符串s中的字符个数。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream>intmain(){//********error*********//********error*********namespacestd;chars[256];cin.getline(s,256);coutstrlen(s)endl;return0;}
问答题使用VC6打开考生文件夹下的工程MyProj10。此工程包含一个源程序文件MyMain10.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。 请按要求完成下列操作,将类的定义补充完成: ①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。 ②完成构造函数Derived(int i)定义,采用初始化列表的方式使基类Base私有成员a初始化为i+1,类Derived的私有成员b初始化为i。请在注释“//* *2* *”之后添加适当的语句。 ③完成类Derived的成员函数show()的类体外的定义。函数show()中要显式调用基类的show()函数,然后要输出私有成员b的值。请在注释“//* *3**”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain10.cpp清单如下: //MyMain10.cpp #include<iostream> using namespace std; class Base public: Base(int x) a=x void show() cout<<a; private: int a; ; //* * *1* * * public: //* * * 2 * * * void show(); private: int b; ; void Derived :: show() //* * * 3 * * * int main() Derived d(1), *pb; pb=&d; pb->show(); return 0;
问答题请使用[答题]菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明了一个人员信息类Person。在Person类中数据成员name、age和address分别存放人员的姓名、年龄和地址。构造函数Person用以初始化数据成员。补充编制程序,使其功能完整。在main函数中分别创建了两个Person类对象p1和p2,并显示两个对象信息,此种情况下程序的输出应为:
Jane 25 Beijing
Tom 22 Shanghai
注意:只能在函数Person中的“//********333********”和“//********666********”之间填入若干语句,不要改动程序中的其他内容。
//proj3.h
#include <iostream>
#include <string>
using namespace std;
class Person{
public:
char name[20];
int age;
char* address;
public:
Person (char* name, int_age,char*_add =NULL); //构造函数
void info_display (); //人员信息显示
~Person (); //析构函数
};
void writeToFile (const char * path="");
//proj3.cpp
#include <iostream>
#include <string>
#include "proj3.h"
using namespace std;
Person::Person (char*_name, int_age, char* _add) :age (_age)
{
//把字符串_name复制到数组name中
//使address指向一个动态空间,把字符串_add复制到该数组中。
//******** 333********
//******** 666********
}
void Person::info_display ()
{
cout <<name << '/t' <<age << '/t';
if (address!=NULL)
cout << address << endl;
}
Person:: ~Person ()
{
if (address !=NULL)
delete[] address;
}
void main ()
{
char add[100];
strcpy(add, "Beijing") ;
Person p1 ("Jane", 25, add) ;
p1.info_display () ;
strcpy(add, "Shanghai");
Person * p2 =new Person ("Tom", 22, add);
p2 -> info_display () ;
delete p2;
writeToFile ("");
}
问答题请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的Matrix是一个用于表示矩阵的类。operator+的功能是实现两个矩阵的加法运算。例如,若有两个3行3列的矩阵则A与B相加的和为请编写openaor+函数。要求:补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matvix.h#include#includeusingnamespacestd;constintM=18;constintN=18;classMatrix{intarray[M][N];public:Matrix(){}intgetElement(inti,intj)const{returnarray[i][j];)voidsetElement(inti,intj,intvalue){array[i][j]=value;)voidshow(constchar*s)const{cout#include"Matrix.h"voidreadFromFile(constchar*filename,Matrix&m){ifstreaminfile(filename);if(!infile){cerr<>d;m.setElement(i,j,d);}}Matrixoperator+(constMatrix&ml,constMatrix&m2){//********333********//********666********}intmain(){Matrixm1,m2,sum;readFromFile("",m1);readFromFile("",m2);sum=ml+m2;m1.show("Matrixm1:");m2.show("Matrixm2:");sum.show("Matrixsum=m1+m2:");writeToFile("",sum);return0;}
问答题使用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打开考生文件夹下的工程kt7_3,此工程包含一个源程序文件kt7_3.cpp,其中含有一个类Circle的定义,但该类的定义并不完整。请按要求完成下列操作,将类Circle的定义补充完整。
(1)为类Circle增加一个构造函数,该函数有一个参数,并在构造时将该参数值赋给成员radius。将该函数实现为一个非内联函数,并且使用参数列表的方式将类成员赋值。请在注释“//**1**”之后添加适当的语句。
(2)为类Circle增加一个成员函数print(),使得可以输出有关圆的信息,比如下列程序
Circlec;
c.SetRadius(5);
c.Print();
将输出:Thecirclehasradiusof5!
请在注释“//**2**”之后添加适当的语句。
(3)完成友元函数voidCompareR(Circle*c1,Circle*c2)的定义,在屏幕中输出c1与c2比较radius大小结果,要求使用if-else结构完成。请在注释“//**3**”之后添加适当的语句。
输出结果如下:
Thecirclehasradusof5!
Thecirclehasradiusof10!
c1
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件kt7_3.cpp清单如下:
#include
classCircle{
public:
Circle():radius(5){}
//**1**
voidSetRadius(intr){radius=r;}
intGetRadius(){returnradius;}
//**2**
friendvoidCompareR(Circle*c1,Circle*c2);
private:
intradius;};
voidCompareR(Circle*c1,Circle*c2)
{//**3**
coutc2"
else
if((c1->GetRadius())==(c2->GetRadius()))
cout
else
if((c1->GetRadius())GetRadius()))
cout
voidmain()
{Circlec1;
c1.SetRadius(5);
c1.Print();
Circlec2(10);
c2.Print();
CompareR(}
问答题请使用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 ****”。 // proj2, 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 cout << ch[0] << "," << chi[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 ;
问答题使用VC6打开
下的源程序文件modi2.cpp。阅读下列函数说明和代码。函数show()的功能是将1、2、3、4四个数字,组成互不相同且无重复数字的四位数,并将这些数输出到屏幕,输出的内容如下:
1234 1243 1324 1342 1423 1432 2134 2143 23142341 2413 2431 3124 3142 3214 3241 3412 3421 4123 41324213 423l 4312 4321
将函数show()补充完整。
注意:请勿改动主函数。
#include <iostream.h>
void show()
{
}
int main()
{
show();
return 0;
}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计总页数:299 已把“C++语言程序设计”翻到第50页 已把“C++语言程序设计”翻到第51页 已把“C++语言程序设计”翻到第52页 已把“C++语言程序设计”翻到第51页 已把书合上。 当前页: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(theTitle)+1]; strcpy(title,theTitle); cout<<endl<<"书名:"<<title<<"总页数:"<<num_pages; ~Book()delete[]title;) bool isClosed()constreturn cur_page==0; //书合上时返回true,否则返回falsebool isOpen()constreturn! isClosed();)//书打开时返回true,否则返回falseint 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<<endl; if(isClosed()) cout<<"书是合上的。"; else //ERROR**********found********** num_pages=0; cout<<"已把书合上。"; cout<<endl; ; int main() Book book("C++语言程序设计",299); book.openAtPage(50); book.openAtNextPage(); book.openAtNextPage(); book.openAtPrevPage(); book.close(); cout<<"当前页:"<<book.current-Page()<<endl; return 0;
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)定义私有数据成员year、month、day,分别用于年、月、日,它们都是int型的数据,请在注释//********1********后添加适当的语句。 (2)完成判断数据成员date2是否是闰年函数intDate::IsLeapYear0的定义,返回数据成员date2是否是闰年的值,是则返回1,否则返回0。请在注释//********2********后添加适当的语句。 (3)设置SetData0的默认时间为:2004-12-1,请在注释//********3********后添加适当的语句。 (4)完成主函数main(),完成输出date2 is aleapyear,请在注释//********4********后添加适当的语句。 提示:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;被100整除也可被400整除的为闰年。 注意:增加或修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。#include<iostream>using namespace std;Class Date{public: //********3******** void SetDate(int y,intm,int n); int IsLeapYear(); Void Print() {cout<<year<<"-"<<month<< "-" <<day<<endl;}Prlvate: //********1********};void Date::SetDate(int y,intm,int d){ year=y; month=m; day=d;}int Date::IsLeapYear(){ //********2******** retUrn} int main(){ Date datel , date2; datel.SetDate(); date2.SetDate(2004,12,1); cout<<"datel:"; datel.Print(); cout<<"date2:"; date2.Print(); //********4******** Cout << "date2 is"<<" ":"not")<<"a leapyear."<<endl; return 0;}
问答题使用VC6打开源程序文件modi3.cpp。其中类TcstClass用于把文件输出到屏幕,然后进行文件的分割。分割的方法如下:第一个文件的大小是文件的前一半,另外一个文件的大小是剩余部分。 此程序将int.txt文件中的内容输出到屏幕,并且将文件按照以上方式分割,存于文件out1.txt和out2.txt中。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。 (1)从输入文件中获得一个字符,并判断是否到文件结尾,如果到文件结尾,则退出循环。请在注释∥********1********后添加适当的语句。 (2)把获得的输入文件的内容存储到buf中,并且用len记录下文件的长度。请在注释∥********2********后添加适当的语句。 (3)将输入文件的后一半内容存储在第二个文件中,请在注释∥********3********后添加适当的语句。 (4)使用文件流对象打开输入文件in.txt,请在注释∥********4********后添加适当的语句。注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。#include<ioStream.h>#include<fstream.h>#include<Stdlib.h>class TestClass{public: TestClaSS(char*fileName) {len=0; fStream infile; infile.open(fileName,ios::in); char ch;∥********1******** while() { cout<<ch;∥********2******** } infile.Close(); } void split() { fstream outfile1; fstream outfile2; outfilel.open(“out1.txt”,ios::out); outfile2.open(“out2.txt”,ios::out); int i=0; for(i=0;i<len/2;i++) { outfilel<<buf[i]; } do{∥********3******** }while(i!=len); outfile1.close(); outfile2.close(); }private: int fun; char buf[1024];};void main(){∥********4******** TestClass Fsp(); Fsp.split();return;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包括类Date(“日期”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
2006-1-1
2005-12-31
2005-12-31
2006-1-1
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Date {
public:
Date(int y=2006, int m=1, int d=1)
// ERROR ********* found*********
: year=y, month=m, day=d
{}
/ERROR ********* found*********
Date (const Date d)
{
this->year=d.year;
this->month=d.month;
this->day=d.day;
}
void print () const
{
cout << year << '-' << month <<'-'<< day << endl;
}
private:
//ERROR ********* found*********
int year (2006), month(1), day(1);
};
int main ()
{
Date d1, d2 (2005, 12, 31), d3;
d1.print ();
d2.print();
d3=d2;
d2=d1;
d1=d3;
d1.print();
d2.print();
return 0;
}
问答题使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成列操作,将类的定义补充完整,实现以下功能: (1)完成CBook类构造函数,对整型变量ID和作者Author进行赋值,请在注释//********1********后添加适当的语句。 (2)完成类CBooks的析构函数,释放申请的内存,请在注释//********2********后添加适当的语句。 (3)完成类CBooks的AddBookMember函数,请在注释//********3********后添加适当的语句。 (4)完成CBooks类,用于由书的ID检索到作者的函数char*GetBookAuthor(int nID),请在注释//********4********后添加适当的语句。 (5)程序的输出结果为: Tom Harry 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。#include<iostream.h>#include<cstring>class CBook{public: int ID; char Author[32];public: CBook(int ID_Number,char*Author Name) { this->ID=ID_Number; //********1******** }};class CBooks{private: class Node { public: Node* next; CBook* book; ]*m_pBook; public: CBooks() { m_pBook=NULL; } ~CBooks() { //********2******** while() { Node* p = m_pBook->next; delete m_pBook->book; delete m_pBook; m_pBook=p; } } int AddBookMenber(int nID,char* Author) { Node* p=m_pBook; Node *q=NULL; //********3******** while() { if(nID==p->book->ID) { return 0; } q=p; p=p->next; } if(p==NULL) { p=new Node; p->next=NULL; p->book= new CBook(nID,Author); } if(q==NULL) { m_Book=p; } else { q->next=p; } return 1; } Char* GetBookAuthor(int nID) { Node *p=m_pBook; //********4******** while() { if(p->book->ID==nID { return p->book->Author; } p=p->next; } return 0; }};int main(){ CBooks books1; books1.AddBookMenber(1,"Tom"); books 1.AddBookMenber(3,"Lee"); books 1.AddBookMenber(4,"Lily"); bookS].AddBOOkMenber(5,”Harry”); cout<<books 1.GetBookAuthor(1)<<end1; cout<<books 1.GetBookAuthor(5)<<end1; return 0;}
问答题使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序。该程序从键盘读入整数,并按从大到小的顺序输出输入整数中互不相等的那些整数。程序一边读入整数,一边构造一个以大到小顺序链接的链表,直至输入0时结束。然后顺序输出链表上各表元的整数值。主函数每读入一个整数,就调用函数fun(),函数fun()将还未出现在链表上的整数按从大到小的顺序插入到链表中。为了插入方便,链表在表首有一个辅助表元。 注意:不能修改程序的其他部分,只能修改fun()函数。#include<iostream>Class NODE{public: int data; NODE*next;};Void fun(NODE*list,int x){}Void main(){ int x; NODE*head,*p;/*首先建立只有辅助表元的空链表*/ head=new NODE; head->next=NULL; std::cout<<"Enter integers,end with 0"<<Std::endl; while(1) { Std::Cin>>x; if(x==0) break; fun(head,x); } for(p=head->next;p!=NULL;p=p->next、 std::cout<<p一>data<<' '; Std::cout<<Std::endl; do{ p=head->next; delete head; head=p; }while(p);}
问答题使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码,实现函数sort(int A[],int n),用选择排序法将数组从大到小排序。 提示:选择排序法的思想是 (1)反复从还未排好序的那部分线性表中选出关键字最小的结点。 (2)按照从线性表中选出的顺序排列结点,重新组成线性表。 (3)直到未排序的那部分为空,使得重新形成的线性表是一个有序的线性表。 补充函数sort(int A[],int n),实现选择排序。 注意:请勿改动主函数。 试题程序: #include<iostream.h> #define N 10 void sort(int A[N],int n) int main() int A[N]=-72,54,-6,7,18,102,0,4,-11,1; sort(A,10); for(int i=0;i<sizeof(A)/sizeof(int);i++) cout<<A[i]<<''; 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******** } TestClass() { //********4******** }};int main(){ TestClass Stu; stu.set(111,"Garfield"); stu.print(); return 0;}
问答题请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Name:Smith Age:21 ID:99999 CourseNum:12 Record:970
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class StudentInfo
{
protected:
//ERROR ********** found**********
char Name[];
int Age;
int ID;
int CourseNum;
float Record;
public:
//ERROR ********** found**********
void StudentInfo(char * name, int age, int ID, int courseNum, float record);
// ERROR ********** found**********
void ~StudentInfo () { delete [] Name; }
float AverageRecord() {
return Record/CourseNum;
}
void show()const;
};
StudentInfo:: StudentInfo (char * name, int age, int ID, int courseNum, float record)
{
Name = strdup(name);
Age = age;
this->ID = ID;
CourseNum = courseNum;
Record = record;
}
void StudentInfo::show ()
{
cout <<"Name: "<<Name<<" Age: "<<Age<<" ID: "<<ID
<<" CourseNum: " << CourseNum <<"Record: "<<Record<
问答题请编写一个函数int CalcDigital(char *str),该函数可返回字符串str中数字字符(即0~9这10个数字)的个数,如字符串“olympic2008”中数字字符的个数为4。请用if条件判断语句与for循环语句来实现该函数。 注意:部分源程序已存在文件test9_2.cpp中。 请勿修改主函数main和其他函数中的任何内容,仅在函数find的花括号中填写若干语句。 文件test9_2.cpp的内容如下: #include<iostream.h> #include<string.h> int CalcDigital(char*str); void main() char *str; str=new char[255]; cout<<"输入字符串:"; cin>>str; int num=CalcDigital(str); cout<<str<<":"<<num<<endl; int CalcDigital(char *str)
问答题使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错,请改正其中的错误,使得程序正常运行,并使程序输出的结果为: 1 1 2 1 注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********error********的下面。#include<iostream.h>class TestClass{ //********error******** //********error******** const int j;public: TestClass() { //********error******** Static int i=0; cout<<++i<<end1; cout<<j<<end1; }};void main(){ TestClass obj 1; TestClass obj 2; obj2.j+=obj1.j; return;}
问答题使用VC6打开下的源程序文件modi1.cpp,该程序运行时有错误,请改正错误,使得程序输出:Hellotest注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//******error******的下面。#include<iostream>//********error********template<T>voidfun(Tt){}//********error********template<bool>voidfun(boolt){}intmsin(){//********error********boolflag=TRUE;fun(flag);fun((int)flag);return0;}
