C++ primer plus第六版课后编程题答案12.2


 

string2.cpp

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
static int num_string;//不是在h头文件中只能在这里
static const int CINLIM=80;//在头文件中只能定义const常量
class String{
private:
	//char *str;
	string str;//改为string吧
	int len;
public:
	String(){
		len=4;
		//str=new char[1];
		str='\0';
		num_string++;
	}
	String(string s)
	{
		//len=strlen(s);
		//str=new char[len+1];
		//strcpy(str,s);
		len=s.length();
		str=s;
		num_string++;
	}
	String(const String &st)
	{
		len=st.len;//st.len()???额,忘了这个是一个String类对象,还是自己定义的..
		//str=new char[len+1];
		//strcpy(str,st.str);
		str=st.str;
		num_string++;
	}
	~String()
	{
		--num_string;
		//delete []str;
	}
	int length()const
	{
		return len;
	}
	String &operator=(const String &st)
	{
		if(this==&st)//this的应用
			return *this;//不能写成return this,*this才是对象,this只是一个引用
		//delete []str;//重新分配大小
		int len=st.len;
		//str=new char[len+1];
		//strcpy(str,st.str);
		//num_string++;
		str=st.str;
		return *this;
	}
	String &operator=(const string s)//const char *s)
	{
		//delete []str;
		int len=s.length();
		//str=new char[len+1];
		str=s;
		return *this;
	}
	char &operator[](int i)
	{
		return str[i];
	}
	const char &operator[](int i)const
	{
		return str[i];
	}

	//a,重载+
	String operator+(const String &st)
	{

		//int lenS=len+st.len;//新的str的大小
		//char *strS=new char[len+1];//存放加起来的字符串
		//strcpy(strS,str);
		//for(int i=len,j=0;i<lenS;i++,j++)
			//strS[i]=st.str[j];//暂时想不到其他更好的,只能这样了//j=0开始
		string strS=str+st.str;
		return String(strS);
	}
	//overload +char
	String operator+(const string s)//const char *s)
	{
		//int lenS=len+strlen(s);
		//char *strS=new char[len+1];
		//strcpy(strS,str);
		//delete []str;
		//str=new char[lenS+1];
		//strcpy(str,strS);
		//for(int i=len;i<lenS;i++)
			//str[i]=s[i];
		string strS=str+" "+s;
		return String(str);
	}
	//overload +=
	String operator+=(const String &st)
	{
		*this=*this+st;
		return *this;
	}


	//ShowLow
	void ShowLow()
	{
		for(int i=0;i<len;i++)
			str[i]=tolower(str[i]);
	}
	void ShowUp()
	{
	for(int i=0;i<len;i++)
			str[i]=toupper(str[i]);
	}
	//char return  次数
	int num(char ch)
	{
		int count=0;
		for(int i=0;i<len;i++)
			if(str[i]==ch)
				count++;
		return count;
	}
	//重载char +String
	friend String operator+(const string s,String &st)
	{
		/*
		int lenS=st.len+strlen(s);//strlen()不能识别空格键//我用string类吧
		char *strS=new char[lenS+1];
		strcpy(strS,s);
		for(int i=strlen(s)-1,j=0;i<lenS;i++,j++)
			strS[i]=st.str[j];
			*/
		string strS=s+" "+st.str;
		return String(strS);


	}
	
	friend bool operator<(const String &st,const String &st2)
	{
		/*
		int len1=st.len;
		int len2=st2.len;
		int LEN=len1>len2?len1:len2;
		int i;
		for(i=0;i<LEN;i++)
		{
			if(st.str[i]>st2.str[i])
				return true;
		}
		if(i==LEN)
			return true;
		return false;
		*///忘了内置的<
		return st.str<st2.str;
	}
	friend bool operator>(const String &st,const String &st2)
	{
		return st2<st;
	}
	friend bool operator==(const String &st,const String &st2)
	{
		return st.str==st2.str;
	}
	

	friend ostream &operator<<(ostream &os,const String &st)
	{
		os<<st.str<<endl;
		return os;
	}
	friend istream &operator>>(istream &is,String &st)
	{
		char temp[CINLIM];//???
		is.get(temp,CINLIM);
		if(is)
			st=temp;
		while(is&&is.get()!='\n')
			continue;
		return is;
	}
};

main122.cpp

#include <iostream>
#include "string2.cpp"
using namespace std;
void main122()
{
	{
	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<<s3<<endl;
	//cout<<s2<<endl;
	//cout<<s1;
	s2=s2+s1;
	//cout<<s2;
	//cout<<"The string \n"<<s2<<"\ncontains "<<s2.num('a')
		//<<" 'A' characters in it. "<<endl;
	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.ShowLow();
		for(int i=0;i<3;i++)
		{
			if(ans==rgb[i])
			{
				cout<<"That's right!"<<endl;
				success=true;
				break;
			}
		
		}
		if(success)
			break;
		else
			cout<<"Try again!"<<endl;
	}
	cout<<"Bye "<<endl;

	}
	cin.get();




}

有点困,全世界晚安

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注