一、字符串练习
去掉字符串【两端】和【中间多余的空格】
-
- 将英文句子两端的空格(1 个或多个或没有空格)去掉
-
- 单词中间的分隔符是一个或多个空格,如果是多个空格,减少为一个空格
-
- 实现这个功能的函数名为 reshape,函数的返回值是字符串的起始地址
#include <iostream>
#include <fstream>
using namespace std;
char* reshape(char* str);
int main(int argc, char** argv) {
//多个英文句子
char buf[4][100] = { " Meeting you in UIBE is the most beautiful scenery in my life. ",
"There is but one secret to sucess--never give up." };
for (int i = 0; i < 4; i++) {
cout << reshape(buf[i]) << "\n";
}
return 0;
}
//在此作答
char* reshape(char* str) {
string s;
char* p = str;
int len = strlen(str);
int i = 0, j = len - 1;
char ans[100] = {0};
while (i < len && str[i] == ' ') {
i++;
p++;
}
while (j >= 0 && str[j] == ' ') {
str[j] = '\0';
j--;
}
int index = 0;
for (int k = i; k <= j; k++) {
if (str[k] == ' ') {
while (k + 1 <= j && str[k + 1] == ' ') {
k++;
}
if (k + 1 < j) {
str[index++] = str[k];
}
}
else {
str[index++] = str[k];
}
}
str[index] = 0;
return str;
}
二、深拷贝和赋值运算符重载
为 Master 类添加深拷贝的拷贝构造函数和赋值运算符
#include <iostream>
#include <cstring>
using namespace std;
struct Master { //硕士研究生类
char name[256]; //研究生姓名
char* pSupervisor; //导师姓名
Master(const char* _name = "", const char* _supervisor = "") {
strcpy(this->name, _name);
pSupervisor = new char[256];
strcpy(pSupervisor, _supervisor);
}
virtual ~Master() { //虚析构函数
delete[] pSupervisor;
}
//在此作答
// 深拷贝的拷贝构造函数
Master(const Master& m) {
strcpy(this->name, m.name);
pSupervisor = new char[256];
strcpy(pSupervisor, m.pSupervisor);
}
// 赋值运算符
Master operator=(const Master& m) {
if (&m != this) {
strcpy(this->name, m.name);
pSupervisor = new char[256];
strcpy(pSupervisor, m.pSupervisor);
}
return *this;
}
};
void print(const Master& stu) {
cout << stu.name << " <- " << stu.pSupervisor << endl;
}
int main() {
Master stu1("Tony", "Ada Lovelace");
print(stu1);
Master stu2 = stu1; //调用拷贝构造函数
strcpy(stu2.name, "Mary");
strcpy(stu2.pSupervisor, "Alan Turing");
print(stu2);
Master stu3;
stu3 = stu1; //调用赋值运算符
strcpy(stu3.name, "Victor");
strcpy(stu3.pSupervisor, "Nikola Tesla");
print(stu3);
/* 程序运行结果为:
Tony <- Ada Lovelace
Mary <- Alan Turing
Victor <- Nikola Tesla
*/
return 0;
}
三、重载加号运算符和流插入运算符
运算符重载
- 重载加号运算符实现 2 个 RMB(人民币)对象的加法运算
- 重载流插入运算符实现将 RBM 对象按给定格式输出到输出流 ostream
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
typedef unsigned int u_int;
class RMB {
int yuan, jiao, fen; //元角分
public:
RMB(u_int _yuan = 0, u_int _jiao = 0, u_int
_fen = 0) :yuan(_yuan), jiao(_jiao), fen(_fen) {
cout << "[Constructor]创建 RBM 对象:" << yuan << "元" << jiao << "角" << fen << "分\n";
}
RMB(double rmb) { //转换构造函数
yuan = (u_int)rmb;
jiao = (u_int)(rmb * 10) % 10;
fen = (u_int)(rmb * 100) % 10;
cout << "[Conversion Constructor]创建 RBM 对象:" << yuan << "元"<<jiao<<"角"<<fen<<"分\n";
}
//在此添加部分代码
// 重载加号运算符
RMB operator+(const RMB& rmb) {
int f = this->fen + rmb.fen;
int j = this->jiao + rmb.jiao;
int y = this->fen + rmb.fen;
if (f >= 10) {
f -= 10;
j++;
}
if (j >= 10) {
j -= 10;
y++;
}
return RMB(y, j, f);
}
// 重载流插入运算符
friend ostream& operator << (ostream& output, RMB& rmb) {
output << rmb.yuan << " 元 " << rmb.jiao << " 角 " << rmb.fen << " 分\n";
return output;
}
}; //类定义结束
//在此作答
int main(int argc, char** argv) {
RMB a(15, 7, 8);
RMB b = 4.67;
RMB c;
c = a + b;
//输出为"y 元 j 角 f 分",请严格按照此格式
cout << c << endl; //20 元 4 角 5 分
ofstream out("out.txt");
out << c << endl; //输出 RMB 对象到文件输出流
out.close();
return 0;
}