使用Vc6打开考生文件夹proj3下的工程proj3,其中声明了NyString类,它是一个用于表示字符串的类。成员函数reverse将字符串反转,例如“abode”反转后就成了“edcba”。请补充完整函数reverse。在main函数中给出了一个测试数

admin2019-06-05  29

问题 使用Vc6打开考生文件夹proj3下的工程proj3,其中声明了NyString类,它是一个用于表示字符串的类。成员函数reverse将字符串反转,例如“abode”反转后就成了“edcba”。请补充完整函数reverse。在main函数中给出了一个测试数据,此情况下程序的输出应该是:
    This is a string
    gnirtS a si sihT
    注意:只需在函数reverse的//********333********和//********666********之间填入若干语句,不要改动程序中的其他内容。
//NyString.h
#include<iostream>
usirig namespace std;
char * dup(const char *);
class NyString{
    char * str;
public:
    NyString(const char * s=""):str(dup(s){}
    NyString(const NyString&m):str(dup(m.str)){}
    ~HySting(){delete[]str;}
    void reverse();
    void show(ostream&os=tout)
const{os<<str<<end1;)
};
inline ostream&operator<<fostrearn& os,const NyString&m){
    m.show(os);
    return os;
  }
  void writeTOFile(const char *
  path);
  //NyString.cpp
  #include"NyString.h"
  char * clup(const ohar * s){
    char * p=new char[strlen(s)+1];
    strcpy(p,s);
    return p;
}
void NyString∷EeveEse(){
    //********333********
    //********666********
}
void writeToFile(const char *path);
//writetoFile.cpp
#include<fstream>
#include"MyString.h"
void writeToFile(const char *path){
    char full150;
    strcpy(full,path);
    Strcat(full,"out.dat");
    OfStream outfile(full);
    if(outfile.fail()){cerr<<"打开输出文件失败!";return;}
    MyString m1:"This is another string";
    MyString m2("字符串反转演不");
    outfile<<m1<<m2:
    m1.reverse();
    m2.reverse();
    outfile<<m1<<m2;
    outfile.close();
  }

选项

答案void MyString∷reverse() { int i,i; char ch; int len=strlen(str); for(i=0,j=len-1;i<j;i++,j--) { ch=str[i]; str[i]=str[j]; str[j]=ch; } }

解析
转载请注明原文地址:https://kaotiyun.com/show/Y18p777K
0

相关试题推荐
最新回复(0)