应用题 使用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;
}
应用题 请使用VC6或使用【答题】菜单打开proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h、proj3.cpp、writeToFile.obj。补充完成重载赋值运算符函数,完成深复制功能。
屏幕上输出的正确结果应该是:
Hello!
Happy new year!
要求:
补充编制的内容写在“// ********333********”与“// ********666********”两行之间。不得修改程序的其他部分。
注意:
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件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;
}
void modString(const char * string2 ) //更改字符串内容
{
if (string2 ! = 0) //如果string2不是空指针,则复制内容
{
if (strlen(string2) ! = length)
{
length = strlen (string2);
delete []sPtr;
sPtr = new char [length +1]; //分配内存
}
strcpy(sPtr, string2);
}
else sPtr [0] = '\0'; //如果string2是空指针,则为空字符串
}
MiniString operator = (const MiniString otherString);
MiniString (const char * s = ' '):length((s ! = 0) ? strlen(s):0) //构造函数
{
s Ptr = 0;
if (length! =0)
setString(s);
}
~MiniString() //析构函数
{delete [] sPtr;}
private:
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString(const char * string2) //辅助函数
{
sPtr = new char[strlen(string2) +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'
MiniString MiniString::operator = (const MiniString otherString)
{//重载赋值运算符函数。提示:可以调用辅助函数setString
// ********333********
// ********666********
}
int main()
{
MiniString str1 ('Hello!'), str2;
void writeToFile(const char *);
str2 = str1; //使用重载的赋值运算符
str2.modString ('Happy new year!');
cout << str1 << '\n';
cout << str2 << '\n';
writeToFile (' ');
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开proj2下的工程proj2,该工程中包含一个程序文件main. epp,其中有日期类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;
return 0;
}
应用题 请使用VC6或使用[答题]菜单打开proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase {
int rows, cols;
public:
MatrixBase(int rows, int cols): rows(rows), cols(cols) {}
int getRows()const {return rows;}//矩阵行数
int getCols()const {return cols;}//矩阵列数
virtual double getElement(int r, int c)const=0;//取第i个元素的值
void show()const {//分行显示矩阵中所有元素
for(int i=0; i<rows; i++) {
cout<<endl;
for(int i=0; j<cols; j++)
//**********found**********
cout<<______<<' ';
}
}
};
//矩阵类
class Matrix: public MatrixBase {
double*val;
public:
//**********found**********
Matrix(int rows, int cols, double m[]=NULL):______ {
//**********found**********
val=______;
for(int i=0; i<rows*cols; i++)
val[i]=(m==NULL?0.0: m[i]);
}
~Matrix() {delete[]val;}
double getElement(int r. int c)const {return val[r*getCols()+c];}
};
//单位阵(主对角线元素都是1,其余元素都是0的方阵)类
class UnitMatrix: public MatrixBase {
public:
UnitMatrix(int rows): MatrixBase(rows, rows) {}
//单位阵行数列数相同
double getElement(int r, int c)const {
//**********found**********
if(______)return 1.0;
return 0.0;
}
};
int main() {
MatrixBase*m;
double d[][5]= {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}, {3, 4, 5, 6, 7}};
m=new Matrix(3, 5,(double*)d);
m->show();
delete m;
cout<<endl;
m=new UnitMatrix(6);
m->show();
delete m;
return 0;
}
应用题
使用VC++2010打开考生文件夹下“proj3”文件夹中的工程proj3.sln。请完成函数fun(char*s),该函数完成以下功能:
(1)把s中的大写字母转换成小写字母,把其中的小写字母转换成大写字母。并且在函数中调用写函数WriteFile()将结果输出到modi2.txt文件中。
例如:s='helloTEST',则结果为:s='HELLOtest'
(2)完成函数WriteFile(char*s),把字符串输入文件中。
提示:打开文件使用的第二参数为ios_base::binary|ios_base::app。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
void WriteFile(char*s)
{
}
void fun(char*s)
{
}
void ClearFile()
{
ofstream out1;
out1.open('modi2.txt');
out1.close();
}
int main()
{
ClearFile();
char s[1024];
cout<<'please input a string:'<<endl;
cin.getline(s,1024);
fun(s);
return 0;
}
应用题请使用VC6或使用[答题]菜单打开proj2下的工程proj2,该工程中包含一个程序文件main.cPP,其中有坐标点类point、线段类Line和三角形类Triangle的定义,还有main函数的定义。程序中两点间距离的计算是按公式实现的,三角形面积的计算是按公式实现的,其中。请在程序中的横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:Side1:9.43398Side2:5Side3:8area:20注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****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;//线段的两个端点//**********found**********Line(Pointp1,Pointp2):______{}doublelength()const{returnp1.distanceTo(p2);}//线段的长度};classTriangle{//角形类public:constPointp1,p2,p3;//三角形的三个顶点//**********found**********Triangle(______):p1(p1),p2(p2),p3(p3){}doublelength1()const{//边p1,p2的长度returnLine(p1,p2).length();doublelength2()const{//边p2,p3的长度returnLine(p2,p3).length();}doublelength3()const{//边p3,p1的长度returnLine(p3,p1).length();}doublearea()const{//三角形面积//**********found**********doubles=______;returnsqrt(s*(s-length1())*(s-length2())*(s-length3()));}};intmain(){Triangler(Point(0.0,8.0),Point(5.0,0.0),Point(0.0,0.0));cout<<'Side1:'<<r.length1()<<endl;cout<<'Side2:'<<r.length2()<<endl;cout<<'Side3:'<<r.length3()<<endl;cout<<'area:'<<r.area()<<endl;return0;}
