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]);