首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABC
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABC
admin
2016-08-19
35
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义了MyString类,一个用于表示字符串的类。成员函数reverse的功能是将字符串进行“反转”。例如,将字符串ABCDEF“反转”后,得到字符串FEDCBA;将字符串ABCDEFG“反转”后,得到字符串GFEDCBA。请编写成员函数reverse。在main函数中给出了一组测试数据,此时程序运行中应显示:
读取输入文件…
一反转前…
STRl:ABCDEF
sTR2=ABCDEFG
…反转后…
STRl=FEDCBA
STR2=GFEDCBA
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中,输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
1 //mgsering.h
2 #include
3 #include
4 using namespace std;
5
6 class MyString{
7 public:
8 HyString(const char*s)
9 {
10 str =new char[strlen(s) +1];
11 strcpy(sir,s);
12 }
13
14 ~NyString(){delete[]str;}
15
16 voLd reverse();
17 friend ostream&operator<(os-tream&os,const HyString&mystr)
18 {
19 os <
20 retuErn os;
21 }
22 private:
23 char*str;
24 };
25 void writeToFile(char *, const HyString&);
1 //main.cpp
2 #include"mystring.h"
3 #inclucle
4
5 void MyString::reVerse()
6 {
7 //********333********
8
9
10 //********666********
11 }
12
13 int main()
14 {
15 char inname[128],pathname[80];
16 strcpy(pathname," ");
17 sprintf(inname,"in.dat",path-name);
18 cout<<"读取输入文件…\n\n";
19 ifstream infile(inname);
20 if(infile.fail()) {
21 cerr<<"打开输入文件失败!";
22 exit(1);
23 }
24
25 char buf[4096];
26 infile.getlihe(bur,4096);
27 HyString str1("ABCDEF"),sir2("AB-CDEFG"),str3(buf);
28 cout<<"---反转前---\n";
29 tout<<"STR1="<
30 cout<<"STR2=" <
31
32 str1.reverse();
33 str2.reverse();
34 str3.reverse();
35 tout<<"---反转后---\n";
36 tout<<"STR1="<
37 cout<<"STR2 =" <
38
39 writeToFile(pathname,$tr3);
40 return 0;
41 }
选项
答案
1 int length=strlen(str);//把字符串Str的长度赋值给lenth 2 for(int i=0,j=length—l;i
解析
主要考查考生对动态数组的掌握,先看题目要求:成员函数reverse的功能是将字符串进行“反转”。再由类的定义可知,字符串存放在动态数组str中,由strlen函数得出字符串的长度,最后一个字符的下标为length-1,第一个字符的下标为0,将这两个字符交换,然后j依次减1同时i依次加1,继续交换,直到i大于j时停止循环即可。
转载请注明原文地址:https://jikaoti.com/ti/pRE0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
已知枚举类型定义语句为:enumToken{NAME,NUMBER,PLUS=5,MINUS,PRINT=10};则下列说法中,错误的是()。
以下各选项中,()中都是C++语言保留字。
下列数组的定义中,会产生错误的是()。
函数fun的返回值是fun(char*a,char*b){intnum=0,n=0;while(*(a+num)!=’\0’)nmn++;while(b[n]){*(a+num)=b[n];num
设有数组定义:chararray[]="China";,则数组array所占的空间为
一个满二叉树其深度为4,则其叶子节点的个数为()。
在结构化方法中,用数据流程图(DFD)作为描述工具的软件开发阶段是______.
下列运算符中,在C++语言中不能重载的是
在一个无向图中,所有顶点的度数之和等于所有边数的( )倍。
随机试题
1984年9月20日,《中华人民共和国药品管理法》正式实施。
支气管肺炎区别于支气管炎的关键是()。
A.软化点B.粒度C.耐热性D.赋形性E.溶出度《中国药典》规定,凝胶贴膏应检查()。
2016年2月26日,甲公司以账面价值为3000万元(未减值)、公允价值为3600万元的库存商品为对价自乙公司股东丙公司处取得对乙公司60%的股权,相关手续于当日办理完毕;当日乙公司可辨认净资产公允价值为6200万元,账面价值为5200万元。2016年3月
依法执政是中国共产党执政的基本方式。下列选项能够体现中国共产党依法执政的是()。
舆论热点的形成虽有偶然性,但也不排除其中有一定的规律,可以被人把握。所以我们也可以看到一些人为制造的热点,也可以看到自然形成的热点事件中某些人刻意在________。为什么会有人这么做呢?动机就在于利用政府的舆情应对机制,一旦舆论造起来,政府部门就要加以解
1927年,轰轰烈烈的大革命失败了。大革命失败给中国共产党最深刻的教训是()
设齐次线性方程组(2E-A)x=0有通解x=kξ1=k(-1,1,1)T,k是任意常数,其中A是二次型f(x1,x2,x3)=xTAx对应的矩阵,且r(A)=1.求二次型f(x1,x2,x3).
[*]
在SQLServer2000中,事务日志备份______。
最新回复
(
0
)