C++ Primer第五版 第十二章编程练习节选(动态内存、类)

//cow.h
#ifndef COW_H_
#define COW_H_

class Cow
{
private:
	char name[20];
	char *hobby;
	double weight;
public:
	Cow();
	Cow(const char *nm, const char *ho, double wt);
	Cow(const Cow &c);
	~Cow();
	Cow & operator=(const Cow &c);
	void showCow()const;
};
#endif

//cow.cpp
#include<iostream>
#include<cstring>
#include"cow.h"

Cow::Cow()
{
	strcpy(name, "default");
	hobby = new char[1];
	hobby[0] = '\0';
	weight = 0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{
	strcpy(name, nm);
	hobby = new char[strlen(ho) + 1];
	strcpy(hobby, ho);
	weight = wt;
}

Cow::Cow(const Cow &c)
{
	strcpy(name, c.name);
	hobby = new char[strlen(c.hobby) + 1];
	strcpy(hobby, c.hobby);
	weight = c.weight;
}

Cow::~Cow()
{
	delete[] hobby;
}

Cow & Cow::operator=(const Cow &c)
{
	if(this == &c)
	{
		return *this;
	}
	delete[] hobby;
	strcpy(name, c.name);
	hobby = new char(strlen(c.hobby) + 1);
	strcpy(hobby, c.hobby);
	weight = c.weight;
	return *this;
}

void Cow::showCow()const
{
	std::cout << "name : " << name << "	hobby : " << hobby << "	weight : " << weight << std::endl;
}

//main.cpp
#include<iostream>
#include"cow.h"

using namespace std;

int main()
{
	Cow c1;
	c1.showCow();
	
	Cow c2("wowo", "swimming", 20);
	c2.showCow();
	
	c1 = c2;
	c1.showCow();
	
	return 0;
}

//strg.h
#include<iostream>

#ifndef STRG_H_
#define STRG_H_
class String
{
private:
	char *str;
	int len;
	static const int CLIMIT = 80;
public:
	String();
	String(const String &s);
	String(const char *c);
	~String();
	
	int has(char c)const;
	void stringlow();
	void stringup();
	
	String operator+(const String &s)const;
	String & operator=(const char *c);
	String & operator=(const String &s);
	
	friend bool operator<(const String &s1, const String &s2);
	friend bool operator>(const String &s1, const String &s2);
	friend bool operator==(const String &s1, const String &s2);
	friend String operator+(const char *c, const String &s);
	friend std::ostream & operator<<(std::ostream &os, const String &s);
	friend std::istream & operator>>(std::istream &is, String &s);
};
#endif


//strg.cpp
#include<cstring>
#include"strg.h"

using namespace std;

String::String()
{
	len = 0;
	str = new char[1];
	str[0] = '\0';
}

String::String(const String &s)
{
	len = strlen(s.str);
	str = new char[len + 1];
	strcpy(str, s.str);
}

String::String(const char *c)
{
	len = strlen(c);
	str = new char[len + 1];
	strcpy(str, c);
}

String::~String()
{
	delete[] str;
}

int String::has(char c)const
{
	int num = 0;
	for(int i = 0; i < len; i++)
	{
		if(str[i] == c)
		{
			num++;
		}
	}
	return num;
}

void String::stringlow()
{
	for(int i = 0; i < len; i++)
	{
		str[i] = tolower(str[i]);
	}
}

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

String String::operator+(const String &s)const
{
	String temp;
	temp.len = len + s.len;
	delete[] temp.str;
	temp.str = new char[temp.len + 1];
	strcpy(temp.str, str);
	strcat(temp.str, s.str);
	return temp;
}

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

String & String::operator=(const String &s)
{
	delete[] str;
	len = strlen(s.str);
	str = new char[len + 1];
	strcpy(str, s.str);
	return *this;
}

bool operator<(const String &s1, const String &s2)
{
	return (strcmp(s1.str, s2.str) < 0);
}

bool operator>(const String &s1, const String &s2)
{
	return (s2.str < s1.str);
}

bool operator==(const String &s1, const String &s2)
{
	return (strcmp(s1.str, s2.str) == 0);
}

String operator+(const char *c, const String &s)
{
	String temp;
	delete[] temp.str;
	temp.len = strlen(c) + strlen(s.str);
	temp.str = new char[temp.len + 1];
	strcpy(temp.str, c);
	strcat(temp.str, s.str);
	return temp;
}

ostream & operator<<(ostream &os, const String &s)
{
	os << s.str;
	return os;
}

istream & operator>>(istream &is, String &s)
{
	char temp[String::CLIMIT];
	is.get(temp, String::CLIMIT);
	if(is)
	{
		s = temp;
	}
	while(is && (is.get()!= '\n'))
	{
		continue;
	}
	return is;
}

//main.cpp
#include<iostream>
#include"strg.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;
	s2 = s2 + s1;
	cout << s2 << endl;
	s2.stringup();
	cout << "The string : " << s2 << " contains " << s2.has('A') << " 'A' characters in it . \n";
	s1 = "red";
	String rgb[3] = {String(s1), String("green"), String("bule")};
	cout << "Enter the 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 << "Congrtuation , you are so smart !" << endl;
				success  = true;
				break;
			}
		}
		if(success)
		{
			break;
		}
		else
		{
			cout << "Try again ! \n" ;
		}
		cout << success << endl;
	}
	cout << "Bye ! \n";
	return 0;
}

 

//stack.h
#include<iostream>

using std::ostream;
using std::istream;

typedef unsigned long Item;

#ifndef STACK_H_
#define STACK_H_
class Stack
{
private:
	enum{MAX = 10};
	Item *pitems;
	int size;
	int top;
public:
	Stack(int n = 10);
	Stack(const Stack &st);
	~Stack();
	bool isempty()const;
	bool isfull()const;
	bool push(const Item &it);
	bool pop(Item &it);
	Stack & operator=(const Stack &st);
	friend ostream & operator<<(ostream &os, const Stack &st);
};
#endif

//stack.cpp
#include<iostream>
#include<cstring>
#include"stack.h"

using namespace std;

Stack::Stack(int n)
{
	if(n > MAX)
	{
		cout << "The maxmum numbers of this stack is " << MAX ;
		cout << " and we will set as default n = " << MAX << endl;
		n = MAX;
	}
	pitems = new Item[n];
	size = 0;
	top = 0;
}

Stack::Stack(const Stack &st)
{
	pitems = new Item[st.size];
	top = 0;
	size = 0;
	for(int i = 0; i < st.size; i++)
	{
		pitems[i] = st.pitems[i];
		top++;
		size++;
	}
}

Stack::~Stack()
{
	delete[] pitems;
}

bool Stack::isempty()const
{
	return top == 0;
}

bool Stack::isfull()const
{
	return top == MAX;
}

bool Stack::push(const Item &it)
{
	if(isfull())
	{
		cout << "THe stack is alread full !" << endl;
		return false;
	}
	pitems[top++] = it;
	size++;
	return true;
}

bool Stack::pop(Item &it)
{
	if(isempty())
	{
		cout << "The stack is alread empty !" << endl;
		return false;
	}
	it = pitems[--top];
	size--;
	return true;
}

Stack & Stack::operator=(const Stack &st)
{
	delete[] pitems;
	pitems = new Item[st.size];
	size = 0;
	top = 0;
	for(int i = 0; i < st.size; i++)
	{
		pitems[i] = st.pitems[i];
		size++;
		top++;
	}	
	return *this;
}

ostream & operator<<(ostream &os, const Stack &st)
{
	for(int i = 0; i < st.top; i++)
	{
		os << st.pitems[i] << " ";
	}
	return os;
}

//main.cpp
#include<iostream>
#include<cstring>
#include"stack.h"

using namespace std;

int main()
{
	Stack st;
	Item *it = new Item[11];
	
	it[0] = 1;
	it[1] = 1;
	for(int i = 2; i < 11; i++)
	{
		it[i] = it[i-1] + it[i-2];
	}
	
	for(int i = 0; i < 11; i++)
	{
		cout << it[i] << " ";
	}
	cout << endl;
	
	for(int i = 0 ; i < 11; i++)
	{
		st.push(it[i]);
	} 
	cout << st << endl;
	
	cout << "-----Stack(const Stack &st)-----" << endl;
	Stack st1(st);
	cout << st1 << endl;
	for(int i = 0; i < 11; i++)
	{
		st1.pop(it[i]);
	}
	
	for(int i = 0; i < 11; i++)
	{
		cout << it[i] << " ";
	}
	
	cout << endl;
	
	cout << "-----Stack & operator=(const Stack &st)-----" << endl;
	Stack st2 = st;
	cout << st2 << endl;

	delete[] it;	
	return 0;
}

practice makes perfect!

猜你喜欢

转载自blog.csdn.net/qq_37172182/article/details/84788985