【C++ Primer Plus习题】12.2

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:


解答:

main.cpp

cpp 复制代码
#include <iostream>
#include "String.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 << "\ncontains " << 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;
	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!" << endl;
	return 0;


	return 0;
}

String.h

cpp 复制代码
#pragma once
#include <iostream>
using namespace std;

class String
{
private:
	char* str;
	int len;
	static int num_strings;
	static const int CINLIM = 80;
public:
	String();
	String(const char*s);
	String(const String&s);
	~String();
	int lengh()const { return len; }

	String& operator=(const String&s);
	String& operator=(const char*s);

	char& operator[](int i);
	const char& operator[](int i)const;



	void stringup();
	void stringlow();
	int has(char c);

	String operator+(const String& s);
	bool operator==(const String& s);

	friend bool operator<(const String& st1, const String& st2);
	friend bool operator>(const String& st1, const String& st2);
	friend ostream& operator<<(ostream& os, const String& s);
	friend istream& operator>>(istream& is, String& s);
	friend String operator+(const char* c, const String& s);

	static int HowMany();
};

String.cpp

cpp 复制代码
#include "String.h"
#include <cctype>

int String::num_strings = 0;

int String::HowMany()
{
	return num_strings;
}

String::String()
{
	//str = NULL;
	//len = 0;
	len = 4;
	str = new char[1];
	str[0] = '\0';
	num_strings++;
}
String::String(const char* s)
{
	len = strlen(s);
	str = new char[len+1];
	strcpy_s(str, len+1, s);
	num_strings++;
}

String::String(const String&s)
{
	len = s.len;
	str = new char[len + 1];
	strcpy_s(str, len + 1, s.str);
	num_strings++;
}

String::~String()
{
	--num_strings;
	delete[] str;
}

String& String::operator=(const String& s)
{
	if (this == &s)return *this;
	if (str)delete[] str;
	len = s.len;
	str = new char[len + 1];
	strcpy_s(str, len + 1, s.str);
	return *this;
}

String& String::operator=(const char* s)
{
	if (str)delete[] str;
	len = strlen(s);
	str = new char[len + 1];
	strcpy_s(str, len + 1, s);
	return *this;
}

char& String::operator[](int i)
{
	return str[i];
}
const char& String::operator[](int i)const
{
	return str[i];
}

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 c)
{
	int count = 0;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == c)
		{
			count++;
		}
	}
	return count;
}

String String::operator+(const String& s)
{
	/*if (!s.str || !str)return String();
	int size = strlen(s.str) + strlen(str) + 1;
	char* result = new char[size];
	strcpy_s(result, strlen(str) + 1, str);
	strcat_s(result,size, s.str);

	return String(result);*/

	String result;
	result.len = s.len + len;
	result.str = new char[result.len + 1];
	strcpy_s(result.str, result.len+1 ,str);
	strcat_s(result.str, result.len + 1, s.str);
	return result;
}

bool operator<(const String& st1, const String& st2)
{
	return strcmp(st1.str, st2.str) < 0;
}
bool operator>(const String& st1, const String& st2)
{
	return st2 < st1;
}
bool String::operator==(const String& s)
{
	bool flag = false;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == s.str[i])
		{
			flag = true;
		}
		else
		{
			return false;
		}	
	}
	return flag;
}

ostream& operator<<(ostream& os, const String& s)
{
	os << s.str;
	return os;
}

//istream& operator>>(istream& is, String& s)
//{
//	char* str=NULL;
//	is >> str;
//	s.str = new char[strlen(str) + 1];
//	strcpy_s(s.str, strlen(str) + 1, str);
//	return is;
//}

istream& operator>>(istream& is, String& s) {
	char buffer[256]; // 使用一个临时缓冲区  
	//is >> buffer; // 读取输入到缓冲区 
	is.getline(buffer, 256);

	// 释放之前的内存(如果有)  
	delete[] s.str;

	// 分配新的内存并复制字符串  
	s.len = strlen(buffer);
	s.str = new char[strlen(buffer) + 1];
	strcpy_s(s.str, strlen(buffer) + 1, buffer); // 使用 strcpy_s 进行安全复制  

	/*char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is)
	{
		s = temp;
	}
	while (is && is.get() != '\n')continue;*/

	return is;
}

String operator+(const char* c, const String& s)
{
	/*String temp;
	int size = strlen(c) + strlen(s.str) + 1;
	char* result = new char[size];
	strcpy_s(result, size, c);
	strcat_s(result,size, s.str);
	temp.len = size;
	temp.str = new char(size);
	strcpy_s(temp.str, size, result);*/
	String temp;
	temp.len = s.len + strlen(c);
	temp.str = new char[temp.len + 1];
	strcpy_s(temp.str,temp.len+1, c);
	strcat_s(temp.str, temp.len + 1, s.str);
	return temp;
}

运行结果:

考查点:

  • 静态数据成员
  • 静态成员函数
  • 拷贝构造
  • 赋值构造
  • 动态内存分配
  • 析构
  • []下标运算符的重载
  • cctype字母大小写转换
  • 字符串的赋值,拼接和比较
  • 重载<<,>>
  • 友元

注意:

  • 有指针的数据成员类,一定要深拷贝!

2024年9月8日08:52:45

相关推荐
DreamByte1 分钟前
Python Tkinter小程序
开发语言·python·小程序
覆水难收呀10 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
阿华的代码王国14 分钟前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
繁依Fanyi20 分钟前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
优思学院26 分钟前
优思学院|如何从零开始自己学习六西格玛?
大数据·运维·服务器·学习·六西格玛黑带·cssbb
weixin_4866811436 分钟前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言36 分钟前
C++入门
开发语言·c++
LN花开富贵42 分钟前
stm32g431rbt6芯片中VREF+是什么?在电路中怎么设计?
笔记·stm32·单片机·嵌入式硬件·学习
怀九日42 分钟前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜43 分钟前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法