16.10.1
- // 16.10.1.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- using namespace std;
- bool reversed_same(string & st);
- int main()
- {
- string str;
- cout << "Enter a sentence or words.\n"
- <<"type \"quit\" to quit\n";
- getline(cin, str);
- while (str != "quit")
- {
- if (reversed_same(str))
- cout << "It's the same after reversing.\n";
- else cout << "It's not the same after reversing.\n";
- getline(cin, str);
- }
- return 0;
- }
- bool reversed_same(string & st)
- {
- string temp = st;
- reverse(temp.begin(), temp.end());
- if (temp == st)
- return true;
- else return false;
- }
16.10.2
- // 16.10.2.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string>
- #include <iostream>
- #include <cctype>
- using namespace std;
- bool reversed_same(string & st);
- int main()
- {
- string str;
- cout << "Enter a sentence or words.\n"
- << "type \"quit\" to quit\n";
- getline(cin, str);
- while (str != "quit")
- {
- if (reversed_same(str))
- cout << "It's the same after reversing.\n";
- else cout << "It's not the same after reversing.\n";
- getline(cin, str);
- }
- return 0;
- }
- bool reversed_same(string & st)
- {
- string only_alpha;
- int limit = st.size();
- for (int i = 0; i < limit; i++)
- if (isalpha(st[i]))
- only_alpha += tolower(st[i]);
- string temp = only_alpha;
- reverse(temp.begin(), temp.end());
- if (temp == only_alpha)
- return true;
- else return false;
- }
16.10.3
- // 16.10.3.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- void Show(string & s) { cout << s; }
- int main()
- {
- ifstream fin;
- fin.open("16103words.txt");
- if (fin.is_open() == false)
- {
- cerr << "Can't open file. Bye \n";
- exit(EXIT_FAILURE);
- }
- vector <string> vec;
- string temp;
- fin >> temp;
- while (fin)
- {
- vec.push_back(temp);
- fin >> temp;
- }
- for_each(vec.begin(), vec.end(), Show);
- fin.close();
- return 0;
- }
16.10.4
- // 16.10.4.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <list>
- #include <algorithm>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std;
- int reduce(long ar[], int n);
- int main()
- {
- srand(time(0));
- long data[50];
- for (int i =0 ; i <50; i++)
- {
- data[i] = rand() % 20;
- }
- cout << "before reducing : 50\n" << "after reducing: ";
- cout << reduce(data, 50);
- return 0;
- }
- int reduce(long ar[], int n)
- {
- list <long> lis;
- for (int i = 0; i < n; i++)
- lis.push_back(ar[i]);
- lis.sort();
- lis.unique();
- int count = lis.size();
- return count;
- }
- //list的成员函数sort(),和unique()更简单好用。
- //使用vector 和非成员函数unique()时,unique失效了,不明原因。
16.10.5
- // 16.10.5.cpp: 定义控制台应用程序的入口点。
- #include "stdafx.h"
- #include <list>
- #include <algorithm>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std;
- template <class T>
- int reduce(T ar[], int n);
- int main()
- {
- srand(time(0));
- long data[50];
- for (int i = 0; i <50; i++)
- {
- data[i] = rand() % 20;
- }
- cout << "before reducing : 50\n" << "after reducing: ";
- cout << reduce(data, 50);
- return 0;
- }
- template <class T>
- int reduce(T ar[], int n)
- {
- list <T> lis;
- for (int i = 0; i < n; i++)
- lis.push_back(ar[i]);
- lis.sort();
- lis.unique();
- int count = lis.size();
- return count;
- }
16.10.6
- //标头.h
- #pragma once
- #ifndef QUEUE_H_
- #define QUEUE_H_
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- // This queue will contain Customer items
- class Customer
- {
- private:
- long arrive; // arrival time for customer
- int processtime; // processing time for customer
- public:
- Customer() : arrive(0), processtime(0) {}
- void set(long when);
- long when() const { return arrive; }
- int ptime() const { return processtime; }
- };
- void Customer::set(long when)
- {
- processtime = rand() % 3 + 1;
- arrive = when;
- }
- typedef Customer Item;
- #endif
- // 16.10.6.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "标头.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <queue>
- using namespace std;
- const int MIN_PER_HR = 60;
- bool newcustomer(double x); // is there a new customer?
- int main()
- {
- srand(time(0)); // random initializing of rand()
- cout << "Case Study: Bank of Heather Automatic Teller\n";
- cout << "Enter maximum size of queue: ";
- int qs;
- cin >> qs;
- queue <Item> line; // line queue holds up to qs people
- cout << "Enter the number of simulation hours: ";
- int hours; // hours of simulation
- cin >> hours;
- // simulation will run 1 cycle per minute
- long cyclelimit = MIN_PER_HR * hours; // # of cycles
- cout << "Enter the average number of customers per hour: ";
- double perhour; // average # of arrival per hour
- cin >> perhour;
- double min_per_cust; // average time between arrivals
- min_per_cust = MIN_PER_HR / perhour;
- Item temp; // new customer data
- long turnaways = 0; // turned away by full queue
- long customers = 0; // joined the queue
- long served = 0; // served during the simulation
- long sum_line = 0; // cumulative line length
- int wait_time = 0; // time until autoteller is free
- long line_wait = 0; // cumulative time in line
- for (int cycle = 0; cycle < cyclelimit; cycle++)
- {
- if (newcustomer(min_per_cust)) // have newcomer
- {
- if (line.size() == qs)
- turnaways++;
- else
- {
- customers++;
- temp.set(cycle); // cycle = time of arrival
- line.push(temp); // add newcomer to line
- }
- }
- if (wait_time <= 0 && !line.empty())
- {
- ;
- line.pop(); // attend next customer
- wait_time = temp.ptime(); // for wait_time minutes
- line_wait += cycle - temp.when();
- served++;
- }
- if (wait_time > 0)
- wait_time--;
- sum_line += line.size();
- }
- 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";
- // cin.get();
- // cin.get();
- return 0;
- }
- bool newcustomer(double x)
- {
- return (rand() * x / RAND_MAX < 1);
- }
16.10.7
- // 16.10.7.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <vector>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- using namespace std;
- vector<int> lotto(int maxnum, int picked);
- void show(int n) { cout << n << "\t"; }
- int main()
- {
- vector <int >winners;
- winners = lotto(51, 6);
- for_each(winners.begin(), winners.end(), show);
- //for_each最后的show函数,只要函数名。不能加括号
- return 0;
- }
- vector<int> lotto(int maxnum, int picked)
- {
- vector<int> temp;
- int val;
- vector <int> result;
- for (int i = 0; i < maxnum; i++)
- {
- val = i + 1;
- temp.push_back(val);
- }
- for (int i = 0; i < picked; i++)
- {
- random_shuffle(temp.begin(), temp.end());
- result.push_back(*temp.begin());
- }
- return result;
- }
16.10.8
- // 16.10.8.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <string>
- #include <set>
- #include <algorithm>
- #include <iostream>
- #include <iterator>
- using namespace std;
- void show(string s) { cout << s << endl; }
- int main()
- {
- set<string> Mat_set;
- set<string> Pat_set;
- string temp;
- cout << "Mat's turn to type,\"quit\" to quit\n";
- getline(cin, temp);
- while (temp != "quit")
- {
- Mat_set.insert(temp);
- getline(cin, temp);
- }
- cout << "Pat's turn to type,\"quit\" to quit\n";
- getline(cin, temp);
- while (temp != "quit")
- {
- Pat_set.insert(temp);
- getline(cin, temp);
- }
- cout << "Number of Mat's friends\t" << Mat_set.size() << endl;
- cout << "Number of Pat's friends\t" << Pat_set.size() << endl;
- set<string> Merged;
- Merged.insert(Mat_set.begin(), Mat_set.end());
- Merged.insert(Pat_set.begin(), Pat_set.end());
- cout << "Number of Merged list.\t" << Merged.size() << endl;
- for_each(Merged.begin(), Merged.end(), show);
- return 0;
- }
16.10.9
- // 16.10.9.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <cstdlib>
- #include <ctime>
- #include <vector>
- #include <list>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- int main()
- {
- srand(time(0));
- long limit = 1000000;
- vector <int> vi0;
- for (int i = 0; i < limit; i++)
- vi0.push_back(rand() % 1000);
- vector <int> vi(vi0.size(),1);
- list <int> li(vi0.size(),1);
- clock_t start = clock();
- sort(vi.begin(), vi.end());
- clock_t end = clock();
- cout << "for vector " << (double)(end - start) / CLOCKS_PER_SEC << endl;
- start = clock();
- li.sort();
- end = clock();
- cout << "for list " << (double)(end - start) / CLOCKS_PER_SEC << endl;
- //experiment 2
- cout << "experiment 2:\n";
- copy(vi0.begin(), vi0.end(), li.begin());
- start = clock();
- copy(li.begin(), li.end(), vi.begin());
- sort(vi.begin(), vi.end());
- copy(vi.begin(), vi.begin(), li.begin());
- end = clock();
- cout << (double)(end - start) / CLOCKS_PER_SEC << endl;
- return 0;
- }
16.10.10
// 16.10.10.cpp: 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct Review {
- std::string title;
- int rating;
- double price;
- };
- bool operator<(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);
- bool worseThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);
- bool FillReview(Review & rr);
- void ShowReview(const shared_ptr<Review> & rr);
- void showmenu();
- bool sorting1(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);
- int main()
- {
- using namespace std;
- vector <shared_ptr<Review>> books;
- Review temp;
- while (FillReview(temp)) {
- shared_ptr<Review> pd_temp(new Review(temp));
- books.push_back(pd_temp);
- }
- if (books.size() > 0)
- {
- cout<< "choose a way to show data.";
- int choice;
- showmenu();
- cin >> choice;
- while (choice!=7)
- {
- switch (choice)
- {
- case 1:
- for_each(books.begin(), books.end(), ShowReview);
- break;
- case 2:
- sort(books.begin(), books.end());
- for_each(books.begin(), books.end(), ShowReview);
- break;
- case 3:
- sort(books.begin(), books.end(), worseThan);
- for_each(books.begin(), books.end(), ShowReview);
- break;
- case 4:
- sort(books.begin(), books.end(), worseThan);
- reverse(books.begin(), books.end());
- for_each(books.begin(), books.end(), ShowReview);
- break;
- case 5:
- sort(books.begin(), books.end(), sorting1);
- for_each(books.begin(), books.end(), ShowReview);
- break;
- case 6:
- sort(books.begin(), books.end(), sorting1);
- reverse(books.begin(), books.end());
- for_each(books.begin(), books.end(), ShowReview);
- break;
- default:
- cout << "wrong number.";
- continue;
- }
- showmenu();
- cin >> choice;
- }
- }
- else
- cout << "No entries. ";
- cout << "Bye.\n";
- // cin.get();
- return 0;
- }
- bool operator<(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
- {
- if (r1->title < r2->title)
- return true;
- else if (r1->title == r2->title && r1->rating < r2->rating)
- return true;
- else
- return false;
- }
- bool worseThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
- {
- if (r1->rating < r2->rating)
- return true;
- else
- return false;
- }
- bool sorting1(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
- {
- if (r1->price < r2->price)
- return true;
- if (r1->price == r2->price && r1->rating < r2->rating)
- return true;
- else
- return false;
- }
- bool FillReview(Review & rr)
- {
- std::cout << "Enter book title (quit to quit): ";
- getline(cin, rr.title);
- if (rr.title == "quit")
- return false;
- cout << "Enter book price: ";
- cin >> rr.price;
- if (!cin)
- return false;
- std::cout << "Enter book rating: ";
- std::cin >> rr.rating;
- if (!cin)
- return false;
- // get rid of rest of input line
- while (std::cin.get() != '\n')
- continue;
- return true;
- }
- void ShowReview(const shared_ptr<Review> & rr)
- {
- cout << "name\trating\tprice\n";
- cout << rr->title << "\t" << rr->rating << "\t" << rr->price << endl;
- }
- void showmenu()
- {
- cout << "Please enter 1,2,3,4,5,6 or 7\n"
- << "1) by original order \t 2) by alphabet order \n"
- << "3) by rating up \t 4) by rating down \n"
- << "5) by pricing up \t 6) by pricing down \n"
- << "7) quit \n";
- }