您的位置:首页 > 编程语言 > C语言/C++

C++ primerplus 第12章课本上例题

2016-05-18 12:32 513 查看
//第12章程序12.4,5,6.
#ifndef STRING1_H
#define STRING1_H
#include<iostream>
using std::ostream;
using std::istream;

class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);//字符数组构造函数
String();
String(const String &s);//copy构造函数
~String();
int length()const{ return len; }
//重载操作符
String & operator=(const String &s);
String & operator=(const char *s);//重载=运算符
char & operator[](int i);
const char& operator[](int i)const;//只能读const对象的。
//重载友元函数
friend bool operator<(const String &st, const String &st2);
friend bool 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);//不能const ,不然怎么输入
//设置静态函数
static int HowMany();
};
#endif


//第12章程序12.4,5,6.
#define _CRT_SECURE_NO_WARNINGS
#include"zhan.h"
#include<iostream>
#include<cstring>
#include<string.h>
using std::cin;
using std::cout;
using std::string;

int String::num_strings = 0;
String::String(const char *s)//字符数组构造函数
{
len = std::strlen(s);
str = new char(len + 1);
strcpy(str, s);
num_strings++;
}
String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;

}
String::String(const String &s)//copy构造函数
{
num_strings++;
len = s.len;
str = new char(len + 1);
std::strcpy(str, s.str);
}
String::~String()
{
--num_strings;
delete[]str;
}
//重载操作符
String & String::operator=(const String &s)
{
if (this == &s)//this 指针是地址
return *this;
delete[]str;
len = s.len;
str = new char(len + 1);//深copy
std::strcpy(str, s.str);
return *this;
}
String & String::operator=(const char *s)//重载=运算符 c风格
{
delete[]str;
len = strlen(s);
str = new char(len + 1);
std::strcpy(str, s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char& String::operator[](int i)const//只能读const对象的。
{
return str[i];
}
//重载友元函数
bool operator<(const String &st, const String &st2)
{
return (std::strcmp(st.str, st2.str)<0);
}
bool operator>(const String &st, const String &st2)
{
return (std::strcmp(st.str, st2.str)>0);
}
bool operator==(const String &st, const String &st2)
{
return (std::strcmp(st.str, st2.str) == 0);

}
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;
}
//设置静态函数
int String::HowMany()
{
return num_strings;
}


//第12章程序12.4,5,6.
#include<iostream>
#include"zhan.h"
#include<stdlib.h>
const int Arsize = 10;
const int Maxlen = 81;
int main()
{
using std::cin;
using std::cout;
using std::endl;
String name;
cout << "Hi,what's your name?\n>>";
cin >> name;

cout << name << ",Please enter up to " << Arsize << " short sayings(empty line to quit):\n";
String sayings[Arsize];//10个

char temp[Maxlen];
int i;
for (i = 0; i < Arsize; i++)
{
cout << i + 1 << ": ";
cin.get(temp, Maxlen);
while (cin&& cin.get() != '\n')//cin.get用来接收“\n”
continue;
if (!cin || temp[0] == '\0')
break;
else
sayings[i] = temp;

}
int total = i;
if (total > 0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total; i++)
cout << sayings[i][0] << ":" << sayings[i] << endl;
int shortest = 0;
int first = 0;
for (i = 1; i < total; i++)
{
if (sayings[i].length() < sayings[shortest].length())
shortest = i;
if (sayings[i] < sayings[first])
first = i;
}
cout << "Shortest saying:\n" << sayings[shortest] << endl;
cout << "First alphabetically:\n" << sayings[first] << endl;
cout << "This program used" << String::HowMany() << " String objects.Bye.\n";
}
else
cout << "No input!Bye.\n";
system("pause");
return 0;
}


//第12章12.10.11.12.

#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()
{
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)//包括copy构造函数和赋值运算符
{
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


//第12章12.10.11.12.
#include"zhan.h"
#include<cstdlib>

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

Queue::Queue(int qs):qsize(qs)//构造函数,这种方式让常量qsize可以在实现的时候赋值
{
front = rear = NULL;
items = 0;
}
Queue::~Queue()
{
Node * temp;
while (front != NULL)
{
temp = front;//先保存
front = front->next;//后讲第二个设置为第一个
delete temp;//把第一个删除
}
}
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 = NULL;
items++;
if (front == NULL)
front = add;
else
rear->next = add;//把尾地址给add连在一起
rear = add;//将add变为尾部
return true;
}
bool Queue::dequeue(Item &item)
{
if (front == NULL)
return false;
item = front->item;
items--;
Node *temp = front;
front = front->next;
delete temp;
if (items == 0)
rear = NULL;
return true;
}


//第12章12.10.11.12.

#include<iostream>
#include"zhan.h"
#include<cstdlib>
#include<ctime>

const int Min_Per_Hr = 60;

bool newcustomer(double x);

int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;

std::srand(std::time(0));//设置时间种子
cout << "Case Study: Bank of Heather Automatic Teller\n";
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 hous: ";
double perhour;
cin >> perhour;
double min_per_cust = Min_Per_Hr;//一小时几个顾客

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\n";
}
else
cout << "No customers!\n";
cout << "Done!\n";
system("pause");
return 0;
}

bool newcustomer(double x)
{
return (std::rand()*x / RAND_MAX < 1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: