【C++ Primer Plus习题】12.2

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

问题:

这里是引用
在这里插入图片描述

解答:
main.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

#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

#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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值