应用题 使用VC++6.0打开下的源程序文件3.cpp。其中定义的类不完整,按要求完成下列操作,将类的定义补充完整。
(1)将文件以追加的方式打开。请在注释1后添加适当的语句。
(2)定义m、n为类TC的公有int型数据成员。请在注释2后添加适当的语句。
(3)定义p为类TC的数据成员指针,并指向类TC的数据成员m。请在注释3后添加适当的语句。
(4)定义p指向类TC的数据成员n。请在注释4后添加适当的语句。
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
void WriteFile(int x)
{
ofstream out1;
//********1********
out1.open('3.txt', );
out1 << x << ′ ′;
out1.close();
}
void ClearFile()
{
ofstream out1;
out1.open('3.txt');
out1.close();
}
class TC
{
public:
void disp()
{
cout << 'm=' << m << endl;
WriteFile(m);
cout << 'n=' << n << endl;
WriteFile(n);
}
//********2********
};
void main()
{
//********3********
ClearFile();
TC a;
a.*p=30;
//********4********
a.*p=45;
a.disp();
}
应用题 请使用菜单命令或直接用VC6打开下的工程prj03。请完善下列程序,实现一个Number类。Add函数计算2个Number类对象之和,并将和作为Number对象返回。这2个Number对象中各包含一个用字符串表示的10进制正整数,均不超过int的表示范围。在main函数中给出了一组测试数据,使用这组数据进行测试时程序的输出应该是:
100
注意:只需在//********333********和//********666********之间填入所编写的若干语句,不要改动程序中的其他内容。
#include<iostream>
#include<cstring>
#include<sstream>
#include 'proj3.h'
using namespace std;
Number::Number(char*n){
num=new char[strlen(n)+1];
strcpy(num, n);
}
Number::Number(Numbern){
num=new char[strlen(n.num)+1];
strcpy(num, n.num);
}
Number::Number(int n){
char*tmp=new char[30];
itoa(n, tmp, 10); //函数itoa的功能是将n转换为字符串,存放于tmp指向的数组中
num=new char[strlen(tmp)+1];
strcpy(num, tmp);
}
Number::~Number(){
if(num)
delete[]num;
}
Number Number::Add(Number n){
//*******333*******
//*******666*******
}
nt main(){
Number num1('32');
Number num2('68');
Number num3=num1.Add(num2);
cout<<num3.GetNum();
writeToFile('c:\test\');
return 0;
}
//proj3\proj3.h
class Number{
private:
char*num; //具体数字,num[0]表示最高位的数
public:
~Number();
Number(char*n);
Number(Numbern);
Number(int n);
Number Add(Number n);
char*GetNum(){return num;}
};
void writeToFile(const char*path);
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)定义复数类CComplex的私有成员变量real和imaginary,分别用来表示复数的实部和虚部,都是double类型的变量。请在注释//********1********后添加适当的语句。
(2)添加复数类CComplex的带一个参数的构造函数,分别将real和imaginary赋值为参数r和0。请在注释//********2********后添加适当的语句。
(3)完成对运算符“+”的重载,分别对复数的实部和虚部相加。请在注释//********3********后添加适当的语句。
(4)完成复数的友元函数Equal(CComplex c1,CComplex c2)的定义,如果两个数的实部和虚部都相等,则返回1,否则返回0,请在注释//********4********后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include<iostream.h>
class CComplex
{
private:
//********1********
public:
CComplex()
{
real = imaginary = 0;
}
CComplex(double r) {
//********2********
}
CComplex operator+(CComplex c1) {
//********3********
temp.real= real+c1.real;
temp.imaginary=
imaginary+c1.imaginary;
return temp;
}
void Set(int re,int imag)
{
real = re;
imaginary = imag;
}
friend bool Equal(CComplex c1,CComplex c2);
};
bool Equal(CComplex c1, CComplex c2)
{
//********4********
}
int main()
{
CComplex complex1(5);
CComplex complex2;
cout<<Equal(complex1,comp lex2)<<endl;
complex2.Set(5,0);
cout<<Equal(complex1,comp lex2)<<endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“书”类Book及其派生出的“教材”类TeachingMaterial的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。该程序的正确输出结果应为:
教材名:C++语言程序设计
页数:299
作者:张三
相关课程:面向对象的程序设计
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Book{ //'书'类
char * title; //书
int num_pages; //页数
char * writer; //作者姓名
public:
//********** found**********
Book(const char * the_title, int pages, const char * the_writer): ______{
title =new char[ strlen (the_titie) +1];
strcpy(title,the_title);
//********** found**********
______
strcpy(writer,the_writer);
}
~Book(){ delete []title; delete [] writer; }
int numOfPages()const{ return num_pages;} //返回书的页数
const char * theTitle () const { return title; } //返回书名
const char * theWriter () const{ return writer; } //返回作者名
};
class TeachingMaterial: public Book{ //'教材'类
char * course;
public:
TeachingMaterial (const char * the_title, int pages, const char * the_writer, const char * the_course)
//********** found**********
:______{
course = new char [ strlen (the_course) +1];
strcpy (course, the_course);
}
~TeachingMaterial () { delete [] course; }
const char * theCourse () const{ return course; } //返回相关课程的名称
};
int main () {
TeachingMaterial abook ('C++语言程序设计', 299, '张三', '面向对象的程序设计');
cout <<'教材名:' << a_book.theTitle() <<endl
<< '页数:' << a_book.numOfPages ()<< endl
<<'作者:' <<a_book.theWriter ()<< endl
//********** found**********
<< '相关课程:' <<______;
cout << endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的IntSet是一个用于表示正整数集合的类。IntSet的成员函数Merge的功能是求当前集合与另一个集合的并集,在Merge中可以使用成员函数IsMemberOf判断一个正整数是否在集合中。请完成成员函数Merge。在main函数中给出了一组测试数据,此时程序的输出应该是:
求并集前:
1 2 3 5 8 10
2 8 9 11 30 56 67
求并集后:
1 2 3 5 8 10
2 8 9 11 30 56 67
1 2 3 5 8 10 9 11 30 56 67
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//Intset.h
#include <iostream>
using namespace std;
const int Max=100;
class IntSet
{
public:
IntSet() //构造一个空集合
{
end = -1;
}
IntSet(int a[], int size)
//构造一个包含数组a中size个元素的集合
{
if (size >=Max)
end=Max-1;
else
end =size-1;
for(int i=0; i<=end; i++)
element[i]=a[i];
}
bool IsMemberOf(int a)
//判断a是否为集合中的元素
{
for(int i=0; i<=end; i++)
if (element[i]==a)
return true;
return false;
}
int GetEnd() {return end;}
//返回最后一个元素的下标
int GetElement(int i) {return element[i]; }
//返回下标i处的元素
IntSet Merge(IntSet set);
//求当前集合与集合set的并集
void Print()
//输出集合中的所有元素
{
for(int i=0; i<=end; i++)
if((i+1)% 20==0)
cout << element[i] << endl;
else
cout <<element[i] <<' ';
cout <<endl;
}
private:
int element[Max];
int end;
};
void writeToFile(const char * );
//main.cpp
#include 'IntSet.h'
IntSet IntSet::Merge(IntSet set)
{
int a[Max],size =0;
//******** 333********
//******** 666********
return IntSet(a, size);
}
int main ()
{
int a[] = {1, 2, 3, 5, 8, 10};
int b[] ={2, 8, 9, 11, 30, 56, 67};
IntSet set1(a, 6), set2(b, 7), set3;
cout <<'求并集前:' <<endl;
set1.Print();
set2.Print();
set3.Print();
set3 =set1.Merge(set2);
cout << endl << '求并集后: ' << endl;
set1.Print();
set2.Print();
set3.Print();
writeToFile('');
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:
(1)声明类objA1,请在注释//********1********后添加适当的语句。
(2)为类objA0增加友元函数func(),请在注释//********2********后添加适当的语句。
(3)为类objA1增加友元函数func(),请在注释//********3********后添加适当的语句。
(4)函数func0返回objA1对象中的变量和objA0的静态变量的乘积,请在注释//********4********后添加适当的语句。
注意:增加代码,或者修改代码的位置已经用符号表示出来。请不要修改其他的程序代码。
#include<iostream.h>
//********1********
class objA0
{
private:
static int m_A0;
//********2********
};
int objA0::m_A0=10;
class objA1
{
private:
int m_A1;
//********3********
public:
objA1(int i)
{
m_A1=i;
}
};
int func(objA1 obj)
{
//********4********
}
int main()
{
objA1 obj0(10);
cout<<func(obj0)<<endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。此工程中定义了一个人员类Person,然后派生出学生类Student和教授类Professor。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
My name is Zhang.
my name is Wang and my G.P.A.is 3.88.
My name is Li, I have 8 publications..
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//********found********”。
#include <iostream>
using namespace std;
class Person{
public:
//********** found**********
______{name =NULL;}
Person (char* s)
{
name = new char[ strlen (s) +1];
strcpy(name, s);
}
~Person()
{
if(name!=NULL) delete [] name;
}
//********** found**********
______Disp() //声明虚函数
{
cout << 'My name is' << name <<'.\n';
}
void setName (char* s)
{
name = new char[ strlen (s) +1];
strcpy (name, s);
}
protected:
char* name;
};
class Student : public Person{
public:
//********** found**********
Student (char * s, double g) ______{}
void Disp ()
{
cout << 'my name is' << name << 'and my G.P.A. is' << gpa <<'.\n';
}
private:
float gpa;
};
class Professor : public Person{
public:
void setPubls (int n) {publs =n;}
void Disp ()
{
cout << 'My name is' <<name <<', I have' << publs <<' publications.\n';
}
private:
int publs;
};
int main ()
{
//********** found**********
______;
Person x ('Zhang');
p = x; p->Disp();
Student y('Wang', 3.88);
p = y; p->Disp();
Professor z;
z. setName ('Li');
z. setPubls (8);
p = z; p->Disp();
return 0;
}
应用题 使用VC++6.0打开下的源程序文件3.cpp。程序通过继承关系,实现对姓名的控制。类TC1实现对名字访问的接口,TC2实现对名字的设置和输出。
程序输出为
TC2Name
May
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)在类TC1中定义接口函数GetName为纯虚函数。请在注释1后添加适当的语句。
(2)函数GetName2用于获得名字的缓存,但只获得允许读操作这个缓存,请在注释2后添加适当的语句。
(3)实现TC2的构造函数,请在注释3后添加适当的语句。
(4)完成TC2的构造函数,实现对名字的处理。请在注释4后添加适当的语句。
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class TC1
{
public:
//********1********
};
class TC2: public TC1
{
public:
void GetName()
{
cout<<'TC2Name'<<endl;
}
//********2********
{
return m_str;
}
//********3********
{
int i;
for(i=0; str[i]!=0; i++)
m_str[i]=str[i];
//********4********
}
private:
char m_str[32];
};
void main()
{
TC1*p;
TC2 obj1('May');
p=obj1;
p->GetName();
cout<<obj1.GetName2()<<endl;
return;
}
应用题请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中包含一个源程序文件main.cpp,其中有坐标点类Point、线段类Line和矩形类Rectangle的定义,还有main函数的定义。程序中两点间的距离的计算是按公式实现的。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:Width:4Height:6Diagonal:7.2111area:24注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。#include<iostream>#include<cmath>usingnamespacestd;classPoint{//坐标点类public:constdoublex,y;Point(doublex=0.0,doubley=0.0):x(x),y(y){}//**********found**********doubledistanceTo(______)const{//到指定点的距离returnsqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));}};classLine{//线段类public:constPointp1,p2;//线段的两个端点Line(Pointp1,Pointp2):p1(p1),p2(p2){}//**********found**********doublelength()const{returnp1.______;}//线段的长度};classRectangle{//矩形类public:constPointupper_left;//矩形的左上角坐标constPointdown_right;//矩形的右下角坐标Rectangle(Pointp1,Pointp2):up-per_left(p1),down_right(p2){}doublewidth()const{//矩形水平边长度//**********found**********returnLine(upperleft,______).length();}doubleheight()const{//矩形垂直边长度returnLine(upper_left,Point(upper_left.x,down_right.y)).length();}doublelengthOfDiagonal()const{//矩形对角线长度returnLine(upper_left,down_right).length();}doublearea()const{//矩形面积//**********found**********return______;}};intmain(){Rectangler(Point(1.0,8.0),Point(5.0,2.0));cout<<'Width:'<<r.width()<<endl;cout<<'Height:'<<r.height()<<endl;cout<<'Diagonal:'<<r.lengthOfDiagonal()<<endl;cout<<'area:'<<r.area()<<endl;return0}
应用题 请使用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;
}
应用题 使用VC++6.0打开下的源程序文件3.cpp。类People包括姓名和年龄两个字段。函数fun获得年龄比较大的人的姓名,然后输出这个姓名到屏幕。
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)完成类的构造函数功能,清在注释1后添加适当的语句。
(2)完成构造函数对姓名的赋值,请在注释2后添加适当的语句。
(3)定义类的友元函数fun,请在注释3后添加适当的语句。
(4)补充函数fun的年龄比较功能,请在注释4后添加适当的语句。
注意:增加或者修改代码的位置已经用符号表示出来,请不要修改其他的程序代码。
试题程序:
#include<iostream.h>
class People
{
public:
//********1********
{
int i;
for(i=0; sUserName[-i]!=0; i++)
{m_UserName[i]=sUserName[i];
}
//********2********
m_Old=nOld;
}
private:
char m_UserName[32];
int m_Old;
//********3*********
};
void fun(char*s, People person1, People person2)
{
//********4********
if()
{
for(int i=0; person1.m_UserName[i]!=0; i++)
{
s[i]=person1.m_UserName[i];
s[i+1]=0;
}
}
else
{
for(int i=0; person2. m_UserName[i]!=0; i++)
{s[i]=person2. m_UserName[i];
s[i+1]=0;
}
}
}
void main()
{
char s[32];
People p1('abc', 20);
People p2('def', 30);
fun(s, p1, p2);
cout<<s<<endl;
return;
}
应用题 请使用VC6或使用[答题]菜单打开proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个operator+函数。程序的正确输出应该是:
两个数据表:
1, 2, 3, 4, 5, 6
3, 4, 5, 6, 7, 8
两个数据表之和:
4, 6, 8, 10, 12, 14
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//DataList. h
#include<iostream>
using namespaee std;
class DataList {//数据表类
int len;
double*d;
public:
DataList(int len, double data[]=NULL);
DataList(DataList data);
int length()const {return len;}
double getElement(int i)const {return d[i];}
DataList operator+(const DataList list)const;//两个数据表求和
void show()const;//显示数据表
};
void writeToFile(char*, const DataList);
//main. cpp
#include'DataList. h'
DataList::DataList(int len, double data[]): len(len) {
d=new double[len];
for(int i=0; i<len; i++)
d[i]=(data==NULL?0.0: data[i]);
}
DataList::DataList(DataList data): len(data. len) {
d=new double[len];
for(int i=0; i<len; i++)
d[i]=data. d[i];
}
DataList DataList::operator+(const DataList list)const {//两个数据表求和
double*dd=new double [1ist. length()];
//********333********
//********666********
return DataList(list. length(), dd);
}
void DataList::show()const {//显示数据表
for(int i=0; i<len-1; i++)
cout<<d[i]<<',';
cout<<d[len-1]<<endl;
}
int main() {
double s1[]={1, 2, 3, 4, 5, 6};
double s2[]={3, 4, 5, 6, 7, 8};
DataList list1(6, s1), list2(6, s2);//定义两个数据表对象
cout<<”两个数据表:”<<endl;
list1. show();
list2. show();
cout<<endl<<'两个数据表之和:'<<endl;
list1+list2). show();
writeToFile(' ', listl+list2);
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,其中定义了MyString类。MyString是一个用于表示字符串的类,其构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中;成员函数reverse的功能是对字符串进行反转操作,例如,字符串“ABCDE”经过反转操作后,会变为“EDCBA”;成员函数print的作用是将字符串输出到屏幕上。
请在横线处填写适当的代码并删除横线,以实现MyString类的功能。此程序的正确输出结果应为:
Before reverse:
abc
defg
After reverse:
cba
gfed
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
class MyString {
public:
MyString (const char* s)
{
//********** found**********
m_str = new char[______];
strcpy (m_str, s);
}
~MyString ()
{
//********** found**********
______;
}
void reverse ()
{
int n = strlen(m_str);
for (int i=0; i < n/2; ++i) {
int tmp = m_str[i];
//********** found**********
m_str[i] = ______;
//********** found**********
______;
}
}
void print ()
{
cout << m_str << endl;
}
//其他成员...
private:
char* m_str;
};
int main(int argc, char * argv[])
{
MyString str1 ('abc'), str2 ('defg');
cout << 'Before reverse: \n';
str1.print ();
str2.print ();
str1. reverse ();
str2.reverse ();
cout << 'After reverse: \n';
str1.print ();
str2.print ();
return 0;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。阅读下列函数说明和代码。函数num(char *str)用于返回字符串中非数字的个数。
例如:abc123abc45
返回值为:6
将函数num补充完整。
注意:请勿改动主函数main。
试题程序:
#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或使用[答题]菜单打开考生文件夹proj2下的工程proj2,此工程中含有一个源程序文件proj2.cpp,其中定义了MyString类。MyString是一个用于表示字符串的类,其构造函数负责动态分配一个字符数组,并将形参指向的字符串复制到该数组中;成员函数reverse的功能是对字符串进行反转操作,例如,字符串“ABCDE”经过反转操作后,会变为“EDCBA”;成员函数print的作用是将字符串输出到屏幕上。
请在横线处填写适当的代码并删除横线,以实现MyString类的功能。此程序的正确输出结果应为:
Before reverse:
abc
defg
After reverse:
cba
gfed
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
class MyString {
public:
MyString (const char* s)
{
//********** found**********
m_str = new char[______];
strcpy (m_str, s);
}
~MyString ()
{
//********** found**********
______;
}
void reverse ()
{
int n = strlen(m_str);
for (int i=0; i < n/2; ++i) {
int tmp = m_str[i];
//********** found**********
m_str[i] = ______;
//********** found**********
______;
}
}
void print ()
{
cout << m_str << endl;
}
//其他成员...
private:
char* m_str;
};
int main(int argc, char * argv[])
{
MyString str1 ('abc'), str2 ('defg');
cout << 'Before reverse: \n';
str1.print ();
str2.print ();
str1. reverse ();
str2.reverse ();
cout << 'After reverse: \n';
str1.print ();
str2.print ();
return 0;
}
应用题 请使用【答题】菜单命令或直接用VC6打开下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组。ValArray类的复制构造函数应实现对象的深层复制。请编写ValArray类的复制构造函数。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:
ValArray v1={1,2,3,4,5}
ValArray v2={1,2,3,4,5}
要求:
补充编制的内容写在“// ********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为boj文件,并且在本程序中调用。
//ValArray.h
#include <iostream>
using namespace std;
class ValArray {
int * v;
int size;
public:
ValArray (const int * p, int n) : size(n)
{
v = new int[size];
for (int i = 0; i < size; i ++)
v[i] = p[i];
}
ValArray (const ValArray other);
~ValArray() {delete [] v;}
void print (ostream out) const
{
out << '{';
for (int i = 0; i < size -1; i ++)
out << v[i] << ',';
out << v[size-1] << '}';
}
void setArray (int i, int val)
{
v[i] = val;
}
};
void writeToFile(const char *);
//main.cpp
#include 'ValArray.h'
ValArray:: ValArray (const ValArray other)
{
// ********333********
// ********666********
}
int main()
{
const int a[] = {1,2,3,4, 5};
ValArray v1(a, 5);
cout << 'ValArray v1 =';
v1.print(cout);
cout << endl;
ValArray v2(v1);
cout << 'ValArray v2 =';
v2.print(cout);
cout << endl;
writeToFile(' ');
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3,此工程包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象myArray中,然后对整数序列按非递减排序,最后由函数writeToFile选择序列中的部分数据输出到文件out.dat中。文件in.dat中的整数个数不大于300个。
要求:
补充编制的内容写在“// **********333**********”与“// **********666**********”两行之间。实现对整数序列按非递减排序,并将排序结果在屏幕上输出。不得修改程序的其他部分。
注意:程序最后已将结果输出到文件out.dat中。输出函数writeToFile已经给出并且调用。
// proj3.cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class intArray
{
private:
int * array; //整数序列首地址
int length; //序列中的整数个数
public:
//构造函数,从文件中读取数据用于初始化新对象。参数是文件名
intArray(char * filename);
void sort(); //对整数序列按非递减排序
~intArray();
void writeToFile (char * filename);
};
intArray:: intArray (char * filename)
{
ifstream myFile(filename);
int len=300;
array = new int[len];
length = 0;
while(myFile >>array[length++]);
length--;
myFile.close();
}
void intArray::sort(){
// **********333**********
// **********666**********
}
intArray:: ~intArray()
{
delete [] array;
}
void intArray:: writeToFile
(char * filename)
{
int step=0;
ofstream outFile(filename);
for(int i=0; i<length; i=i+step)
{
outFile << array [i] << endl;
step ++;
}
outFile.close();
}
void main()
{
intArray myArray ('in.dat');
myArray.sort();
myArray, writeToFile ('out.dat');
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数sort的作用是将集合中的整数按升序进行排序。请编写这个sort函数。此程序的正确输出结果应为:
5 28 2 4 5 3 2 75 27 66 31
5 28 2 4 5 3 2 75 27 66 31 6
5 28 2 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19 4
2 3 4 4 5 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“// *******333*******”与“// *******666*******”之间。不得修改程序的其他部分。
注意:相关文件包括:main.cpp、Integers.h。
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。
//Integers.h
#ifndef INTEGERS
#define INTEGERS
#include <iostream>
using namespace std;
const int MAXELEMENTS = 100;
//集合最多可拥有的元素个数
class Integers{
int elem[ MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
Integers(): counter(0) {}
//创建一个空集合
Integers (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 sort();
//将集合中的整数按由小到大的次序进行排序
void show() const;
//显示集合中的全部元素
};
void writeToFile(const char * path);
#endif
//main.cpp
#include 'Integers.h'
#include <iomanip>
Integers::Integers (int data[], int size): counter(0) {
for (int i=0; i<size; i++)
add(data[i]);
}
void Integers::add (int element) {
if(counter < MAXELEMENTS)
elem [counter + +] = element;
}
void Integers::remove(int element) {
int j;
for(j = counter-1; j >=0; j--)
if(elem[j] == element)
break;
for(int i=j; i<counter-1; i++)
elem[i] = elem[i+1];
counter--;
}
void Integers::sort() {
// ********333********
// *******666*******
}
void Integers::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};
Integers 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();
s.sort(); s.show();
writeToFile(' ');
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程prog3,其中声明了ValArray类,该类在内部维护一个动态分配的整型数组v。ValArray类的成员函数equals用于判断两个对象是否相等。两个ValArray对象相等,当且仅当两者的元素个数size相等,并且整型数组v的对应元素分别相等。如果两个对象相等,则equals返回true,否则返回false。请编写成员函数equals。在main函数中给出了一组测试数据,此种情况下程序的输出结果应为:
v1={1,2,3,4,5}
v2={1,2,3,4}
v3={1,2,3,4,6}
v4={1,2,3,4,5}
v1!=v2
v1!=v3
v1==v4
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//VatArray.h
#include <iostream>
using namespace std;
class ValArray {
int* v;
int size;
public:
ValArray const int* p, int n): size(n)
{
v = new int[size];
for (int i=0; i<size; i++)
v[i]=p[i];
}
~ValArray() { delete [] v; }
bool equals(const ValArray other);
void print(ostream out) const
{
out <<'{';
for (int i=0; i<size-1; i++)
out<<v[i]<<', ';
out<<v[size-1]<<'}';
}
};
void writeToFile(const char * );
//main.cpp
#include 'ValArray.h'
bool ValArray::equals (const ValArray other)
{
//******** 333********
//******** 666********
}
int main()
{
const int a[]={1, 2, 3, 4, 5};
const int b[]={1, 2, 3, 4};
const int c[]={1, 2, 3, 4, 6};
const int d[]={1, 2, 3, 4, 5};
ValArray vl (a, 5);
ValArray v2(b, 4);
ValArray v3(c, 5);
ValArray v4 (d, 5);
cout<<'v1=';
v1.print(cout);
cout<<endl;
cout<<'v2 =';
v2.print(cout);
cout<<endl;
cout<<'v3=';
v3.print(cout);
cout<<endl;
cout<<'v4=';
v4.print(cout);
cout<<endl;
cout<<'v1'<<(v1.equals (v2)?'==': '!=') <<'v2' <<endl;
cout<<'vl'<<(v1.equals (v3)?'==': '!=') <<'v3' <<endl;
cout<<'v1'<<(v1.equals (v4)?'==':'!=')<<'v4'<<endl;
writeToFile('');
return 0;
}
应用题请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵则调用max_value函数,返回值为3。请编写成员函数max_value。要求:补充编制的内容写在“//*******333*******”与“//*******666*******”之间,不得修改程序的其他部分。注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。//Matrix.h#include<iostream>#include<iomanip>usingnamespacestd;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;}intmax_value()const;voidshow(constchar*s)const{cout<<endl<<s;for(inti=0;i<M;i++){cout<<endl;for(intj=0;j<N;j++)cout<<setw(4)<<array[i][j];}}};voidreadFromFile(constchar*,Matrix);voidwriteToFile(char*,constMatrix);//main.cpp#include'Matrix.h'#include<fstream>voidreadFromFile(constchar*f,Matrixm){ifstreaminfile(f);if(infile.fail()){cerr<<'打开输入文件失败!';return;}intk;for(inti=0;i<M;i++)for(intj=0;j<N;j++){infile>>k;m.setElement(i,j,k);}}intMatrix::max_value()const{//********333********//********666********}intmain(){Matrixm;readFromFile('',m);m.show('Matrix:');cout<<endl<<'最大元素:'<<m.max_value()<<endl;writeToFile('',m);return0;}
