首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串
请打开考生文件夹下的解决方案文件proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串
admin
2020-07-23
27
问题
请打开考生文件夹下的解决方案文件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 ostream&operator<<(ostream&os,const MyString&mystr)
{
os<<mystr.str;
return os;
}
private:
char*str;
};
void writeToFile(char*,constMyString&);
//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);
MyStringstrl(”ABCDEF”),
Str2("ABCDEFG"),Sstr3(buf);
cout<<"---反转前---\n";
cout<<"STR1="<<str1<<endl;
cout<<"STR2="<<str2<<endl<<endl;
Strl.reverse( );
Str2.reverse( );
Str3.reverse( );
cout<<"---反转后---\n";
cout<<"STR1=”<<str1<<endl;
cout<<"STR2="<<Str2<<endl<<endl;
writeToFile(pathname,str3);
returnR0;
}
选项
答案
int length:strlen(str);//把字符串str的长度赋值给lenth for(int i=0,j=length-1;i<j;i++,j--) //从i=0,j=length-1,i<j为条件开始遍历,并把Str[i]和Str[j]交换 { char temp=str[i];//给定义的临时变量temp赋值为str[i] str[i]=str[j];//给str[i]赋值str[j] str[j]=temp;//给str[j]赋值为temp
解析
主要考查考生对动态数组的掌握,先看题目要求:成员函数reverse的功能是将字符串进行“反转”。再由类的定义可知,字符串存放在动态数组str中,由strlen函数得出字符串的长度,最后一个字符的下标为length-1,第一个字符的下标为0,将这两个字符交换,然后j依次减1同时i依次加1,继续交换,直到i大于j时停止循环即可。
转载请注明原文地址:https://kaotiyun.com/show/l9yp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下类定义:classTest{public:Test(){a=0;c=0;} //①intf(inta)const{this->a=a;} //②staticint
有如下类定义:classPoint{intxx,yy;public:Point():xx(0),yy(0){}Point(intx,inty=0):xx(x),yy(y){}};若执行语句Pointa
有如下类定义:classFoo{public:Foo(intv):value(v){} //①~Foo(){} //②private:Foo(){}
下列函数模板的定义中,合法的是()。
下列关于派生类构造函数和析构函数的说法中,错误的是()。
关于函数重载,下列叙述中错误的是()。
在类Derived中,函数show()不能直接访问的数据成员是
下列关于运算符函数的表述中,错误的是
有如下程序:#includeusingnamespacestd;intmain(){inta[10];for(int*p=a;p
除了显式定义的构造函数外,类中还有可能包含编译器自动生成的构造函数,因此一个类所拥有的构造函数的至少有
随机试题
某市环保局应当地居民的举报对排放烟尘超标的某化工厂作出了处罚5万元的行政处罚,并责令其限期整改。化工厂对环保局的行政处罚决定不服,向法院提起诉讼。诉讼期间,原告找到被告要求协商解决,被告同意,遂变更了原来的处罚决定,对原告只加收两倍的排污费。原告向法院申请
神经递质的信息传递方式A.突触分泌B.旁分泌C.内分泌D.自分泌E.外分泌
企业的加权平均资本成本指的是
甲公司2015年发现2013年有笔实际资产损失10万元在当年未扣除,已知甲公司2013年应纳税所得额为8万元,2014年应纳税所得额为3万元,2015年应纳税所得额为12万元,甲公司将上述损失在2015年进行资产损失申报,该公司2015年实际应缴纳的企业所
现有关系表:学生(宿舍编号,宿舍地址,学号,姓名,性别,专业,出生日期),它的主键是()。
不仅人上了年纪会难以集中注意力,就连蜘蛛也有类似的情况。年轻蜘蛛结的网整齐均匀,角度完美;年老蜘蛛结的网可能出现缺口,形状怪异。蜘蛛越老,结的网就越没有章法。科学家由此认为,随着时间的流逝,这种动物的大脑也会像人脑一样退化。以下哪项如果为真,最能质疑科学家
Inmostcultures,whenyoumeetacquaintancesforthefirsttimeduringaday,itisnormaltogreetthem.Themainpurposeoft
WhatdidHelenrecentlypass?
Somefindingsareissuedrecentlyaboutattendingacommunitycollege.Mostworkerswhohavea【C1】______fromacommunitycollege
A--trafficregulationB--guidepostC--trafficlightD--trafficpoliceE--safet
最新回复
(
0
)