首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹proj3下的工程proj3,其中声明了MyString类,它是一个用于表示字符串的类。成员函数reverse将字符串反转,例如“abcde”反转后就成了“edcba”。请补充完整函数reverse。在main函数中给出了一个测试数
使用VC6打开考生文件夹proj3下的工程proj3,其中声明了MyString类,它是一个用于表示字符串的类。成员函数reverse将字符串反转,例如“abcde”反转后就成了“edcba”。请补充完整函数reverse。在main函数中给出了一个测试数
admin
2020-10-21
17
问题
使用VC6打开考生文件夹proj3下的工程proj3,其中声明了MyString类,它是一个用于表示字符串的类。成员函数reverse将字符串反转,例如“abcde”反转后就成了“edcba”。请补充完整函数reverse。在main函数中给出了一个测试数据,此情况下程序的输出应该是:
This is a stnng
gnirts a si sihT
注意:只需在函数reverse的//**********333**********和//**********666**********之间填入若干语句,不要改动程序中的其他内容。
//MyString.h
#include
using namespace std;
char * dup(eonst char*);
class MyString{
char*str;
public:
MyString(const char*s=""):str(dup(s)){}
MyString(eonst MyString&m):str(dup(m.str)){}
~MyString(){delete[]str;}
void reverse();
void show(ostream&os=cout)eonst{os<
};
inline ostream&operator<<(ostream&os,const MyString&m){
m.show(os);
return os;
}
void writeToFile(const char*path);
//MyString.cpp
#include"MyString.h"
char*dup(const char*s){
char*p=new char[strlen(s)+1];
strcpy(p,s);
return p;
}
void MyString::reverse(){
//**********333**********
//**********666**********
}
void writeToFile(const char*path);
//writetoFile.cpp
#include
#include"MyString.h"
void writeToFile(const char*path){
char full[50];
strcpy(full,path);
strcat(full,"out.dat");
ofstream outfile(fun);
if(outfile.fail()){eelT<<"打开输出文件失败!";return;}
MyString ml="This is another string":
MyString m2("字符串反转演示");
outfile<
m1.reverse();m2.reverse();
outfile<
outfile.close();
}
选项
答案
Void MyString::reverse() { int i,j; char ch: int len=strlen(str); for(i=0,j=len-1;i
解析
程序定义了MyString类,它包含一个成员:str字符指针;默认构造函数和复制构造函数通过dup()函数,动态分配地址p,将参数拷贝到p指向的内存空间,再使用p初始化str;show()成员函数将str指向的字符串输出;reverse()成员函数将str指向的字符串反转;重载的<<运算符函数调用形参m对象的show()函数输出str指向的字符串。
main()函数首先构造一个MyStrig类对象m1,构造过程中传入字符串“This is a string”,所以m1对象中的str指向一个动态分配的内存空间,该段内存存放拷贝的字符串“This is a string”,输出m1后,再调用m1.reverse()函数,将str指向的字符串进行反转,再输出m1,题意要求我们完成reverse()成员函数的定义,完成反转字符串的功能。
由题意,我们仅有的信息就是str指向一个字符串,它是char*类型,需要对str指向的字符串进行反转,需要获得字符串的长度,然后将首尾字符进行交换,直到交换到中间字符串为止,所以我们定义两个变量i、j,表示元素下标,分别从首(i=0)和尾(长度一1)向中间遍历该字符串,每次遍历,首先判断i是否小于j,若i小于j则将i和j下标对应的字符进行交换,交换完成后i自增l,j自减1,直到i不小于j为止。
转载请注明原文地址:https://jikaoti.com/ti/r2l0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:语句存在错误
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1。其中有线段类Line的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:Endpoint1
下列关于类和对象的叙述中,错误的是()。
有如下程序:#includeusingnamespacestd;classsample{private:intx,y;public:sample(inti,intj)
存在定义inta[10],x,*pa;,若pa=&a[0],下列的哪个选项和其他3个选项不是等价的()?
下面程序的输出结果为()。#includevoidmain(){intx;int&y=x;∥变量引用31=99;cout
关于运算符重载,下列表述中正确的是()。
在下列关系运算中,不改变关系表中的属性个数但能减少元组个数的是()。
数据结构主要研究的是数据的逻辑结构、数据的运算和()。
随机试题
工艺系统的内部热源有()。
当今世界格局发展的趋势是()
肛门拭子法常用于诊断的寄生虫病有
有关鞍内颅咽管瘤的描述中,错误的是
A、三点校正的紫外分光光度法B、高效液相色谱法C、四氮唑比色法D、气相色谱法E、双相滴定法;下列药物的含量测定方法为维生素E
下列科目,属于损益类的是()。
各单位要依法设置的账簿有()。
计算机网络中传输介质传输速率的单位是bps,其含义是()。
Ayoungman(Example:0)goingtojointhearmy(军队)andhadto【C1】______amedicalexamination.Thedoctorwassittingatadeskwhe
A—DocumentmanagementB—DigitizedtechnologyC—SoftwaretechnologyD—TextformattingE—Formprocessi
最新回复
(
0
)