[C++]实验三

// 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修改

// 清翔兔

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值