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<iostream>
#include"Cow.h"
#include<string>
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<<endl;
}
Cow &Cow::operator+(const Cow &c)
{
	
	strcat(name, c.name);
	return *this;
}

void Cow::Stringlow()
{
	int i, len;
	len = strlen(name);
	for(i=0;i<len;i++)
	{
		if (name[i] >= 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<iostream>
#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<iostream>
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<iostream>
#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<iostream>
#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<string>
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<iostream>
#include"stock.h"
#include<string>
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<iostream>
#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<iostream>
#include<cstdlib>
#include<ctime>
#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值