操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
This object is no.1
注意:只修改注释“// ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass():count(0) {cout <<'This object is';}
// ERROR *******found*******
void Inc() const
{cout <<'no.' << ++count << endl;}
private:
// ERROR *******found*******
int count = 0;
};
int main()
{
MyClass * obj = new MyClass;
// ERROR *******found*******
obj.Inc();
return 0;
}
操作题 使用VC6打开proj1下的工程proj1,其中有“沙发”类Sofa和主函数main的定义。程序中位于每个//ERROR**********found**********下的语句行有错误,请加以更正。更正后程序的输出应该是:
座位数:3
颜色:红色
注意:只能修改每个//ERROR**********found**********下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Sofa{ //“沙发”类
int seats; //座位数
char color[10]; //颜色
public:
//ERROR******found******
Sofa(int s,const char*co)
{
//ERROR******found******
if(co=NULL)
color[0]='\0';
else
strcpy(color,co);
}
//ERROR******found******
const char*getSeats()const{return seats;}
const char*getColor()const{return color;}
};
int main(){
Sofa safa(3);
cout<<'座位数:'<<sara.getSeats()<<endl;
cout<<'颜色:'<<sara.getColor()<<endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Door(“门”)和主函数main的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
打开503号门...门是锁着的,打不开。
打开503号门的锁...锁开了。
打开503号门...门打开了。
打开503号门...门是开着的,无须再开门。
锁上503号门...先关门...门锁上了。
注意:只修改每个“//ERROR ********found********”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Door{
int num; //门号
bool closed; //true表示门关着
bool locked; //true表示门锁着
public:
Door(int num){
// ERROR *******found*******
num = this -> num;
closed = locked = true;
}
bool isClosed() const {return closed;}
//门关着时返回true,否则返回false
bool isOpened() const {return! closed;}
//门开着时返回true,否则返回false
bool isLocked() const {return locked;}
//门锁着时返回true,否则返回false
bool isUnlocked() const {return! locked;}
//门未锁时返回true,否则返回false
void open() { //开门
cout << endl << '打开' << num << '号门...';
// ERROR *****found*****
if (closed)
cout << '门是开着的,无须再开门。';
else if(locked)
cout << '门是锁着的,打不开。';
else{
closed = false;
cout << '门打开了';
}
}
void close() { //关门
cout << endl << '关上' << num << '号门...';
if(closed)
cout << '门是关着的,无须再关门。';
else{
closed = true;
cout << '门关上了。';
}
}
// ERROR *******found*******
void lock() const { //锁门
cout << endl << '锁上' << num << '号门...';
if(locked)
cout << '门是锁着的,无须再锁门。';
else{
if(! closed) {
cout << '先关门...';
closed = true;
}
locked = true;
cout << '门锁上了。';
}
}
void unlock() { //开锁
cout << endl << '开' << num << '号门的锁...';
if(! locked)
cout << '门没有上锁,无须再开锁。';
else{
locked = false;
cout << '锁开了。';
}
}
};
int main() {
Door door(503);
door.open();
door.unlock();
door.open()
door.open();
door.lock();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件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<cstring>
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)j
outFile<<str;
outFile.close();
}
void main()
{
doc myDoc('in.dat¨);
myDoc.reverse();
myDoc.writeToFile('out.dat');
}
操作题
使用VC++2010打开考生文件夹下“proj1”文件夹中的工程proj1.sln,但该程序运行时有错,请改正程序中的错误,使程序输出的结果为:
100
37
32
注意:错误的语句在//********error********的下面,修改该语句即可。
#include<iostream.h>
//******error******
void main (
//******error******
int m = 0142;
//******error******
int n = 0X27;
int q = 32;
cout<<m<<endl;
cout<<n<<endl;
cout<<q<<endl;
return;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“// ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
1 2 3 4
注意:模板参数名用T。只修改注释“// ERROR *******found*******”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
//将数组元素个数Size定义为4
// ERROR *******found*******
const int Size;
template <typename T>
class MyClass
{
public:
MyClass (T * p)
{
for(int i=0;i < Size;i ++)
array[i] = p[i];
}
void Print();
private:
T array[Size];
};
template <typename T>
// ERROR *******found*******
void MyClass::Print()
{
for(int i = 0;i <Size;i ++)
cout << array[i] << '\t';
}
int main()
{
int intArray[Size] = {1,2,3,4};
// ERROR *******found*******
MyClass <double> obj (intArray);
obj.Print();
cout << endl;
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。此工程中包括类Date(“日期”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
2006-1-1
2005-12-31
2005-12-31
2006-1-1
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Date {
public:
Date(int y=2006, int m=1, int d=1)
// ERROR ********* found*********
: year=y, month=m, day=d
{}
/ERROR ********* found*********
Date (const Date d)
{
this->year=d.year;
this->month=d.month;
this->day=d.day;
}
void print () const
{
cout << year << '-' << month << '-'<< day << endl;
}
private:
//ERROR ********* found*********
int year (2006), month(1), day(1);
};
int main ()
{
Date d1, d2 (2005, 12, 31), d3;
d1.print ();
d2.print();
d3=d2;
d2=d1;
d1=d3;
d1.print();
d2.print();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Base:Good Luck!
Derived:Good Luck!
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Base
{
// ERROR ******** found********
private:
char* msg;
public:
Base(char* str)
{
// ERROR ******** found********
msg=new char[strlen(str)];
strcpy (msg, str);
cout << 'Base: ' <<msg << endl;
}
// ERROR ******** found********
~Base() { delete msg; }
};
class Derived:public Base
{
public:
Derived (char* str):Base(str) {}
void Show () { cout <<'Derived:' <<msg << endl; }
};
int main ()
{
Derived obj ('Good Luck! ');
obj.Show();
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1,其中有点类Point和线段类Line和主函数main的定义,程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出应为:
p1=(8,4)p2=(3,5)
注意:只修改两个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Point {
double x, y;
public:
Point (double x = 0.0, double y = 0.0)
// ERROR *******found*******
{x=x; y=y;}
double getX() const {return x;}
double getY() const {return y;}
// ERROR *******found*******
void show() const {cout <<'<<x<<','<<y<<')'}
};
class Line{
Point p1,p2;
public:
Line (Point pt1, Point pt2)
// ERROR *******found*******
{pt1 = p1; pt2 = p2;}
Point getP1() const {return p1;}
Point getP2() const {return p2;}
};
int main() {
Line line (Point(8, 4), Point(3,5));
cout << 'p1 =';
line.getP1(). show();
cout << 'p2 =';
line.getP2(). show();
cout << endl;
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件...
---反转前---
STR1=ABCDEF
STR2=ABCDEFG
---反转后---
STR1=FEDCBA
STR2=GFEDCBA
要求:
补充编制的内容写在“//********333********”与“//*********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mgsering.h
#include<iostream>
#include<cstring>
using namespace std;
class MyString{
public:
MyString(const char*s)
{
str=new char[strlen(s)+1];
strcpy(str, s);
}
~MyString() {delete [] str; }
void reverse();
friend ostreamoperator<<(ostream os, const MyString mystr)
{
os<<mystr.str;
return os;
}
private;
char*str;
};
void writeToFile(char*, const MyString);
//main.cpp
#include'mystring.h'
#include<fstream>
void MyString::reverse()
{
//******333******
//*******666******
}
int main()
{
char inname[128], pathname[80];
strcpy(pathname, '');
sprintf(inname, 'in.dat', pathname);
cout<<'读取输入文件...\n\n';
ifstream infile (inname);
if(infile.fail()){
cerr<<'打开输入文件失败!';
exit(1);
}
char buf[4096];
infile.getline(buf, 4096);
MyString str1('ABCDEF'), str2('ABCDEFG'), str3(buf);
cout<<'---反转前---\n';
cout<<'STR1='<<str1<<endl;
cout<<'STR2='<<str2<<endl<<endl;
str1.reverse();
str2.reverse();
str3.reverse();
cout<<'---反转后---\n';
cout<<'STR1='<<str1<<endl;
cout<<'STR2='<<str2<<endl<<endl;
writeToFile(pathname, str3);
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*, constDataList);
//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或使用【答题】菜单打开proj1下的工程proj1。其中有线段类Line的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
End point 1 = (1,8),End point 2 = (5,2),length = 7.2111。
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Line;
double length(Line);
class Line{ //线段类
double x1,y1; //线段端点1
double x2,y2; //线段端点2
public:
// ERROR *******found*******
Line (double x1, double y1, double x2, double y2) const {
this -> x1 = x1;
this -> y1 = y1;
this -> x2 = x2;
this -> y2 = y2;
}
double getX1() const {return x1;}
double getY1() const {return y1;}
double getX2() const {return x2;}
double getY2() const {return y2;}
void show() const {
cout << 'End point 1 = ('<< x1 << ',' << y1;
cout << '), End point 2 = ('<< x2 << ',' << y2;
// ERROR *******found*******
cout << '), length = ' << length (this)
<< '。' << endl;
}
};
double length (Line i) {
// ERROR *******found*******
return sqrt ((1.x1 - 1.x2) * (1.x1 - 1.x2) + (1.y1 - 1.y2) * (1.y1 - 1.y2));
}
int main() {
Line r1(1.0,8.0,5.0,2.0);
r1.show();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3,其中该工程中包含定义了用于表示姓名的抽象类Name、表示“先名后姓”的姓名类Name1(名、姓之间用空格隔开)和表示“先姓后名”的姓名类Name2(姓、名之间用逗号隔开);程序应当显示:
John Smith
Smith, John
但程序中有缺失部分,请按照以下提示,把缺失部分补充完整:
(1)在“//**1** ****found****”的下方是函数show中的一个语句,它按先名后姓的格式输出姓名。
(2)在“//**2** ****found****”的下方是函数getWord中的一个语句,它把一个字符序列复制到head所指向的字符空间中,复制从start所指向的字符开始,共复制endstart个字符。
(3)在“//**3** ****found****”的下方是函数createName中的语句,它根据指针p的值决定返回何种对象:如果p为空,直接返回一个Name1对象,否则直接返回一个Name2对象。注意:返回的Name1或Name2对象必须是动态对象,返回的实际是指向它的指针。
注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。填写的内容必须在一行中完成,否则评分将产生错误。
//proj3.cpp
#include <iostream>
using namespace std;
class Name{
protected:
char * surname; //姓
char * firstname; //名
public:
~Name(){ delete[] surname; delete[] firstname; }
virtual void show() =0;
};
class Name1:public Name{
public:
Name1(const char * name);
//**1** **********found**********
void show() {______;}
};
class Name2:public Name {
public:
Name2 (const char * name);
void show ()
{ cout << surname <<',' <<firstname; }
};
char * getWord (const char * start, const char * end)
{
char * head = new char [ end - start + 1];
//**2** **********found**********
for (int i = 0; i<end - start; i ++) ______;
head[ end - start] ='\0';
return head;
}
Name1::Name1 (const char * name)
{
char * p=strchr(name,');
firstname = getWord (name, p);
surname =new char[ strlen (p)];
strcpy (surname, p+1);
}
Name2::Name2 (const char * name)
{
char * p =strchr (name,',');
surname =getWord (name, p);
firstname =new char[ strlen (p) ];
strcpy (firstname, p+1 );
}
Name * createName(const char * s)
{
char * p=strchr (s,',');
//**3** ********** found**********
if (p) ______;
}
int main ()
{
Name * n;
n = createName ('John Smith');
n->show(); cout <<endl;
delete n;
n = createName ('Smith, John');
n->show(); cout <<endl;
delete n;
return 0;
}
操作题请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3。此工程中包含一个源程序文件proj3.cpp,其中定义了用于表示平面坐标系中的点的类MyPoint和表示三角形的类MyTriangle;程序应当显示:6.828432但程序中有缺少部分,请按照以下提示,把缺失部分补充完整:(1)在“//**1******found****”的下方是构造函数的定义,它用参数提供的3个顶点对point1、point2和point3进行初始化。(2)在“//**2******found****”的下方是成员函数perimeter的定义,该函数返回三角形的周长。(3)在“//**3******found****”的下方是成员函数area的定义中的一条语句。函数area返回三角形的面积。方法是:若a、b、c为三角形的3个边长,并令s=,则三角形的面积A为。注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“****found****”。//proj3.cpp#include<iostream>#include<cmath>usingnamespacestd;classMyPoint{//表示平面坐标系中的点的类doublex;doubley;public:MyPoint(doublex,doubley){this->x=x;this->y=y;}doublegetX()const{returnx;}doublegetY()const{returny;}voidshow()const{cout<<'('<<x<<','<<y<<')';}};classMyTriangle{//表示三角形的类MyPointpoint1;//三角形的第一个顶点MyPointpoint2;//三角形的第二个顶点MyPointpoint3;//三角形的第三个顶点public:MyTriangle(MyPointp1,MyPointp2,MyPointp3);doubleperimeter()const;//返回三角形的周长doublearea()const;//返回三角形的面积};//**1************found**********MyTriangle::MyTriangle(MyPointp1,MyPointp2,MyPointp3):______{}doubledistance(MyPointp1,MyPointp2)//返回两点之间的距离{returnsqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));}//**2************found**********doubleMyTriangle______{returndistance(point1,point2)+distance(point2,point3)+distance(point3,point1);}doubleMyTriangle::area()const{//**3************found**********doubles=______;//使用perimeter函数returnsqrt(s*(s-distance(point1,point2))*(s-distance(point2,point3))*(s-distance(point3,point1)));}intmain(){MyTriangletri(MyPoint(0,2),MyPoint(2,0),MyPoint(0,0));cout<<tri.perimeter()<<endl<<tri.area()<<endl;return0;}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程包含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
(4,4)
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class Point {
public:
//ERROR ********found********
Point(double x,double y)_x(x),_y(y){}
double GetX() const {return _x;}
double GetY() const {return _y;}
void Move (double xOff, doubleyOff) const
{_x+=xOff;_y+=yOff;}
protected:
double x,y;
};
int main()
{
Point pt(1.5,2.5);
pt.Move(2.5,1.5);
//ERROR ********found******** 以下语句输出pt成员_x和_y的值
cout<<'('<<pt._x<<','<<pt._y<<')'<<endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
Name:Smith Age:21 ID:99999 CourseNum:12 Record:970
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class StudentInfo
{
protected:
// ERROR *******found*******
char Name;
int Age;
int ID;
int CourseNum;
float Record;
public:
StudentInfo(char * name, int Age, int ID, int courseNum, float record);
// ERROR *******found*******
void ~StudentInfo() {}
float AverageRecord() {
return Record/CourseNum;
}
void show() const {
cout << 'Name:' << Name << 'Age:' << Age << 'ID:' << ID << 'CourseNum:' << CourseNum << 'Record:' << Record << endl;
}
};
// ERROR *******found*******
StudentInfo StudentInfo (char * Name, int Age, int ID, int CourseNum, float Record)
{
Name = name;
Age = age;
this -> ID = ID;
CourseNum = courseNum;
Record = record;
}
int main()
{
StudentInfo st ('Smith',21,99999,12,970);
st.show();
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,其中有矩形类Rectangle、函数show和主函数main的定义。程序中位于每个“//ERROR ****found****”下一行的语句有错误,请加以改正。改正后程序的输出结果应该是:
Upper left=(1,8),down right=(5,2),area=24.
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle{
double x1, y1; //左上角坐标
double x2, y2; //右下角坐标
public:
// ERROR ********** found**********
Rectangle (double x1, y1; double x2, y2){
this ->x1=x1;
this ->y1=y1;
this ->x2=x2;
this ->y2=y2;
}
double getX1 ()const{ return x1; }
double getY1 ()const{ return y1; }
double getX2 ()const{ return x2; }
double getY2 ()const{ return y2; }
double getHeight () const { return fabs(y1-y2);}
double getWidth () const { return fabs(x1-x2);}
double area () const { return getHeight()* getWidth(); }
};
// ERROR ********** found**********
void show (Rectangle r) const{
cout <<'Upper left = (';
// ERROR ********** found**********
cout << r.x1 <<', ' << r.y1 <<'), down right = (' <<r.x2 <<', ' <<r.y2;
cout << '), area =' << r. area () << '. ' << endl;
}
int main(){
Rectangle r1 (1, 8, 5, 2);
show(r1);
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件...
---反转前---
STR1=ABCDEF
STR2=ABCDEFG
---反转后---
STR1=FEDCBA
STR2=GFEDCBA
要求:
补充编制的内容写在“//********333********”与“//*********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mgsering.h
#include<iostream>
#include<cstring>
using namespace std;
class MyString{
public:
MyString(const char*s)
{
str=new char[strlen(s)+1];
strcpy(str, s);
}
~MyString() {delete [] str; }
void reverse();
friend ostreamoperator<<(ostream os, const MyString mystr)
{
os<<mystr.str;
return os;
}
private;
char*str;
};
void writeToFile(char*, const MyString);
//main.cpp
#include'mystring.h'
#include<fstream>
void MyString::reverse()
{
//******333******
//*******666******
}
int main()
{
char inname[128], pathname[80];
strcpy(pathname, '');
sprintf(inname, 'in.dat', pathname);
cout<<'读取输入文件...\n\n';
ifstream infile (inname);
if(infile.fail()){
cerr<<'打开输入文件失败!';
exit(1);
}
char buf[4096];
infile.getline(buf, 4096);
MyString str1('ABCDEF'), str2('ABCDEFG'), str3(buf);
cout<<'---反转前---\n';
cout<<'STR1='<<str1<<endl;
cout<<'STR2='<<str2<<endl<<endl;
str1.reverse();
str2.reverse();
str3.reverse();
cout<<'---反转后---\n';
cout<<'STR1='<<str1<<endl;
cout<<'STR2='<<str2<<endl<<endl;
writeToFile(pathname, str3);
return 0;
}
操作题 请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
The extension is:CPP
注意:只修改注释“//ERROR****found****”的下一行语句,不要改动程序中的其他内容。
// proj1.cpp
#include <iostream>
using namespace std;
class MyClass {
char * p;
public:
MyClass(char c)
{p = new char; * p=c;}
// ERROR ******** found********
MyClass (const MyClass copy) { p =new char; * p = * (copy.p); }
// ERROR ******** found********
下列析构函数用于释放字符指针
~MyClass() { free p; }
MyClass operator = (const MyClass rhs)
{
if ( this == rhs ) return * this;
*p = * (rhs.p);
// ERROR ******** found********
return this;
}
char GetChar() const { return * p; }
};
int main()
{
MyClass obj1('C'), obj2 ('P');
MyClass obj3(obj1);
obj3 = obj2;
cout << 'The extension is:'
<<obj1.GetChar () <<obj2.GetChar()
<<obj3.GetChar () << endl;
return 0;
}
操作题 请使用VC6或使用【答题】菜单打开proj1下的工程proj1。其中有线段类Line的定义。程序中位于每个“// ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
End point 1 = (1,8),End point 2 = (5,2),length = 7.2111。
注意:只修改每个“// ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Line;
double length(Line);
class Line{ //线段类
double x1,y1; //线段端点1
double x2,y2; //线段端点2
public:
// ERROR *******found*******
Line (double x1, double y1, double x2, double y2) const {
this -> x1 = x1;
this -> y1 = y1;
this -> x2 = x2;
this -> y2 = y2;
}
double getX1() const {return x1;}
double getY1() const {return y1;}
double getX2() const {return x2;}
double getY2() const {return y2;}
void show() const {
cout << 'End point 1 = ('<< x1 << ',' << y1;
cout << '), End point 2 = ('<< x2 << ',' << y2;
// ERROR *******found*******
cout << '), length = ' << length (this)
<< '。' << endl;
}
};
double length (Line i) {
// ERROR *******found*******
return sqrt ((1.x1 - 1.x2) * (1.x1 - 1.x2) + (1.y1 - 1.y2) * (1.y1 - 1.y2));
}
int main() {
Line r1(1.0,8.0,5.0,2.0);
r1.show();
return 0;
}
