Jump to content

99 bottles of beer: Difference between revisions

→‎{{header|C++}}: Preprocessing solution
(→‎{{header|C++}}: added template metaprogramming solution)
(→‎{{header|C++}}: Preprocessing solution)
Line 347:
bottle_countdown<100, 1>::print();
}</c>
 
=== A preprocessor solution ===
Of course, with the template metaprogramming solution, the program has still do the conversion of numbers to strings at runtime, and those function calls also cost unnecessary time. Couldn't we just compose the complete text at compile time, and just output it at run time? Well, with the preprocessor, that's indeed possible:
 
<c>#include <iostream>
#include <ostream>
 
#define BOTTLE(nstr) nstr " bottles of beer"
 
#define WALL(nstr) BOTTLE(nstr) " on the wall"
 
#define PART1(nstr) WALL(nstr) "\n" BOTTLE(nstr) \
"\nTake one down, pass it around\n"
 
#define PART2(nstr) WALL(nstr) "\n\n"
 
#define MIDDLE(nstr) PART2(nstr) PART1(nstr)
 
#define SONG PART1("100") CD2 PART2("0")
 
#define CD2 CD3("9") CD3("8") CD3("7") CD3("6") CD3("5") \
CD3("4") CD3("3") CD3("2") CD3("1") CD4("")
 
#define CD3(pre) CD4(pre) MIDDLE(pre "0")
 
#define CD4(pre) MIDDLE(pre "9") MIDDLE(pre "8") MIDDLE(pre "7") \
MIDDLE(pre "6") MIDDLE(pre "5") MIDDLE(pre "4") MIDDLE(pre "3") \
MIDDLE(pre "2") MIDDLE(pre "1")
 
int main()
{
std::cout << SONG;
}</c>
 
An inspection of the generated executable proves that it indeed contains the complete text of the song in one block.
 
=={{header|Common Lisp}}==
973

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.