系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:本章练习第2题
涉及标准函数及关键词
toupper,tolower(), strcpy_s(),strcat_s(),strcmp,strlen(),new[],delete[].
实现如下效果
输出应与下面相似:
Please enter yourname:Fretta Farbo
My name is Fretta Farbo.
The string
MY NAME IS FRETTA FARBO ANDIAMA++ STUDENT.
contains 6’'characters in it.
Enter the name of a primary color for mixing light: yellowTry again!
BLUE
That’s right!
Bye
原文链接:https://blog.csdn.net/zhyjhacker/article/details/139555079
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着学习的升入,练习是必不可少的重要环节
提示:以下是本篇文章正文内容,下面案例可供参考
一、2.通过完成下面的工作来改进 String类
声明(即将 Stringl.h升级为 String2.h)。
a.对+运算符进行重载
使之可将两个字符串合并成1个。
b.提供一个 Stinglow()成员函数
将字符串中所有的字母字 符转换为小写(别忘了cctype 系列字符函数)。
c.提供 String()成员函数
将字符串中所有字母字符转换成大写。
d.提供一个这样的成员函数
它接受一个 char参数,返回该字符在字符串中出现的次数。使用下面的程序来测试您的工作:
二、头文件的定义
1.头文件
代码如下(示例):
#ifndef PE12_2_H_
#define PE12_2_H_
namespace NString
{
class String
{
public:
static const unsigned buffMaxSize = 256;
String(const char* text = " ");
String(const String& str);
~String();
unsigned getLength(void)const;
const char* toCstr(void)const;
void stringUp(void);//to up
void stringlow(void);//to low
unsigned has(char ch)const;
String& operator=(const String& str);
String& operator+=(const String& str);
friend std::istream& operator>>(std::istream& is, String& str);
char& operator[](unsigned index);
const char& operator[](unsigned index)const;
private:
unsigned textLength;
char* pText;
void assignMember(const char* text);
};
std::ostream& operator<<(std::ostream& os, const String& str);
bool operator<(const String& lvalue, const String& rvalue);
bool operator>(const String& lvalue, const String& rvalue);
bool operator==(const String& lvalue, const String& rvalue);
bool operator<=(const String& lvalue, const String& rvalue);
bool operator>=(const String& lvalue, const String& rvalue);
String operator+(const String& lvalue, const String& rvalue);
}
#endif // !PE12_2_H_
2.方法
代码如下(示例):
//pe12_2.cpp
#include<iostream>
#include<cstring>
#include<cctype>
#include"pe12_2.h"
namespace NString
{
using std::ostream;
using std::istream;
String::String(const char* text)
{
assignMember(text);
}
String::String(const String& str)
{
assignMember(str.toCstr());
}
String::~String()
{
delete[]pText;
}
void
String::stringlow(void)
{
for (unsigned i = 0; i < textLength; i++)
{
pText[i] = (char)tolower((int)pText[i]);
}
}
void
String::stringUp(void)
{
for (unsigned i = 0; i < textLength; i++)
{
pText[i] = (char)toupper((int)pText[i]);
}
}
unsigned
String::getLength(void)const
{
return (textLength);
}
unsigned
String::has(char ch)const
{
unsigned chCnt = 0;
for (unsigned i = 0; i < textLength; i++)
{
if (ch == pText[i])
{
++chCnt;
}
}
return chCnt;
}
const char*
String::toCstr(void)const
{
return pText;
}
//私有成员
void
String::assignMember(const char* text)
{
textLength = strlen(text);
pText = new char[textLength + 1];
strcpy_s(pText, textLength+1, text);
}
//重载运算符
String&
String::operator=(const String& str)
{
if (&str == this)
{
return *this;
}
delete[]pText;
assignMember(str.toCstr());
return *this;
}
String&
String::operator+=(const String& str)
{
return (*this = *this + str);
}
char&
String::operator[](unsigned index)
{
return(pText[index]);
}
const char&
String::operator[](unsigned index)const
{
return (pText[index]);
}
ostream&
operator<<(ostream& os, const String& str)
{
os << str.toCstr();
return os;
}
istream& operator>>(istream& is, String& str)
{
char text[String::buffMaxSize];
if (is>>text)
{
str = text;
}
is.ignore(String::buffMaxSize, '\n');
return is;
}
bool
operator<(const String& lvalue, const String& rvalue)
{
return (strcmp(lvalue.toCstr(), rvalue.toCstr()) < 0);
}
bool
operator>(const String& lvalue, const String& rvalue)
{
return (rvalue < lvalue);
}
bool
operator==(const String& lvalue, const String& rvalue)
{
return (!(rvalue < lvalue) && !(rvalue > lvalue));
}
bool
operator<= (const String & lvalue, const String & rvalue)
{
return (!(lvalue > rvalue));
}
bool
operator>= (const String & lvalue, const String & rvalue)
{
return (!(lvalue < rvalue));
}
String operator+(const String& lvalue, const String& rvalue)
{
char* pText = new char[lvalue.getLength() + rvalue.getLength() + 1];
//int test1 = lvalue.getLength() + rvalue.getLength() + 1;
//int test2 = sizeof(pText);
//int test3 = strlen(pText);
strcpy_s(pText, lvalue.getLength()+1 , lvalue.toCstr());
//strcat_s第二个参数是总的缓冲区的大小。这里和strcpy_s是不一样的。需要注意
strcat_s(pText, lvalue.getLength() + rvalue.getLength() +1, rvalue.toCstr());
String temp(pText);
delete[]pText;
return (temp);
}
}
测试函数(题目提供)
//pe12-2.cpp
#include<iostream>
using namespace std;
#include "pe12_2.h"
int main()
{
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2; //overloaded<<operator
cin >> s3; //overloaded >>operator
s2 = "My name is " + s3;//overloaded = ,+OPerators
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup(); //converts string to uppercase
cout << "The string\n" << s2 << "\ncontains " << s2.has('A') << " 'a' characters in it.\n";
s1 = "red"; //String(const char*),
//then String & operator=(const String&)
String rgb[3] = { String(s1),String("greed"),String("blue") };
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin>>ans)
{
ans.stringlow();//converts string to lowercase
for (int i = 0; i < 3; i++)
{
if (ans == rbg[i]) //overloaded == operator
{
cout << "That's right.\n";
success = true;
break;
}
}
if (success)
{
break;
}
else
{
cout << "Try again!\n";
}
}
cout << "Bye\n";
return 0;
}
总结
提示:这里对文章进行总结:
例如:
1,对strcpy_s函数赋值用strlen取到长度需+1,对字符串而言,如果是数组的话,不需要数组长度。但是需要在数组追加最后一个元素‘\0’
2,strcpy_s,函数做追加的话中间的大小是整个字符串的大小,而不是追加上来的大小。这里要熟知
3,最后就是一定要多练习。c++是实操性很强大的工具,需要熟练的练习,工具比较繁琐。但是带来的好处就是功能强大。
标签:12,const,String,lvalue,C++,char,pText,Plus,operator From: https://blog.csdn.net/zhyjhacker/article/details/139573805