2011/11/12

[ArchLinux]開機時啟動Num Lock

X.org

If you use startx to start your X session, simply install the numlockx package and add it to your ~/.xinitrc file.
Install numlockx:
# pacman -S numlockx
Add it to ~/.xinitrc before exec:
#!/bin/sh
#
# ~/.xinitrc
#
# Executed by startx (run your window manager from here)
#

numlockx &

exec your_window_manager


2011/11/11

wxDev C++ 安裝

簡單的寫一下初學C++的工具「Dev C++」的安裝過程
(目前Dev C++官方已經來到了7.4版)

2011/11/02

[C++]讀檔寫到陣列


一支用來讀取文字檔內容並且將資料轉成數值供運算的程式
其中每一行的資料數n可以自訂
可以儲存的資料這邊預設最多到100行

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    int n;
    string Loc;
    string Line;
    string Get = "";
    int Row = 0;
    int Col = 0;
    int End = 0;
 
    cout << "請輸入檔案路徑:";
    getline(cin,Loc);
    cout << "每行有幾筆資料:";
    cin >> n;
 
    int Box[100][n];
 
    for(int i=0; i<100; i++)
    {
        for(int j=0; j<n; j++)
        {
            Box[i][j] = -1;
        }
    }
 
    ifstream infile(Loc.c_str(),ios::in);
 
    if(infile)
    {
        while(!infile.eof())
        {
            getline(infile,Line);
            Line = Line + " ";
         
            for(int i=0; i<Line.length(); i++)
            {
                if(Line[i] != ' ')
                {
                    Get = Get + Line[i];
                }
                else
                {
                    Box[Row][Col] = atoi(Get.c_str());
                    Get = "";
                    Col++;
                }
             
            }
            Row++;
            End++;
            Col = 0;
        }
        Line = "";
    }
    //
    else
    {
        cout << "Failed..." << endl;
    }
 
    for(int i=0; i<End; i++)
    {
        for(int j=0; j<n; j++)
        {
            cout << Box[i][j] << " ";
        }
        cout << endl;
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

2011/10/31

[C++]簡易寫檔


#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    string FileLocation;
    string Msg;
 
    cout << "請輸入輸出檔路徑:";
    getline(cin,FileLocation); //ex: c:\test.txt
   
    ofstream outfile(FileLocation.c_str());
 
    while(true)
    {
        cout << "請輸入一段文字:";
        getline(cin,Msg);
     
        if(Msg == "EXIT")
        {
            break;
        }
     
        outfile << Msg << endl;
        Msg = "";
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

2011/10/23

[Archlinux]Yaourt安裝

參考:
https://wiki.archlinux.org/index.php/Yaourt_(%E6%AD%A3%E9%AB%94%E4%B8%AD%E6%96%87)

Linux 手動校時

安裝完Archbang之後
進到系統發現右下角的時間並不正確
但是Openbox下時間的更改方法疑似和以前慣用的Gnome不同
直接按下工具列上時間的部份
但是並沒有找到可以調整的地方
(題外話:Gnome 3之後感覺愈變愈花俏了...)

所以就開終端機手動調整了~
作法鳥哥的Linux私房菜中有詳盡解說

*參考:http://linux.vbird.org/linux_server/0440ntp.php#date (第十五章、時間伺服器:NTP伺服器)


*後記
找到問題其實是出在rc.conf裏面的設定
安裝Archbang的時候在time and date選項裡把它選成UTC
參考Arch的官方文件這樣改
HARDWARECLOCK="localtime"
就正常了!!

[ArchLinux]Archbang中nvidia驅動&解析度設定

安裝Archbang的時候預設會安裝顯卡開源驅動
系統安裝完成之後如果要換成閉源驅動(以下以Nvidia為例)
實作步驟如下:

1.需要先移除衝突的套件libgl
pacman -Rdd libgl


2.接著安裝Nvidia驅動
pacman -S nvidia nvidia-utils

安裝需要先重新啟動,但是重啟之後在Screen Resolution中,能夠調整的解析度只有1024*768、800*600,並沒有達到手邊顯示卡能夠支援的最高解析度
所以參考了這幾篇:

3.為了讓系統出現更高解析度的選項,到/etc/X11/xorg.conf.d/ 底下,編輯20-gpudriver.conf這個檔案
(這邊以nano編輯器操作,也可以vi、vim來編輯)
cd /etc/X11/xorg.conf.d/  (可以先 ls 確認20-gpudriver.conf是否存在)
nano 20-gpudriver.conf

4.在Section選項中找到"Driver"欄位,因為我是用nvidia的顯示卡,所以把欄位內容改成"nvidia"
這邊的設定值原本是"vesa",(大概是這樣所以系統無法正確抓到driver?)
猜想這邊應該是就廠牌來填入資料
裝好驅動後依照廠牌填入nvidia、ati、intel...(依實際狀況而定)


離開前別忘了儲存設定!!

5.重新啟動電腦即可在Screen Resolution中更換支援的更高解析度




*後記
後來在家目錄下發現了Archbang的說明文件(檔名DOC)

2011/10/13

[C++]取出字串中的單字


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    //取出字串中的單字,其中單字與單字間以一個或數個空白隔開
    string str;
    string Temp = "";
 
    cout << "請輸入一段文字:";
    getline(cin,str);
    str = str + " "; //加入空白讓取值時判斷是否到達字串邊界
 
    for(int i=0; i<str.length(); i++)
    {
        if(str[i] != ' ')
        {
            Temp = Temp + str[i];
        }
        else if((str[i] == ' ')&&(str[i+1] != ' ')) //抓到最後一個空白才輸出
        {
            cout << Temp << endl;
            Temp = ""; //清空Temp的值讓迴圈進行下一次取新的值
        }
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

寫程式用的字型

最近重灌電腦
安裝IDE時順便上網找找有哪些適合拿來寫程式的字型
後來在這篇找到
作者推薦的字型裡實際用過看起來都滿舒服的
而且都有附上字型檔的載點
在這邊作個筆記備忘

P.S 文中提到的微軟Consolas字型可以從微軟網站免費下載
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17879

2011/10/11

[C++]求解兩數GCD、LCM (遞迴)


用遞迴來寫其實比較容易體會輾轉相除的過程

#include <cstdlib>
#include <iostream>

using namespace std;

int GCD(int a,int b)
{
    if(b == 0)
    {
        return a;
    }
    else
    {
        return GCD(b, a%b);
    }
}
int main(int argc, char *argv[])
{
    int m, n;
    cout << "請輸入第一個數:";
    cin >> m;
    cout << "請輸入第二個數:";
    cin >> n;
 
    int GetGcd = GCD(m,n);
    cout << "GCD: " << GetGcd << endl;
    cout << "LCD: " << GetGcd*(m/GetGcd)*(n/GetGcd) << endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

[C++]求解兩數GCD、LCM (非遞迴)

今天碰到的題目
利用輾轉相除法來做
非遞迴的版本


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int max,min,tmpm,tmpn,temp;
 
    cout << "請輸入第一個數:";
    cin >> max;
    cout << "請輸入第二個數:";
    cin >> min;
 
    if(max < min)
    {
        temp = max;
        max = min;
        min = temp;
    }
 
    tmpm = max;
    tmpn = min;
 
    if(max % min == 0)
    {
        cout << "GCD = " << min << endl;
        cout << "LCM = " << max << endl;
    }
    //
    else
    {
        while(min > 0)
        {
            max = max % min;
            min = min % max;
            if(min == 0)
            {
                cout << "GCD = " << max << endl;
                cout << "LCM = " << max*(tmpm/max)*(tmpn/max) << endl;
            }
        }
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}



2011/09/17

Jdownloader在Ubuntu下載完成自動關機

一直忙到今天才發現
hepha前輩在小弟提問之後馬上寫了篇教學文
之前一直沒有辦法解決的AutoShutdown問題終於順利解決了!
十分感謝前輩的協助

教學文網址:
 (手把手玩Linux-Jdownloader下載完畢時自動關機)

2011/08/18

Install Firefox 6.0 On Ubuntu By PPA


Firefox Stable Channel Packages

從上面的URL找到對應的Ubuntu版本
然後將來源加入到source list中

sudo gedit /etc/apt/sources.list

Ubuntu 10.04
deb http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu lucid main 
deb-src http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu lucid main 

Ubuntu 10.10
deb http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu maverick main 
deb-src http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu maverick main 

Ubuntu 11.04
deb http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu natty main 
deb-src http://ppa.launchpad.net/mozillateam/firefox-stable/ubuntu natty main 

2011/08/17

[ACM]Q10310 Dog and Gopher

嗯 首先呢 這是Dog&Gopher進階版(其實是誤解題意之下的產物)
上週卡到暑輔考試
 一直沒碰的Dog&Gopher
週末完成結果卡在pow()的用法上 compiler一直不給過...
昨天到學校待了一天 跟著老師Debug 修修補補之後才把原本的bug處理完...
重點是
後來才發現這題其實是不需要讀檔 用cin來寫就可以了(悲劇~~往好處想是多練習了一次開檔案處理???)

 不過這段Code應該還可以更精簡...

原題:http://luckycat.kshs.kh.edu.tw/homework/q10310.htm
測資:下載

2011/08/16

[轉載]ACM初學者中文簡介

ACM Online Judge Guide
介紹ACM線上解題系統的入門須知,方便初學者進入狀況

ACM Online Judge (一樣是說明文)

[c++]簡易剖析範例

#include <iostream>
#include <stdlib.h>

using namespace std;

void Parse(string source,string key)
{
    string Temp = "";
    source = source + key;

    for(int i=0; i<source.length(); i++)
    {
        if(source[i]!= key[0])
        {
            Temp = Temp + source[i];
        }
        else
        {
            cout << Temp << endl;
            Temp = "";
        }
    }
}
int main()
{
    string str;
    string keyword;

    cout << "Please input str:";
    getline(cin,str);
    cout << "Please input keyword:";
    getline(cin,keyword);
    cout << "Parse Result:" << endl;
    Parse(str,keyword);

    return 0;
}

2011/08/14

那些年,我們一起追的女孩 MV

Ubuntu安裝VLCPlayer

VLCPlayer是Linux下好用的DVD播放軟體
(個人喜歡基於MPlayerSMPlayer,也是不錯的選擇!)

Ubuntu 10.04以上的版本可以直接這樣下指令安裝
sudo apt-get update
sudo apt-get install vlc vlc-plugin-pulse mozilla-plugin-vlc

Ubuntu 10.04則需要先匯入PPA來源

sudo add-apt-repository ppa:lucid-bleed/ppa
sudo apt-get update
sudo apt-get install vlc vlc-plugin-pulse mozilla-plugin-vlc

2011/08/13

Ubuntu更換鏡像站

突然想到的問題所以就記下來
昨天剛裝好Ubunut 10.04.3
下更新指令時tw.archive.ubuntu.com總是會連不上
大概是台灣的鏡像站壞了
如此一來就無法透過台灣的鏡像站來進行套件的安裝及更新
這時候就要先透過修改鏡像來源以取得需要的套件

Ubuntu 10.04 安裝 LibreOffice

首先Oracle和OpenOffice的未來發展、為何原開發團隊投入LibreOffice的緣由
有興趣者可以參考以下連結:

(話說Ubuntu 11.04之前版本使用的應該叫Go OO,算是OpenOffice的一個分支,到了Ubuntu 11.04之後就正式把LibreOffice作為預設的辦公室軟體了)


進入重點
想要在Ubuntu 10.04中安裝LibreOffice需要藉由PPA的方式

1.首先刪除原本的OpenOffice
sudo apt-get remove openoffice*.*

2.加入PPA來源
sudo add-apt-repository ppa:libreoffice/ppa

3.更新一下來源
sudo apt-get update

4.安裝LibreOffice以及中文語系檔
sudo apt-get install libreoffice libreoffice-l10n-zh-tw

5.(這個應該是加上Gnome的框架我想,一開始忘記安裝LibreOffice介面醜醜的,還請前輩不吝指正囉)
sudo apt-get install libreoffice-gnome



※參考來源:

Ubuntu 10.04 安裝 CodeBlock 10.05

昨天剛剛從Ubuntu 11.04換回到10.04
在安裝CodeBlock時發現10.04官方套件庫裡的版本還停在10.02
(其實還有10.04滿多套件停在舊版,想要換到新版的話可以藉由PPA或者到官網下載新版安裝程式來更新)

所以參考了這篇:
http://dexter7311.pixnet.net/blog/post/27180890

依照文中的步驟把需要的套件裝好
需要注意的地方是版本代號的地方要改一下
文中的指令是edgy及feisty分別是6.10與7.04的代號
由於這邊用的是10.04 所以要把代號的部份改為lucid

一切程序完成之後
再到CodeBlock官網
Distro的地方找不到Ubuntu?
由上到下的Distro分別是Debian、Fedora、OpenSUSE、Mandriva
別擔心~Distro選擇的地方我們選用Debian的檔案來下載
最後依照參考文章中的作法
在終端機中完成安裝

解決Ubuntu解壓縮亂碼

在Ubuntu解壓縮.rar檔案
通常會裝上rar套件
但是解壓某些壓縮檔時
解出來的檔案會有亂碼的情形
爬文之後發現是rar作怪

解決的辦法是把rar、unrar移除
並且安裝7z-full和7z-rar

叫出終端機~
sudo apt-get remove rar
sudo apt-get remove unrar

sudo apt-get install 7z-full
sudo apt-get install 7z-rar

參考來源:http://dexter7311.pixnet.net/blog/post/25277201

解決Ubuntu下.txt亂碼問題

在Ubuntu下以預設的gedit打開.txt文字檔時
會出現中文亂碼的問題
解決的辦法是把BIG5編碼加入到gedit中

打開終端機輸入
gconf-editor

依序進入app→gedit-2→preferences→encoding
打開auto_detected
加入BIG5並且將BIG5選項移到最上方

調整視窗按鈕位置-Metacity Window Buttons

Ubuntu將原本的視窗按鈕(關閉、放大、縮小)預設放在左上角
在這裡可以用Metacity Window Buttons依照喜好來設置視窗按鈕的排列方式

官方網站:https://launchpad.net/mwbuttons
載點:http://launchpad.net/mwbuttons/trunk/v0.2/+download/mwbuttons-0.2.4.tar.gz

進到終端機,切換到mwbuttons-0.2.4.tar.gz所在的目錄底下

1.解壓縮
tar xvfz mwbuttons-0.2.4.tar.gz

2.切換到解開的目錄下
cd mwbuttons-0.2.4.tar.gz/

3.安裝
sudo ./install.sh

完成之後就可以在「應用程式」→「附屬應用程式」→「Metacity Window Buttons」來進行設定

2011/07/30

Ubuntu安裝Oracle Java JDK

Ubuntu預設套件庫來源是安裝OpenJDK
對於習慣用Oracle Java JDK(原Sun JDK)的人來說
可以參考下面這篇文章來替換

如何於Ubuntu安裝Oracle Java JDK?
http://single9.net/2011/05/how-to-install-java-jdk-in-ubuntu/

爬了一下網路上的設定方法
個人感覺上面是最簡單直覺的作法
唯一的小發現是編輯完JDK的路徑之後
必須先登出一次再登入
系統才能正確抓到Java的設定

2011/07/28

利用stringstream進行字串與數值的轉換

以往都用atoi()、atof()來將字串轉成整數或是小數
這邊也可以用stringstream來實做

下面是把字串轉成數值
當然反過來也可以把數值轉為字串
簡單的實作了以下code

2011/07/24

2011/07/14

[轉貼] Windows 7 安裝後開啟 AHCI

參考資料:
http://iphonedaily.com/windows-7-ahci-enable-ahci-after-install/ Windows 7 AHCI-安裝後開啟 AHCI 的方法

※其他關於AHCI的參考資料:
何謂AHCI?如何在AHCI系統上正確安裝作業系統
在已安裝的作業系統上開啟AHCI傳輸模式
在現有的xp中打開ahci並安裝驅動程式 (Xp使用者請參考這篇)


P.S 在Linux系統kernel中已經包含AHCI的驅動,只要在BIOS中開啟AHCI功能即可。

C++指定路徑讀檔

在Windows、Linux底下實做過
其實可以這樣寫

假設輸入檔叫做input.txt
在Windows下,輸入檔放在D:\
輸入檔路徑給 D:\input.txt

在Ubuntu下,輸入檔放在使用者家目錄 /home/kevin
輸入檔路徑給 /home/kevin/input.txt

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    string Loc;
    string LineData = "";
   
    cout << "請輸入檔案路徑:";
    getline(cin,Loc);

    ifstream infile(Loc.c_str(),ios::in);
 
    if(infile)
    {
        cout << "Succeeded..." << endl;
        while(!infile.eof())
        {
            getline(infile,LineData);
            cout << LineData << endl;
            LineData = "";          
        }
    }
   
    else
    {
        cout << "Failed..." << endl;
    }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

2011/07/13

C++在Widows下指定讀檔路徑



#include <iostream>
#include <fstream>

using namespace std;

int main()

{
    string InFile;
    int SlashNo = 0;
    string LineData = "";

    cout << "Input File Location:";
    getline(cin,InFile);

    for(int i=0; i<InFile.length(); i++)
    {
        if(InFile[i] == '\\')
        {
            SlashNo++;
        }
    }

    cout << "SlashNo = " << SlashNo << endl;


    int TotalLength = InFile.length()+SlashNo+1;
    char RealInputFile[TotalLength];
    int jdx  =0;

    for(int i=0; i<InFile.length(); i++)
    {
        if(InFile[i] == '\\')
        {
            RealInputFile[jdx] = InFile[i];
            jdx++;
            RealInputFile[jdx] = InFile[i];
        }

        else
        {
            RealInputFile[jdx] = InFile[i];
        }
        jdx++;
    }

    RealInputFile[TotalLength-1] = '\0';
    cout << "Current File Location : " << RealInputFile << endl;

    ifstream Source(RealInputFile,ios::in);

    if(Source)
    {
        cout << "Read Secceeded" << endl;
        while(!Source.eof())
        {
            getline(Source,LineData);
            cout << LineData << endl;
            LineData = "";
        }
    }

    else
    {
        cout << "Read Failed" << endl;
    }

    return 0;
}



2011/07/12

Ubuntu將CD/DVD存成.iso

在Windows底下
要將CD/DVD製作成.iso來儲存
常用Alcohol 120%、Daemon Tools等軟體來達成

在Linux底下
只要用一行指令就可以達到相同的功能~

dd if=/dev/cdrom of=輸出檔名


例如:

dd if=/dev/cdrom of=Ubuntu.iso
即從CD-ROM裝置讀取資料存成Ubuntu.iso

2011/07/11

在Ubuntu安裝hifree

hifree官方只有釋出Windows平台的版本
因此在Linux系統中需要藉由Wine模擬出運作的環境
相關的設定可以參考這篇:
http://jclin.blogspot.com/2011/04/linux-hifree.html  "在Linux上裝hifree"

解決Windows 7 出現「無法載入遠端連線管理員」的問題

剛剛打開Windows7
預設自動撥號連線出現了「無法載入遠端連連現管理員」的對話框
Google之後找到解法如下:

1.到控制台→系統管理工具→服務
2.找到Remote Access Auto Connection Manager,按右鍵→【啟動】
3.按右鍵→【內容】,將【啟動類型】欄位改為【自動】
4.重新啟動電腦

2011/07/10

Install Ubuntu One Client On Kubuntu

How do I install the GNOME Ubuntu One client on Kubuntu?

Although less than ideal, you should be able to get the GNOME Ubuntu One client working in KDE (Kubuntu) running these commands at a terminal session:

1. sudo apt-get install ubuntuone-client* 
2. u1sdtool -c


That will install GNOME dependencies and then kick off the process to setup your computer with Ubuntu One.

Note, you may notice getting prompted twice for your keyring password. To work around this, please try the following:

1.In a terminal session run:
 rm ~/.gnome2/keyrings/default*

2.Add the following lines to /etc/pam.d/kdm: 
auth optional pam_gnome_keyring.so try_first_pass session optional pam_gnome_keyring.so auto_start

3.Logout / login

4.In a terminal session run: 
u1sdtool -c

2011/07/09

Ubuntu安裝字型

有時候在Ubuntu下會需要一些在Windows上的字型
這時候可以先在Windows下把字型檔拷貝出來放到同一個資料夾內
例如:字型檔全部放入叫作「windows」 的資料夾,存到家目錄內
(家目錄路徑:/home/使用者名稱)

接著到Ubuntu,打開終端機

1.取得root權限,由於Ubuntu的字型檔路徑在/usr/share/fonts/truetype底下
需要有root 的權限才能夠進行變更。                  

sudo -i

接著輸入密碼


2.接著把檔案複製到/usr/share/fonts/truetype之下

cp -rp /home/使用者名稱/windows /usr/share/fonts/truetype
就可以將字型檔案複製到Ubuntu系統中


3.重新載入系統字型檔
fc-cache -f -v

收工!

Ubuntu為套件庫加入來源公鑰

當我們在Ubuntu的Source List加入新的來源
執行sudo apt-get update更新套件資訊時
有時候會碰到
「由於無法取得它們的公鑰,以下簽章無法進行驗證NO_PUBKEY 一串代號

只要在終端機輸入
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 上面出現的代號



就可以成功加入公鑰了

2011/07/03

Ubuntu下的簡易轉檔軟體-Mobile Media Converter

提供簡單的轉檔功能
還可以從Youtube抓取影片
有點類似Windows下的YoutubeDownloader HD

參考下列網址
http://it-easy.tw/ubuntu-mobile-media-converter/
出自 電腦不難

將Ubuntu 11.04下的OpenJDK換成Sun JDK

1.安裝JRE
sudo add-apt-repository ppa:ferramroberto/java
sudo apt-get update
sudo apt-get install sun-java6-jre sun-java6-plugin


2.安裝JDK
sudo apt-get install sun-java6-jdk


3.手動切換到Sun JDK
update-java-alternatives -s java-6-sun




※2011/08/13更新
可以參考這篇:ubuntu平台上用eclipse開發java或android程式

2011/07/02

Kubuntu下將輸入法換成gcin

依照版本將下列來源加入 /etc/apt/sources.list

lucid 10.04

deb http://ppa.launchpad.net/elleryq/gcin/ubuntu lucid main
deb-src http://ppa.launchpad.net/elleryq/gcin/ubuntu lucid main



maverick 10.10 

deb http://ppa.launchpad.net/elleryq/gcin/ubuntu maverick main
deb-src http://ppa.launchpad.net/elleryq/gcin/ubuntu maverick main



natty 11.04 

deb http://ppa.launchpad.net/elleryq/gcin/ubuntu natty main
deb-src http://ppa.launchpad.net/elleryq/gcin/ubuntu natty main



可以在終端機下命令
sudo -i (取得管理員權限)vim /etc/apt/sources.list (透過vim文字編輯器來編輯套件來源)

接著把來源加入到檔案中(自己是加到檔案的最底端)

儲存後把來源更新一下
sudo apt-get update

切換輸入法
im-switch -s gcin


登出再登入後即可使用gcin


P.S 在Kubuntu 11.04上面實作,gcin已經解決和特效衝突的「小灰」(殘影)bug了

2011/05/14

Angry Bird 網頁版

Google推出Angry Bird 網頁版
好像用支援HTML 5的瀏覽器就可以執行了(Chrome、IE9、Firefox 4.0)
還滿KUSO的XD
http://chrome.angrybirds.com/

週末小記

週六必須上課的日子
前兩天剛考完第二次段考
連續兩天熬夜
很抗拒一大早起床啊...

2011/04/23

Windows下用Eclipse寫C++

安裝以下工具:

1.JRE  (Eclipse本身是Java打造,需要JRE執行環境,若要寫Java程式則必須裝JRE、JDK)
2.Eclipse
3.CDT (讓Eclipse支援C/C++)
4.MinGW (C/C++ Compiler)

2011/04/01

亂數

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
    bool Find = false;
    int A,B;//表示範圍的起始與結束 
    int Change;
    int Num;
    int Temp;
    int Index = 0;

    srand(time(NULL));

    cout << "請輸入範圍起始值:";
    cin >> A;
    cout << "請輸入範圍結束值:";
    cin >> B;
    
    if(A>B)
    {
        Change = A;
        A = B;
        B = Change;
    }
    
    cout << "請輸入亂數個數:";
    cin >> Num;

    int Box[Num];

    for(int i=0; i<Num; i++)
    {
        Box[i] = 0;
    }
    Box[Num-1] = -1;

    while(Box[Num-1] == -1)
    {
        Find = false;//此處旗標需重新設定為false! 
        Temp = rand()%B+A;
        for(int j=0; j<Num; j++)
        {
            if(Box[j] == Temp)
            {
                Find = true;
                break;
            }
        }
        //
        if(!Find)
        {
            Box[Index] = Temp;
            Index++;
        }
    }
    //輸出產生的亂數 
    cout << "產生的亂數:" << endl;

    for(int k=0; k<Num; k++)
    {
        cout << Box[k] << " ";
    }

    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

2011/02/03

民國轉西元紀年

#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    string InName = "";
    string OutName = "";
    string LineData = "";
    string TempData = "";
    stringstream Final;
    int Data;

    string List[10000][3];
    int Row = 0;
    int Col = 0;

    for(int i=0; i<100; i++)
    {
        for(int j=0; j<3; j++)
        {
            List[i][j] = "-1";
        }
    }

    cout << "Please Input Source File:";
    getline(cin,InName);
    cout << "Please Input Output File:";
    getline(cin,OutName);
    ifstream infile(InName.c_str(),ios::in);
    ofstream outfile(OutName.c_str(),ios::app);

    if(infile)
    {
        while(!infile.eof())
        {
            getline(infile,LineData);
            LineData = LineData + "/";

            for(int i=0; i<LineData.length(); i++)
            {
                if(LineData[i] != '/')
                {
                    TempData = TempData + LineData[i];
                }
                else
                {
                    List[Row][Col] = TempData;
                    Col++;
                    TempData = "";
                }
            }
            Col = 0;
            Row++;
            LineData = "";
        }
    }
    //讀取失敗時提示錯誤
    else
    {
        cout << "WRONG" << endl;
    }
    //進行轉換
    for(int i=0; i<Row; i++)
    {
        if(List[i][0] != "-1")
        {
            Data = atoi(List[i][0].c_str())+1911;
            Final << Data;
            List[i][0] = Final.str();
            Final.str("");
        }
        else
        {
            break;
        }
    }
    //輸出檔案
    for(int i=0; i<Row; i++)
    {
        for(int j=0; j<3; j++)
        {
            outfile << List[i][j];

            if(j<2)
            {
                outfile << "/";
            }
        }
        outfile << endl;
    }

    cout << "DONE." << endl;
    return 0;
}