C++PrimerPlus_12 学习笔记

深拷贝和浅拷贝区别

深拷贝最后两个指针是指向两个不同的区域,浅拷贝最终是指向同一次地址内存

值传递的危险  原因是指向的是同一块内存中

C++推荐使用nullptr做为空指针

下面是字符串类的 示例代码:

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

class Strone
{
	char* str;
	int len;
public:
	Strone();
	Strone(char* string);
	int length() const;
	friend bool operator<(const Strone& str1, const Strone& str2);
	friend bool operator>(const Strone& str1, const Strone& str2);
	friend bool operator==(const Strone& str1, const Strone& str2);
	friend istream& operator>>(istream& is, Strone& st);
	char& operator[](int i);
	const char& operator[](int i) const;
};

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

Strone::Strone(char* string)
{
	len = strlen(string);
	str = new char[len + 1];
	strcpy(str, string);
	str[len] = '\0';
}

int Strone::length() const
{
	return len;
}

bool operator>(const Strone& str1, const Strone& str2)
{
	return (std::strcmp(str1.str, str2.str) > 0);
}

bool operator<(const Strone& str1, const Strone& str2)
{
	return (std::strcmp(str1.str, str2.str) < 0);
}

bool operator==(const Strone& str1, const Strone& str2)
{
	return (std::strcmp(str1.str, str2.str) == 0);
}

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

istream& operator>>(istream& is, Strone& st)
{
	is >> st.str;
	return is;
}

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

class TestConst
{
private:
	const int num;
	double m_pai;
public:
	TestConst(int n, int p);
};

TestConst::TestConst(int n, int p) : num(n), m_pai(p)
{
	cout << "TestConst construct function..." << endl;
	cout << "num = " << num << endl;
	cout << "m_pai = " << m_pai << endl;
}

int main()
{
//	char* str1 = new char[4];
//	char* str2 = new char[4];
//	std::strcpy(str1, "abc");
//	std::strcpy(str2, "abd");
//	str1[3] = '\0';
//	str2[3] = '\0';
//	Strone t1(str1);
//	const Strone t2(str2);
//	
//	cout << "t1 < t2 = " << (t1 < t2) << endl;

	TestConst t(5, 3.14);
}

 #queue.h

#ifndef QUEUE_H_
#define QUEUE_H_

class Customer
{
private:
	long arrive;
	int processtime;
public:
	Customer() {arrive = processtime = 0;}
	void set(long when);
	long when() const { return arrive; }			//客户到达时间
	int ptime() const { return processtime; }		//客户处理时间
};

typedef Customer Item;

class Queue
{
private:
	struct Node { Item item; struct Node* next; };
	enum {Q_SIZE = 10};
	Node* front;
	Node* rear;
	int items;
	const int qsize;
	Queue(const Queue& q) : qsize(0) {}
	Queue & operator=(const Queue& q) {return *this; };
public:
	Queue(int qs = Q_SIZE);
	~Queue();
	bool isempty() const;
	bool isfull() const;
	int queuecount() const;
	bool enqueue(const Item& item);
	bool dequeue(Item &item);
};

#endif

#queue.cpp 

#include "queue.h"
#include <cstdlib>

Queue::Queue(int qs) : qsize(qs)
{
	front = rear = 0;
	items = 0;
}

Queue::~Queue()
{
	Node* item;
	while( front != rear )
	{
		item = front;
		front = front->next;
		delete item;
	}
}

bool Queue::isempty() const
{
	return (items == 0);
}

bool Queue::isfull() const
{
	return (items == qsize);
}

int Queue::queuecount() const
{
	return items;
}

bool Queue::enqueue(const Item& item)
{
	if (isfull())
		return false;
		Node* add = new Node;
		add->item = item;
		add->next = 0;
		items++;
		if (0 == front)
			front = add;
		else
			rear->next = add;
		rear = add;
		return true;
}

bool Queue::dequeue(Item& item)
{
	if (isempty())
		return false;
	item = front->item;
	Node* temp = front;
	front = front->next;
	delete temp;
	items--;
	if (items == 0)
		rear = 0;
	return true;
}

void Customer::set(long when)
{
	processtime = std::rand() % 3 + 1;
	arrive = when;
}

#test.cpp  测试,调用上面两个 

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "queue.h"
using namespace std;

const int MIN_PER_HR = 60;

bool newcustomer(double x);

int main()
{
	srand(time(0));   	//随机初始化rand
	
	cout << "Case Study: Bank of Heather Automatic Teller" << endl;
	cout << "Enter maximum size of queue: ";
	int qs;
	cin >> qs;
	Queue line(qs);
	
	cout << "Enter the number of simulation hours: ";
	int hours;
	cin >> hours;
	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 temp;            //新客户数据
	long turnaways=0;     //被排满队拒之门外
	long customers=0;     //加入队列
	long served =0;       //在模拟期间服务
	long sum_line=0;      //累计线长
	int wait_time =0;     //等待自动取款机空闲
	long line_wait =0;    //累计排队时间
	
	for (int cycle = 0; cycle < cyclelimit; cycle++)
	{
		if (newcustomer(min_per_cust))
		{
			if (line.isfull())
				turnaways++;
			else
			{
				customers++;
				temp.set(cycle);
				line.enqueue(temp);
			}
		}
		if (wait_time <= 0 && !line.isempty())
		{
			line.dequeue(temp);
			wait_time = temp.ptime();
			line_wait += cycle - temp.when();
			served++;
		}
		if (wait_time > 0)
			wait_time--;
		sum_line += line.queuecount();
	}
	
	if (customers > 0)
	{
		cout << "customers accepted: " << customers << endl;
		cout << " customers served: " << served << endl;
		cout << "        turnaways: " << turnaways << endl;
		cout << "average queue size: ";
		cout.precision(2);
		cout.setf(ios_base::fixed, ios_base::floatfield);
		cout << (double)sum_line / cyclelimit << endl;
		cout << "average wait time: " << (double)line_wait / served << " minutes" << endl;
	}
	else
		cout << "No customers!" << endl;
	cout << "Done" << endl;
	
	return 0;
}
//x=顾客之前的平均时间,以分钟为单位
//如果顾客在这一分钟出现,返回值为真

bool newcustomer(double x)
{
	return (rand() * x / RAND_MAX < 1);
}

代码成功运行示例: 最终模拟结果

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值