Wednesday, September 23, 2009

數字系統(十進制轉二、八、十六進制)

很久沒練習了C的語法了,心血來潮就寫了這篇。

BinarySystem()是將二進制數字轉換成十進制
第13行:Bin[i]-48是因為Bin是字元(char),所以要透過ASCII(美國資訊交換標準碼)轉換成數字。

#include<iostream>
#include<conio.h>
void BinarySystem(){
char Bin[11];
int LastDigit=0;
int Decimal=0, Weight=1;
printf("請輸入二進制數字(最多10位元):");
scanf("%s", Bin);
while(Bin[LastDigit]!='\0')
LastDigit++;
LastDigit--;
for(int i=LastDigit; i>=0; i--){
Decimal=Decimal+(Bin[i]-48)*Weight;
Weight*=2;
}
printf("二進制數字:%s\n十進制數字:%d\n", Bin, Decimal);
}

OctalSystem()是將八進制數字轉換成十進制

void OctalSystem(){
char Bin[11];
int LastDigit=0;
int Decimal=0, Weight=1;
printf("請輸入八進制數字(最多10位元):");
scanf("%s", Bin);
while(Bin[LastDigit]!='\0')
LastDigit++;
LastDigit--;
for(int i=LastDigit; i>=0; i--){
Decimal=Decimal+(Bin[i]-48)*Weight;
Weight*=8;
}
printf("八進制數字:%s\n十進制數字:%d\n", Bin, Decimal);
}

HexSystem()是將十六進制數字轉換成十進制
第43行:因為十六進制有A、B、C、D、E、F因此需要更改ASCII轉換成數字的參數

void HexSystem(){
char Bin[11];
int LastDigit=0;
int Decimal=0, Weight=1;
printf("請輸入十六進制數字(最多10位元,字母請大寫):\n");
scanf("%s", Bin);
while(Bin[LastDigit]!='\0')
LastDigit++;
LastDigit--;
for(int i=LastDigit; i>=0; i--){
if(Bin[i]>=65)
Decimal=Decimal+(Bin[i]-55)*Weight;
else
Decimal=Decimal+(Bin[i]-48)*Weight;
Weight*=16;
}
printf("十六進制數字:%s\n十進制數字 :%d\n", Bin, Decimal);
}

選擇轉換模式

void SelectMode(){
char Mode;
printf("選擇模式:A.二進位; B.八進位; C.十六進位;\n");
Mode=_getche();
printf("\n");
if(Mode=='A')
BinarySystem();
else if(Mode=='B')
OctalSystem();
else if(Mode=='C')
HexSystem();
else{
printf("沒有這種模式!\n");
}
}

主程式,利用do-while的方式重複做。

int main(){
char Choose;
printf("數字系統:轉換成十進位");
do{
SelectMode();
printf("是否要重作?(Y/N)\n");
Choose=_getche();
printf("\n");
}while(Choose=='Y' || Choose=='y');
system("pause");
return 0;
}

因為當中有十六進制,所以採用字元(char)的放式儲存變數。
雖然比使用數學運算式來的耗資源,但是用字元(char)更方便看懂計算過程。

Monday, September 21, 2009

最小公倍數(LCM)

最小公倍數(Least Common Multiple, LCM)是兩個整數共有倍數中最小的一個。

計算最小公倍數時,通常會藉助最大公因數(gcd/hcf)來輔助計算。
公式如下:


計算最小公倍數
請輸入兩個數字:
1st: 55
2nd: 44
最大公因數為:11
最小公倍數為:220
請按任意鍵繼續 . . .

#include <iostream>
using namespace std;
int GCD(int a, int b){
if(a%b){
return GCD(b, a%b);
}
else{
return (b);
}
}
int main(){
int x, y, gcd, lcm;
cout << "計算最小公倍數\n" << "請輸入兩個數字:\n";
cout << "1st: ";
cin >> x;
cout << "2nd: ";
cin >> y;
gcd=GCD(x,y);
cout << "最大公因數為:" << gcd << endl;
lcm=x*y/gcd;
cout << "最小公倍數為:" << lcm << endl;
system("Pause");
return 0;
}

Friday, September 11, 2009

最大公因數(GCD)

最大公因數(Greatest Common Divisor,簡寫為G.C.D.;或Highest Common Factor,簡寫為H.C.F.)
這是很多程式的入門練習題,尤其是使用遞迴呼叫(recursive call)的方式。

如何找出兩個數的最大公因數呢?
最常見的方式是輾轉相除法---輾轉相除法,又名歐幾里得演算法(Euclidean algorithm)乃求兩個正整數之最大公因數的演算法。
這是已知最古老的演算法, 其可追溯至前300年。首次出現於歐幾里得的《幾何原本》(第VII卷,命題i和ii)中,而在中國則可以追溯至東漢出現的《九章算術》。這演算法並不需要把二數作質因數分解。

以下分別以C++實作這兩個方法:

《九章算術》
計算最大公因數
請輸入兩個數字:
1st: 96
2nd: 72
96>72, 96-72=24
24<72, 72-24=48
24<48, 48-24=24
24=24, 最大公因數為:24
請按任意鍵繼續 . . .

#include <iostream>
using namespace std;
int fun(int a, int b)
{
if(a>b)
{
cout << a << ">" << b << ", " << a << "-" << b << "=" << a-b << endl;
return fun(a-b,b);
}
else if(a<b)
{
cout << a << "<" << b << ", " << b << "-" << a << "=" << b-a << endl;
return fun(a,b-a);
}
else
{
cout << a << "=" << b << ", ";
return (a);
}
}
int main(){
int x,y;
cout << "計算最大公因數\n" << "請輸入兩個數字:\n";
cout << "1st: ";
cin >> x;
cout << "2nd: ";
cin >> y;
cout << "最大公因數為:" << fun(x,y) << endl;
system("Pause");
return 0;
}


《幾何原本》
計算最大公因數
請輸入兩個數字:
1st: 96
2nd: 72
96>72, 96%72=24
72%24=0, 最大公因數為:24
請按任意鍵繼續 . . .

#include<iostream>
using namespace std;
int fun(int a, int b){
if(a%b){
cout<<a<<">"<<b<<", "<<a<<"%"<<b<<"="<<a%b<<endl;
return fun(b, a%b);
}
else{
cout<<a<<"%"<<b<<"=0, ";
return (b);
}
}
int main(){
int x,y;
cout<<"計算最大公因數\n"<<"請輸入兩個數字:\n";
cout<<"1st: ";
cin>>x;
cout<<"2nd: ";
cin>>y;
if(x>y)
cout<<"最大公因數為:"<<fun(x,y)<<endl;
else if(x<y)
cout<<"最大公因數為:"<<fun(y,x)<<endl;
else
cout<<"最大公因數為:"<<x<<endl;
system("Pause");
return 0;
}

Sunday, August 02, 2009

調整Vista Auto-Tuning功能

由於買了新的NB搭載新的作業系統Vista,所以讓我開始對他的各項功能感到興趣。其中"改善Vista網路連線",這個詞一直在Google上出現Vista相關的議題。原來是vista新功能"Auto-Tuning"。

什麼是"Auto-Tuning"?
他是能根據網路流量調整接收資料量的緩衝區大小,在Vista中共有Disabled、HighlyRestricted、Restricted、Normal、Experimental等五種設定,後面會在提到他們的意義與指令。
  
如何調整調整Vista Auto-Tuning功能
1.點選"開始",在指令列打上cmd後,將ctrl+shift+enter同時按以取得使用者最高權限。


2.檢查現在的狀況輸入:
netsh interface tcp show global
P.S:我的設定已經是關閉的狀態。

3.關掉Auto-Tuning功能
netsh interface tcp set global autotuninglevel=disabled



附錄:Auto-Tuning功能共有五種參數可供選擇設定---
1. 設為 Disabled    
Disable the autotunning feature in Vista completely、and fit and lock the RWIN receive
window to default value 65,536 bytes.
輸入:
netsh interface tcp set global autotuninglevel=disabled
   
2. 設為HighlyRestricted    
Allow for the receive window to grow beyond the default value、but do so very conservatively.
In this mode、Vista will by default use RWIN of 16,384 bytes with a scale factor of 2.
輸入:
   netsh interface tcp set global autotuninglevel=highlyrestricted
3. 設為Restricted
Allow the receive window to grow beyond its default value、but limit such growth in
some scenarios.
輸入:
   netsh interface tcp set global autotuninglevel=restricted
4.設為 Normal (這是Windows 7,Server 2008,及Vista的預設值)   
Allow for the receive window to grow to accommodate almost all scenarios. The default  
setting in Vista. Specifying this command mean you want to turn back on AutoTuning feature.
輸入:
   netsh interface tcp set global autotuninglevel=normal
5.設為Experimental    
Allow for the receive window to grow to accommodate extreme scenarios. Note The  
experimental value can decrease performance in common scenarios. This value should be
used only for research purposes.
輸入:
   netsh interface tcp set global autotuninglevel=experimental

Saturday, January 05, 2008

VSFTPD在Fedora core 8上安裝全記錄

  • 先檢查是否有安裝vsftpd
[root@localhost ~]# rpm -qa|grep vsftpd
vsftpd-2.0.5-19.fc8

如果沒有,先使用yum安裝吧!
[root@localhost ~]# yum -y install vsftpd

  • 啟動vsftpd
[root@localhost ~]# /etc/init.d/vsftpd start
正在啟動 vsftpd 中的 vsftpd: [ 確定 ]

如果要在開機時就開啟,那先去鍵入「ntsysv」設定吧!

  • 將防火牆中vsftpd所使用的port打開
[root@localhost ~]# iptables -I RH-Firewall-1-INPUT 1 -p tcp --dport 21 -i eth0 -j ACCEPT
[root@localhost ~]# iptables -L -n
......
Chain RH-Firewall-1-INPUT (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
......
儲存剛剛插入的chain中的規則
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 確定 ]
規則將會儲存在 /etc/sysconfig/iptables 檔案中,會在服務啟動時生效,包括重新開機之後。