Rick a écrit :
> At 1/20/2007 08:50 PM, you wrote:
>> /// \file
>> /// $Id: main.cpp 23 2007-01-21 01:40:43Z vawjr $
>> /// $LastChangedDate: 2007-01-20 18:40:43 -0700 (Sat, 20 Jan 2007) $
>> /// $LastChangedRevision: 23 $
>> /// $LastChangedBy: vawjr $
>> /// $HeadURL: http://rudbekassociates.com/MyOldCVS/Rick/main.cpp $
>>
>> #include <iostream>
>> #include <string>
>> #include <exception>
replace by
#include <stdexcept>
>> #include <numeric>
>> #include <algorithm>
>> #include <iomanip>
>>
you can remove it
>> using namespace std;
>>
>> /// something unique to throw if we find something that's not a "letter"
>> struct not_a_letter: public runtime_error
>> {
>> not_a_letter(char const* reason): runtime_error(reason){}
may be explicit ?
>> };
>>
>> /// this is how you write a function to talk with std::accumulate
>> /// it calls a function with the current "total" and the new element
>> unsigned int score_letter(unsigned int sum_so_far, char letter)
>> {
>> /// we declare this static so that it only gets initialized
>> (and allocated)
>> /// once instead of each time we call the function (which
>> would be for each
>> /// character
>> static int letter_value[] = {1, 2, 3, 4, 5, 6, 7, 8,
>> 9, // a - i
>> 10, 20, 30, 40, 50, 60, 70,
>> 80, 90, // j - r
>>
>> 100,200,300,400,500,600,700,800}; // s - z
>>
>> /// is the letter in the range of characters for which we
>> have scoring values
>> if ('a' <= letter && letter <= 'z')
>> {
>> return sum_so_far + letter_value[letter - 'a'];
>> }
>> /// oops, it's not in range, let someone know
>> /// (and tell them a little bit about what's wrong)
>> if ('A' <= letter && letter <= 'Z')
>> throw not_a_letter("we don't allow uppercase letters
>> in this program.");
>> throw not_a_letter("we don't allow punctuation in this
>> program.");
>> }
>>
>> /// this processes one "word", outputs the word followed by it's score
>> /// if the score processes throws not_a_letter, we output whatever
>> message is inside
>> /// the exception, otherwise we output the total score for the word
>> void score_word(string const& the_word)
>> {
>> cout << the_word << ": "; /// output the word
>> /// we tell the compiler that we want to look at exceptional
>> conditions
>> try
>> {
>> /// we start at the betinning (.begn()) and go
>> /// to the end (.end()) if nothing goes wrong we
>> will have
>> /// a total to output.
>> cout << accumulate(the_word.begin(), the_word.end(),
>> 0, score_letter) << '\n';
>> }
>> catch (not_a_letter& e)
>> {
>> /// output whatever error message decided at the
>> point the error was found
>> cout << e.what() << '\n';
>> }
>> }
>>
>> /// Now that we've done the preliminaries, the main program is dead
>> simple
>> int main(int argc, char* argv[])
>> {
>> /// make sure we have something to score (argc will be at
>> least 1 because the
>> /// name of the program (as invoked) is put into argv[0] so
>> there is at least
>> /// one thing in the argv[] array
>> if (argc < 2)
>> {
>> /// no args... show the way to use the program
>> cout << "Usage: " << *argv << " word[ word]...\n";
may be a return 1; here
>> }
>> /// Ok, we've got some args.... do them all
>> /// note that we start with argc+1 because the 0th name in
>> argv[] is the program name
>> /// argv+argc is one past the end of the of the valid data.
>> This is how "ranges" are
>> /// used in the Standard Library i.e. half-open ... incusive
>> at the beginning,
>> /// non-inclusive at the end
>> for_each(argv+1, argv+argc, score_word);
>> return 0;
you can remove return 0; here
>> }