1.8.3 讲故事

2013-10-07 14:42:09 · 作者: · 浏览: 57

1.8.3  讲故事

接下来程序用变量来讲故事。

  1. //tell the story  
  2. cout << "\nA brave group of " << adventurers << " set out on a quest ";  
  3. cout << "-– in search of the lost treasure of the Ancient Dwarves. ";  
  4. cout << "The group was led by that legendary rogue, " << leader << ".\n";  
  5. cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";  
  6. cout << "All fought bravely under the command of " << leader;  
  7. cout << ", and the ogres were defeated, but at a cost. ";  
  8. cout << "Of the adventurers, " << killed << " were vanquished, ";  
  9. cout << "leaving just " << survivors << " in the group.\n";  
  10. cout << "\nThe party was about to give up all hope. ";  
  11. cout << "But while laying the deceased to rest, ";  
  12. cout << "they stumbled upon the buried fortune. ";  
  13. cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";  
  14. cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);  
  15. cout << " pieces to keep things fair of course.\n";  
  16. return 0;  
  17. }  

程序的代码和惊险的叙述都非常清晰。然而要指出的是,为了计算探险队队长持有的金块数目,程序在表达式GOLD_PIECES % survivors中使用了取模运算符。该表达式计算GOLD_PIECES / survivors的余数,也就是将幸存的探险家私藏的金块平分后剩下的金块数目。