VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 827|回复: 6

[已解决]如何区分这道题中的两种风格?

[复制链接]
41_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-14 19:27:41 | 显示全部楼层 |阅读模式
10驿站币
【问题描述】补全如下代码
【输入形式】请根据主函数进行判断。
【输出形式】已经在主函数里给出,无需关心。
【样例输入】
x 10
test string
And Another line
【样例输出】
strA =
strB = xxxxxxxxxx
strC = test string
strA + strB + strC = xxxxxxxxxxtest string
strC += strD = test stringAnd Another line
~strD = aND aNOTHER LINE
test stringAnd Another line

【样例说明】无
【评分标准】
5组测试数据,权重相同。

#include  <iostream>
#include  <string.h>
using  std::ostream;
#define  FreeIf(x)  {if(x)  {delete[]  x;  x  =  nullptr;}}

class  TString{
public:
        TString();
        TString(const  char  *  pStr);
        //  构造一个长度为len,每个字母均是ch的字符串
        //  例如TString('b',  5)将得到
        //  "bbbbb"的一个TString对象
        TString(char  ch,  int  len);
        //  为简单起见,此处不使用引用计数,仅考虑深拷贝
        TString(const  TString  &  other);
        ~TString();
         
        //  在当前字符串之后追加other
        TString  &  operator  +=(const  TString  &  other);
        //  把当前字符串与other拼接后返回,并且保证当前字符串不改变
        TString  operator  +  (const  TString  &  other)  const;
        //  返回当前字符串的c风格字符串
        const  char  *  c_str(void)  const;
        //  将当前字符串内的所有字符的大小互换
        //  例如:"AbCd"→"aBcD"
        //  当前字符串的内容发生变化
        TString  &  operator~(void);
        friend  ostream&  operator<<(ostream  &  os,  const  TString&  str);
private:
        char  *  m_pData;
        int  m_nLength;
};



这部分是要写的。
















int  main(void){
        TString  strA;
        std::cout  <<  "strA  =  "  <<  strA  <<  std::endl;


        char  ch;
        int  num;
        std::cin  >>  ch  >>  num;
        TString  strB(ch,  num);
        std::cout  <<  "strB  =  "  <<  strB  <<  std::endl;
         
        char  str[81];
        std::cin.getline(str,  80);//  for  newline  character
        std::cin.getline(str,  80);
        TString  strC(str);
        std::cout  <<  "strC  =  "  <<  strC  <<  std::endl;

        std::cout  <<  "strA  +  strB  +  strC  =  "  <<  strA  +  strB  +  strC  <<  std::endl;

        //  std::cin  >>  str;
        std::cin.getline(str,  80);
        TString  strD(str);
        std::cout  <<  "strC  +=  strD  =  "  <<  (strC+=strD)  <<  std::endl;

        std::cout  <<  "~strD  =  "  <<  ~strD  <<  std::endl;
        auto  var  =  strC.c_str();  //  请思考,此处的var是什么类型?
        std::cout  <<  var  <<  std::endl;
        return  0;
}
我连题都看不懂,到底这道题要求的是什么?麻烦大佬们帮我看一下!
最佳答案
57_avatar_small
2020-4-14 19:27:42
代码如下:
  1. #include  <iostream>
  2. #include  <string.h>
  3. using  std::ostream;
  4. #define  FreeIf(x)  {if(x)  {delete[]  x;  x  =  nullptr;}}

  5. class  TString {
  6. public:
  7.         TString();

  8.         TString(const  char  *  pStr);

  9.         //  构造一个长度为len,每个字母均是ch的字符串
  10.         //  例如TString('b',  5)将得到
  11.         //  "bbbbb"的一个TString对象
  12.         TString(char  ch, int  len);

  13.         //  为简单起见,此处不使用引用计数,仅考虑深拷贝
  14.         TString(const  TString  &  other);

  15.         ~TString();

  16.         //  在当前字符串之后追加other
  17.         TString  &  operator  +=(const  TString  &  other);

  18.         //  把当前字符串与other拼接后返回,并且保证当前字符串不改变
  19.         TString  operator  +  (const  TString  &  other)  const;

  20.         //  返回当前字符串的c风格字符串
  21.         const  char  *  c_str(void)  const;

  22.         //  将当前字符串内的所有字符的大小互换
  23.         //  例如:"AbCd"→"aBcD"
  24.         //  当前字符串的内容发生变化
  25.         TString  &  operator~(void);

  26.         friend  ostream&  operator<<(ostream  &  os, const  TString&  str);

  27. private:
  28.         char  *  m_pData;
  29.         int  m_nLength;
  30. };

  31. TString::TString()
  32.         : m_pData(NULL)
  33.         , m_nLength(0)
  34. {
  35. }

  36. TString::TString(const  char  *  pStr){
  37.         if (NULL == pStr)
  38.         {
  39.                 return;
  40.         }

  41.         m_nLength = strlen(pStr);
  42.         m_pData = new char[m_nLength + 1]{0};
  43.         memcpy(m_pData, pStr, m_nLength);
  44. }

  45. TString::TString(char  ch, int  len) {
  46.         m_nLength = len;
  47.         m_pData = new char[m_nLength + 1 ]{ 0 };
  48.         memset(m_pData, ch, m_nLength);
  49. }

  50. TString::TString(const  TString  &  other) {
  51.         m_nLength = other.m_nLength;
  52.         m_pData = new char[m_nLength + 1] { 0 };
  53.         memcpy(m_pData, other.m_pData, m_nLength);
  54. }

  55. TString::~TString() {
  56.         FreeIf(m_pData)
  57. }

  58. TString & TString::operator+=(const TString & other)
  59. {
  60.         if (NULL == other.c_str())
  61.         {
  62.                 return *this;
  63.         }

  64.         //&#160;必须判断是否相等的,而且要+=的,这样相当于调用了自身,但是这次直接下面去了,不进入if的&#160;
  65.         if (this == &other)
  66.         {
  67.                 TString copy(*this);
  68.                 return *this += copy;
  69.         }

  70.         m_nLength += other.m_nLength;

  71.         char *p_old = m_pData;
  72.         m_pData = new char[m_nLength + 1];
  73.         strcpy(m_pData, p_old);
  74.         strcat(m_pData, other.c_str());

  75.         FreeIf(p_old);

  76.         return *this;
  77. }

  78. TString TString::operator+(const TString & other) const
  79. {
  80.         TString ret;
  81.         ret.m_nLength = other.m_nLength + this->m_nLength;
  82.         ret.m_pData = new char[ret.m_nLength + 1]{0};
  83.         memcpy(ret.m_pData, this->c_str(), this->m_nLength);
  84.         memcpy(ret.m_pData+ this->m_nLength, other.c_str(), other.m_nLength);
  85.         return ret;
  86. }

  87. const char * TString::c_str(void) const
  88. {
  89.         return m_pData;
  90. }

  91. TString & TString::operator~(void)
  92. {
  93.         for (int i = 0; i < m_nLength; i++)
  94.         {
  95.                 if (m_pData[i] >= 'a' && m_pData[i] <= 'z')
  96.                         m_pData[i] = m_pData[i] - 32;
  97.                 else if (m_pData[i] >= 'A' && m_pData[i] <= 'Z')
  98.                         m_pData[i] = m_pData[i] + 32;
  99.                 else
  100.                         continue;
  101.         }

  102.         return *this;
  103. }

  104. ostream & operator<<(ostream & os, const TString & str)
  105. {
  106.         if (str.c_str() != NULL)
  107.         {
  108.                 std::cout << str.c_str();
  109.         }
  110.         return os;
  111. }

  112. int  main(void) {
  113.         TString  strA;
  114.         std::cout << "strA  =  " << strA << std::endl;

  115.         char  ch;
  116.         int  num;
  117.         std::cin >> ch >> num;
  118.         TString  strB(ch, num);
  119.         std::cout << "strB  =  " << strB << std::endl;

  120.         char  str[81];
  121.         std::cin.getline(str, 80);//  for  newline  character
  122.         std::cin.getline(str, 80);
  123.         TString  strC(str);
  124.         std::cout << "strC  =  " << strC << std::endl;

  125.         std::cout << "strA  +  strB  +  strC  =  " << strA + strB + strC << std::endl;

  126.         //  std::cin  >>  str;
  127.         std::cin.getline(str, 80);
  128.         TString  strD(str);
  129.         std::cout << "strC  +=  strD  =  " << (strC += strD) << std::endl;

  130.         std::cout << "~strD  =  " << ~strD << std::endl;
  131.         auto  var = strC.c_str();  //  请思考,此处的var是什么类型?此处var为const char*。具体原因可搜索c++关键字auto
  132.         std::cout << var << std::endl;
  133.         return  0;
  134. }
复制代码

最佳答案

查看完整内容

代码如下:




上一篇:链表删除最后一个节点的问题
下一篇:新建的MFC程序,直接编译为什么会不通过?
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-14 19:27:42 | 显示全部楼层    本楼为最佳答案   
bestAnswer
代码如下:
  1. #include  <iostream>
  2. #include  <string.h>
  3. using  std::ostream;
  4. #define  FreeIf(x)  {if(x)  {delete[]  x;  x  =  nullptr;}}

  5. class  TString {
  6. public:
  7.         TString();

  8.         TString(const  char  *  pStr);

  9.         //  构造一个长度为len,每个字母均是ch的字符串
  10.         //  例如TString('b',  5)将得到
  11.         //  "bbbbb"的一个TString对象
  12.         TString(char  ch, int  len);

  13.         //  为简单起见,此处不使用引用计数,仅考虑深拷贝
  14.         TString(const  TString  &  other);

  15.         ~TString();

  16.         //  在当前字符串之后追加other
  17.         TString  &  operator  +=(const  TString  &  other);

  18.         //  把当前字符串与other拼接后返回,并且保证当前字符串不改变
  19.         TString  operator  +  (const  TString  &  other)  const;

  20.         //  返回当前字符串的c风格字符串
  21.         const  char  *  c_str(void)  const;

  22.         //  将当前字符串内的所有字符的大小互换
  23.         //  例如:"AbCd"→"aBcD"
  24.         //  当前字符串的内容发生变化
  25.         TString  &  operator~(void);

  26.         friend  ostream&  operator<<(ostream  &  os, const  TString&  str);

  27. private:
  28.         char  *  m_pData;
  29.         int  m_nLength;
  30. };

  31. TString::TString()
  32.         : m_pData(NULL)
  33.         , m_nLength(0)
  34. {
  35. }

  36. TString::TString(const  char  *  pStr){
  37.         if (NULL == pStr)
  38.         {
  39.                 return;
  40.         }

  41.         m_nLength = strlen(pStr);
  42.         m_pData = new char[m_nLength + 1]{0};
  43.         memcpy(m_pData, pStr, m_nLength);
  44. }

  45. TString::TString(char  ch, int  len) {
  46.         m_nLength = len;
  47.         m_pData = new char[m_nLength + 1 ]{ 0 };
  48.         memset(m_pData, ch, m_nLength);
  49. }

  50. TString::TString(const  TString  &  other) {
  51.         m_nLength = other.m_nLength;
  52.         m_pData = new char[m_nLength + 1] { 0 };
  53.         memcpy(m_pData, other.m_pData, m_nLength);
  54. }

  55. TString::~TString() {
  56.         FreeIf(m_pData)
  57. }

  58. TString & TString::operator+=(const TString & other)
  59. {
  60.         if (NULL == other.c_str())
  61.         {
  62.                 return *this;
  63.         }

  64.         //&#160;必须判断是否相等的,而且要+=的,这样相当于调用了自身,但是这次直接下面去了,不进入if的&#160;
  65.         if (this == &other)
  66.         {
  67.                 TString copy(*this);
  68.                 return *this += copy;
  69.         }

  70.         m_nLength += other.m_nLength;

  71.         char *p_old = m_pData;
  72.         m_pData = new char[m_nLength + 1];
  73.         strcpy(m_pData, p_old);
  74.         strcat(m_pData, other.c_str());

  75.         FreeIf(p_old);

  76.         return *this;
  77. }

  78. TString TString::operator+(const TString & other) const
  79. {
  80.         TString ret;
  81.         ret.m_nLength = other.m_nLength + this->m_nLength;
  82.         ret.m_pData = new char[ret.m_nLength + 1]{0};
  83.         memcpy(ret.m_pData, this->c_str(), this->m_nLength);
  84.         memcpy(ret.m_pData+ this->m_nLength, other.c_str(), other.m_nLength);
  85.         return ret;
  86. }

  87. const char * TString::c_str(void) const
  88. {
  89.         return m_pData;
  90. }

  91. TString & TString::operator~(void)
  92. {
  93.         for (int i = 0; i < m_nLength; i++)
  94.         {
  95.                 if (m_pData[i] >= 'a' && m_pData[i] <= 'z')
  96.                         m_pData[i] = m_pData[i] - 32;
  97.                 else if (m_pData[i] >= 'A' && m_pData[i] <= 'Z')
  98.                         m_pData[i] = m_pData[i] + 32;
  99.                 else
  100.                         continue;
  101.         }

  102.         return *this;
  103. }

  104. ostream & operator<<(ostream & os, const TString & str)
  105. {
  106.         if (str.c_str() != NULL)
  107.         {
  108.                 std::cout << str.c_str();
  109.         }
  110.         return os;
  111. }

  112. int  main(void) {
  113.         TString  strA;
  114.         std::cout << "strA  =  " << strA << std::endl;

  115.         char  ch;
  116.         int  num;
  117.         std::cin >> ch >> num;
  118.         TString  strB(ch, num);
  119.         std::cout << "strB  =  " << strB << std::endl;

  120.         char  str[81];
  121.         std::cin.getline(str, 80);//  for  newline  character
  122.         std::cin.getline(str, 80);
  123.         TString  strC(str);
  124.         std::cout << "strC  =  " << strC << std::endl;

  125.         std::cout << "strA  +  strB  +  strC  =  " << strA + strB + strC << std::endl;

  126.         //  std::cin  >>  str;
  127.         std::cin.getline(str, 80);
  128.         TString  strD(str);
  129.         std::cout << "strC  +=  strD  =  " << (strC += strD) << std::endl;

  130.         std::cout << "~strD  =  " << ~strD << std::endl;
  131.         auto  var = strC.c_str();  //  请思考,此处的var是什么类型?此处var为const char*。具体原因可搜索c++关键字auto
  132.         std::cout << var << std::endl;
  133.         return  0;
  134. }
复制代码

运行截图

运行截图

评分

参与人数 2威望 +2 驿站币 +4 热心值 +4 收起 理由
58_avatar_small thzzl + 2 + 2 赞一个!
51_avatar_small Syc + 2 + 2 + 2 赞一个!

查看全部评分

57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-15 10:54:28 | 显示全部楼层

这道题的目的是将TString的成员函数和operator<<函数完成
41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-4-15 16:32:40 | 显示全部楼层

在我的电脑上显示缺少分号怎么回事?
41_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-4-15 16:34:04 | 显示全部楼层
QQQin 发表于 2020-4-15 16:32
在我的电脑上显示缺少分号怎么回事?

TString::TString(const  char  *  pStr){
        if (NULL == pStr)
        {
                return;
        }

        m_nLength = strlen(pStr);
        m_pData = new char[m_nLength + 1]{0};
        memcpy(m_pData, pStr, m_nLength);
}
在上面的{0}处显示缺少分号
TString::TString(char  ch, int  len) {
        m_nLength = len;
        m_pData = new char[m_nLength + 1 ]{ 0 };
        memset(m_pData, ch, m_nLength);
}
在上面的{0}处显示缺少分号
TString::TString(const  TString  &  other) {
        m_nLength = other.m_nLength;
        m_pData = new char[m_nLength + 1] { 0 };
        memcpy(m_pData, other.m_pData, m_nLength);
}在这三个部分的{0}处显示缺少分号,并且无法运行。
57_avatar_middle
最佳答案
25 
在线会员 发表于 2020-4-15 16:52:11 | 显示全部楼层
QQQin 发表于 2020-4-15 16:34
TString::TString(const  char  *  pStr){
        if (NULL == pStr)
        {

可能是编码问题,这句代码删除自己重新输入相同的代码试试。
61_avatar_middle
最佳答案
0 
在线会员 发表于 2020-4-17 08:36:38 | 显示全部楼层
非常感谢,但有两个地方没太明白
1.为什么动态数组要初始化为0?不初始化会有什么影响?
2.在memset函数处用strcpy和strcat代替,会有什么影响?(代码如下)
TString::TString(char  ch, int  len) {
        m_nLength = len;
        m_pData = new char[m_nLength+1]();
        strcpy(m_pData, &ch);
        for (int i = 0; i < len-1; i++) {
                strcat(m_pData,&ch);
        }
}
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-28 03:27

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表