C++ primer plus 第十二章编程练习

1、

/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/03
From:C++ primer plus 第十二章编程练习 第1题
*********************************************************************************************/

/***********************************cow.h***************************************************/
#ifndef COW_H_
#define COW_H_
#include<iostream>

using namespace std;
class Cow
{
	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"stdafx.h"
#include"cow.h"


Cow::Cow()
{
	strcpy_s(name, "fuckfuck");
	hobby = new char[10];
	strcpy_s(hobby,10, "eat");
	weight = 0.0;
}
Cow::Cow(const char * nm, const char * ho, double wt)
{
	strcpy_s(name, nm);
	hobby = new char[strlen(ho) + 1];
	strcpy_s(hobby, strlen(ho) + 1, ho);
	weight = wt;
}
Cow::Cow(const Cow &c)
{
	strcpy_s(name, c.name);
	hobby = new char[strlen(c.hobby) + 1];
	strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);
	weight = c.weight;
}

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

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

void Cow::ShowCow() const
{
	cout << "name::" << name << endl;
	cout << "hobby::" << hobby << endl;
	cout << "weight::" << weight << endl;
}
/***********************************main.cpp***************************************************/
#include"stdafx.h"
#include"cow.h"
int main()
{
	Cow c1;
	c1.ShowCow();
	Cow c2("moumou", "drink", 43.6);
	c2.ShowCow();
	Cow c3(c2);
	c3.ShowCow();
	c1 = c2;
	c1.ShowCow();
	return 0;
}

2、

/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/04
From:C++ primer plus 第十二章编程练习 第2题
*********************************************************************************************/
/***********************************string2.h**********************************************************/

#ifndef STRING2_H_
#define STRING2_H_

#include<iostream>
#include<cctype>
using namespace std;
class String
{
private:
	char * str;
	int len;
	static const int CINLIM = 80;

public:
	String();
	String(const char * s);
	String(const String & st);
	String & operator=(const String &st);
	String & operator=(const char *s);
	~String();
	void stringlow();
	void stringup();
	int has(char a);
	String  operator+(const char * s);
	friend String  operator+(const char * s,String st);
	friend String  operator+(String st1, String st2);
	friend ostream & operator<<(ostream & os, const String & st);
	friend istream & operator>>(istream & is, String & st);
	friend bool operator==(const String &st1, const String &st2);
	

};


#endif
/***********************************string2.cpp**********************************************************/
#include"stdafx.h"
#include"string2.h"

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

}
String::String(const char * s)
{
	len = strlen(s);
	str = new char[len + 1];
	strcpy_s(str, len + 1, s);
}
String::String(const String & st)
{
	len = st.len;
	str = new char[len + 1];
	strcpy_s(str, len + 1, st.str);
}
String & String::operator=(const String &st)
{
	if (&st == this)
		return *this;
	else
	{
		delete[]str;
		len = st.len;
		str = new char[len + 1];
		strcpy_s(str, len + 1, st.str);
		return *this;
	}
}
String & String::operator=(const char *s)
{
	delete[]str;
	len = strlen(s);
	str = new char[len + 1];
	strcpy_s(str, len + 1, s);
	return *this;
}
String::~String()
{
	delete[]str;
}
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]);
}
int String::has(char a)
{
	int s=0;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == a)
			s++;
	}
	return s;
}
String  String::operator+(const char * s)
{
	int l = len + strlen(s);
	char * p = new char[l + 1];
	strcpy_s(p, l+1, str);
	strcat_s(p, l+1, s);
	return String(p);
}
String  operator+(const char * s, String st)
{
	int l = strlen(s) + st.len;
	char *p = new char[l + 1];
	strcpy_s(p, l+1, s);
	strcat_s(p, l+1, st.str);
	return String(p);
}
String  operator+(String st1, String st2)
{
	int l = st1.len + st2.len;
	char * p = new char[l + 1];
	strcpy_s(p, l+1, st1.str);
	strcat_s(p, l+1,st2.str);
	return String(p);
}
ostream & operator<<(ostream & os, const String & st)
{
	os << st.str;
	return os;
}
istream & operator>>(istream & is, String & st)
{
	char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is)
		st = temp;
	while (is&&is.get() != '\n')
		continue;
	return is;
}
bool operator==(const String &st1, const String &st2)
{
	return(strcmp(st1.str, st2.str)==0);
}

/***********************************main.cpp**********************************************************/
#include"stdafx.h"
#include"string2.h"
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;
	cout << s2 << ".\n";
	s2 = s2 + s1;
	s2.stringup();
	cout << "The string\n" << s2 << "\ncontains " << s2.has('A') << " 'A' charaters in it.\n";
	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.stringlow();
		for (int i = 0; i < 3; i++)
		{
			if (ans == rgb[i])
			{
				cout << "That's right!\n";
				success = true;
				break;
			}
		}
		if (success)
			break;
		else
			cout << "Try again!\n";
	}
	cout << "Bye\n";
	return 0;

}

3、

/********************************************************************************************
Author:Tang Qingyun
Time:2017/04/04
From:C++ primer plus 第十二章编程练习 第3题
*********************************************************************************************/
/***********************************stock20.h**********************************************************/
#ifndef STOCK20_H_
#define STOCK20_H_

#include<iostream>
using namespace std;
class stock
{
private:
	char * company;
	int shares;
	double share_val;
	double total_val;
	void set_tot(){ total_val = shares*share_val; }
public:
	stock();
	stock(const char * s, long n = 0, double pr = 0.0);
	~stock();
	void buy(long num, double price);
	void sell(long num, double price);
	void update(double price);
	const stock & topval(const stock & s)const;
	friend ostream & operator<<(ostream & os, const stock & sto);

};
#endif
/***********************************stock20.cpp**********************************************************/
#include"stdafx.h"
#include"stock20.h"
stock::stock()
{
	company = new char[1];
	company[0] = '\0';
	shares = 0;
	share_val = 0.0;
	total_val = 0.0;
}
stock::stock(const char * s, long n, double pr)
{
	company = new char[strlen(s) + 1];
	strcpy_s(company, strlen(s) + 1, s);
	if (n < 0)
	{
		cout << "Number of shares can't be negative;" << company << " shares set to 0.\n";
		shares = 0;
	}
	else
		shares = n;
	share_val = pr;
	set_tot();
}
stock::~stock()
{
	delete[]company;
}
void stock::buy(long num, double price)
{
	if (num < 0)
	{
		cout << "Number of shares purchased can't be negative. " << "Transaction is aborted.\n";
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
}

void stock::sell(long num, double price)
{
	if (num < 0)
	{
		cout << "Number of shares sold can't be negative. " << "Transaction is aborted.\n";
	}
	else if (num>shares)
	{
		cout << "You can't sell more than you have! " << "Transaction is aborted.\n";
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}

void stock::update(double price)
{
	share_val = price;
	set_tot();
}
const stock & stock::topval(const stock & s)const
{
	if (s.total_val > total_val)
		return s;
	else
		return *this;
}

ostream & operator<<(ostream & os, const stock & sto)
{
	ios_base::fmtflags orig = os.setf(ios_base::fixed, ios_base::floatfield);
	streamsize prec = os.precision(3);

	os << "company: " << sto.company
		<< " shares: " << sto.shares << '\n';
	os << " share price:$ " << sto.share_val;

	os.precision(2);
	os << " Total Worth: $" << sto.total_val << '\n';
	os.setf(orig, ios_base::floatfield);
	os.precision(prec);

	return os;
}

/***********************************main.cpp**********************************************************/
#include"stdafx.h"
#include"stock20.h"

const int STKS = 4;
int main()
{
	stock stocks[STKS] =
	{
		stock("NanoSmart", 12, 20.0),
		stock("Boffo Objects", 200, 2.0),
		stock("Monolithic Obelisks", 130, 3.25),
		stock("Fleep Enterprises", 60, 6.5)
	};
	cout << "stock holdings:\n";
	int st;
	for (st = 0; st < STKS; st++)
		cout << stocks[st];
	const stock * top = &stocks[0];
	for (st = 1; st < STKS; st++)
		top = &top->topval(stocks[st]);
	cout << "\nMost valuable holding:\n";
	cout << *top;
	return 0;
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值