開発環境
- OS X Lion - Apple(OS)
- Dart SDK (開発環境)
- Dart (プログラミング言語)
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の10章(章全部で復習), 10.5(練習問題続き)、「壁にビールが99本」をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
「壁にビールが99本」
コード
sample.dart
import 'dart:math' as math; String englishNumber(int number){ if(number < 0){ return '自然数(0から)を入力してください。'; } if(number == 0){ return 'zero'; } var num_string = ''; var ones_place = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; var tens_place = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; var teenagers = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]; var others = [[2, 'hundred'], [3, 'thousand'], [6, 'million'], [9, 'billion'], [12, 'trillion'], [15, 'quandrillion'], [18, 'quintillion'], [21, 'sextillion'], [24, 'septillion']]; var left = number; while(others.length > 0){ var other = others.removeLast(); var n = other[0]; var name = other[1]; var d = math.pow(10, n); var write = left ~/ d; left -= write * d; if (write > 0){ var pre = englishNumber(write); num_string = num_string + pre + ' ' + name; if(left > 0){ num_string += ' '; } } } var write = left ~/ 10; left -= write * 10; if(write > 0){ if(write == 1 && left > 0){ num_string += teenagers[left - 1]; left = 0; } else { num_string += tens_place[write - 1]; } if (left > 0){ num_string += ' '; } } write = left; left = 0; if(write > 0){ num_string += ones_place[write - 1]; } return num_string; } void main(){ var n = 10000; var s; while(n > 3){ n -= 1; if(n < 9990){ continue; } s = englishNumber(n); print('${s[0].toUpperCase() + s.substring(1)} bottles of beer on the wall, ' + '${s} bottles of beer!'); print('Take one down, pass it around, ${s} bottles of beer on the wall!'); if (n == 9990) { print(new List.filled(100, '*').join()); } } s = englishNumber(2); print('${s[0].toUpperCase() + s.substring(1)} bottles of beer on the wall, ' + '${s} bottles of beer!'); s = englishNumber(1); print('Take one down, pass it around, ${s} bottle of beer on the wall!'); print('${s[0].toUpperCase() + s.substring(1)} bottle of beer on the wall, ' + '${s} bottle of beer!'); print('Take one down, pass it around, no more bottles of beer on the wall!'); }
入出力結果
Nine thousand nine hundred ninety nine bottles of beer on the wall, nine thousand nine hundred ninety nine bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety nine bottles of beer on the wall! Nine thousand nine hundred ninety eight bottles of beer on the wall, nine thousand nine hundred ninety eight bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety eight bottles of beer on the wall! Nine thousand nine hundred ninety seven bottles of beer on the wall, nine thousand nine hundred ninety seven bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety seven bottles of beer on the wall! Nine thousand nine hundred ninety six bottles of beer on the wall, nine thousand nine hundred ninety six bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety six bottles of beer on the wall! Nine thousand nine hundred ninety five bottles of beer on the wall, nine thousand nine hundred ninety five bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety five bottles of beer on the wall! Nine thousand nine hundred ninety four bottles of beer on the wall, nine thousand nine hundred ninety four bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety four bottles of beer on the wall! Nine thousand nine hundred ninety three bottles of beer on the wall, nine thousand nine hundred ninety three bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety three bottles of beer on the wall! Nine thousand nine hundred ninety two bottles of beer on the wall, nine thousand nine hundred ninety two bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety two bottles of beer on the wall! Nine thousand nine hundred ninety one bottles of beer on the wall, nine thousand nine hundred ninety one bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety one bottles of beer on the wall! Nine thousand nine hundred ninety bottles of beer on the wall, nine thousand nine hundred ninety bottles of beer! Take one down, pass it around, nine thousand nine hundred ninety bottles of beer on the wall! **************************************************************************************************** Two bottles of beer on the wall, two bottles of beer! Take one down, pass it around, one bottle of beer on the wall! One bottle of beer on the wall, one bottle of beer! Take one down, pass it around, no more bottles of beer on the wall!
0 コメント:
コメントを投稿