应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,其中定义了Employee类和Manager类。Employee用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager是Employee的公有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Manager的属性还包括级别(level)。Employee类的成员函数print用于输出雇员的信息;Manager类的成员函数print负责输出经理的信息。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
Name:Sally Smith
Dept:Sales
Level:2
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee (string name, string dept):
// **********found**********
______
{}
virtual void print() const;
string dept() const //返回部门名称
{
// **********found**********
______
}
virtual ~Employee() {}
private:
string name_;
string dept_;
};
class Manager: public Employee {
public:
Manager (string name, string dept, int level):
// **********found**********
______
{}
virtual void print() const;
private:
int level;
};
void Employee::print() const
{
cout << 'Name:' << name_ << endl;
cout << 'Dept:' << dept_ << endl;
}
void Manager::print() const
{
// **********found**********
cout << 'Level:' << level_ << endl;
}
int main()
{
Employee * emp = new Manager('Sally Smith', 'Sales', 2);
emp -> print();
delete emp;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。其中定义的类并不完整,按要求完成列操作,将类的定义补充完整,实现以下功能:
(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_pBook =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');
books1.AddBookMenber (3,'Lee');
books1.AddBookMenber (4,'Lily');
books1.AddBookMenber (5,'Harry');
cout<<books1.GetBookAuthor (1)<<endl;
cout<<books1.GetBookAuthor (5)<<endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数IsPalindromes (cha* string)实现的功能是判定给定的字符串是否构成回文字符串,如果是则返回1,否则返回0。
如:1234554321或者1234321都认为是回文字符串。
如果串为空或一个字母时,均认为是回文字符串。
注意:不能修改程序的其他部分,只能补充IsPalindromes()函数。
#include <iostream.h>
#define MAXLEN 1024
bool IsPalindromes(char*string)
{
}
void main()
{
char str[MAXLEN];
cout<<'请输入一行文字'<<endl;
cin.getline(str,MAXLEN);
cout<<IsPalindromes(str)<<endl;
return;
}
应用题 使用VC++6.0打开下的源程序文件2.cpp。完成fun()函数,其功能是:求出M行N列二维数组每行元素中的最小值,并计算它们的和。和通过形参传回主函数并输出。
注意:不能修改程序的其他部分,只能修改fun()函数。
试题程序:
#include <iostream.h>
#define M 2
#define N 4
void fun(int a[M][N],int *sum)
{
}
void main()
{
int x[M][N]={7,6,5,2,4,2,8,3};
int s;
fun(x,s);
cout<<s<<endl;
return;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char * s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符c是否在一个集合中,若在,则返回true;否则返回false。
构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“// TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。
提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
const int MAXNUM = 100;
class Set {
private:
int num; //元素个数
char setdata[MAXNUM]; //字符数组,用于存储集合元素
public:
Set(char *s); //构造函数,用字符串s构造一个集合对象
bool InSet(char c);
//判断一个字符c是否在集合中,若在,返回true,否则返回false
void Print() const; //输出集合中所有元素
};
Set::Set(char * s)
{
num = 0;
while (* s) {
// **********found**********
if (______) //TODO:添加代码,测试元素在集合中不存在
// **********found**********
______; //TODO:添加一条语句,加入元素至集合中
s++;
}
}
bool Set::InSet (char c)
{
for (int i = 0; i < num; i++)
// **********found**********
if (______) //TODO:添加代码,测试元素c是否与集合中某元素相同
// **********found**********
______; //TODO:添加一条语句,进行相应处理
return false;
}
void Set::Print () const
{
cout << 'Set elements:' << endl;
for(int i = 0; i < num; i++)
cout << setdata[i] << '';
cout << endl;
}
int main()
{
char s[MAXNUM];
cin.getline (s, MAXNUM - 1); //从标准输入中读入一行
Set setobj(s); //构造对象setobj
setobj.Print(); //显示对象setobj中内容
return 0;
}
应用题
请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(int maxspeed,int weight):______
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){return Weight;}
};
//**********found**********
class bicycle:______ public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){return Height;};
};
//**********found**********
class motorcar:______public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed,int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
//**********found**********
class motorcycle:______
{
public:
motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,height),motorcar(maxspeed,weight,1){}
};
void main()
{
motorcycle a(80,150,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
}
应用题 请使用【答题】菜单命令或直接用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;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。函数convert (cha* des,char* str)实现的功能是:
(1)如果字符串最后面有空格,则全部删除;
(2)转换后的数据放到des,并且指针作为返回值返回。
注意:不能修改程序的其他部分,只能补充convert()函数。
#include <iostream.h>
#include <ctype.h>
#define MAXLEN 1024
char* convert(char* des,char*str)
{
}
void main()
{
char dest[MAXLEN];
char *string=' abc def ';
cout << string <<'<--' << endl;
cout<<convert(dest,string) <<'<--'<<endl;
return;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码。函数sort(int m,int n,int l)实现将三个整数m、n、l由大到小输出。m最大,l最小。
程序分析:程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果m<n则将m与n的值进行交换,然后再用m与l进行比较,如果m<l则将m与l的值进行交换,这样能使m最大。然后再将n与l进行比较,若n<l则将n与l的值互换,互换后则l最小。
将函数sort(int m,int n,int l)补充完整,实现三个数的排序。
注意:请勿改动主函数。
#include <iostream.h>
void sort(int m, int n, int l)
{
}
int main()
{
int x=9;
int y=13;
int z=-3;
sort(x,y,z);
cout<<x<<','<<y<<','<<z<<endl;
return 0;
}
应用题 请使用【答题】菜单命令或直接用VC6打开考生文件夹下的工程proj3,其中声明的是一个人员信息类,补充编制程序,使其功能完整。在main函数中给出了一组测试数据,此种情况下程序的输出应该是:Zhang 20 Tsinghua。
注意:只能在函数address_change的“// ********333********”和“// ********666********”之间填入若干语句,不要改动程序中的其他内容。
程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <string>
using namespace std;
class Person{
char name[20];
int age;
char * address;
public:
Person(){ age = 0;address = 0;}
void name_change (char *_name);
//名字修改函数
void age_change(int_age);
//年龄修改函数
void address_change (char *_add);
//地址修改函数
void info_display();
//人员信息显示
~Person();
//析构函数
};
void writeToFile(const char * path);
proj3.cpp
#include <iostream>
#include <string>
#include 'proj3.h'
using namespace std;
void Person::name_change(char*_name)
{
strcpy(name, _name);
}
void Person::age_change(int _age)
{
age = _age;
}
void Person::address _change (char * add)
{
if (address!=NULL) delete[]address;
// ********333********
// ********666********
}
void Person::info_display(){
cout << name << '\t'
<< age << '\t';
if(address!=NULL)
cout << address << endl;
cout << endl;
}
Person::~Person(){
if(address!=NULL)
delete[] address;
}
void main()
{
Person p1;
p1.name_change('Zhang');
p1.age_change(20);
p1.address_change('Tsinghua University');
p1.address_change('Tsinghua');
p1.info_display();
writeToFile(' ');
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码。函数sort(int m,int n,int l)实现将三个整数m、n、l由大到小输出。m最大,l最小。
程序分析:程序实现时,可以把最大的数放到m上,先将m与n进行比较,如果m<n则将m与n的值进行交换,然后再用m与l进行比较,如果m<l则将m与l的值进行交换,这样能使m最大。然后再将n与l进行比较,若n<l则将n与l的值互换,互换后则l最小。
将函数sort(int m,int n,int l)补充完整,实现三个数的排序。
注意:请勿改动主函数。
#include <iostream.h>
void sort(int m, int n, int l)
{
}
int main()
{
int x=9;
int y=13;
int z=-3;
sort(x,y,z);
cout<<x<<','<<y<<','<<z<<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2。其中有类Point(“点”)、Rectangle(“矩形”)和Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x轴的正方向是水平向右的,y轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是:
--圆形----------
圆心=(3,2)
半径=1
面积=3.14159
--外切矩形----------
左上角=(2,1)
右下角=(4,3)
面积=4
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <cmath>
using namespace std;
//平面坐标中的点
//本题坐标系统中,x轴的正方向水平向右,y轴的正方向竖直向下。
class Point {
public:
Point(double x=0.0, double y=0.0): x_(x), y_(y) {}
double getX() const {return x_;}
double getY() const {return y_;}
void setX(double x) {x_=x;}
void setY(double y) {y_=y;}
private:
double x_; //x坐标
double y_; //y坐标
};
//矩形
class Rectangle {
public:
Rectangle (Point p, int w, int h)
: point (p), width (w), height (h) {}
double area() const //矩形面积
{
return width * height;
}
Point topLeft() const //左上角顶点
{
return point;
}
Point bottomRight() const
//右下角顶点(注:y轴正方向竖直向下)
{
// **********found**********
return Point(______);
}
private:
Point point; //左上角顶点
double width; //水平边长度
double height; //垂直边长度
};
//圆形
class Circle {
public:
Circle (Point p, double r):center(p), radius(r) {}
Rectangle boundingBox() const; //外切矩形
double area() const //圆形面积
{
// **********found**********
return PI *______;}
public:
static const double PI; //圆周率
private:
Point center; //圆心
double radius; //半径
};
const double Circle::PI = 3.14159;
Rectangle Circle::boundingBox() const
{
// **********found**********
Point pt(______);
int w, h;
// **********found**********
w = h =______;
return Rectangle(pt, w, h);
}
int main ()
{
Point p(3, 2);
Circle c(p, 1);
cout << --圆形---------- \n';
cout << '圆心 = (' << p.getX() << ',' << p.getY() << ')\n';
cout << '半径 =' << 1 << endl;
cout << '面积 =' << c.area() << endl << endl;
Rectangle bb = c.boundingBox();
Point t1 = bb.topLeft();
Point br = bb.bottomRight();
cout << '--外切矩形-----------\n';
cout << '左上角 = (' << t1.getX() << ',' << t1.getY() << ')\n';
cout << '右下角 = (' << br.getX() << ',' << br.getY() << ')\n';
cout << '面积 =' << bb.area() << endl;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类Rectangle和Circle的声明,二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPerim()。程序中位于每个“//****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
The area of the Circle is 78.5
The perimeter of the Circle is 31.4
The area of the Rectangle is 24
The perimeter of the Rectangle is 20
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Shape
{
public:
Shape() {}
~Shape() {}
//********** found**********
______float GetArea () =0;
//********** found**********
______float GetPerim () =0;
};
class Circle : public Shape
{
public:
Circle (float radius): itsRadius (radius) {}
~Circle() {}
float GetArea () { return 3.14 * itsRadius * itsRadius; }
float GetPerim ()
{return 6.28 * itsRadius;}
private:
float itsRadius;
};
class Rectangle : public Shape
{
public:
//********** found**********
Rectangle(float len, float width):
______{};
~Rectangle(){};
virtual float GetArea()
{ return itsLength * itsWidth; }
float GetPerim ()
{ return 2 * itsLength + 2 * itsWidth;}
virtual float GetLength() { return itsLength; }
virtual float GetWidth () { return itsWidth; }
private:
float itsWidth;
float itsLength;
};
int main()
{
//********** found**********
______
sp = new Circle(5);
cout << 'The area of the Circle is' << sp->GetArea () << endl;
cout << 'The perimeter of the Circle is' << sp->GetPerim () << endl;
delete sp;
sp = new Rectangle(4, 6);
cout << 'The area of the Rectangleis' << sp->GetArea() << endl;
cout << 'The perimeter of the Rectangle is' << sp -> GetPerim () <<endl;
delete sp;
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或使用【答题】菜单打开proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为:
20
10
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class CPolygon {
public:
// **********found**********
______//纯虚函数area声明
void printarea (void)
// **********found**********
{cout <<______<< endl;}
};
class CRectangle: public CPolygon {
int width; //长方形宽
int height; //长方形高
public:
CRectangle (int w, int h): width (w), height (h) {}
int area (void) {return (width * height);}
};
class CTriangle : public CPolygon {
int length; //三角形一边长
int height; //该边上的高
public:
CTriangle (int 1, int h): length (1), height (h) {}
// **********found**********
int area (void) {return (______)/2;}
};
int main() {
CRectangle rect (4,5);
CTriangle trgl (4,5);
// **********found**********
______*ppoly1, *ppoly2;
ppoly1 = ▭
ppoly2 = trgl;
ppoly1 -> printarea();
ppoly2 -> printarea();
return 0;
}
应用题
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.cpp,补充编制C++程序proj3.cpp,其功能是读取文本文件in.dat中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符序列反转,并输出到文件out.dat中。文件in.dat的长度不大于1000字节。
要求:
补充编制的内容写在“//**********333**********”与“//**********66666**********”两行之间。实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。不得修改程序的其他部分。
注意:程序最后已将结果输出到文件out.dat中,输出函数writeToFile已经给出并且调用。
//proj3.cpp
#include<iostream>
#include<fstream>
#include<estring>
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 myDoc('in.dat');
myDoc.reverse();
myDoc.writeToFile('out.dat');
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义的IntArray是一个用于表示整型一维数组的类。成员函数swap可以将数组中的两个指定元素交换位置;成员函数sort的功能是将数组元素按照升序排序。请编写成员函数sort。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件…
---排序前---
a1=3 1 2
a2=5 2 7 4 1 6 3
---排序后---
a1=1 2 3
a2=1 2 3 4 5 6 7
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//IntArray.h
#include <iostream>
#include <string.h>
using namespace std;
class IntArray {
public:
IntArray(unsigned int n)
{
size = n;
data = new int[size];
}
~IntArray() { delete [] data; }
int getSize() const { return size; }
int operator [] (unsigned int i) const { return data[i]; }
void swap(int i, int j)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
void sort();
friend ostream operator << (ostream os, const IntArray array)
{
for (int i=0; i<array.getSize (); i++)
os << array[i] << '';
return os;
}
private:
int * data;
unsigned int size;
};
void readFromFile ( const char *, IntArray);
void writeToFile (char*, const IntArray );
//main.h
#include <fstream>
#include 'IntArray. h'
void IntArray::sort ()
{
//******** 333********
//******** 666********
}
void readFromFile (const char * f, IntArray m)
{
ifstream infile (f);
if (infile.fail ()) {
cerr << '打开输入文件失败!';
return;
}
int i=0;
while (!infile.eof()) {
infile >> m[i++];
}
}
int main ()
{
IntArray a1(3), a2(7), a3(1000);
a1[0]=3, a1[1]=1, a1[2]=2;
a2[0]=5, a2[1]=2, a2[2]=7, a2[3]=4, a2[4]=1, a2[5]=6, a2[6]=3;
readFromFile ('in.dat', a3);
cout << '---排序前---\n';
cout <<'a1=' << a1 <<endl;
cout <<'a2=' << a2 <<endl<<endl;
a1.sort ();
a2.sort();
a3.sort();
cout <<'---排序后--- \n';
cout <<'a1=' <<a1<<endl;
cout <<'a2=' <<a2<<endl<<endl;
writeToFile('', a3);
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp。其中定义了类Set和用于测试该类的主函数main。类Set是一个用于描述字符集合的类,在该字符集合中,元素不能重复(将“a”和“A”视为不同元素),元素最大个数为100。为该类实现一个构造函数Set(char * s),它用一个字符串来构造一个集合对象,当字符串中出现重复字符时,只放入一个字符。此外,还要为该类实现另一个成员函数InSet(char c),用于测试一个字符c是否在一个集合中,若在,则返回true;否则返回false。
构造函数Set和成员函数InSet的部分实现代码已在文件proj2.cpp中给出,请在标有注释“// TODO:”的行中添加适当的代码,将这两个函数补充完整,以实现其功能。
提示:在实现构造函数时,可以调用InSet函数来判断一个字符是否已经在集合中。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//proj2.cpp
#include <iostream>
using namespace std;
const int MAXNUM = 100;
class Set {
private:
int num; //元素个数
char setdata[MAXNUM]; //字符数组,用于存储集合元素
public:
Set(char *s); //构造函数,用字符串s构造一个集合对象
bool InSet(char c);
//判断一个字符c是否在集合中,若在,返回true,否则返回false
void Print() const; //输出集合中所有元素
};
Set::Set(char * s)
{
num = 0;
while (* s) {
// **********found**********
if (______) //TODO:添加代码,测试元素在集合中不存在
// **********found**********
______; //TODO:添加一条语句,加入元素至集合中
s++;
}
}
bool Set::InSet (char c)
{
for (int i = 0; i < num; i++)
// **********found**********
if (______) //TODO:添加代码,测试元素c是否与集合中某元素相同
// **********found**********
______; //TODO:添加一条语句,进行相应处理
return false;
}
void Set::Print () const
{
cout << 'Set elements:' << endl;
for(int i = 0; i < num; i++)
cout << setdata[i] << '';
cout << endl;
}
int main()
{
char s[MAXNUM];
cin.getline (s, MAXNUM - 1); //从标准输入中读入一行
Set setobj(s); //构造对象setobj
setobj.Print(); //显示对象setobj中内容
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,此工程中包含一个头文件shape.h,其中包含了类Shape、Point和Triangle的声明;包含程序文件shape.cpp,其中包含了类Triangle的成员函数和其他函数的定义;还包含程序文件proj2.cpp,其中包含测试类Shape、Point和Triangle的程序语句。请在程序中的横线处填写适当的代码并删除横线,以实现上述功能。此程序的正确输出结果应为:
此图形是一个抽象图形,周长=0,面积=0
此图形是一个三角形,周长=6.82843,面积=2
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
//shape.h
class Shape {
public:
virtual double perimeter() const {return 0;} //返回形状的周长
virtual double area() const {return 0;} //返回形状的面积
virtual const char * name() const {return '抽象图形';} //返回形状的名称
};
class Point{ //表示平面坐标系中点的类
double x;
double y;
public:
// **********found**********
Point (double x0, double y0):
______{} //用x0、y0初始化数据成员x、y
double getX() const {return x;}
double getY() const {return y;}
};
class Triangle: public Shape{
// **********found**********
______;
//定义3个私有数据成员
public:
Triangle (Point p1, Point p2, Point p3): point1 (p1), point2 (p2), point3 (p3) {}
double perimeter () const;
double area() const;
const char * name() const {return '三角形';}
};
//shape.cpp
#include 'shape.h'
#include <cmath>
double length (Point p1, Point p2)
{
return sqrt ((p1.getX() -p2.getX()) * (p1.getX() -p2.getX()) + (p1.getY() -p2.getY()) * (p1.getY() -p2.getY()));
}
double Triangle:: perimeter () const
{//一个return语句,它利用length函数计算并返回三角形的周长
// **********found**********
______;
}
double Triangle::area() const
{
double s = perimeter()/2.0;
return sqrt (s * (s - length (point1, point2)) * (s - length (point2, point3)) * (s - length (point3, point1)));
}
//proj2.cpp
#include 'shape.h'
#include <iostream>
using namespace std;
// **********found**********
______ //show函数的函数头(函数体以前的部分)
{
cout << '此图形是一个' << shape.name() << ',周长=' << shape.perimeter() << ',面积=' << shape.area() << endl;
}
int main()
{
Shape s;
Triangle tri(Point(0,2), Point(2,0), Point(0,0));
show(s);
show(tri);
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motocycle类。要求将vehicle作为虚基类,避免二义性问题。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
A vehicle is running!
A vehicle has stopped!
A bicycle is running!
A bicycle has stopped!
A motorcar is running!
A motocycle is running!
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
vehicle(): MaxSpeed(0), Weight(0){}
vehicle(int max_speed, int weight): MaxSpeed (max_speed ), Weight
(weight){}
//********** found**********
______Run()
{
cout << 'Avehicle is running!' << endl;
}
//********** found**********
______Stop()
{
cout << 'A vehicle has stopped!' << endl;
}
};
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(): Height(0){}
bicycle(int max_speed, int weight,int height)
:vehicle (max_speed, weight), Height(height){};
void Run () {cout << 'A bicycle is running!' << endl; }
void Stop() {cout << 'A bicycle has stopped!' << endl; }
};
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(): SeatNum(0) {}
motorcar (int max_speed, intweight, int seat_num)
//********** found**********
:______{}
void Run() {cout << 'A motorcar is running!' << endl; }
void Stop () {cout << 'A motorcar has stopped!' << endl; }
};
//********** found**********
class motorcycle: ______
{
public:
motorcycle(){}
motorcycle (int max_speed, int weight, int height, int seet_num): bicycle(max_speed, weight, height), motorcar (max_speed, weight, seet_num){};
~motorcycle () {};
void Run () {cout << 'A motorcycle is running!' << endl; }
void Stop() {cout << 'A motorcycle has stopped!' << endl; }
};
int main()
{
vehicle * ptr;
vehicle a;
bicycle b;
motorcar c;
motorcycle d;
a.Run(); a. Stop();
b.Run(); b. Stop();
ptr = c; ptr->Run();
ptr = d; ptr->Run();
return 0;
}
