<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Bootstrap Practice</title> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container" style="padding:20px 0"> <p>product a <span class="label label-primary">NEW!</span></p> <p>Inbox <span class="badge">5</span></p> <p>Inbox <span class="badge"></span></p> <div class="alert alert-info"> <button class="close" data-dismiss="alert">×</button> おしらせ </div> <div class="panel panel-primary"> <div class="panel-heading"> お知らせ </div> <div class="panel-body"> こんにちは! </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html>
Category: uncategorized
GNU Emacsを触ってみよう
mac os10では標準でunixコマンドラインにインストールされているので確認してみましょう。
mac-no-MacBook-Air:~ mac$ emacs --version GNU Emacs 22.1.1 Copyright (C) 2007 Free Software Foundation, Inc. GNU Emacs comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of Emacs under the terms of the GNU General Public License. For more information about these matters, see the file named COPYING.
emacsの起動は、emacs、終了はcommand+cです。
画面:
-window:画面
-buffer:タブ
-mode line:今開いているbufferの情報
-mini buffer:コマンドの情報
ファイルは、コマンドキーで、ctr-x, ctr-fで、ディレクトリが表示されるので、編集したいファイルを開きます。
移動は以下のコマンドでも動けます。
c-f
c-b
c-p
c-n
c-a
c-e
angular.js
angular.module('myapp', []) .controller('MainController', ['$scope', function($scope) { $scope.users = [ {"name":"taguchi", "score":52.22}, {"name":"tanaka", "score":38.22}, {"name":"yamada", "score":11.11}, {"name":"hayashi", "score":5.25}, {"name":"tanahashi", "score":82.4}, {"name":"yasuda", "score":55.21}, {"name":"minami", "score":32.8}, {"name":"yanagi", "score":72.2} ]; }]) .controller('UserItemController', ['$scope', function($scope) { $scope.increment = function() { $scope.user.score++; }; }]);
<!DOCTYPE html> <html lang="ja" ng-app="myapp"> <head> <meta charset="UTF-8"> <title>Angularの練習</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <script src="myscript.js"></script> </head> <body> <h1>AngularJSの練習</h1> <div ng-controller="MainController"> <ul> <li ng-repeat="user in users" ng-controller="UserItemController"> {{user.name}} {{user.score}} <button ng-click="increment()">+1</button> </li> </ul> </div> </body> </html>
Rails textのインクルード
PHPのように、テキストをインクルードできます。
コントローラのアクションに記述した変数がビューに反映されて、ブラウザ上に変数値が表示されます。
/app/controllers/top_controller.rb
class TopController < ApplicationController def index @message = "Hello, rails" @txtmessage = "what's up?" end end
続いてです。
/app/viws/top/index.html.erb
<h1><%= @message %></h1> <p><%= @txtmessage %></p>
ブラウザを更新し、リクエストを送ります。
サーバーのWEBrickは起動したままにしましょう。
railsの起動
Railsのインストールが済んだら、コマンドプロンプトからアプリケーションを作成します。
C:\Users\hoge>cd \ C:\>mkdir rails C:\>cd rails C:\rails>rails new sakura C:\rails>cd sakura C:\rails\sakura>rails server
=> Booting WEBrick
=> Rails 4.2.7.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-10-25 04:13:56] INFO WEBrick 1.3.1
[2016-10-25 04:13:56] INFO ruby 2.3.1 (2016-04-26) [x64-mingw32]
[2016-10-25 04:13:56] INFO WEBrick::HTTPServer#start: pid=7256 port=3000
http://localhost:300 もしくは http://127.0.0.1:3000/ で、下記のようにrailsの初期画面が表示されたらインストール成功です。
画面上の「About your application’s environment」をクリックすると、状態が表示されます。
WEBrickは、Rubyで書かれたウェブサーバーで、Rubyをインストールすると一緒にインストールされます。ソースコードをウェブサーバーにアップロードすることなく、1台のパソコンでRailsアプリケーションの開発と動作確認ができます。
いったん Ctl + C でWEBrickを終了したら、作成したフォルダを見てみましょう。
字句解析
トークンごとに、その属性を表示します。文字種表も使います。識別子には英数字、下線を利用、先頭は英字が下線です。エスケープ文字や漢字、コメント機能もないものとします。
/*--------------------*/ /* 字句解析 token_p.cpp */ /*--------------------*/ #include <iostream> #include <fstream> #include <iomanip> #include <string> #include <cstdlib> #include <cctype> using namespace std; enum TknKind { Lparen=1, Rparen, Plus, Minus, Multi, Divi, Assign, Comma, DblQ, Equal, NotEq, Less, LessEq, Great, GreatEq, If, Else, End, Print, Ident, IntNum, String, Letter, Digit, EofTkn, Others, END_list }; struct Token { TknKind kind; string text; int intVal; Token() { kind = Others; text = ""; intVal = 0; } Token(TknKind k, const string& s, int d=0){ kind = k; text = s; intVal = d; } }; void initChTyp(); Token nextTkn(); int nextCh(); bool is_ope2(int c1, int c2); TknKind get_kind(const string& s); TknKind ctyp[256]; Token token; ifstream fin; struct KeyWord { const char *keyName; TknKind keyKind; }; KeyWord KeyWdTbl[] = { {"if", If }, {"else", Else }, {"end", End},{"print", Print }, {"(", Lparen}, {")", Rparen}, {"+", Plus }, {"-", Minus }, {"*", Multi },{"/", Divi }, {"=", Assign}, {",", Comma }, {"==", Equal }, {"!=", NotEq }, {"<", Less }, {"<=", LessEq }, {"", END_list}, }; int main(int argc, char *argv[]) { if (argc == 1) exit(1); fin.open(argv[1]); if (!fin) exit(1); cout << "text kind intVal\n"; initChTyp(); for (token = nextTkn(); token.kind != EofTkn; token = nextTkn()){ cout << left << setw(10) << token.text << right << setw(3) << token.kind << " " << token.intVal << endl; } return 0; } void initChTyp() { int i; for(i=0; i<256; i++) { ctyp[i] = Others; } for(i='0'; i<='9'; i++){ ctyp[i] = Digit; } for(i='A'; i<='Z'; i++){ ctyp[i]=Letter; } for(i='a'; i<='z'; i++){ ctyp[i]=Letter; } ctyp['(']= Lparen; ctyp[')'] = Rparen; ctyp['<']=Less; ctyp['>'] = Great; ctyp['+']= Plus; ctyp['-'] = Minus; ctyp['*']= Multi; ctyp['/'] = Divi; ctyp['_']= Letter; ctyp['=']= Assign; ctyp[',']= Comma; ctyp['"']= DblQ; } Token nextTkn() { TknKind kd; int ch0, num = 0; static int ch = ' '; string txt = ""; while (isspace(ch)){ ch = nextCh();} if (ch == EOF) return Token(EofTkn, txt); switch (ctyp[ch]){ case Letter: for ( ; ctyp[ch]==Letter || ctyp[ch]==Digit; ch=nextCh()){ txt += ch; } break; case Digit: for (num=0; ctyp[ch]==Digit; ch=nextCh()){ num = num*10 +(ch-'0'); } return Token(IntNum, txt, num); case DblQ: for (ch=nextCh(); ch!=EOF && ch!='\n' && ch!='"'; ch=nextCh()){ txt += ch; } if (ch != '"'){ cout << "文字列リテラルが閉じていない\n"; exit(1);} ch = nextCh(); return Token(String, txt); default: txt += ch; ch0 = ch; ch = nextCh(); if (is_ope2(ch0, ch)){ txt += ch; ch = nextCh();} } kd = get_kind(txt); if (kd == Others){ cout << "不正なトークンです:" << txt << endl; exit(1); } return Token(kd, txt); } int nextCh() { static int c = 0; if (c == EOF) return c; if ((c = fin.get())== EOF) fin.close(); return c; } bool is_ope2(int c1, int c2) { char s[] = " "; if (c1=='\0' || c2 == '\0') return false; s[1] = c1; s[2]= c2; return strstr(" <= >= == != ", s) != NULL; } TknKind get_kind(const string& s) { for (int i =0; KeyWdTbl[i].keyKind != END_list; i++){ if (s == KeyWdTbl[i].keyName) return KeyWdTbl[i].keyKind; } if (ctyp[s[0]] == Letter ) return Ident; if (ctyp[s[0]] == Digit) return IntNum; return Others; }
アルゴリズム
効率の良いアルゴリズムをつくるには、複雑性だけでなく、計算時間(running time)、領域(storage space)など、動的なものも考慮しなければならない。つまり、CPU・メモリの性能や、サーバー環境を熟知した上で、プログラムを組むことに他ならない。
また、高レベルのものは、問題の重要な構造に対応している。
アセンブラツール
よく使われる代表的なもの
1.MASA
Microsoft Macro Assemblerの略ともいわれ、当初は、Macro Assembler。MicrosoftのOS(DOSやWindows)上で実行するプログラムのために開発されたアセンブラです。
; hello.asm include ¥masm32¥include¥masm32rt.inc .code start: print "Hello, Assembly Language!",13,10 exit end start
2.NASM
NASM(Netwide Assembler)はGNUのLGPLで公開されているフリーソフトウェアのアセンブラです。このアセンブラはIntel x86ファミリーをターゲットとしており、様々な形式で出力することができます。
; ha.asm bits 16 org 0x100 mov ah, 2 ;文字出力を指定 mov dl,[msg] ;msgの先頭'H' int 21h mov dl,[msg+7] ; msgの8番目'a' int 21h mov ax,4C00h ;プログラム終了 int 21h msg db "Hello, assembler$"
3.GNU Assembler(GAS)
UNIX系の各種アーキテクチャ(x86, 680×0, SPARC, VAX)向けのアセンブラファミリーです。AT&T表記と呼ばれる構文を使っています。もともと、gcc(GNU C, C++コンパイラ)との親和性が高く、UNIX系OSでC/C++とリンクするアセンブリ言語プログラムを記述するためによく使われます。
# hello.S .text .global _start _start: movl $len,%edx movl $msg,%ecx movl $1,%ebx movl $4,%eax int $0x80 # exit movl $0, %ebx movl $1, %eax int $0x80 .data msg: .ascii "Hello, world!¥n" len = . - msg
言語判定
<html><meta charset="utf-8"><body><?php // フォームからの入力を得る $text = empty($_POST['text']) ? "" : $_POST['text']; $result = ""; if ($text != ''){ // 言語を判定する $data = count_text($text); $ann = fann_create_from_file('./lang.net'); $r = fann_run($ann, $data); $i = array_max_index($r); $lang_list = ['英語', 'タガログ語', 'インドネシア語']; $result = "<h3>".$lang_list[$i]."でしょう!</h3><ul>"; foreach ($r as $i => $v){ result .= "<li>".$lang_list[$i].":".floor($v*100)."%</li>"; } $result .= "</ul>"; } $text_enc = htmlspecialchars($text); // 配列で値が最大のインデックスを返す function array_max_index($a){ $mv = -1; $mi = -1; foreach ($a as $i => $v){ if ($mv < $v){ $mv = $v; $mi = $i; } } return $mi; } // アルファベットの個数を数える function count_text($text){ $text = strtolower($text); $text = str_replace(" ", '', $text); $cnt = array_fill(0, 26, 0); for ($i = 0; $i < strlen($text); $i++){ $c = ord(substr($text, $i, 1)); if (97 <= $c && $c <= 122){ $c -= 97; $cnt[$c]++; } } return $cnt; } ?> <h1>三ヶ国語の言語判定</h1> <form method="post"> <textarea name="text" rows=5 cols=60><?php echo $text_enc ?> </textarea><br> <input type="submit" value="言語の判定"> </form> <div><?php echo $result ?></div>
simple XML
phpでxmlを扱うにも、いろいろな方法があります。文字列のxmlを読み込んで解析し、xmlの内容を出力します。
<?php // set XML $xml_str = <<<XML <?xml version='1.0'?> <items> <item id="101"> <name>石鹸</name> <price>510</price> </item> <item id="102"> <name>ブラシ</name> <price>330</price> </item> </items> XML; // analyze XML $xml = simplexml_load_string($xml_str); // display each item info foreach ($xml->item as $it){ $attr = $it->attributes(); echo "(id:".$attr["id"].")"; echo $it->name." - ".$it->price." 円\n"; }