開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の4章(モジュール)、4.8(練習問題)、1から13をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
4.8(練習問題)、1から13.
コード
sample.dart
import 'dart:html';
void main() {
query("#run_dart").onClick.listen((MouseEvent event){
var start = new DateTime.now().millisecondsSinceEpoch,
n = 30,
result = run(event);
query('#pre0').text = window.navigator.userAgent + '\n' +
'${(new DateTime.now().millisecondsSinceEpoch - start) / 1000}秒\n' +
result;
});
query('#clear').onClick.listen((MouseEvent event){
query('#pre0').text = '';
});
}
String run(MouseEvent event){
var result = '',
alkaline_earth_metals = [4, 12, 20, 38, 56, 88],
country_populations = [1295, 23, 7, 3, 47, 21],
temps = [25.2, 16.8, 31.4, 23.9, 28, 22.5, 19.6],
cool_temps,
warm_temps,
temps_in_celsius,
temps_in_fahrenheit;
result += '1. ${alkaline_earth_metals}\n';
result += '2.\nラジウムの原子番号: ${alkaline_earth_metals[5]} ' +
'${alkaline_earth_metals.last}\n';
result += '3.リストの長さ ${alkaline_earth_metals.length}\n';
alkaline_earth_metals.sort((num a, num b){
return a - b;
});
result += '4. 最も高い原子番号: ${alkaline_earth_metals.last}\n';
result += '6.1行にお1つずつ表示\n';
alkaline_earth_metals.forEach((e){
result += '${e}\n';
});
result += '7. 1行に全て表示\n';
alkaline_earth_metals.forEach((e){
result += '${e} ';
});
result += '\n';
result += '8. ${country_populations}\n';
result += '合計: ${country_populations.fold(
0, (prev, elem) => prev + elem)}\n';
result += '9. ${temps}\n';
temps.sort();
result += '10. ソート ${temps}\n';
cool_temps = temps.takeWhile((e) => e < 20);
warm_temps = temps.sublist(cool_temps.length);
result += '11.\ncool_temps: ${cool_temps.toList()}\nwarm_temps: ${warm_temps}\n';
temps_in_celsius = [];
temps_in_celsius.addAll(cool_temps);
temps_in_celsius.addAll(warm_temps);
result += '12. 摂氏: ${temps_in_celsius}\n';
temps_in_fahrenheit = temps_in_celsius.map((e) => e * 9 / 5 + 32);
result += '13. 華氏に変換: ${temps_in_fahrenheit.toList()}';
return result;
}
0 コメント:
コメントを投稿