|
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;
}
我连题都看不懂,到底这道题要求的是什么?麻烦大佬们帮我看一下!
代码如下:
- #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;
- };
- TString::TString()
- : m_pData(NULL)
- , m_nLength(0)
- {
- }
- 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);
- }
- TString::TString(char ch, int len) {
- m_nLength = len;
- m_pData = new char[m_nLength + 1 ]{ 0 };
- memset(m_pData, ch, m_nLength);
- }
- 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);
- }
- TString::~TString() {
- FreeIf(m_pData)
- }
- TString & TString::operator+=(const TString & other)
- {
- if (NULL == other.c_str())
- {
- return *this;
- }
- // 必须判断是否相等的,而且要+=的,这样相当于调用了自身,但是这次直接下面去了,不进入if的 
- if (this == &other)
- {
- TString copy(*this);
- return *this += copy;
- }
- m_nLength += other.m_nLength;
- char *p_old = m_pData;
- m_pData = new char[m_nLength + 1];
- strcpy(m_pData, p_old);
- strcat(m_pData, other.c_str());
- FreeIf(p_old);
- return *this;
- }
- TString TString::operator+(const TString & other) const
- {
- TString ret;
- ret.m_nLength = other.m_nLength + this->m_nLength;
- ret.m_pData = new char[ret.m_nLength + 1]{0};
- memcpy(ret.m_pData, this->c_str(), this->m_nLength);
- memcpy(ret.m_pData+ this->m_nLength, other.c_str(), other.m_nLength);
- return ret;
- }
- const char * TString::c_str(void) const
- {
- return m_pData;
- }
- TString & TString::operator~(void)
- {
- for (int i = 0; i < m_nLength; i++)
- {
- if (m_pData[i] >= 'a' && m_pData[i] <= 'z')
- m_pData[i] = m_pData[i] - 32;
- else if (m_pData[i] >= 'A' && m_pData[i] <= 'Z')
- m_pData[i] = m_pData[i] + 32;
- else
- continue;
- }
- return *this;
- }
- ostream & operator<<(ostream & os, const TString & str)
- {
- if (str.c_str() != NULL)
- {
- std::cout << str.c_str();
- }
- return os;
- }
- 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是什么类型?此处var为const char*。具体原因可搜索c++关键字auto
- std::cout << var << std::endl;
- return 0;
- }
复制代码
|
上一篇: 链表删除最后一个节点的问题下一篇: 新建的MFC程序,直接编译为什么会不通过?
|