应用题
请打开考生文件夹下的解决方案文件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;
}
应用题 使用VC6打开考生文件夹下的工程test34_3。此工程包含一个test34_3.cpp,其中定义了表示栈的类stack。源程序中stack类的定义并不完整,请按要求完成下列操作,将程序补充完整。
(1)定义类stack的私有数据成员sp和size,它们分别为整型的指针和变量,其中sP指向存放栈的数据元素的数组,size为栈中存放最后一个元素的下标值。请在注释“//**1**”之后添加适当的语句。
(2)完成类stack的构造函数,该函数首先从动态存储空间分配含有100个元素的int型数组,并把该数组的首元素地址赋给指针sp,然后将该数组的所有元素赋值为0,并将size赋值为-1(size等于-1表示栈为空)。请在注释“//**2**”之后添加适当的语句。
(3)完成类stack的成员函数push的定义。该函数将传入的整型参数x压入栈中,即在size小于数组的最大下标情况下, size自加1,再给x赋值。请在注释“//**3**”之后添加适当的语句。
(4)完成类stack的成员函数pop的定义,该函数返回栈顶元素的值,即在size不等于-1的情况下,返回数组中下标为size的元素的值,并将size减1。请在注释“//**4**”之后添加适当的语句。
程序输出结果如下:
the top elem:1
the pop elem:1
the stack is empty
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件test34_3.cpp清单如下:
#include<iostream.h>
class stack
{
//** 1 **
public:
stack ( );
bool empty(){return size==-1;}
bool full() {return size==99;}
void push(int x);
void pop();
void top();
};
stack::stack()
{
//** 2 **
for(int i=0; i<100; i++)
*(sp+i)=0;
size=-1;
}
void stack::push(int x)
{
//** 3 **
cout<<'the stack is full'<<end1;
else
{
size++;
*(sp+size) = x;
}
}
void stack::pop()
{
//** 4 **
cout<<'the stack is empty'<<end1;
else
{
cout<<'the pop elem:'<<*(sp+size)<<end1;
size--;
}
}
void stack::top()
{
if iempty() )
cout<<'the stack is empty'<<end1;
else
{
cout<<'the top elem:'<<*(sp+size)<<end1;
}
}
void main ( )
{
stack s;
s.push(1);
s.top();
s.pop();
s.top();
}
应用题
请打开考生文件夹下的解决方案文件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或使用【答题】菜单打开proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为:
排序前:7,1,3,11,6,9,12,10,8,4,5,2
排序后:1,2,3,4,5,6,7,8,9,10,11,12
要求:
补充编制的内容写在“// ********333********”与“// ********666********”两行之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序调用。
//DataList.h
#include <iostream>
using namespace std;
class DataList{ //数据表类
int len;
double * d;
public:
DataList(int len, double data[] = NULL);
~DataList() {delete[]d;}
int length() const {return len;} //数据表长度(即数据元素的个数)
double getElement (int i) const {return d[i];}
void sort(); //数据表排序
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]);
}
void DataList::sort() { //数据表排序
// ********333********
// ********666********
}
void DataList::show() const { //显示数据表
for (int i = 0; i < len - 1; i++) cout << d[i] << ',';
cout << d[len - 1] << endl;
}
int main() {
double s[] = {7,1,3,11,6,9,12,10,8,4,5,2};
DataList list(12,s);
cout << '排序前:';
list.show();
list.sort();
cout << endl << '排序后:';
list.show();
writeToFile(' ', list);
return 0;
}
应用题 使用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<<endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
车牌号:冀ABC1234 品牌:ForLand 类别:卡车当前档位:0 最大载重量:12
车牌号:冀ABC1234 品牌:ForLand 类别:卡车当前档位:2 最大载重量:12
车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5
车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:-1 座位数:5
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
#include <iomanip
#include <cmath>
using namespace std;
class AutoMobile{ //“汽车”类
char * brand; //汽车品牌
char * number; //车牌号
int speed; //档位1、2、3、4、5,空档:0,倒挡:-1
public:
AutoMobile(const char * the_brand, const char * the_number): speed(0) {
brand = new char[strlen (the_brand) +1];
// **********found**********
______;
// **********found**********
______;
strcpy (number, the number);
}
~AutoMobile {delete[] brand; delete[] number;}
const char * theBrand const {return brand;} //返回品牌名称
const char * theNumber const {return number;} //返回车牌号
int currentSpeed const {return speed;} //返回当前档位
void changeGearTo (int the_speed) { //换到指定档位
if (speed >= -1 speed <= 5)
speed=the_speed;
}
virtual const char * category const =0; //类别:卡车、小轿车等
virtual void show const {
cout << '车牌号:' << theNumber
// **********found**********
<< '品牌:' <<
<< '类别:' << category
<< '当前档位:' << currentSpeed ;
}
};
class Car: public AutoMobile{
int seats; //座位数
public:
Car (const char * the_brand, const char * the_number, int the_seats): AutoMobile (the_brand, the_number), seats (the_seats) {}
int numberOfSeat const {return seats;}
//返回座位数
const char * category const {return '小轿车';} //返回汽车类别
void show const {
AutoMobile::show ;
cout << '座位数:' << numberOf Seat << endl;
}
};
class Truck: public AutoMobile{ //“卡车”类
int max_load; //最大载重量
public:
Truck (const char * the_brand, const char * the_number, int the_max_load): AutoMobile (the_brand, the_number), max_load(the_max_load) {}
int maxLoad const {return max_load;} //返回最大载重量
// **********found**********
const char * category ______ //返回汽车类别
void show const {
AutoMobile::show ;
cout << '最大载重量:' << maxLoad << endl;
}
};
int main {
Truck truck ('ForLand','冀ABC1234', 12);
truck.show ;
truck.changeGearTo(2);
truck.show ;
Car car ('QQ', '沪XYZ5678',5);
car.show ;
car.changeGearTo(-1);
car.show ;
cout << endl;
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,函数void Insert(node*q)使程序能够完成如下功能:从键盘输入一行字符,调用该函数建立反序的无头结点的单链表,然后输出整个链表。
注意:请勿修改主函数main和其他函数中的任何内容,只需在画线处编写适当代码,也不能删除或移动//************found************。
//源程序proj2.cpp
#include<iostream>
using namespace std;
struct node{
char data;
node*link:
}*head; //链表首指针
void Insert(node*q){ //将节点插入链表首部
//************found************
______;
head=q;
}
int main(){
char ch;
node *p;
head=NULL:
cout<<'Please input the string'<<endl;
while((ch=cin.get())!='\n'){
//************found************
______;//用new为节点p动态分配存储空间
p->data=ch;
//************found************
______; //在链表首部插入该节点
}
p=head;
while(p!=NULL){
cout<<p->data;
p=p->link;
}
cout<<endl;
return 0:
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,其中定义了Component类、Composite类和Leaf类。Component是抽象基类,Composite和Leaf是Component的公有派生类。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
Leaf Node
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class Component {
public:
//声明纯虚函数print()
// **********found**********
______
};
class Composite:public Component {
public:
// **********found**********
void setChild(______)
{
m_child = child;
}
virtual void print() const
{
m_child -> print();
}
private:
Component * m_child;
};
class Leaf:public Component {
public:
virtual void print() const
{
// **********found**********
______
}
};
int main()
{
Leaf node;
Composite comp;
comp.setChild(node);
Component * p = ∁
p -> print();
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为:
排序前:7,1,3,11,6,9,12,10,8,4,5,2
排序后:1,2,3,4,5,6,7,8,9,10,11,12
要求:
补充编制的内容写在“// ********333********”与“// ********666********”两行之间。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序调用。
//DataList.h
#include <iostream>
using namespace std;
class DataList{ //数据表类
int len;
double * d;
public:
DataList(int len, double data[] = NULL);
~DataList() {delete[]d;}
int length() const {return len;} //数据表长度(即数据元素的个数)
double getElement (int i) const {return d[i];}
void sort(); //数据表排序
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]);
}
void DataList::sort() { //数据表排序
// ********333********
// ********666********
}
void DataList::show() const { //显示数据表
for (int i = 0; i < len - 1; i++) cout << d[i] << ',';
cout << d[len - 1] << endl;
}
int main() {
double s[] = {7,1,3,11,6,9,12,10,8,4,5,2};
DataList list(12,s);
cout << '排序前:';
list.show();
list.sort();
cout << endl << '排序后:';
list.show();
writeToFile(' ', list);
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3,此工程包含一个源程序文件proj3.cpp,其中定义了用于表示二维向量的类MyVector;程序应当显示(6,8)。但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“// **1** ****found****”的下方是构造函数的定义,它用参数提供的坐标对x和y进行初始化。
(2)在“// **2** ****found****”的下方是减法运算符函数定义中的一条语句。两个二维向量相减生成另一个二维向量:其X坐标等于两向量X坐标之差,其Y坐标等于两向量Y坐标之差。
(3)在“// **3** ****found*****”的下方,语句的功能是使变量v3获得新值,它等于向量v1与向量v2之和。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。
// proj3.cpp
#include <iostream >
using std::ostream;
using std::cout;
using std::endl;
class MyVector { //表示二维向量的类
double x; //X坐标值
double y; //Y坐标值
public:
MyVector (double i = 0.0, double j = 0.0); //构造函数
MyVector operator + (MyVector j); //重载运算符+
friend MyVector operator - (MyVector i, MyVector j); //重载运算符-
friend ostream operator << (ostream os, MyVector v); //重载运算符<<
};
// **1** *******found*******
______ (double i, double j): x(i),y(j) {}
MyVector MyVector::operator + (MyVector j) {
return MyVector (x + j. x, y + j. y);
}
MyVector operator - (MyVector i, MyVector j)
{// **2** *******found*******
return MyVector (______);
}
ostream operator << (ostream os, MyVector v) {
os << '('<< v.x << ',' << v.y << ')'; //输出向量v的坐标
return os;
}
int main()
{
MyVector v1(2,3), v2(4,5), v3;
// **3** *******found*******
v3 =______;
cout << v3 << endl;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln。此程序的功能是将out1.txt文件中的内容输出到屏幕与文件中。输出如下:
李一 1.78m 21
王一 1.65m 23
out2.txt文件的内容如下:
李一 1.78m 21
王一 1.65m 23
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
(1)以输入方式打开文件out1.txt,请在注释//********1********后添加适当的语句。
(2)以输出方式打开文件out2.txt,请在注释//********2********后添加适当的语句。
(3)从文件中获得一个字符,判断是否结束,如果结束则退出输出。请在注释//********3********后添加适当的语句。
(4)把获得的字符输出到文件中,请在注释//********4********后添加适当的语句。
注意:仅在函数指定位置添加语句,请勿改动主函数main与其他函数中的任何内容。
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
char ch;
fstream infile, outfile;
//********1********
infile.open ('out1.txt');
if(!infile)
{
cout << 'out1.txt文件不能打开'<<endl;
abort();
}
//********2********
outfile.open('out2.txt');
if(!outfile)
{
cout <<'out2.txt文件不能打开' <<endl;
abort();
}
//********3********
while()
{
cout<<ch;
//********4********
}
cout <<endl;
infile.close();
outfile.close();
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“// ****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
#include <iostream>
using namespace std;
class Room{ //“房间”类
int room_no; //房间号
double length; //房间长度(m)
double width; //房间宽度(m)
public:
Room(int the_room_no, double the_length, double the_width):room_no (the_room_no), length(the_length), width (the_width) {}
int theRoomNo()const{return room_no;}
//返回房间号
double theLength()const {return length;} //返回房间长度
double theWidth()const {return width;} //返回房间宽度
// **********found**********
double theArea()const{______} //返回房间面积(矩形面积)
};
class Office: public Room{ //“办公室”类
char * depart; //所属部门
public:
Office (int the_room_no, double the_length, double the_width, const char * the_depart)
// **********found**********
:______{
depart = new char[strlen(the_depart) +1];
strcpy(______);
}
~Office() {delete [] depart;}
const char * theDepartment() const{return depart;} //返回所属部门
};
int main(){
// **********found**********
Office ______;
cout << '办公室房间号:' << an_office.theRoomNo() << endl
<< '办公室长度:' << an_office.theLength() << endl
<< '办公室宽度:' << an_office.theWidth() << endl
<< '办公室面积:' << an_office.theArea() << endl
<< '办公室所属部门:' << an_office.theDepartment() << endl;
return 0;
}
应用题 请使用VC6或使用【答题】菜单打开proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了CharShape类、Triangle类和Rectangle类。
CharShape是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用于显示不同字符图形的相同操作接口。Triangle和Rectangle是CharShape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,用于实现各自的显示操作。程序的正确输出结果应为:
*
***
*****
*******
########
########
########
请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。
(1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。
(2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。
(3)为类外函数fun添加合适的形参。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“// ****found****”。
// proj2.cpp
#include <iostream>
using namespace std;
class CharShape {
public:
CharShape(char ch) : _ch(ch) {};
virtual void Show() = 0;
protected:
char _ch; //组成图形的字符
};
class Triangle : public CharShape {
public:
Triangle (char ch, int r) :
CharShape(ch), _rows(r) {}
void Show();
private:
int _rows; //行数
};
class Rectangle: public CharShape {
public:
Rectangle (char ch, int r, int c) : CharShape (ch), _rows(r), _cols(c) {}
void Show ();
private :
int _rows, _cols; //行数和列数
};
void Triangle::Show()
//输出字符组成的三角形
{
for (int i = 1; i < = _rows; i ++) {
// ********found********
for (int j = 1; j < =______; j ++)
cout << _ch;
cout << endl;
}
}
void Rectangle::Show()
//输出字符组成的矩形
{
// ********found********
for (int i = 1; i < =______; i++) {
// ********found********
for (int j = 1; j < =______; j++)
cout << _ch;
cout << endl;
}
}
// ********found******** 为fun函数添加形参
void fun (______) {cs. Show();}
int main()
{
Triangle tri ('*', 4);
Rectangle rect('#', 3, 8);
fun(tri);
fun(rect);
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成proj3.h,重载复合赋值运算符+=。
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include <iostream>
#include <iomanip>
using namespace std;
class MiniString
{public:
friend ostream operator << (ostream output, const MiniString s )
//重载流插入运算符
{output << s.sPtr; return output;
}
friend istream operator >> ( istream input, MiniString s )
//重载流提取运算符
{char temp [100]; //用于输入的临时数组
temp[0] = '\0';
input >> setw (100) >> temp;
int inLen = strlen(temp); //输入字符串长度
if( inLen!= 0)
{
s.length = inLen; //赋长度
if( s.sPtr! = 0) delete []s.sPtr; //避免内存泄漏
s.sPtr = new char [s.length + 1];
strcpy( s.sPtr, temp );
//如果s不是空指针,则复制内容
}
else s.sPtr [0] = '\0';
//如果s是空指针,则为空字符串
return input;
}
MiniString ( const char * s=''): length (( s!= 0 )? strlen(s) : 0 ){ setString(s); }
~MiniString() { delete [] sPtr; }//析构函数
//************* 333***********
//+=运算符重载
//************* 666***********
private:
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString( const char * string2 ) //辅助函数
{
sPtr = new char [length + 1];
//分配内存
if ( string2 ! = 0 )
strcpy( sPtr, string2 );
//如果string2不是空指针,则复制内容
else sPtr [0] = '\0';
//如果string2是空指针,则为空字符串
}
};
//proj3.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include 'proj3.h'
int main ()
{
MiniString str1 ('World'), str2 ('Hello');
void writeToFile (char * );
str2 + = str1; //使用重载的+=运算符
cout << str2 << '\n';
writeToFile ('');
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。阅读下列函数说明和代码,补充空出的代码。sum(int n)计算所有n的因子之和(不包括1和自身)。注意:不能修改程序的其他部分,只能补充sum()函数。
#include <iostream.h>
int sum(int n)
{
}
void main()
{
cout<<sum(10)<<endl;
cout<<sum(200)<<endl;
cout<<sum(400)<<endl;
return;
}