c++primerplus第六版第十二章第二题

#pragma once
#include<iostream>
using namespace std;
class stringg
{
	int len;
	char *str;
public:
	stringg();
	stringg(const char*p);
	stringg(const stringg&c);
	stringg &operator=(const char*p);
	stringg &operator=(const stringg&c);
	//stringg operator+(const stringg&c);
	//stringg operator+(const char*c);
	char &operator[](int i) { return str[i]; };
	const char &operator[](int i) const{ return str[i]; };
	void stringlow();
	void stringup();
	int has(char c);
	bool operator==(const stringg&c);
	friend stringg operator+(const stringg&a, const stringg&b);
	//friend stringg operator+(const char*p,  stringg&c) ;
	friend ostream &operator<<(ostream&os, const stringg &c);
	friend istream &operator>>(istream&is,  stringg&c);

	~stringg();
};

//cpp


```cpp
#include "stringg.h"
#include<cstring>
#include<cctype>

stringg::stringg(const char*p)
{
	len = strlen(p);
	str = new char[len + 1];
	strcpy_s(str, len + 1, p);

}
stringg::stringg(const stringg&c)
{
	len = strlen(c.str);
	str = new char[len + 1];
	strcpy_s(str, len + 1, c.str);

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

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



}
//stringg stringg::operator+(const stringg&c)
//{
//	stringg a;
//	a.len = len + c.len;
//	a.str = new char[a.len + 1];
	/*strcpy_s(a.str, len + 1, str);
	strcpy_s(a.str+len, c.len + 1, str);*/
//	for (int i = 0; i < len; i++)
//	{
//		a.str[i] = str[i];
//	}
//
//	for (int i = len; i < a.len; i++)
//	{
//		a.str[i] = c[i - len];
//	}
//
//	a.str[a.len] = '\0';
//	return a;
//
//
//}
//stringg stringg::operator+(const char*c)
//{
//	stringg a;
//	a.len = len + strlen(c);
//	a.str = new char[a.len + 1];
//	/*strcpy_s(a.str, len + 1, str);
//	strcpy_s(a.str+len, strlen(c) + 1, str);*/
//	for (int i = 0; i < len; i++)
//	{
//		a.str[i] = str[i];
//	}
//
//	for (int i = len; i < a.len; i++)
//	{
//		a.str[i] = c[i - len];
//	}
//
//	a.str[a.len] = '\0';
//	return a;
//
//}
//char &stringg::operator[](int i) { return str[i]; };
stringg operator+(const stringg&a, const stringg&b)
{
	   stringg c;
		c.len = a.len + b.len;
		c.str = new char[c.len + 1];
		strcpy_s(c.str, a.len + 1, a.str);
		strcpy_s(c.str+a.len, b.len + 1, b.str);
		//strcat_s(c.str, b.len , b.str);
		/*for (int i = 0; i < a.len; i++)
		{
			c.str[i] =a.str[i];
		}
	
		for (int i = a.len; i < c.len; i++)
		{
			c.str[i] = b[i - a.len];
		}*/
	
		c.str[c.len] = '\0';
		return c;
}
void stringg::stringlow()
{
	for (int i = 0; i < len; i++)
		str[i] = tolower(str[i]);

}
void stringg::stringup()
{
	for (int i = 0; i < len; i++)
		str[i] = toupper(str[i]);
	

}
int stringg::has(char c)
{
	int count = 0;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == c)
			count++;
	}
	return count;
}
bool stringg::operator==(const stringg&c)
{
	return strcmp(str, c.str) == 0;

}

ostream &operator<<(ostream&os, const stringg &c)
{
	os << c.str;
	return os;
 }
istream &operator>>(istream&is, stringg&c)
{
	char  temp[80];
	is.get(temp, 80);
	if (is)
		c = temp;//输入字符不为空时 
	while (is && is.get() != '\n')
		continue;
	return is;


 }
//stringg operator+(const char*p,  stringg&c)
//{
//	return stringg(p)+ c;
//
//}
stringg::stringg()
{
	len = 0;
	str = new char[1];
	str[0] = '\n';

}


stringg::~stringg()
{
	delete[]str;
}
#include<iostream>
#include "stringg.h"
using namespace std;
int main()
{
	stringg s1(" and i am a c++ student.");
	stringg s2 = "please enter your name:";
	stringg s3;
	cout << s2;
	cin >> s3;
	s2 = "my name is " + s3;
	cout << s2 << ".\n";
	s2 = s2 + s1;
	s2.stringup();
	cout << "the string\n" << s2 << "\ncontoins" << s2.has('A')
		<< "'a'characters in it.\n";
	s1 = "red";
	stringg rgb[3] = { stringg(s1),stringg("green"),stringg("blue") };
	cout << "enter the best color:";
	stringg ans;
	bool success = false;
	while (cin >> ans)
	{
		ans.stringlow();
		for (int i = 0; i < 3; i++)
		{
			if (ans == rgb[i])
			{
				cout << "righe\n";
				success = true;
				break;
			}

		}
		if (success)
			break;
		else
			cout << "try again\n";
	}
	cout << "bye";

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值