通过下面的工作来改进String类声明(即将String1.h升级为String2.h)。
a. 对+运算符进行重载,使之可将两个字符串合并成一个。
b. 提供一个Stringlow()成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
c. 提供String()成员函数,将字符串中所有字母字符转换成大写。
d. 提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。
使用下面的程序来测试您的工作:
// pe12_2.cpp
#include <iostream>
using namespace std;
#include "string2.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("green"), 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 == rgb[i]) // overloaded == operator
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
return 0;
}
输出应与下面相似:
Please enter your name: Fretta Farbo
My name is Fretta Farbo.
The string
MY NAME IS FRETTA FARBO AND I AM A C++ STUDENT.
contains 6 'A' characters in it.
Enter the name of a primary color for mixing light: yellow
Try again!
BLUE
That's right!
Bye
这道题是C++ Primer Plus习题及答案-第十二章的第二题,以下为自己写的答案,仅以此作为记录,若有不足欢迎指出:
string2.h:
#pragma once
#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
#include <istream>
using namespace std;
class String {
public:
String();
String(const char* str);
String(string& str);
void stringup();
void stringlow();
int has(const char str)const;
String operator+(String& object);
private:
string str;
int len = 0;
private:
friend ostream& operator<<(ostream& os, String& object);
friend istream& operator>>(istream& is, String& object);
friend String operator+(const char* str1, String& object);
friend bool operator==(const String& object1, const String& object2);
};
string2.cpp:
#include "String.h"
String::String() {
str = "未知";
len = str.length();
}
String::String(const char* str) {
this->str = str;
len = strlen(str);
}
String::String(string& str) {
this->str = str;
len = str.length();
}
void String::stringup() {
for (int i = 0; i < len; i++) {
str[i] = toupper(str[i]);
}
}
void String::stringlow() {
for (int i = 0; i < len; i++) {
str[i] = tolower(str[i]);
}
}
int String::has(char str) const {
int count = 0;
for (int i = 0; i < len; i++) {
if (this->str[i] == str) {
count++;
}
}
return count;
}
String String::operator+(String& object) {
str = str.append(object.str);
return String(str);
}
ostream& operator<<(ostream& os, String& object) {
os << object.str;
return os;
}
istream& operator>>(istream& is, String& object) {
getline(is, object.str);//可以包括空格的输入
//is >> object.str;
return is;
}
String operator+(const char* str1, String& object) {
string tmp = string(str1) + object.str;
return String(tmp);
}
bool operator==(const String& object1, const String& object2) {
if (object1.str == object2.str && object1.str.length() == object2.str.length()) {
return true;
}
return false;
}
main.cpp:
#include <iostream>
#include "String2.h"
using namespace std;
int main() {
String s1(" and I am a C++ student.");
String s2 = "Please enter your name:";
String s3;
cout << s2;
cin >> s3;
s2 = "My name is " + s3;
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup();
cout << "The string\n" << s2 << "\n contanins " << s2.has('A') << " 'A' characters in it.\n";
s1 = "red";
String rgb[3] = { String(s1),String("green"),String("blue") };
cout << "Enter the name of a primary color for mixing light:";
String ans;
bool success = false;
//cin.get();
while (cin >> ans) {
ans.stringlow();
for (int i = 0; i < 3; i++) {
if (ans == rgb[i]) {
cout << "That's right!\n";
success = true;
break;
}
}
if (success) {
break;
}
else {
cout << "Try again!\n";
}
}
cout << "Bye\n";
return 0;
}
运行结果: