#include //-------------------------------------------------------------------------- // Data Definition //----------------- // A list of digits is either // - an empty list // or a structure consisting of // - first as digit // - rest as list of digits //-------------------------------------------------------------------------- struct ListOfDigit { int first; ListOfDigit *rest; }; //-------------------------------------------------------------------------- // Data example //-------------------------------------------------------------------------- ListOfDigit *empty = 0; ListOfDigit L1 = { 1, empty }; ListOfDigit L2 = { 4, &L1 }; ListOfDigit L3 = { 7, &L2 }; //-------------------------------------------------------------------------- // Code Pattern //-------------------------------------------------------------------------- //... F(ListOfDigit * L) //{ // if (L == 0) // { // ... // } // else // { // ... L->first ... // ... F(L->rest) ... // } //} //-------------------------------------------------------------------------- // Prototypes //-------------------------------------------------------------------------- int list2num(ListOfDigit *); int list2num_A(ListOfDigit * L, int multiplier, int acc); //-------------------------------------------------------------------------- // main //-------------------------------------------------------------------------- int main() { std::cout << "list2num(empty) == " << list2num(empty) << "\n"; std::cout << "list2num(&L1) == " << list2num(&L1) << "\n"; std::cout << "list2num(&L2) == " << list2num(&L2) << "\n"; std::cout << "list2num(&L3) == " << list2num(&L3) << "\n"; std::cout << "list2num_A(empty, 10, 2) == " << list2num_A(empty, 10, 2) << "\n"; std::cout << "list2num_A(&L1, 10, 9) == " << list2num_A(&L1, 10, 9) << "\n"; std::cout << "list2num_A(&L2, 10, 7) == " << list2num_A(&L2, 10, 7) << "\n"; return 0; } //-------------------------------------------------------------------------- // @description convert a list of digits to a number // @param L a list of digits // @return the corresponding number // @contract list2num : ListOfDigit -> number // @example list2num(empty) == 0 // @example list2num(L1) == 1 // @example list2num(L2) == 14 // @example list2num(L3) == 147 //-------------------------------------------------------------------------- int list2num(ListOfDigit * L) { if (L == 0) { return 0; } else { return list2num_A(L->rest, 10, L->first); } } //-------------------------------------------------------------------------- // @description convert a list of digits to a number // @param L a list of digits // @param multiplier the current multiplier // @param acc the accumulated number so far // @return the corresponding number // @contract list2num_A : ListOfDigit number number -> number // @example list2num_A(empty, 10, 2) == 2 // @example list2num_A(L1, 10, 9) == 19 // @example list2num_A(L2, 10, 7) == 147 //-------------------------------------------------------------------------- int list2num_A(ListOfDigit * L, int multiplier, int acc) { if (L == 0) { return acc; } else { return list2num_A(L->rest, multiplier*10, L->first * multiplier + acc); } }