#include "stdafx.h"
#include
#include
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;
}
#include "stdafx.h"
#include
#include
#include
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;
}
#include "stdafx.h"
#include
#include
#include
#include
#include
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 vec;
string temp;
fin >> temp;
while (fin)
{
vec.push_back(temp);
fin >> temp;
}
for_each(vec.begin(), vec.end(), Show);
fin.close();
return 0;
}
#include "stdafx.h"
#include
#include
#include
#include
#include
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 lis;
for (int i = 0; i < n; i++)
lis.push_back(ar[i]);
lis.sort();
lis.unique();
int count = lis.size();
return count;
}
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
template
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
int reduce(T ar[], int n)
{
list lis;
for (int i = 0; i < n; i++)
lis.push_back(ar[i]);
lis.sort();
lis.unique();
int count = lis.size();
return count;
}
#include
#include
#include
#include
using namespace std;
// This queue will contain Customer items
class Customer
{
private:
long arrive;
int processtime;
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;
const int MIN_PER_HR = 60;
bool newcustomer(double x); // is there a new customer?
int main()
{
srand(time(0));
cout << "Case Study: Bank of Heather Automatic Teller\n";
cout << "Enter maximum size of queue: ";
int qs;
cin >> qs;
queue - line;
cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;
long cyclelimit = MIN_PER_HR * hours; // # of cycles
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.size() == qs)
turnaways++;
else
{
customers++;
temp.set(cycle);
line.push(temp); // add newcomer to line
}
}
if (wait_time <= 0 && !line.empty())
{
;
line.pop()
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);
}
#include "stdafx.h"
#include
#include
#include
using namespace std;
using namespace std;
vector lotto(int maxnum, int picked);
void show(int n) { cout << n << "\t"; }
int main()
{
vector winners;
winners = lotto(51, 6);
for_each(winners.begin(), winners.end(), show);
//for_each最后的show函数,只要函数名。不能加括号
return 0;
}
vector lotto(int maxnum, int picked)
{
vector temp;
int val;
vector 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;
}
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
void show(string s) { cout << s << endl; }
int main()
{
set Mat_set;
set 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 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;
}
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
srand(time(0));
long limit = 1000000;
vector vi0;
for (int i = 0; i < limit; i++)
vi0.push_back(rand() % 1000);
vector vi(vi0.size(), 1);
list 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;
}
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
struct Review {
std::string title;
int rating;
double price;
};
bool operator<(const shared_ptr & r1, const shared_ptr & r2);
bool worseThan(const shared_ptr & r1, const shared_ptr & r2);
bool FillReview(Review & rr);
void ShowReview(const shared_ptr & rr);
void showmenu();
bool sorting1(const shared_ptr & r1, const shared_ptr & r2);
int main()
{
using namespace std;
vector > books;
Review temp;
while (FillReview(temp)) {
shared_ptr 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 & r1, const shared_ptr & 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 & r1, const shared_ptr & r2)
{
if (r1->rating < r2->rating)
return true;
else
return false;
}
bool sorting1(const shared_ptr & r1, const shared_ptr & 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 & 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";
}