1.8.3 讲故事
接下来程序用变量来讲故事。
//tell the story
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "-– in search of the lost treasure of the Ancient Dwarves. ";
cout << "The group was led by that legendary rogue, " << leader << ".\n";
cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";
cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";
cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";
return 0;
}
程序的代码和惊险的叙述都非常清晰。然而要指出的是,为了计算探险队队长持有的金块数目,程序在表达式GOLD_PIECES % survivors中使用了取模运算符。该表达式计算GOLD_PIECES / survivors的余数,也就是将幸存的探险家私藏的金块平分后剩下的金块数目。