ページ

2011年4月12日火曜日

[C++] バブルソート書いてみた

このエントリーをはてなブックマークに追加



・2011年2月頃にC++で書いたバブルソート

#include <iostream>
#include <vector>
#include <ctime>

#define N 10

using namespace std;
typedef unsigned int size_t;

namespace sort{

//==============================================
// 交換関数
// argument :
// type : 交換したい数値01
// type : 交換したい数値02
//==============================================
template <class type> void swap(type * x, type * y){
type tmp = *x;
*x = *y;
*y = tmp;
}

/************************************************
/* バブルソートクラス
************************************************/
class CBubble{
public:
template <class type> void sort(type * X, bool flag){
for(size_t i = 0; i < X->size() - 1; i++){
for(size_t j = X->size() - 1; j > i; j--){
if(flag){
// 昇順
if(X->at(j - 1) > X->at(j)){
swap(&X->at(j - 1), &X->at(j));
}
}
else{
// 降順
if(X->at(j - 1) < X->at(j)){
swap(&X->at(j - 1), &X->at(j));
}
}
}
}
}
};

};

using namespace sort;


//===============================================
// main
//===============================================
int main(const int argc, char ** argv){

srand((size_t)time(NULL));


vector<float> X;

for(size_t i = 0; i < N; i++){
X.push_back((float)rand() / 3);
}

CBubble * objBubble = new CBubble;
objBubble->sort(&X, 1);

for(size_t i = 0; i < N; i++){
cout << X.at(i) << endl;
}

delete objBubble;

return 0;
}

2011年4月8日金曜日

[php] livedoor Auth を使用してみる

このエントリーをはてなブックマークに追加



<?php

class Clivedoor{

// REQUEST URL
private $requestURL = "http://auth.livedoor.com/login/?";
// Get User ID URL
private $userIdURL = "http://auth.livedoor.com/rpc/auth";


// 「元データ」と「共通鍵」を合成して、その結果に対しハッシングして ハッシュ値を生成 (HMAC)
// secret_keyはhttp://auth.livedoor.com/でアプリケーションの登録をして取得
private $secret_key = "dofijrg39t8ughvw0(sample)";
// ハッシュの名前
private $hash = "sha1";


// 使用する要素
private $app_key = "app_key";
private $format = "format";
private $perms = "perms";
private $t = "t";
private $token = "token";
private $userdata = "userdata";
private $v = "v";
private $sig = "sig";
// ログインの確認
private $loginFlag = "livedoor";
// $_GET[$this->userdata]の中身
public $callBackParameter = "";
// 要素を格納する配列 (URL)
private $paramURL = Array();
// 要素を格納する配列 (XML)
private $paramXML = Array();



//===========================================
// 初期設定
//===========================================
public function __construct(){

// 要素はアルファベット順に並べてパラメータとつなげてhash_hmac();
// app_keyはhttp://auth.livedoor.com/でアプリケーションの登録をして取得
$this->paramURL[$this->app_key] = "asdw9850tugbiodhf2(sample)";
$this->paramURL[$this->perms] = "id";
$this->paramURL[$this->t] = time();
$this->paramURL[$this->userdata] = $this->loginFlag;
// 浮動小数ではなく文字列 "1.0" / ver 1.0 (2011/3/3)
$this->paramURL[$this->v] = "1.0";
$this->paramURL[$this->sig] = $this->makeSig($this->paramURL);

// Login Success
if(isset($_GET[$this->userdata])){
if($_GET[$this->userdata] === $this->loginFlag){
$this->callBackParameter = $_GET[$this->userdata];
$this->paramXML[$this->app_key] = $_GET[$this->app_key];
$this->paramXML[$this->format] = "xml";
$this->paramXML[$this->t] = $_GET[$this->t];
$this->paramXML[$this->token] = $_GET[$this->token];
$this->paramXML[$this->v] = $_GET[$this->v];
$this->paramXML[$this->sig] = $this->makeSig($this->paramXML);
}
}

}



//===========================================
// シグネチャの作成
// argument :
// $array : パラメータ
// return value :
// シグネチャ(パラメータと値をアルファベット順にソートして, hmac(sha1)でハッシュ化)
//===========================================
private function makeSig($array){

// 元データの作成
$buf = "";
foreach($array as $key=>$value){
$buf .= $key.$value;
}
// hmacによるハッシュの作成
return hash_hmac($this->hash, $buf, $this->secret_key);

}



//===========================================
// REQUEST URLの作成
// return value :
// REQUEST URL
//===========================================
public function makeURL(){

$url = $this->requestURL;

// REQUEST URLの生成
$count = 0;
foreach($this->paramURL as $key=>$value){
if($count){ $url .= "&"; }
if(!$count){ $count++; }
$url .= $key."=".$value;
}

return $url;
}



//===========================================
// User情報が格納されたXMLの取得
// return value :
// XML
//===========================================
public function makeXML(){

$url = $this->userIdURL;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->paramXML);
$userId = curl_exec($ch);
curl_close($ch);

preg_match("/(message>)([^<]*)/", $userId, $message);
preg_match("/(livedoor_id>)([^<]*)/", $userId, $livedoor_id);

// ログインに成功
if($message[2] === "SUCCESS"){
// userIDを返す
return $livedoor_id[2];
}
// ログインに失敗
return "ログインに失敗しました. お手数ですが, もう一度お試しください.";
}


};



// Clivedoorのインスタンス化
$objLivedoor = new Clivedoor();

print('<a href="'.$objLivedoor->makeURL().'">'.$objLivedoor->makeURL().'</a>');

print("<br>");

// もし成功しているならユーザIDを表示する
if($objLivedoor->callBackParameter){print($objLivedoor->makeXML());}

?>