C++ primer plus第六版第16章编程练习

1.不单独写文件

#include <iostream>
#include <string>
using namespace std;
bool IsPalindrome(string input) {
	int n = input.size();
	for (int i = 0; i < n / 2; ++i) {
		if(input[i] != input[n-1-i])
			return false;
	}
	return true;
}
int main()
{
    string input;
    cout << "Please enter a string:"; 
    getline(cin, input);
    if (IsPalindrome(input))
	cout << input << " is a panlindrome!\n";
    else
	cout << input << " is not a panlindrome!\n";
    return 0;
}

2.函数实现不同:

bool IsPalindrome(string input) {
	string temp;
	for (auto s : input) {
		if (isalpha(s))
			temp += tolower(s);
	}
	int n = temp.size();
	for (int i = 0; i < n / 2; ++i) {
		if (temp[i] != temp[n - 1 - i])
			return false;
	}
	return true;
}

3.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	ifstream fin;
	fin.open("chapter16.cpp");
	if (fin.is_open() == false) {
		cerr << "Can't open file.Bye.\n";
		exit(EXIT_FAILURE);
	}
	vector<string> strings;
	string item;
	int count = 0;
	while (fin) {
		fin >> item;
		strings.push_back(item);
		++count;
	}
	fin.close();
	for (int i = 0; i < strings.size(); ++i)
		cout << i << ": " << strings[i] << endl;
	cout << "Done.\n";
	return 0;
}

4. reduce函数:

int reduce(long ar[], int n) {
  std::sort(ar, ar + n);
  long* ite_end = std::unique(ar, ar + n);
  return ite_end - ar;
}

main:

int main() {
  long ar[] = {56, 23, 8, 6, 9, 6, 8, 6, 97};
  int n = reduce(ar, 9);
  return 0;
}

5.reduce修改为:

template <class T>
int reduce(T ar[], int n) {
  std::sort(ar, ar + n);
  T* ite_end = std::unique(ar, ar + n);
  return ite_end - ar;
}

6.仍然使用之前的customer类:

class Customer {
 public:
  Customer() { arrive = processtime = 0; }
  void set(long when) {
    processtime = std::rand() % 3 + 1;
    arrive = when;
  }
  long when() const { return arrive; }
  int ptime() const { return processtime; }

 private:
  long arrive;
  int processtime;
};

main中修改为使用queue,添加#include <queue>

using namespace std;
const int MIN_PER_HR = 60;
bool newcustomer(double x) { return (std::rand() * x / RAND_MAX < 1); }

int main() {
  cout << "Case Study:Banck of Heather Automatic Teller\n";
  cout << "Enter maximum size of queue:";
  int qs;
  cin >> qs;
  cout << "Set simulation hours: 100\n";
  int hours = 100;
  long cyclelimit = MIN_PER_HR * hours;
  double perhour = 15;
  double min_per_cust;
  queue<Customer> line;
  Customer temp;
  long turnaway = 0;
  long customers = 0;
  long served = 0;
  long sum_line = 0;
  int wait_time = 0;
  long line_wait = 0;
  double average_wait = 0;
  do {
    min_per_cust = MIN_PER_HR / perhour;
    turnaway = 0;
    customers = 0;
    served = 0;
    sum_line = 0;
    wait_time = 0;
    line_wait = 0;
    average_wait = 0;
    while (!line.empty()) {
      temp = line.back();
      line.pop();
    }

    for (int cycle = 0; cycle < cyclelimit; ++cycle) {
      if (newcustomer(min_per_cust)) {
        if (line.size() == qs)
          turnaway++;
        else {
          customers++;
          temp.set(cycle);
          line.push(temp);
        }
      }
      if (wait_time <= 0 && !line.empty()) {
        temp = line.front();
        line.pop();
        wait_time = temp.ptime();
        line_wait += cycle - temp.when();
        served++;
      }
      if (wait_time > 0) wait_time--;
      sum_line += line.size();
    }
    if (served > 0) average_wait = (double)line_wait / served;
    cout << "第" << perhour - 14 << "次实验的平均等候时间为:" << average_wait
         << "分钟,每小时到达的客户数为:" << perhour << "个。\n";
    perhour++;
  } while (average_wait < 1);

  return 0;
}

7.lott函数:

vector<int> lotto(int max_num, int count) {
  vector<int> nums, res;
  for (int i = 1; i <= max_num; ++i) nums.push_back(i);
  random_shuffle(nums.begin(), nums.end());
  for (int i = 1; i <= count; ++i) res.push_back(nums[i - 1]);
  return res;
}

main函数:

  vector<int> nums = lotto(52, 6);
  int n = nums.size();
  for (auto num : nums) cout << num << "\n";

8.实现为:

int main() {
  vector<string> mats, pats, invited;
  string name;
  cout << "Enter Mat's friends' name(q for quit):";
  getline(cin, name);
  while (name != "q") {
    mats.push_back(name);
    cout << "Enter Mat's friends' name(q for quit):";
    getline(cin, name);
  }
  cout << "Enter Pat's friends' name(q for quit):";
  getline(cin, name);
  while (name != "q") {
    pats.push_back(name);
    cout << "Enter Pat's friends' name(q for quit):";
    getline(cin, name);
  }
  invited = mats;
  invited.insert(invited.end(), pats.begin(), pats.end());
  std::sort(invited.begin(), invited.end());
  int n = std::unique(invited.begin(), invited.end()) - invited.begin();
  cout << "The invited friends' name are:\n";
  for (int i = 0; i < n; ++i) cout << invited[i] << "\n";
  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值