開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の17章(高度なポインタ)、17-12(プログラミング実習)、実習17-1.をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
17-12(プログラミング実習)、実習17-1.
コード
sample.dart
import 'dart:html';
void main(){
String s = 'kamimura';
InputElement input = querySelector('#f0');
InputElement run = querySelector('#run_dart');
InputElement clear = querySelector('#clear');
Element pre = querySelector('#pre0');
run.onClick.listen((MouseEvent event){
pre.text = window.navigator.userAgent + '\n';
Tree tree = new Tree();
try{
File f = input.files.first;
FileReader reader = new FileReader();
reader.readAsText(f);
reader.onLoad.listen((Event event){
String s = reader.result;
tree.scan(s);
pre.text += '$tree';
});
} catch(e){
pre.text += '$e';
}
});
clear.onClick.listen((MouseEvent event) => pre.text = '');
}
class Node{
String word;
Tree left;
Tree right;
List<int> linenos = [];
Node(String word, n){
this.word = word;
left = new Tree();
right = new Tree();
linenos.add(n);
}
}
class Tree{
Node root;
static int current_lineno = 1;
Tree(){
root = null;
}
void insert(String word, int n){
if(root == null){
root = new Node(word, n);
return;
}
int result = root.word.compareTo(word);
if (result == 0){
root.linenos.add(n);
} else if(result > 0){
if (root.left == null){
root.left = new Tree();
}
root.left.insert(word, n);
} else {
if (this.root.right == null){
root.right = new Tree();
}
root.right.insert(word, n);
}
}
void scan(String text){
List<String> chars = text.split('');
String word = '';
RegExp pattern = new RegExp('[A-Za-z]');
for(String ch in chars){
if (ch == '\n'){
current_lineno += 1;
}
if(pattern.hasMatch(ch)){
word += ch;
} else if(word != ''){
insert(word, current_lineno);
word = '';
} else {
word = '';
}
}
}
String toString(){
if(root == null){
return '';
}
return root.left.toString() +
root.word + ':${root.linenos.join(', ')}\n' +
root.right.toString();
}
}
0 コメント:
コメントを投稿