Visual C++ 2008入门经典 第八章深入理解类 练习题 简单

// 第八章深入理解类 练习题2012-11-04
//.cpp: 主项目文件。


//第一题
/*#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
class ECS
{
private:
	int value;
	bool boolecs;
public:
	ECS(int va=0, bool ecs = false):value(va), boolecs(ecs){
	
	}
	void print();

	//重载运算符+
	ECS operator+(const ECS& e) const
	{
	     return ECS(e.value + value, boolecs || e.boolecs);
	}
	//重载运算符+
	ECS operator+(const int& i) const
	{
	    return ECS(value+i, boolecs);
	}
};
void ECS::print()
{
	if(boolecs){
	   cout<<"E";
	}
	cout<<this->value;
}

//重载全局运算符++
ECS operator+(const int& a, const ECS& ecs)
{
    return ecs + a;
}




int main(array<System::String ^> ^args)
{
	//这里的a=3直接为3,是用的默认构造函数吗?
	ECS a = 3, c;
	ECS b(5,true);

	cout<<"a=";
	a.print();
	cout<<endl;

	cout<<"b=";
	b.print();
	cout<<endl;

	c = a + b;
	cout<<"a + b = ";
	c.print();
	cout<<endl;

	c = 7 + b;
	cout<<"7 + b = ";
	c.print();
	cout<<endl;

	c = a + 9;
	cout<<"a + 9 = ";
	c.print();
	cout<<endl;

	c = 8 + a + 9 + b;
	cout<<"8 + a + 9 + b = ";
	c.print();
	cout<<endl;
    
	system("pause");
	return 0;
}*/

//第二题
/*#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;

class IString
{
private:
	char* p;
	int length;
public:
	IString(const char* ch=0):p(0), length(0)
	{
		if(ch != 0){
		    length = strlen(ch);
			p = new char[length+1];
			strcpy_s(p, length+1, ch);	
		}
	}

	//复制构造函数
	IString(const IString& str)
	{
		//cout<<"str:"<<str<<endl;
		//妈的这里怎么判断是自己的呢
		//if(this === str){		
		//}
		//开始复制
		length = str.length;
		p = new char[length+1];
		strcpy_s(p,length +1, str.p);
	}

	//赋值运算符
	IString& operator=(const IString& str)
	{
	    length = str.length;
		delete p;
		p = new char[length+1];
		strcpy_s(p, length+1, str.p);
		return *this;
	}



	~IString(){
	   delete p;
	}
	
	void print()
	{
	    cout<<"p:"<<p<<" length:"<<length<<endl;
	}

};


int main(array<System::String ^> ^args)
{
	
	IString str("Hello");
	str.print();

    IString str1(str);
	str1.print();



	IString str2("Xlc");
	str2.print();

	IString str3;
	str3 = str2;
	str3.print();

    system("pause");
	return 0;
}*/


//第三题
/*#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;

class IString
{
private:
	char* p;
	int length;
public:
	IString(const char* ch=0):p(0), length(0)
	{
		if(ch != 0){
		    length = strlen(ch);
			p = new char[length+1];
			strcpy_s(p, length+1, ch);	
		}
	}

	IString(const char ch, int len=1):p(0), length(0)
	{
	    length = len;
		if(length > 1){
		     p = new char[length+1];
			 memset(p, ch, length);
			 p[length] = '\0';
		}	
	}

	//另外一个构造函数
	IString(int i):p(0),length(0)
	{
	     char sTmp[20];
		 //_itoa_s(i,buffer,2,10);//10表示10进制2表示字符长度buffer表示存放结果
		 _itoa_s(i, sTmp, 20, 10);

		 cout<<"sTmp:"<<sTmp<<endl;

		 length = strlen(sTmp);
		 if(length > 0){
		     p = new char[length+1];
			 strcpy_s(p, length+1, sTmp);
		 }
	}


	//复制构造函数
	IString(const IString& str)
	{
		//cout<<"str:"<<str<<endl;
		//妈的这里怎么判断是自己的呢
		//if(this === str){		
		//}
		//开始复制
		length = str.length;
		p = new char[length+1];
		strcpy_s(p,length +1, str.p);
	}




	//赋值运算符
	IString& operator=(const IString& str)
	{
	    length = str.length;
		delete p;
		p = new char[length+1];
		strcpy_s(p, length+1, str.p);
		return *this;
	}
    ~IString(){
	   delete p;
	}
	
	void print()
	{
	    cout<<"p:"<<p<<" length:"<<length<<endl;
	}

};


int main(array<System::String ^> ^args)
{
	
	IString str("Hello");
	str.print();
    IString str1(str);
	str1.print();
	IString str2("Xlc");
	str2.print();
	IString str3;
	str3 = str2;
	str3.print();

	IString str4('V', 10);
	str4.print();

	IString str5(5555);
	str5.print();



    system("pause");
	return 0;
}
*/

//第四题
//我感觉不行,刚才也想到了,但没有用this==*str成功
/*#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;

class IString
{
private:
	char* p;
	int length;
public:
	IString(const char* ch=0):p(0), length(0)
	{
		if(ch != 0){
		    length = strlen(ch);
			p = new char[length+1];
			strcpy_s(p, length+1, ch);	
		}
	}
	IString(const char ch, int len=1):p(0), length(0)
	{
	    length = len;
		if(length > 1){
		     p = new char[length+1];
			 memset(p, ch, length);
			 p[length] = '\0';
		}	
	}
	//另外一个构造函数
	IString(int i):p(0),length(0)
	{
	     char sTmp[20];
		 //_itoa_s(i,buffer,2,10);//10表示10进制2表示字符长度buffer表示存放结果
		 _itoa_s(i, sTmp, 20, 10);

		 cout<<"sTmp:"<<sTmp<<endl;

		 length = strlen(sTmp);
		 if(length > 0){
		     p = new char[length+1];
			 strcpy_s(p, length+1, sTmp);
		 }
	}
	//复制构造函数
	IString(const IString& str)
	{
		//cout<<"str:"<<str<<endl;
		//妈的这里怎么判断是自己的呢
		//if(this === str){		
		//}
		//开始复制
		length = str.length;
		p = new char[length+1];
		strcpy_s(p,length +1, str.p);
	}
	//赋值运算符
	IString& operator=(const IString& str)
	{
		//if(*this != str){ //这个刚才都没进去,*this是指当前对像的值了,而str只是一个IString&的引用
		if(this != &str){ //这里应该用this当前的地址不等于str的地址才对,&str取地址
			length = str.length;
			delete p;
			p = new char[length+1];
			strcpy_s(p, length+1, str.p);
		}
		return *this;		
	}
    ~IString(){
	   delete p;
	}	
	void print()
	{
	    cout<<"p:"<<p<<" length:"<<length<<endl;
	}
};


int main(array<System::String ^> ^args)
{
	
	IString str("Hello");
	
    str = str;
	str.print();


    system("pause");
	return 0;
}*/


//第五题
/*
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;

class IString
{
private:
	char* p;
	int length;
public:
	IString(const char* ch=0):p(0), length(0)
	{
		if(ch != 0){
		    length = strlen(ch);
			p = new char[length+1];
			strcpy_s(p, length+1, ch);	
		}
	}
	IString(const char ch, int len=1):p(0), length(0)
	{
	    length = len;
		if(length > 1){
		     p = new char[length+1];
			 memset(p, ch, length);
			 p[length] = '\0';
		}	
	}
	//另外一个构造函数
	IString(int i):p(0),length(0)
	{
	     char sTmp[20];
		 //_itoa_s(i,buffer,2,10);//10表示10进制2表示字符长度buffer表示存放结果
		 _itoa_s(i, sTmp, 20, 10);

		 cout<<"sTmp:"<<sTmp<<endl;

		 length = strlen(sTmp);
		 if(length > 0){
		     p = new char[length+1];
			 strcpy_s(p, length+1, sTmp);
		 }
	}
	//复制构造函数
	IString(const IString& str)
	{
		//cout<<"str:"<<str<<endl;
		//妈的这里怎么判断是自己的呢
		//if(this === str){		
		//}
		//开始复制
		length = str.length;
		p = new char[length+1];
		strcpy_s(p,length +1, str.p);
	}
	//赋值运算符
	IString& operator=(const IString& str)
	{
		//if(*this != str){ //这个刚才都没进去,*this是指当前对像的值了,而str只是一个IString&的引用
		if(this != &str){ //这里应该用this当前的地址不等于str的地址才对,&str取地址
			length = str.length;
			delete p;
			p = new char[length+1];
			strcpy_s(p, length+1, str.p);
		}
		return *this;		
	}

	//重载operator+()运算符
	IString operator+(const char* ch)
	{
		 //int len = strlen(ch);
		 //重新设置长度
		 //int _len = length+len; //两个字符串合并以后的长度
		 //char* temp = new char[_len]; //重新定义字符串指针
		 //当两个字符串合并到一起去
		 //int i=0;
		 //for(i=0; i<length; i++){
		 //    temp[i] = p[i];
		 //}
		 //for(int j=0; i<_len; j++, i++){
		 //     temp[i] = ch[j];
		 //}
		 //temp[_len] = '\0'; //合并成功
		 //cout<<"temp:"<<temp<<endl;
		 //return IString(temp);
		 
		 //上面是自己写的
		 //下面是答案上给的	     
		 return *this + IString(ch);
	}

	IString operator+(const IString& s)
	{
	      size_t _length = length + s.length + 1;
		  char* temp = new char[_length];
		  strcpy_s(temp, _length, p);   //把p复制到temp里面去
		  //cout<<"temp:"<<temp<<endl;
		  //strcpy_s(temp, _length, s.p); //这个也在智能了吧,直接把两个这么合并起来了,还知道第一次的p被加到了什么地方为止
		  //弄不懂这里样
		  //哥我错了,这里用的是strcat_s
		  strcat_s(temp,_length, s.p);
		  //cout<<"temp:"<<temp<<endl;
		  return IString(temp);
	}

	//重载+=
	IString& operator+=(const IString& str)
	{
	     *this = *this + str; //用当前对像+str,因为上面已经重新了该+运算符
		 return *this; //直接返回
	}


    ~IString(){
	   delete p;
	}	
	void print()
	{
	    cout<<"p:"<<p<<" length:"<<length<<endl;
	}
};


int main(array<System::String ^> ^args)
{
	
	IString str("Hello");

	IString str1 = str + " C++";
	str1.print();


	IString str2(" PHPchina");
	str += str2;
	str.print();
    system("pause");
	return 0;
}*/

//第六题
/*
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
class CStack
{
public:
	CStack(int n=10); //构造函数
	~CStack();        //析造函数
	void push(int i); //追加数据
	int pop();
	void print();
private:
	int* pList;       //定义一个指针
	int size;         //指针长度
	int next;         //当前的索引值
};

//创建一个长度为n的int指定,保存到pList中去
CStack::CStack(int n):size(n),next(0)
{
	pList = new int[size];
}
CStack::~CStack()
{
    delete []pList; //清空pList指针
}


void CStack::push(int i)
{
	//当下一条指针小于99时,就还可以追加上去
	//next是指当前的索引值
	if(next < 99){
	    pList[next++] = i;
	}
}
//返回最后一条值
int CStack::pop()
{
    //cout<<"next:"<<next<<endl;
	return pList[--next];
	//这里的返回并没有把该地址去掉,而只是在next长度中减一了,不能读出来了
}

void CStack::print()
{
     cout<<'[';
	 //这里从最后开始循环到最前面
	 for(int i=next-1; i>=0; i--){
	       //cout<<"i="<<i<<' '<<pList[i];
		  cout<<' '<<pList[i];
	 }
	 cout<<"]"<<endl;
}


int main(array<System::String ^> ^args)
{

	CStack s(20);
	s.push(5);
	s.push(10);
	s.push(8);
	s.print();
	cout<<"最上面的数值为:"<<s.pop()<<endl;
	s.print();
    system("pause");
	return 0;
}*/


//第七题
/*#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
//ref 引用类
ref class Box
{
public:
	//默认构造函数
	Box()
	{
	     Box(1.0,1.0,1.0);
	}
	//带三个参数的构造函数
	Box(double lv, double wv, double hv)
	{
	     lv = lv <= 0.0 ? 1.0 : lv;
		 wv = wv <= 0.0 ? 1.0 : wv;
		 hv = hv <= 0.0 ? 1.0 : hv;

		 length = lv > wv ? lv : wv;
		 width = wv < lv ? wv : lv;
		 height = hv;
	}
	//计算面积
	double Volume()
	{
	    return length * width * height;
	}
	//属性(property)是很多时髦的面向对象编程语言提供的一种特性。通过属性,程序员可以如同访问成员数据一样访问一组
	property double Length	
	{
		double get(){ return length;}    
	}
	property double Width{
		double get(){ return width;}
	}
	property double Height{
		double get(){ return height;}
	}

	//重载运算符operator+
	Box^ operator+(Box^ box){
	    //返回一个新的Box引用类
		return gcnew Box(length > box->length ? length : box->length,
			             width > box->width ? width : box->width,
						 height + box->height);
	}

	//重载运算符operator/
	int operator/(Box^ box)
	{
	     int tc1 = 0;
		 int tc2 = 0;
		 //第一个的宽度和高度除以box的高宽,看能装下几个
		 tc1 = safe_cast<int>(length / box->length) * safe_cast<int>(width / box->width);

		 //第当前对像的高度除以box的宽度,用当前对像的宽度除以box的高度,看能装几个
		 tc2 = safe_cast<int>(length / box->width) * safe_cast<int>(width/box->length);
         //Console::WriteLine("tc1:{0} tc1:{1}", tc1, tc2);

		 //返回数量
		 return static_cast<int>(height/box->height) * (tc1 > tc2 ? tc1 : tc2);
	}

	//重载运算符operator*();
	Box^ operator*(int n)
	{
		if(n%2 == 0){ //如果是能被2整除的数
			//宽*2 然后高度加n/2
			return gcnew Box(length, 2.0*width, (n/2)*height);
		}else{
		    return gcnew Box(length, width, n*height);
		}
	}

	//重载%运算符
	double operator%(Box^ box)
	{
		 //先用this/box看当前this能装下几个box,然后来*每个box的面积
		 //用当前的面积减去
	     return Volume() - (this/box)*box->Volume(); 
	}

	//重载运算符>=
	bool operator>=(double value)
	{
	     return value >= this;
	}

	//重载<=
	bool operator<=(double value)
	{
	     return value >= this;     
	}

	//重载 > 
	static bool operator>(double value, Box^ box)
	{
	     return value > box->Volume();
	}
	static bool operator<(double value, Box^ box)
	{
	    return value < box->Volume();
	}

	static bool operator>(Box^ box, double value)
	{
	    return value < box->Volume();
	}
	static bool operator<(Box^ box, double value)
	{
	    return value > box->Volume();
	}

	static bool operator>=(double value, Box^ box)
	{
	    return value >= box->Volume();
	}
	static bool operator<=(double value, Box^ box)
	{
	    return value <= box->Volume();
	}

	static bool operator==(double value, Box^ box)
	{
	    return value == box->Volume();
	}
	static bool operator==(Box^ box, double value)
	{
	    return box->Volume() == value;
	}

	static Box^ operator*(int n, Box^ box)
	{
	    return box*n;
	}
private:
	double length;
	double width;
	double height;
};





int main(array<System::String ^> ^args)
{
	Box^ candy = gcnew Box(1.5, 1.0, 1.0);//一块糖的大小
	Box^ candyBox = gcnew Box(7.0, 4.5, 2.0); //一个小包装的大小
	Box^ carton = gcnew Box(30.0, 18.0, 18.0);//一个大礼盒的包装大小

	int numCandies = candyBox / candy; //一个小包装育装多少块	 
	int numCboxes = carton / candyBox;  //一个大礼盒能装多少小包装
	double space = carton % candyBox;   //还余下多少空间

	Console::WriteLine("candyBox能装 {0} 块糖果", numCandies);
	Console::WriteLine("carton能装 {0} candyBox包糖果", numCboxes);
	Console::WriteLine("carton能装 {0} candyBox包糖果后,还余下的空间为{1}", numCboxes, space);

	//长度
	for(double length=3.0; length<=7.5; length+=0.5){
		//宽度
		for(double width = 3.0; width<=5.0; width+=0.5){
		    //高度
			for(double height=1.0; height<= 2.5; height+=0.5){
			       Box^ tryBox = gcnew Box(length, width, height);
				   //装满以事余下的空间要小于一盒tryBox的空间
				   //要能正好装盒candy的数量
				   //并且装装的糖果块数要大于30块
				   if(carton % tryBox < tryBox->Volume() && tryBox % candy == 0.0 && tryBox/candy >= 30)
				   {
					   Console::WriteLine(L"\n\nTrial Box L={0} W={1} H={2}", tryBox->Length, tryBox->Width, tryBox->Height);
					   Console::WriteLine(L"一共能装 {0} 块糖果",tryBox/candy);
					   Console::WriteLine(L"一大礼箱能装 {0} 多少盒", carton/tryBox);
				   }
			}
		}
	}
    system("pause");
	return 0;
}*/


//第八题:
/*
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
	const int MaxLength = 100;
	//开始处理输
	string str;	
	cout<<"请输入字符串吧(*结束):"<<endl;
	getline(cin,str, '*');
	cout<<"str:"<<str<<endl;


	
	//首先处理一下用户需要替换的单词
	string repString;
	cout<<"请输入你人替换的单词(*结束):"<<endl;
	getline(cin, repString, '*');
	cout<<"repString:"<<repString<<endl;

	//或者放入一个字符串数组中去
	size_t start = 0;
	size_t end = 0;
	size_t count = 0;
	string keyStrings[MaxLength];
    //关键词的数成已经完成
	while(true){
	     start = repString.find_first_not_of(' ', start);
		 if(start == string::npos){
		     break;
		 }
		 end = repString.find_first_of(' ', start);
		 if(end == string::npos){
		     end = repString.length();
		 }
		 if(count > MaxLength){
			 cout<<"超出了字符串数组的长度."<<endl;
		     break;
		 }
		 string s = repString.substr(start, end-start);
		 keyStrings[count++] = s;
		 start = end;
		 end = string::npos;
	}
	//开始查换和替换str字符串中出现的keyStrings的值
	for(size_t i=0; i<count; i++){
		 size_t _start = 0;
		 size_t _end=0;
	     cout<<keyStrings[i]<<endl;
		 while(true){
			 //没有理解这个函数哦
			 //find_first_of是当keyStrings[i]中出现的任意字符,就做数了,而不是字符串
			 _start = str.find_first_of(keyStrings[i], _start);
			 if(_start == string::npos){
			    break;
			 }
			 cout<<"_start:"<<_start<<endl;
			 _end = str.find_first_of(' ', _start);
			 if(_end == string::npos){
			    _end = str.length();
			 }
			 cout<<"开始位置是_start:"<<_start<<" _end:"<<_end<<endl;
			 string s = str.substr(_start, _end-_start);
			 cout<<"s:"<<s<<endl;
			 cout<<endl;
			 _start = _end;
			 _end = string::npos;
		 }
	}
    system("pause");
	return 0;
}
////////////////失败	
*/

//第八题答案上这么写的,练习一下
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
	string text; //写义字符串text
	const size_t maxwords(100); //最大的字符长度100
	string words[maxwords];     //定义一个长度为100的字符串数组
	size_t wordcount(0);        //当前字符串的长度

	cout<<"输入要搜索的文本以#号结束"<<endl;
	getline(cin,text, '#'); //将输入的字符串保存到text中
	string textcopy(text);  //声明一个textcopy中然后把text初始给他

	//转换为小字
	for(size_t t = 0; t<textcopy.length(); t++)
	{
	     textcopy[t] = tolower(textcopy[t]);
	}

	while(true)
	{
	    cout<<"输入一个词,是要被替换的字符串或输入#号结束:"<<endl;
		cin>>words[wordcount]; //妈的保存到了wrods中去了
		if(words[wordcount] == "#"){ //如果为#退出
		   break;
		}
		//如果输入的长度已经大于了数组的容量,直接退出
		if(++wordcount == maxwords){
		   cout<<"已经超出了字符串数组的最大值"<<endl;
		   break;
		}
	}


	//开始循环查找所输入的需要替换的值了
	for(size_t i = 0; i<wordcount; i++)
	{
	     string wordcopy = words[i];
		 //对当前词转换为小写
		 for(size_t j=0; j<wordcopy.length(); j++){
		      wordcopy[i] = tolower(wordcopy[i]);
		 }

		 //应该是创建一个以wordcopy为长度的字符串,值为*
		 string asterisks(wordcopy.length(), '*');
		 cout<<"Each occurrence of: "<<words[i]<<"  will be replaced by  "<<asterisks<<" . "<<endl;

		 size_t position = 0;
		 //循环条件为在textcopy中查找wordcopy的位置,从position开始,直接为string::npos为止
		 while((position = textcopy.find(wordcopy, position)) != string::npos)
		 {
			 cout<<"position:"<<position<<endl;
			 //如果为位置为0时
			 if(position == 0){
				 //当当前的关键词长度与 text的长度一样时
				 if(position+wordcopy.length() == text.length())
				 {
					  text.replace(position, wordcopy.length(), asterisks); //替换
				 
				 //功能:如果参数是字母字符,函数返回非零值,否则返回零值。
				 //如果该值不为字母 检查过去的这个词不是字母
				 }else if(!isalpha(textcopy[position+wordcopy.length()])){
				      text.replace(position, wordcopy.length(), asterisks);
				 }  

			 //判断textcopy[position-1]的值是否为字母,如果不是字母执行
			 }else if(!isalpha(textcopy[position-1]))
			 {
				 if(position + wordcopy.length() == text.length())
				 {
				     text.replace(position, wordcopy.length(), asterisks);
				 }else if(!isalpha(textcopy[position+wordcopy.length()])){
				     text.replace(position, wordcopy.length(), asterisks);
				 }			 
			 }
			 position += wordcopy.length();
			 //然后移动position的长度
		 }
		 cout<<"处理后的原始字符串是现在:"<<text<<endl;
	}	

    system("pause");
	return 0;
}

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2753552.html