2013年7月26日 星期五

Linux建立新的硬碟

fdisk -l
使用fdisk -l列出現有的硬碟~~~

???@???-lubuntu:~$ sudo fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders, total 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d073b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   124780543    62389248   83  Linux
/dev/sda2       124782590   125827071      522241    5  Extended
/dev/sda5       124782592   125827071      522240   82  Linux swap / Solaris

Disk /dev/sdb: 68.7 GB, 68719476736 bytes
255 heads, 63 sectors/track, 8354 cylinders, total 134217728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xbbb1368f

   Device Boot      Start         End      Blocks   Id  System

有一顆sdb沒有被分割~~~

使用fdisk /dev/sdb分割硬碟

???@???-lubuntu:~$ sudo fdisk /dev/sdb

Command (m for help): m   //輸入m找help
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n   //新增partation
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p   //主要磁碟
Partition number (1-4, default 1): 1
First sector (2048-134217727, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-134217727, default 134217727):
Using default value 134217727

Command (m for help): w   //寫入後離開
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

再看一次,已經分割好了~~~

???@???-lubuntu:~$ sudo fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders, total 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d073b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   124780543    62389248   83  Linux
/dev/sda2       124782590   125827071      522241    5  Extended
/dev/sda5       124782592   125827071      522240   82  Linux swap / Solaris

Disk /dev/sdb: 68.7 GB, 68719476736 bytes
171 heads, 8 sectors/track, 98112 cylinders, total 134217728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xbbb1368f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   134217727    67107840   83  Linux

使用mkfs.ext3 /dev/sdb1格式化成ext3格式

???@???-lubuntu:~$ sudo mkfs.ext3 /dev/sdb1 
mke2fs 1.42.5 (29-Jul-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
4194304 inodes, 16776960 blocks
838848 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
512 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Asus Eee PC 1005PE w/ Android x86使用Bluetooth分享手機網路

很簡單,因為基本的profile都包了~~~
把手機藍芽網路分享打開,讓Eee PC跟手機透過藍芽連線~~~

在Eee PC開啟控制台視窗,輸入"netcfg"會看到bnep0,Bluetooth網路~~~
由於預設並沒有設定,所以不會獲得IP.

下"netcfg bnep0 dhcp",透由DHCP獲得位置,網路就可以用了~~~
不知道能不能用dhcpcd bnep0,應該來試試的 ><

但是Bluetooth網路並沒有設定DNS server,不認得yahoo、google等這些網址,
可以透由"getprop net.dns1"看看DNS的設定。
透由"setprop net.dns1 8.8.8.8"設定DNS。
瀏覽器就可以用Bluetooth上網了。

/sys/class/net/bnep0,在這裡可以看到有bnep0。

透由pand設定Bluetooth role。

Name
pand - BlueZ Bluetooth PAN daemon

Description
The pand (PAN daemon) allows your computer to connect to Ethernet networks using Bluetooth.
Synposis
pand <options>
Options
--show --list -l
   Show active PAN connections
--listen -s
   Listen for PAN connections
--connect -c <bdaddr>
   Create PAN connection
--search -Q[duration]
   Search and connect
--kill -k <bdaddr>
   Kill PAN connection
--killall -K
   Kill all PAN connections
--role -r <role>
   Local PAN role (PANU, NAP, GN)
--service -d <role>
   Remote PAN service (PANU, NAP, GN)
--ethernet -e <name>
   Network interface name
--device -i <bdaddr>
   Source bdaddr
--nosdp -D
   Disable SDP
--encrypt -E
   Enable encryption
--secure -S
   Secure connection
--master -M
   Become the master of a piconet
--nodetach -n
   Do not become a daemon
--persist -p[interval]
   Persist mode
--cache -C[valid]
   Cache addresses
--pidfile -P <pidfile>
   Create PID file
--devup -u <script>
   Script to run when interface comes up
--devdown -o <script>
   Script to run when interface comes down
--autozap -z
   Disconnect automatically on exit
Scripts
    The devup/devdown script will be called with bluetooth device as first argument and bluetooth destination address as second argument.

2013年7月25日 星期四

在Asus Eee PC 1005PE安裝Android x86心得

        在還沒流行Tablet時,買了一台Asus Eee PC 1005PE,現在開始覺得不管跑windows xp/7都很慢,也試過lubuntu,還是頓噸的,直到用習慣Android後,突然想來找找,看看能不能將Android上在Eee PC,找到真的有支援Eee PC的Android呀!!! x86版本的 ^^

在這裡下載ios,光碟映像檔~~~
Android x86 - Porting Android to x86

        一開始試用android-x86-4.2-20130228.iso,不過一直不能安裝,找不到boot裝置,看別人的blog大家都可以安裝,終於找到是硬碟格式的問題。 雖然安裝過程可以自己格式化,不過有時候iso因為識別硬碟格式錯誤,安裝就會有問題。 所以先格式化再來安裝,就OK了。 不過這已經是後話了,因為一直以為有問題就先用了android-x86-4.0-RC2-eeepc.iso安裝,一切都沒問題。

android-x86-4.0-RC2-eeepc.iso遇到的問題:
        WiFi可以動作,可以連接AP Router,但連接我的Xperia V無線分享就是有問題,無法取得IP,連iPhone就沒問題,讓我越來越佩服iPhone了。 雖然改了設定檔後可以取得IP但卻無法連到網際網路,DNS server也設了,還是不行,後來爬了很久發現原來Android 4.0 WiFi有些bug呀!!! 我不可能換手機,只好再回去試 Android 4.2了。
        在回去試Android 4.2之前,有先試過用Bluetooth PAN連接,讓Eee PC透過Bluetooth連接至Xperia V然後上網,真的可以用耶,可以用瀏覽器瀏覽網頁,但是Google Play等都不能用呀 ><
原來Andoird-x86-4.0只支援WiFi ><

        4.2安裝完後,目前WiFi都沒問題,只是偶爾會自己斷線重連,也下載了一些APP,但是APP很少,很多都不支援x86,PPS不能用呀 >< 而且很多都是直式的APP,有的會讓螢幕轉90度,而且還不會恢復,搞死人呀 Orz
幸好找到快捷鍵,可以方便轉回來 =.=
Ctrl-P         開啟設定
Alt+F1       進入console模式
Alt+F7       回到GUI模式
Alt+F9       正常畫面
Alt+F10     畫面旋轉 180 度
Alt+F10     畫面旋轉 180 度
Alt+F11     畫面向左旋轉 90 度
Alt+F12     畫面向右旋轉 90 度
4.2有個最嚴重的問題,就是休眠後就起不來了,找不到醒來的按鍵,瞎啦!!!
(重開不就得了) 不,重開後,你會看到黑畫面,需要按鍵回鎖定頁,找不到那個鍵 Orz
就等於不能用了 >< 還好從boot流程可以進到鎖定頁,可以解鎖繼續使用。

Android x86目前還不成熟,不過各方都還在努力,期待有天我的小筆電能復活 ^^

Android x86 4.0:
1. WiFi有些問題。(但一般連線都算正常)
2. 內建APP較完整。
3. 畫面解析度較差。
4. 不會有休眠起不來的問題。

Android x86 4.2:
1. WiFi尚算OK。
2. 畫面較細緻。
3. 內建APP較少。(我找不到內建的Mail)
4. 休眠後就起不來了。
    (這個很嚴重,因為最長休眠時間只能設到30分鐘,表示你不能放著不用,不用就要關機,        這是另類的節能省碳、愛地球)

P.S. 4.2 iso檔並沒有eeepc字樣,而4.0有,4.0應該對eeepc支援度較高吧。

記得來安裝LIME HD中文輸入法。

2013年7月24日 星期三

找不到5G WiFi AP Router

找不到5GHz的WiFi AP Router!!!

網路上找了一下,Device是支援5280MHz~5320MHz、5745~5825MHz
而WiFi AP Router自動掃描設定在5240MHz,Device沒支援~~~
設成5745MHz,就認到了~~~

2013年7月4日 星期四

出現找不到xxx-gcc

安裝Cross Toolchain時,會export環境變數~~~
可以cat /etc/profile
#Cross Toolchain PATH
export PATH="/opt/target/bin:$PATH"
#
確定有寫export,但好像因為帳號權限問題,還是啥問題,就是認不到~~~
手動在compiler前export就可以了 ><

Show MTD

cat /proc/mtd
dev: size erasesize name
mtd0: 00X00000 000X0000 "ALL"
mtd1: 000X0000 000X0000 "Bootloader"
mtd2: 00XX0000 000X0000 "Kernel"

Build mkbootargs有問題

Build mkbootargs有問題,link不到一些library。
這問題很奇怪~~~

看了Makefile的寫法,
CC = gcc
TARGET = mkbootargs
all:
$(CC) -lz -o $(TARGET) mkbootargs.c
clean:
@rm $(TARGET) -rvf
@rm *.bin -rvf

把它改成醬,
$(CC) -o $(TARGET) mkbootargs.c -lz

很神奇,既然Build成功了,
這是怎麼回事 ><

Ubuntu TFTP Server建置

  1. mkdir /tftproot
  2. vi /etc/xinetd.d/tftp
    service tftp
    {
    socket_type = dgram
    protocol = udp
    wait = yes
    user = root
    server = /usr/sbin/in.tftpd
    server_args = -s /tftproot
    disable = no
    per_source = 11
    cps = 100 2
    flags = IPv4
    }
  3. vi /etc/inetd.conf
    #tftp dgram udp wait nobody /usr/sbin/tcpd /usr/sbin/in.tftpd
  4. /etc/init.d/xinetd restart

安裝Embedded Linux開發環境

Ubuntu好多library都沒裝,花了些時間找出來,把該裝的裝一裝

apt-get install bison
apt-get install flex
apt-get install gpp
apt-get install g++
apt-get install zlib1g-dev
apt-get install libncurses5-dev
apt-get install tftpd
apt-get install tftp
apt-get install xinetd
apt-get install build-essential

2013年7月3日 星期三

Linux搜尋檔案內容

find / -name "*.c" -exec grep -H "test" {} \;
搜尋根目錄底下*.c內含有test字樣的檔案
grep所接的參數為"test"空白{}空白\;

寫成bash檔,來使用
findkey.sh

#!/bin/bash
if [ "$1" = "-h" ] || [ "$1" = "-H" ] || [ "$1" == "--h" ] || [ "$1" == "--H" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
echo "findkey path filename keyword"
else
sudo find $1 -name $2 -exec grep -H $3 {} \;
echo "---THE END---"
fi
exit 0

findfile.sh

#!/bin/bash
if [ "$1" = "-h" ] || [ "$1" = "-H" ] || [ "$1" == "--h" ] || [ "$1" == "--H" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
echo "findfile path filename"
else
sudo find $1 -name $2
echo "---THE END---"
fi
exit 0

2013年7月1日 星期一

Lenovo ThinkPad X220

最近工作用的NB Lenovo ThinkPad X220
都有莫名奇妙無法開機的問題~
不過放了一段時間後沒用, 又可以開機了~
送修回來後, 結論是~~~
要把電池拔掉, 按住power鍵, 放完電再開機~
這這這這這~~~瞎~太瞎了~整個瞎~~~
結果還是壞了, 連硬碟都壞了 ><
又送修回來後, 目前還算正常~
好家在~ 好家在~~

設為首頁(JavaScript)

< script language="javascript" >
url=parent.frames.location;
document.write(< a href="#" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(url);" >
< font size=\"2\" color=\"#FFFF99\" >設為首頁< /font >< /a > ');
< /script >

加入我的最愛(JavaScript)

 < script language="javascript" > 
 document.write("< A href=\"javascript:bookmark()\">加入我的最愛< /font >< /A > ");  
< /script >

留言板(ASP)

talk.htm
< script language="javascript" > 
< !--  
function check()  
{  
project=document.myform.project.value;  
word=document.myform.word.value;  
if(project=="")  
{  
alert("沒有輸入主題!"); 
return;  
}  
else  
{  
if(word=="")  
{  
alert("沒有任何留言!"); 
return;  
}  
}  
document.myform.submit();  
}
 -- > 
< /script >

< html > 
< head >< title >留言板< /title >< /head >

< body background="../pic/bg.gif" vlink="#0000FF" >
< form action="../asp/tform.asp" Method=post name="myform" > 
< div align="left" >
< div align="center" > 
< center >  
< table border="1" width="634" height="1" bordercolor="#008000" >  
< tr >  
< td width="607" align="right" height="4" valign="top" colspan="2" > 
< p align="center" >
< font color="#0000FF" >& nbsp;& nbsp;& nbsp;& nbsp;< font size="5" >  < /font > < /font >
< font color="#000000" >< font size="5" >< b >留& nbsp;& nbsp;& nbsp;  言& nbsp;& nbsp;& nbsp; 板< /b >< /font >& nbsp;< /font >
< font color="#0000FF" >& nbsp;& nbsp;< /font >< font color="#0000FF" >  & nbsp;< /font >
< /p >  
< /td >  
< /tr >  
< tr >  
< td width="119" align="right" height="1" valign="top" >姓名:< /td >  
< td width="231" height="1" >< input type="text" name="name" size="27" tabindex="1" >
< font size="2" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;
< font color="#FF0000" >& nbsp;< /font >
< /font >
< /td >  
< /tr >  
< tr >  
< td width="119" align="right" height="1" valign="top" >旅遊地點:< /td >  
< td width="500" height="1" >< input type="text" name="address" size="27" tabindex="2" >< /td >  
< /tr >  
< tr >  
< td width="119" align="right" height="1" valign="top" >主題:< /td >  
< td width="500" height="1" >< input type="text" name="project" size="33" tabindex="3" >< /td >  
< /tr >  
< tr >  
< td width="119" align="right" height="16" valign="top" >  < p align="right" >留言:< /p >  < /td >  
< td width="500" height="16" >
< font size="2" >
< textarea rows="6" name="word" cols="68" tabindex="4" >< /textarea >
< /font >
< /td >  
< /tr >  
< tr >  
< td width="119" align="right" height="87" valign="top" >心情圖示:< /td >  
< td width="500" height="87" valign="top" align="right" nowrap >  
< p align="left" >  
< font size="2" >  
< input type="radio" value="../pic/person1.gif" name="person" tabindex="5" >
< img border="0" src="../pic/person1.gif" width="50" height="61" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; 
< input type="radio" value="../pic/person2.gif" name="person" tabindex="5" >
< img border="0" src="../pic/person2.gif" width="48" height="62" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;  
< input type="radio" value="../pic/person3.gif" name="person" tabindex="5" >
< img border="0" src="../pic/person3.gif" width="49" height="62" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;  
< input type="radio" value="../pic/person4.gif" name="person" tabindex="5" >& nbsp;& nbsp;
< img border="0" src="../pic/person4.gif" width="44" height="61" >
< br >  
< input type="radio" value="../pic/person5.gif" name="person" tabindex="5" >& nbsp;& nbsp;
< img border="0" src="../pic/person5.gif" width="53" height="59" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;  
< input type="radio" value="../pic/person6.gif" name="person" tabindex="5" >& nbsp;& nbsp;
< img border="0" src="../pic/person6.gif" width="51" height="59" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;  
< input type="radio" value="../pic/person7.gif" name="person" tabindex="5" >& nbsp;& nbsp;
< img border="0" src="../pic/person7.gif" width="42" height="56" >  & nbsp;& nbsp;& nbsp;& nbsp;& nbsp;
< input type="radio" value="no" name="person" tabindex="5" checked >
< /font >
< font color="#FF0000" >不選擇< /font >
< font size="2" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;< /font >
< /p >  
< /td >  
< /tr >  
< /table >  
< /center >  
< /div >
< /div > 
< p align="center" > 
< input type="button" value="留言" tabindex="6" onclick="check()" >
< font size="2" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; & nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; < /font >
< a href="../asp/tbook.asp" target="_self" >觀看留言板< font size="2" >& nbsp;< /font >< /a >
< font size="2" >& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; < /font >
< input type="reset" value="清除" name="reset" tabindex="7" >
< /p >  
< /form >  
< /body >                                              
< /html >

< html >
< head >< title >留言板< /title >< /head >
< body background="../pic/bg.GIF" vlink="#0000FF" > 
< /html >

tbook.asp
< HTML >
< HEAD >< TITLE >留言板< /TITLE >< /HEAD >
< BODY background="../pic/bg.gif" vlink="#0000FF" >
< H2 ALIGN=CENTER >
< b >留& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;言& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;板< /b >
< img border="0" src="../pic/acdrule.gif" width="100%" height="10" >
< /H3 >
< % 
SQL = "Select * From tbook Order By time DESC" 
Set conn = Server.CreateObject("ADODB.Connection") 
DBPath =Server.MapPath("../db/free.mdb") 
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &DBPath 
SET rs =Server.CreateObject("ADODB.Recordset") 
rs.open sql,conn,3,2 
If rs.BOF=True Then 
Response.Write "< h2 >< p align=center >抱歉目前無資料!< /p >< /h2 >" 
Response.Write "< p align=center >< a href=""javascript:history.back()"" >返回上一頁< /a >< /p >"
Response.End 
End If 
Page = CLng(Request("Page")) rs.PageSize=5 
If Page <  1 Then Page = 1 
If Page  > rs.PageCount Then Page = rs.PageCount 
ShowOnePage rs, Page
% >
< DIV ALIGN=right >
< A HREF="../html/talk.htm" >開始留言< /A >
< A HREF="../index.htm" >回首頁< /A > 
< %  
If Page <  > 1 Then Response.Write "< A HREF=tbook.asp?Page=1 >第一頁< /A > " 
Response.Write "< A HREF=tbook.asp?Page=" & (Page-1) & " >上一頁< /A > " 
End If 
If Page <  > rs.PageCount Then Response.Write "< A HREF=tbook.asp?Page=" & (Page+1) & " >下一頁< /A > " 
Response.Write "< A HREF=tbook.asp?Page=" & rs.PageCount & " >最後一頁< /A > " 
End If
% >
頁次:< FONT COLOR="Red" >< %=Page% >/< %=rs.PageCount% >< /FONT >
< /BODY >
< /HTML >
< %  
Sub ShowOnePage( rs, Page ) 
rs.AbsolutePage = Page 
For iPage = 1 To rs.PageSize 
RsToTbook rs rs.MoveNext 
If rs.EOF Then Exit For 
Next 
End Sub  
Sub RsToTbook( rs ) 
word=Replace(rs("word"), vbCrLf, "< BR >" ) 
If Len(rs("pic")) <  > 0 Then PicHTML = "< IMG SRC=" & rs("pic") & " >" 
Else PicHTML = "" 
End If
% >
< p align=left >
< TABLE BOBDER=0 WIDTH=640 > 
< TR >< TD WIDTH=60 >< %=PicHTML% >< /TD > 
< TD width=200 > 
< table bobder=0 align=left > 
< TR > < td >主 題:& nbsp;< %=rs("project")% >< /TD > < /TR > 
< TR > < TD >姓名:& nbsp;< %=rs("name")% >< /TD > < /TR > 
< TR > < td >時 間:< %=rs("time")% >< /TD > < /TR > 
< /table > 
< /TD > 
< TD width=380 BGCOLOR=#00FFFF valign="top" align=left >< %=word% >< /TD >  
< /TR >
< /TABLE >
< img border="0" src="../pic/acdrule.gif" width="100%" height="10" >
< % 
End Sub
% >

計數器(ASP)

< %
If Session("counter")=Empty Then
 Set conn = Server.CreateObject("ADODB.Connection")
 DBPath =Server.MapPath("../db/free.mdb")
 conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &DBPath
 SET rs =Server.CreateObject("ADODB.Recordset")
 rs.open "counter",conn,3,2
 If rs.BOF=True Then
 rs.AddNew
 rs("counter")=0
 rs.Update
 End If
 rs("counter")=rs("counter")+1
 rs.Update
Else
 Set conn=Server.CreateObject("ADODB.Connection")
 DBPath=Server.MapPath("../db/free.mdb")
 conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" &DBPath
 SET rs=Server.CreateObject("ADODB.Recordset")
 rs.open "counter",conn,3,2
 If rs.BOF=True Then
 rs.AddNew
 rs("counter")=0
 rs.Update
 End If
End If
Session("counter")=rs("counter")
% >

< html >

< head >
< title >計數器< /title >
< /head >

< body bgcolor="#0000FF" vlink="#0000FF" >
< font color="#FFFF99" >< %=rs("counter")% >< /font >
< /body >

< /htm l>

猜數字(C語言)

/* 猜數字遊戲 */
/* 使用C為開發工具 */
#include /* 包含DOS I/O函數 */
#include /* 包含亂數函式 */
#include /* 包含直接I/O函式 */

/* 程式開始 */
void main(void)
{
 clrscr();
 int i,j,k=0,l=0; /* k,l判斷A,B */
 char answer[5],input[4]; /* answer為答案;input為玩家輸入*/
 answer[0]=58; /* char為'0' */
 randomize(); /* 設定亂數由時間取得 */
 while(1) /* 進入遊戲 */
 {
 /* 電腦產生數值 */
 for(i=1;i
 {
 answer[i]=rand()%11+48; /* 亂數值介於1-10,除以質數 */
 /* 判斷是否有重複值 */
 for(j=0;j
 {
 if(answer[i]==answer[j])
 {
 i--;
 break;
 }
 }
 }
 /* 玩家開始玩遊戲 */
 while(1)
 {
 printf("\nPlease input four number or input Esc to exit:\n");
 /* 得到玩家輸入的值 */
 for(i=0;i
 {
 input[i]=getche();
 /* 假如輸入為esc則for迴圈 */
 if(input[i]==27)
 break;
 /* 判斷玩家輸入是否為0-9的數目 */
 if(input[i]57)
 {
 printf("\nWrong! You are not input number:\n");
 i=-1;
 }
 /* 判斷玩家是否輸入相同的值 */
 for(j=0;j
 {
 if(input[i]==input[j])
 {
 printf("\nWrong! You are input repeat number9:\n");
 i=-1;
 break;
 }
 }
 }
 /* 假如輸入為esc則跳出while迴圈並設key為1跳出遊戲 */
 if(input[i]==27)
 break;
 /* 判斷為多少A多少B */
 for(i=0;i
 {
 for(j=1;j
 {
 if(input[i]==answer[j])
 {
 if(i==j-1)
 k++;
 else
 l++;
 }
 }
 }
 /* 4A為正確 */
 if(k==4)
 {
 printf("\nYou are right!\n");
 k=0;
 l=0;
 break;
 }
 /* 輸出AB */
 else
 {
 printf("\n%dA%dB\n",k,l);
 k=0;
 l=0;
 }
 }
 /* 要繼續玩輸入'y' */
 printf("\nIf you want to play, please input y:\n");
 if(getche()!='y')
 break;
 }
}