開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dart (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の4章(モジュール)、4.8(練習問題)、1、2をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
4.8(練習問題)、1、2.
コード
sample.dart
import 'dart:math' as math;
bool isLeapYear(DateTime date){
var d = new DateTime(date.year),
next_d = new DateTime(d.year + 1),
duration = next_d.difference(d);
if (duration.inDays == 366){
return true;
}
return false;
}
void main(){
print('1.');
for (var n in [(-4.3).round().abs(), math.sin(34.5).ceil()]){
print(n);
}
print('2.');
var td = new DateTime.now(),
d = new DateTime(td.year);
while (!isLeapYear(d)){
d = d.add(const Duration(days:365));
}
print('次のうるう年: ${d.year}年');
var count = 0;
for(var year = 2000; year <= 2050; year += 1){
if (isLeapYear(new DateTime(year))){
count += 1;
}
}
print('2000年から2050年までの間にあるうるう年の回数: $count回');
var weekday = new DateTime(2016, 7, 29).weekday;
print({
DateTime.MONDAY:'Monday',
DateTime.TUESDAY: 'Tuesday',
DateTime.WEDNESDAY: 'Wednesday',
DateTime.THURSDAY: 'Thursday',
DateTime.FRIDAY: 'Friday',
DateTime.SATURDAY: 'Saturday',
DateTime.SUNDAY: 'Sunday'}[weekday]);
}
入出力結果
1. 4 1 2. 次のうるう年: 2016年 2000年から2050年までの間にあるうるう年の回数: 13回 Friday
0 コメント:
コメントを投稿