C++PrimePlus(第六版)第十二章课后编程练习

1小牛
题目看错把第二题的代码也写了!

/******************Cow.h*******************/
#pragma once
#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;
	//overloaded
	Cow &operator+(const Cow &c);
	void Stringlow();
	void Stringupp();
	int String_num(char c)const;
};
#endif // !COW_H_
/******************Cow.cpp*******************/
#include
#include"Cow.h"
#include
using std::cout;
using std::endl;

Cow::Cow()
{
	name[0] = '\0';
	hobby = NULL;
	weight = 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()
{
	delete[]hobby;
}

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

void Cow::ShowCow()const
{
	cout << name << endl;
	cout << hobby << endl;
	cout << weight<= 65 && name[i] <= 90)
			name[i]=name[i] + 32;
	}
}
void Cow::Stringupp()
{
	int i, len;
	len = strlen(name);
	for (i = 0; i < len; i++)
	{
		if (name[i] >= 97 && name[i] <= 122)
			name[i]=name[i] - 32;
	}
}
int Cow::String_num(char c)const
{
	int i, len,num=0;
	len = strlen(name);
	for (i = 0; i < len; i++)
	{
		if (name[i] ==c)
			num++;
	}
	return num;
}
#include
#include"Cow.h"
using std::cout;
using std::endl;
using std::cin;

int main()
{
	int n;
	Cow petter,piss;
	Cow jack("jackchen", "footbal", 100);
	Cow xiaoli("CHONGZheng", "sleeping", 200);
	Cow laoli("AAABBBCCCdddeeeeAA", "sleeping", 200);
	petter = jack;
	jack.ShowCow();
	piss=jack + xiaoli;
	cout << "piss string" << endl;
	piss.ShowCow();
	xiaoli.ShowCow();
	xiaoli.Stringlow();
	xiaoli.ShowCow();
	n=laoli.String_num('A');
	cout << n << endl;
	//system("PAUSE");
	return 0;
}

2、

/************String.h*****************/
#pragma once
#ifndef STRING_H_
#define STRING_H_
#include
using std::istream;
using std::ostream;

class String {
private:
	char *str;
	int len;
	static int num_strings;
	static const int CINLIM = 80;
public:
	String();
	String(const char *str);
	String(const String &str);
	~String();
	void StrLow();
	void StrUpp();
	int has(char c)const;
	//overloaded method
	String& operator=(const String &st);
	String &operator=(const char *st);
	char &operator[](int i);
	const char &operator[](int i)const;
	String operator+(const String &st);
	//friends
	friend String operator+(const String &st, const String &st2);
	friend bool operator==(const String &st, const String &st2);
	friend ostream &operator<<(ostream &os, const String &st);
	friend istream &operator>>(istream &is,String &st);
};
#endif // !STRING_H_
/************String.cpp*****************/
#include
#include"String.h"
int String::num_strings = 0;
using std::endl;
String::String()
{
	len = 4;
	str = new char[1];
	str[0] = '\0';
	num_strings++;
}
String::String(const char *st)
{
	len = strlen(st);
	str = new char[len + 1];
	strcpy(str, st);
	num_strings++;
}

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

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

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

String operator+(const String &st, const String &st1)
{
	String str2;
	str2.len = strlen(st.str) + strlen(st1.str);
	str2.str = new char[str2.len + 1];
	strcpy(str2.str, st.str);
	strcat(str2.str, st1.str);
	return str2;
}
void String::StrLow()
{
	int i = 0;
	for (i = 0; i < len; i++)
		str[i]=tolower(str[i]);
}
void String::StrUpp()
{
	int i = 0;
	for (i = 0; i < len; i++)
		str[i] = toupper(str[i]);
}
int String::has(char c)const
{
	int i = 0,num=0;
	for (i = 0; i < len; i++)
		if (str[i] == c)
			num++;
	return num;
}


bool operator==(const String &st, const String &st2)
{
	return (strcmp(st.str, st2.str) == 0);

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

String &String::operator=(const char *s)
{
	delete[]str;
	len = std::strlen(s);
	str = new char[len + 1];
	strcpy(str, s);
	return *this;
}
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;
}

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



char &String::operator[](int i)
{
	return str[i];
}

const char &String::operator[](int i)const
{
	return str[i];
}

/************pe12_2.cpp*****************/
#include
#include"String.h"
using namespace std;
int main()
{
	String s1("and I am a C++ student.");
	String s2 = "Please enter your name: ";
	String s3;
	cout << s1 << endl;
	cin >> s3;
	s2 = "My name is " + s3;
	cout << s2 << ".\n";
	s2 = s2 + s1;
	s2.StrUpp();
	cout << "The string \n" << s2 << "\ncontains " << s2.has('A')
		<< " 'A' character 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.StrLow();
		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;
}
/******************stock.h*******************/
#pragma once
#ifndef STOCK_H_
#define STOCK_H_
#include
using std::ostream;
using std::string;
class Stock
{
private:
	char *com;
	int len;
	int shares;
	double share_val;
	double total_val;
	void set_tot() { total_val = shares * share_val; }
public:
	Stock();
	Stock(const string &co, long n = 0, double pr = 0.0);
	Stock(const char *st, 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 &s);

};
#endif // !STOCK_H_


/******************stock.cpp*******************/
#include
#include"stock.h"
#include
using std::endl;
using std::cout;

Stock::Stock()
{
	len = 4;
	com = new char[1];
	com[0] = '\0';
	shares = share_val = total_val = 0;

}
/*
Stock::Stock(const string &co, long n = 0, double pr = 0.0)
{
	len = strlen(com);
	com = new char[len + 1];
	strcpy(com, co);
	shares = n;
	share_val = pr;
}
*/
Stock::Stock(const char *co, long n , double pr )
{
	len = strlen(co);
	com = new char[len + 1];
	strcpy(com, co);

	if (n < 0)
	{
		cout << "Number of shares can't be negative;"
			<< com << "shares set to 0.\n";
		shares = 0;
	}
	else
		shares = n;
	share_val = pr; 
	set_tot();


}
Stock::~Stock()
{
	delete[]com;
}

void Stock::buy(long num, double price)
{
	if (num < 0)
	{
		cout << "Number of shares urchased 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();
}

ostream &operator<<(ostream &os, const Stock &s)
{
	os << "Company" << s.com
		<< "Shares " << s.shares << '\n';
	os << "share price: $" << s.share_val;

	os << "Toatal worth: $" << s.total_val << '\n';
	return os;
}

const Stock &Stock::topval(const Stock &s)const
{
	if (s.total_val > total_val)
		return s;
	else
		return *this;
}
/******************useStock.cpp*******************/
#include
#include"stock.h"
using namespace std;
const int STKS = 4;
int main()
{
	Stock stock[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 << "Stocking holding:\n";
	int st;
	for (st = 0; st < STKS; st++)
	{
		cout << stock[st];
	}

	const Stock *top = &stock[0];
	for (st = 1; st < STKS; st++)
		top = &top->topval(stock[st]);
	std::cout << "\n Most valueable holding:\n";
	cout << top;
	return 0;

}

6、模拟两台ATM机,本程序代码只提供主程序代码。

#include
#include
#include
#include"queue.h"
using std::endl;
using std::cout;
using std::cin;
using std::ios_base;
const int MIN_PER_HR = 60;

bool newcustomer(double x);

int main()
{
	std::srand(std::time(0));
	cout << "Case Study:Back of Heather Automatic Teller\n";
	while (1)
	{
	cout << "Enter maximum size of nqueue: ";
	int qs;
	cin >> qs;
	Queue line1(qs), line2(qs);

	cout << "Enter the number of simulation hours: ";
	int hours;
	cin >> hours;
	//simulation will run 1 cycle per minute
	long cyclelimit = MIN_PER_HR * hours;

	cout << "Enter the average number of customers per hour: ";
	double perhour;
	cin >> perhour;
	double min_per_cust;
	min_per_cust = MIN_PER_HR / perhour;

	Item temp1, temp2;
	long turnaways = 0;
	long customers = 0;
	long served1 = 0,served2=0;
	long sum_line = 0;
	int wait2_time = 0,wait1_time=0;
	long line1_wait =0,line2_wait=0;
	
		for (int cycle = 0; cycle < cyclelimit; cycle++)
		{
			if (newcustomer(min_per_cust))
			{
				if (line1.isfull() && line2.isfull())
					turnaways++;
				else
				{
					customers++;
					if (line1.queuecount() > line2.queuecount())
					{
						temp2.set(cycle);
						line2.enqueue(temp2);
					}
					else
					{
						temp1.set(cycle);
						line1.enqueue(temp1);
					}
				}
			}
			if (wait1_time <= 0 && !line1.isempty())
			{
				line1.dequeue(temp1);
				wait1_time = temp1.ptime();
				line1_wait += cycle - temp1.when();
				served1++;
			}
			if (wait2_time <= 0 && !line2.isempty())
			{
				line2.dequeue(temp2);
				wait2_time = temp2.ptime();
				line2_wait += cycle - temp2.when();
				served2++;
			}
			if (wait1_time > 0)
				wait1_time--;
			if (wait2_time > 0)
				wait2_time--;
			sum_line += line1.queuecount() + line2.queuecount();

		}
		if (customers > 0)
		{
			cout << "customers accepted: " << customers << endl;
			cout << "  customers served: " << served1 + served2 << endl;
			cout << "           turnaways: " << turnaways << endl;
			cout << "average queue size: ";
			cout.precision(2);
			cout.setf(std::ios_base::fixed, ios_base::floatfield);
			cout << (double)sum_line / cyclelimit << endl;
			cout << "line1 average wait time:    "
				<< (double)line1_wait / served1 << " minutes\n";
			cout << "line2 average wait time:    "
				<< (double)line1_wait / served2 << " minutes\n";
		}
		else
			cout << "No customers\n";
		system("PAUSE");
	}
	cout << "Done!\n";

	return 0;
}
bool newcustomer(double x)
{
	double y;
	y = (std::rand()*x / RAND_MAX);
	return y < 1 ? 1 : 0;
}

你可能感兴趣的:(C/C++)