sprintf in C & String.format in Java

可將字串插入可以變動的int
用for就可以產生一大批
ex. 1-01.txt, 1-02.txt, 1-03.txt ….等 很像但是有規則的字串
最常用在檔名處理上

sprintf in C:

sprintf(str, “%d”, num);
printf(“str = %sn”, str);

—————————————————————

String.format in Java:

// Store the formatted string in ‘result’
String result = String.format(“%4d”, i * j);
// Write the result to standard output
System.out.println( result );

float vs. double in Java?

public class everyday{
 
    public static void main(String[] args){
        System.out.println(“Hello Java”);
        float result=0;
        result = add(22.2,11.1);
        System.out.println(“result: “+ result);
    }
 
    public static float add(float n1, float n2){
        return (n1+n2);
    }
}

compile後,竟然跑出這樣的error message?

不太懂給一個值時,系統要怎麼判斷值為double還是float?

難道一定要hard coding把22.2和11.1強制casting到(float)嗎?

System Programming ( Signal ) in C language – Fishing Game

#include <stdio.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
bool fishingRodFlag= false;
bool fishEscapeFlag= true;
bool waitingFishAlarm= true;
static int fish=0;
static int pid=0;
static int ranFishUpTime=0;
static int ranFishEscapeTime=0;
void sig_handler(int used){
switch(used){
case SIGINT:
if(!fishingRodFlag){
printf(“nCast the fishing rod!n”);
printf(“Bait into water, waiting fish…n”);
fishingRodFlag = true;
waitingFishAlarm = true;
fishEscapeFlag = true;
ranFishUpTime = (rand()%3)+3;
alarm(ranFishUpTime);
}else{
printf(“nPull the fishing rodn”);
if(!fishEscapeFlag){
fish++;
printf(“Catch a fish!n”);
alarm(0);
}else{
if(!waitingFishAlarm)
printf(“The bait is eatenn”);
}
fishingRodFlag = false;
alarm(0);
printf(“nFishing rod is ready!n”);
}
break;
case SIGTSTP:
printf(“nTotally catch fishes: %dn”,fish);
kill(pid, SIGSTOP);
break;
case SIGALRM:
if(waitingFishAlarm){
if(fishingRodFlag){
printf(“nFish is bitting, pull the fishing rod!n”);
ranFishEscapeTime= 3;  // 3 secs later will escape
alarm(ranFishEscapeTime);
fishEscapeFlag = false;
}
waitingFishAlarm = false;
}else{
fishEscapeFlag = true;
if(fishEscapeFlag){
printf(“nThe fish was escaped!n”);
}
}
break;
}
}
int main(int argc, char* argv[]){
pid = getpid();
//printf(“%d”, pid);
signal(SIGINT, sig_handler);
signal(SIGTSTP, sig_handler);
srand(time(NULL));
signal(SIGALRM, sig_handler);
printf(“nnThis program designed by RongSonHo from CCU comm departmentn”);
printf(“ for System Programming course.n”);
printf(“Designed Date: 2016/05/25(Wed.)n”);
printf(“nnWelcome to RS Fishing Game!n”);
printf(“This Game use system signal to implement.n”);
printf(“nRules:n”);
printf(“ Casting down the fishing rod / Pulling up the rod: Ctrl+Cn”);
printf(“ Exit the Game and get the fishing result: Ctrl+Zn”);
printf(“nnFishing rod is ready!n”);
while(1)
sleep(1);
return 0;

}

How to setup mysql on mac os x

1. 抓mysql community server:

http://dev.mysql.com/downloads/mysql/
2. 到system preference-> mysql -> Start MySQL Server
3. 打開terminal-> 設定環境變數
 1) 輸入指令: sudo vim /etc/bashrc  (ReadOnly file)
 2) 在最下面新增
        #mysql
        alias mysql=’/usr/local/mysql/bin/mysql’
        alias mysqladmin=’/usr/local/mysql/bin/mysql/mysqladmin’
=> 打完按ESC => :wq! 儲存
3) 輸入指令:source ~/.bash_profile (應該是重開bash
4) 設定密碼:mysqladmin -uroot password 0000  (0000為密碼,自行修改
5) 測試登入:mysql -u root -p
=> 輸入 0000
or
     輸入指令:mysql

* 參考:
1. 陳雲濤的部落格:http://violin-tao.blogspot.tw/search/label/Database
2. Mac 安裝 MySQL筆記:http://f.pil.tw/thread-108370-1-1.html

4. 打開MySQLWorkbench
會看到

點進去就看到

Linux file/folder usage

1. /bin : 存放系統工具程式檔案們的資料夾,例如 pwd, rm, echo, cat, mkdir, mv, mv, dd, chmod…

2. /dev: 存放裝置相關檔案們的資料夾,例如

3. /etc: 系統開機時,所需要讀取的檔案,例如 人員的帳密、系統設定、要開啟的服務
ex. networks
ps. 在/etc工作要記得備份!!or 修改時也要記得鎖定,不要讓別人也能開

4. /home: 家目錄 ??
5. /lib: 各種library, 例如 java, python…
6. /usr: 很重要的資料夾,放置系統程式和指令,例如

7. /var: 系統工作時,預設的目錄,例如 使用者的登陸檔案資訊、還沒寄出的郵件…

參考:
1. 鳥哥linux: http://linux.vbird.org/linux_basic/redhat6.1/linux_05file.php

Raspberry pi Recall

1. 先接TTL到電腦

電腦輸入command: 
screen /dev/cu.usbserial
2. TTL連上後,讓Rpi連上網路(重新整理網路設定,並重新連線)

Rpi輸入command:
/etc/init.d/networking restart

3. Rpi連上網路後,查看IP
Rip輸入command:
hostname -I
or
ifconfig

4. 以SSH連線(這樣在Rpi打code比較順,否則用TTL連線的只能顯示一行,根本不能打code)
電腦輸入command:
ssh root@172.20.10.5

5. 在Rpi以GPIO控制LED

待續

參考:
1. 以不同使用者登入ssh:https://www.raspberrypi.org/documentation/remote-access/ssh/unix.md
2. 葉難(寫Python)(GPIO寫LED閃爍):http://yehnan.blogspot.tw/2012/07/raspberry-pigpioled.html
3. CodeSchool(寫)Java:http://www.codedata.com.tw/java/java-embedded-7-raspberry-pi-gpio-implementation-1/
4. 智慧生活(寫)Python:http://cheng-min-i-taiwan.blogspot.tw/2013/04/raspberry-pi-python.html