C++程序设计教程 第3版——习题十三4-6

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、问题

请添加图片描述
请添加图片描述
请添加图片描述

二、代码

4-5

代码如下:

4.#include<iostream>
#include<cstring>
using namespace std;
class String
{
	char*str;
	int len;
public:
	String(const char*s=NULL)
	{
		if(s)
		{
			len=strlen(s);
			str=new char[len+1];
			strcpy(str,s);
		}
		else
		{
			str=NULL;
			len=0;
		}
	}
	String(const String &s)
	{
		len=s.len;
		if(s.str)
		{
			str=new char[len+1];
			strcpy(str,s.str);
		}
		else str=NULL;
	}
	~String()
	{
		if(str) delete [] str;
	}
	void set(char*s)
	{
    	len=strlen(s);
		if(s)
		{
			delete [] str;
			str=new char[len+1];
			strcpy(str,s);
		}
		else
		{
			delete [] str;
			str=NULL;
		}
	}
	void show()
	{
		if(str) cout<<str<<endl;
		else cout<<"该字符串为空。"<<endl;
	}
	int getlen()
	{
		return len;
	}
	void delchar(char c)
	{
		int i,j;
		char*t=new char[len+1];
		for(i=j=0;i<len;i++)
		{
			if(str[i]!=c)
			{
				t[j]=str[i];
				j++;
			}
		}
		t[j]='\0';
		len=strlen(t);
		set(t);
		delete [] t;
	}
	String operator-(char ch)
	{
		delchar(ch);
		return *this;
	}
	String&operator=(String&s)
	{
		set(s.str);
		return*this;
	}
	String&operator+=(String&s)
	{
		len+=s.len;
		char*t=new char[len+1];
		for(int i=0;str[i]!='\0';i++)
		{
			t[i]=str[i];
		}
		t[i]='\0';
		strcat(t,s.str);
		set(t);
		delete [] t;
		return *this;
	}
	bool operator==(String&s)
	{
		int i=0;
		while(i<len)
			if(str[i++]!=s.str[i++]) return false;
		return true;
	}
	friend String operator+(String&,String&);
};
String operator+(String&s1,String&s2)
{
	String t;
	t.len=s1.len+s2.len;
	t.str=new char[t.len+1];
	strcpy(t.str,s1.str);
	strcat(t.str,s2.str);
	return t;
}
int main()
{
	String s1("学生学习"),s2("C++程序设计");
	String s3(s2),s4("aaabaaa"),s5;
	s3.show();
	s3.set("pppa");
	s3.show();
	s4.show();
	cout<<"s4长度:"<<s4.getlen()<<endl;
	s4.delchar('a');
	s4.show();
	s4=s4-'b';
	cout<<s4.getlen()<<endl;
	s5=s1;
	s5.show();
	s1+=s2;
	s1.show();
	if(s1==s3) cout<<"相等"<<endl;
	else cout<<"不等"<<endl;
	s4=s1;
	if(s4==s1) cout<<"相等"<<endl;
	else cout<<"不等"<<endl;
	s2=s1+s4;
	s2.show();
	return 0;
}
5.#include<iostream>
using namespace std;
class Matrix
{
	int*p,rows,cols;
public:
	Matrix(int r=0,int c=0)
	{
		rows=r;
		cols=c;
		if(r*c) input();
		else p=NULL;
	}
	Matrix(Matrix&b)
	{
		int i=0;
		rows=b.rows;
		cols=b.cols;
		p=new int[rows*cols+1];
		for(i=0;i<(rows*cols);i++)
		p[i]=*((b.p)+i);
		p[i]='\0';
	}
	void input()
	{
		int i=0;
		p=new int[rows*cols+1];
		for(i=0;i<rows*cols;i++)
			cin>>p[i];
		p[i]='\0';
	}
	Matrix operator+(Matrix&b)
	{
		for(int i=0;i<rows*cols;i++)
			p[i]+=b.p[i];
		return *this;
	}
	void operator=(Matrix&b)
	{
		int i=0;
		if(p) delete [] p;
		rows=b.rows;
		cols=b.cols;
		p=new int[rows*cols+1];
		for(;i<rows*cols;i++)
			p[i]=b.p[i];
		p[i]='\0';
	}
	void show()
	{
		for(int i=0;i<rows;i++)
		{
			for(int j=0;j<cols;j++)
			{
				cout<<p[i*cols+j]<<'\t';
			}
			cout<<endl;
		}
	}
	~Matrix()
	{
		delete [] p;
	}
};
int main()
{
	Matrix A(3,4),C;
	Matrix B(A);
	A.show();
	B.show();
	C=A+B;
	C.show();
	return 0;
}

6

代码如下:

#include<iostream>
using namespace std;
class Shape
{
public:
	virtual float Area(void)=0;
	virtual void SetData(float,float=0)=0;
};
class Triangle:public Shape
{
	float w,h;
public:
	Triangle(float ww=0,float hh=0){ w=ww;h=hh;}
	float Area(void){ return w*h/2;}
	void SetData(float ww,float hh=0){ w=ww;h=hh;}
};
class Rectangle:public Shape
{
	float w,h;
public:
	Rectangle(float ww=0,float hh=0){ w=ww;h=hh;}
	float Area(void){ return w*h;}
	void SetData(float ww,float hh=0){ w=ww;h=hh;}
};
class Squre:public Shape
{
	float s;
public:
	Squre(float ww=0){ s=ww;}
	float Area(void){ return s*s;}
	void SetData(float ww,float hh=0){ s=ww;}
};
class Circle:public Shape
{
	float r;
public:
	Circle(float ww=0){ r=ww;}
	float Area(void){ return r*r*3.14;}
	void SetData(float ww,float hh=0){ r=ww;}
};
float CalcArea(Shape*s)
{
	return (s->Area());
}
int main()
{
	Triangle A(3,4);
	Rectangle B(4,5);
	Squre C(7);
	Circle D(1);
	cout<<CalcArea(&A)<<'\t'<<CalcArea(&B)<<'\t'<<CalcArea(&C)<<'\t'<<CalcArea(&D)<<endl;
	return 0;
}

总结

如有帮助,还望点赞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值