// Exmp3.1
/*
1.Write a simple menu program based on an array of function pointers.
Each item of the menu corresponds to a function that can only include a simple text output statement.
*/
#include <iostream>
using namespace std;
void m1(){ cout << "Hello! No.1" << endl; }
void m2(){ cout << "Hello! No.2" << endl; }
void m3(){ cout << "Hello! No.3" << endl; }
void main()
{
typedef void (*PV) ();
PV menu[3] = { m1 , m2 , m3 };
cout << "Please choose 1 to 3:" << endl;
int i;
cin >> i;
menu [i-1] ();
}
// 清翔兔
// Exmp3.2
/*
2. Read a sequence of words from input.
Use Qiut as a word that terminates the input.
Print the words in order they were entered.
Don’t print a word twice.
Modify the program to sort the words before printing them. (
Using Set for storing words)
*/
// This exercise as the same as TCPL ; Pages 105 ; No.11
// used set class ...
// ...Sort the words before printing them.
#include <iostream>
#include <string>
#include <set>
#include <vector>
using namespace std;
void main()
{
string word ;
typedef set<string> StringSet;
typedef StringSet::iterator SetIter;
typedef vector<SetIter> SetIterVec;
typedef SetIterVec::iterator VecSetIter;
SetIterVec wordlist1;
StringSet wordlist2;
while ( cin >> word && word!="Quit" )
{
//加入到list2
pair< SetIter , bool> const &temp = wordlist2.insert( word );
//加入到list1
if ( temp.second )
wordlist1.push_back ( temp.first );
}
cout << "/nResult ( Not in order ):" << endl;
for ( VecSetIter iter = wordlist1.begin(); iter!= wordlist1.end() ; iter++ )
cout << **iter << endl; // 打印结果1
// 此处,在VS6.0下,将无法通过。
cout << "/nResult ( In order ):" << endl;
for ( SetIter iter = wordlist2.begin(); iter!= wordlist2.end() ; iter++ )
cout << *iter << endl; // 打印结果2
}
// 清翔兔 05/09/20
// 05/10/15修改
// 清翔兔