[程設習題] C Primer Plus Ch4 #2

2.     請試著寫一個程式,它會要求你打入你的名字,然後用以下方式顯示之。


        a.     印出名字在二個很緊黏的雙引號裡。


        b.     印出名字在一個20字元寬,包在雙引號裡的區域。


        c.     印出名字在一個20字元寬,包在雙引號裡的區域,名字需靠左顯示。


        d.     印出名字在一個比名字多3個字元的區域裡。


 


程式碼如下






#include “stdafx.h”


#include <stdio.h>                              //引入stdio.h


#include <stdlib.h>                             //引入stdlib.h


#include <string.h>                             //引入string.h檔,這樣才可以使用strlen函數


 


int main(int argc, char* argv[])


{


     char name[20];                             //宣告變數name,型態為個字元陣列


     int len_name;                              //宣告變數len_name


     printf(“Please enter your first name:\n”); //印出字串


     scanf(“%s”, name);                         //程式等待使用者輸入姓名,assignname字元陣列


     len_name = strlen(name);                   //算出name有多少有效字元,assignlen_name變數裡


     printf(“Print type (a):\n”);               //印出字串


     printf(“\”%s\”\n\n”, name);                //印出在二個很緊黏的雙引號裡


     printf(“Print type (b):\n”);              


     printf(“\”%20s\”\n\n”, name);              //在一個字元寬,包在雙引號裡的區域


     printf(“Print type (c):\n”);              


     printf(“\”%-20s\”\n\n”, name);             //印出在一個字元寬,包在雙引號裡的區域,名字需靠左顯示


     printf(“Print type (d):\n”);              


     printf(“\”%s\”\n\n”, len_name+3, name);   //印出在一個比名字多個字元的區域裡,用號決定區域的大小


 


     system(“PAUSE”);                           //「按任意鍵繼續」的程式,讓程式暫停


     return 0;                                  //函數結束,傳回整數並跳回原本呼叫的地方


}

[程設習題] C Primer Plus Ch3 #3

3.  請寫出一個程式,他會先嗶一聲,然後打出以下文字


Startled by the sudden sound, Sally shouted, “By the Great Pumpkin, what was that!”


 


程式碼如下






#include “stdafx.h”


#include <stdio.h>                                              //引入stdio.h


#include <stdlib.h>                                             //引入stdlib.h


 


 


int main(int argc, char* argv[])


{


     printf(“\aStartled by the sudden sound, Sally shouted,”    //印出字串,用二個雙引號斷成二行


                “\”By the Great Pumpkin, what was that!\”\n”);


     system(“PAUSE”);                                           //「按任意鍵繼續」的程式,讓程式暫停


     return 0;                                                  //函數結束,傳回整數並跳回原本呼叫的地方


}

[程設習題] C Primer Plus Ch3 #2

2.  請寫出一個程式,他能讓你打入ASCII(例如66),然後他能印出ASCII碼對應出的字元。


 


程式碼如下






#include “stdafx.h”


#include <stdio.h>                                                       //引入stdio.h


#include <stdlib.h>                                                      //引入stdlib.h


 


int main(int argc, char* argv[])


{


     char txt1;                                                          //宣告一個變數txt1,型態是字元


     printf(“Please Enter the ASCII code of the character:\n”);          //印出字串


     scanf(“%d”, &txt1);                                                 //程式等待使用者打入字元的ASCII


     printf(“The ASCII code is %d and the character is %c”,txt1,txt1);   //印出字元ASCII碼代表的字元


     printf(“\n\n”);                                                     //印出字串


     system(“PAUSE”);                                                    //「按任意鍵繼續」的程式,讓程式暫停


     return 0;                                                           //函數結束,傳回整數並跳回原本呼叫的地方


}

[程設習題] C Primer Plus Ch3 #1

1.  請試著找出整數與浮點數過大溢位(overflow) 和浮點數過小溢位(underflow),請使用嘗試接近的方式來達成。


 


程式碼如下






#include “stdafx.h”


#include <stdio.h>                                   //引入stdio.h


#include <stdlib.h>                                  //引入stdlib.h


 


int main(int argc, char* argv[])


{


     int i,m1=2147483646;                            //宣告變數i和變數m1,且m1給初始值為


     float k1=1e+36,k2;                              //宣告k1k2變數,k1的初始值為的次方


float n1=(float)1e-35,n2;                       //宣告n1n2變數,n1的初始值為的-35次方


 


/——————————————————————–/


 


     printf(“Let’s find the integer overflow\n”);


     printf(“m1     = %d\n”,m1);                         


     for (i=0;i<=3;i++)                              //使用for迴圈,給i給初始值為,當i小於或等於時停止迴圈,每次迴圈執行完i就加


     {


           printf(“m1 + %d = %d\n”,i,(m1+i));         //印出num1i的值,觀察情況


     }


/——————————————————————–/


 


     printf(“\nLet’s find the float overflow\n”);    //印出字串,找出浮點數的過大溢位


    


     printf(“k1\t      = %e\n”, k1);                 //印出k1的初始值


     k2=(float)1e+36(float)1.0e+1;                  //讓的次方乘上的次方(輸入為Float)assignk2


     printf(“k1 * (1.0e+1) = %e\n”, k2);             //印出結果


     k2=(float)1e+36(float)1.0e+2;                  //10的次方乘上的次方,assignk2


     printf(“k1 * (1.0e+2) = %e”


  “\t//overflow前最大\n”, k2);                       //印出結果


     k2=(float)1e+36(float)1.0e+3;                  //讓的次方乘上的次方,assignk2


     printf(“k1 * (1.0e+3) = %e\n”, k2);             //印出結果


     k2=(float)1e+36(float)1.0e+4;                  //10的次方乘上的次方,assignk2


     printf(“k1 * (1.0e+4) = %e\n”, k2);            //印出結果


/——————————————————————–/


 


     printf(“\nLet’s find the float underflow\n”);   //印出字串,找出浮點數的過小溢位


     printf(“n1\t    = %e\n”, n1);                  //印出n1的初始值


     n2=(float)1e-35(float)1e-1;                    //10-35次方乘上的次方(輸入為Float)assignn2


     printf(“n1 * (1e-1) = %e\n”, n2);               //印出結果


     n2=(float)1e-35(float)1e-2;                    //10-35次方乘上的次方,assignn2


 


     printf(“n1 * (1e-2) = %e”


              “\t//underflow前最小\n”, n2);           //印出結果


     n2=(float)1e-35(float)1e-3;                    //10-35次方乘上的次方,assignn2


     printf(“n1 * (1e-3) = %e\n”, n2);               //印出結果


     n2=(float)1e-35(float)1e-4;                    //10-35次方乘上的次方,assignn2


     printf(“n1 * (1e-4) = %e\n”, n2);               //印出結果


 


/——————————————————————–/


     system(“PAUSE”);                               //「按任意鍵繼續」的程式,讓程式暫停


     return 0;                                       //函數結束,傳回整數並跳回原本呼叫的地方


}

[程設習題] C Primer Plus Ch2 #6

6.  寫一個程式,它在螢幕上要印成這樣子


Smile! Smile! Smile!


Smile! Smile!


Smile!


這程式只能宣稱一個印出Smile!的函數,其他部分需由這函數來完成


 


程式碼如下






// ch2_wr6.cpp : Defines the entry point for the console application.


//


 


#include “stdafx.h”


#include <stdio.h>                   //引入stdio.h


void prt_smile_txt(void);       //宣稱一個prt_jolly_txt函數


int main(int argc, char* argv[]) {


     int i,j;                        //宣告變數my_year和變數cal_value


 


     for(i=1;i<=3;i++){              //使用迴圈,把assigni變數當作起始值,當i小於或等於時停止迴圈,每次迴圈執行完畢i就加


           for(j=i;j<=3;j++){         //使用迴圈,把i變數值assignj變數當作起始值,當j小於或等於時停止迴圈,每次迴圈執行完畢j就加


                prt_smile_txt();     //呼叫prt_smile_txt函數


           }


           printf(“\n”);              //印出換行字串


     }


 


     return 0;                       //函數結束,傳回整數並跳回原本呼叫的地方


}


 


void prt_smile_txt(void){       //prt_smile_txtFunction head,回傳值設為沒有回傳值,參數設為沒有參數


 


     printf(“Smile!”);               //印出字串


}

[程設習題] C Primer Plus Ch2 #4

4.  寫一個程式,它在螢幕上要印成這樣子


For he’s a jolly good fellow!


For he’s a jolly good fellow!


For he’s a jolly good fellow!


Which nobody can deny!


這程式裡面(除了main函數以外) 須宣稱二個函數 (function)


一個函數是只能印出For he’s a jolly good fellow! 這一句


另一個函數是只能印出Which nobody can deny! 這一句



程式碼如下






// ch2_wr4.cpp : Defines the entry point for the console application.


//


 


#include “stdafx.h”


#include <stdio.h>                                       //引入stdio.h


#include <stdlib.h>                                        //引入stdlib.h


 


void prt_jolly_txt (void);                                 //宣稱一個prt_jolly_txt函數,回傳值設為沒有回傳值,參數設為沒有參數


void prt_deny_txt (void);


int main(int argc, char* argv[]) {


     int i;                                                //宣告一個變數i


     for(i=1;i<=3;i++){                                   //使用for迴圈,把1 assigni變數當作起始值,當i小於或等於3時停止迴圈,每次迴圈執行完畢i就加


           prt_jolly_txt();                                //呼叫prt_jolly_txt函數


     }


     prt_deny_txt();                                       //呼叫prt_deny_txt函數


     system(“PAUSE”);                                      //「按任意鍵繼續」的程式


     return 0;                                             //函數結束,傳回整數0並跳回原本呼叫的地方


}


 


void prt_jolly_txt (void){                                 //prt_jolly_txtFunction head,回傳值設為沒有回傳值,參數設為沒有參數


 


     printf(“For he’s a jolly good fellow!\n”);            //印出字串


}


void prt_deny_txt (void){                                  //prt_deny_txtFunction head,回傳值設為沒有回傳值,參數設為沒有參數


 


     printf(“Which nobody can deny!\n”);                   //印出字串


 


}

[程設習題] C Primer Plus Ch2 #3

 


寫一個可以轉換你的年齡的程式,它可以把你的年齡轉換成你已經活的天數(只計算整數年)


 


程式碼如下







#include “stdafx.h”


#include <stdio.h>                                                //引入stdio.h


#include <stdlib.h>                                               //引入stdlib.h


int main(int argc, char* argv[]) {


     int my_year, cal_value;                                      //宣告變數my_year和變數cal_value


     my_year = 18;                                                //我的年齡assignmy_year的變數


     cal_value = my_year * 365;                                   //年齡計算成天數assigncal_value變數


     printf (今年我%d \n”, my_year);                           //印出字串


     printf (已經活了%d 的日子,繼續把握每一天\n”, cal_value);


     system(“PAUSE”);                                             //「按任意鍵繼續」的程式


     return 0;                                                    //函數結束,傳回整數0並跳回原本呼叫的地方


}

[筆記][極重要]openwebmail的關鍵設定,設定SELinux的允許規則

(感動)找這份文件找真的很久
因為卡在這裡真的很久,找書找不到相關內容
網路也找了很久,都是把SELinux關掉等等方法
SELinux不應該被關掉!
因為他也幫我檔了很多網路的嘗試攻擊

像是useradd危險的動作…等等

在這裡我先交代一下openwebmail的安裝
如果已經安裝了,請跳過這段
關於openwebmail的設定,你可以參考官方的文件
http://dslab.ee.ncku.edu.tw/~tung/openwebmail/install.html
(以下內容部分節錄自官方文件)
作業系統:Fedora Core 9為例
照官方文件設定,在
[root@webmail ~]# /var/www/cgi-bin/openwebmail/openwebmail-tool.pl –init
之後
本機瀏覽
http://localhost/cgi-bin/openwebmail/openwebmail.pl
會出現以下錯誤
無法寫入 /var/log/openwebmail.log! (Permission denied)

請繼續往下看

以下內容引用網址:http://www.blueshop.com.tw/board/show.asp?subcde=BRD200606051501341I7&fumcde=FUM20050110200903ZWZ&rplcnt=10
在文字模式下打入以下指令
[root@webmail ~]#touch /var/log/openwebmail.log
[root@webmail ~]#chcon -u system_u /var/log/openwebmail.log
[root@webmail ~]#chcon -t httpd_sys_script_rw_t /var/log/openwebmail.log
[root@webmail ~]#chcon -t httpd_unconfined_script_exec_t
[root@webmail ~]#/var/www/cgi-bin/openwebmail/openwebmail*
指令說明
touch /var/log/openwebmail.log <==這是產生一個log檔
chcon -u system_u /var/log/openwebmail.log <==這是修改檔案使用者權限
chcon -t httpd_sys_script_rw_t /var/log/openwebmail.log <==這是修改寫入權限
chcon -t httpd_unconfined_script_exec_t
var/www/cgi-bin/openwebmail/openwebmail* <==這是修改檔案由什麼套件專用
這些都是因為SELinux造成的,原本檔案、目錄權限只有使用檔案前面那串(rwxrwxrwx),在使用SELinux後,會多出一種管理context
type,context type會限制該目錄、檔案的使用原則,所以,在使用有安裝SELinux時,常常會出現apach、ftp明明都設定好了,卻還是不能使用,多半出問題,都是SELinux在做怪。
感謝原作大大,難解的linux終於有solution了

[筆記]IIS支援PHP5

喔…我真是蠢
上次弄個老半天,看了無數的文章
才發現這個困難並不困難
怎麼會沒看到這個選項?
那我花了好多天,看了頭暈轉向的文章怎麼辦?
算是學費吧
要IIS支援PHP,
客倌們,看好樓
1. 到PHP官網下載最新”穩定”版PHP
http://www.php.net/downloads.php
2. 請到官網選擇Windows Binaries
PHP 5.2.6 installer 下載
3. 安裝時選IIS ISAPI module
再選擇安裝目錄,外掛設定(GD zip rar…等等)
123站著穿 有沒有很簡單?
這大概是收錄裡面最簡單的一篇了

[筆記]Fedora 9下的VNC的設定

本文部份節錄和翻譯自http://fedoranews.org/tchung/vnc/index.shtml
(他真的寫的太優了…佩服佩服)
————————————————–
安裝VNC

打入下列指令去檢查用戶端和伺服器是否有安裝好vnc

[tchung@tchung101 tchung]$ rpm -q vnc vnc-server
vnc-4.0-0.beta4.3.2
vnc-server-4.0-0.beta4.3.2
[tchung@tchung101 tchung]$

————————————————–
修改vncservers的設定檔
請自行加入多出來的指令

[tchung@tchung101 tchung]$ sudo vi /etc/sysconfig/vncservers

# The VNCSERVERS variable is a list of display:user pairs.
#
# Uncomment the line below to start a VNC server on display :1
# as my ‘myusername’ (adjust this to your own). You will also
# need to set a VNC password; run ‘man vncpasswd’ to see how
# to do that.
#
# DO NOT RUN THIS SERVICE if your local area network is
# untrusted! For a secure way of using VNC, see
# URL:http://www.uk.research.att.com/vnc/sshvnc.html

# VNCSERVERS="1:myusername"
VNCSERVERS="1:tchung"
(↑加入上面這行,tchung改成linux可登入帳號)

————————————————–
建立密碼檔

再還沒運作VNC時,我們先用vncpasswd指令 建立一個密碼檔
(這個密碼檔會在家目錄建立一個隱藏目錄.vnc)

[tchung@tchung101 tchung]$ vncpasswd
Password:
Verify:
[tchung@tchung101 tchung]$ ls -d .vnc
.vnc
[tchung@tchung101 tchung]$ ls .vnc
passwd
[tchung@tchung101 tchung]$

————————————————–
讓VNC執行成服務

[tchung@tchung101 tchung]$ sudo /sbin/service vncserver start
Starting VNC server: 1:tchung [ OK ]
[tchung@tchung101 tchung]$

讓我們來看看.vnc目錄有什麼內容,你應該會跟我有差不多的東西

[tchung@tchung101 tchung]$ cd .vnc
[tchung@tchung101 .vnc]$ ls
passwd tchung101:1.log tchung101:1.pid xstartup
[tchung@tchung101 .vnc]$

————————————————–

修改xstartup的內容

修改xstartup的內容,將紅色部分的註解拿掉
要不然你只能看到一片灰色的螢幕和指令列
(以下是xstartup的內容)

#!/bin/sh

# Uncomment the following two lines for normal desktop:

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc

(↑將註解#拿掉)

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80×24+10+10 -ls -title "$VNCDESKTOP Desktop" &
twm &

我們才剛剛修改過xstartup 讓我們先重新起動vnc服務

[tchung@tchung101 tchung]$ sudo /sbin/service vncserver restart
Shutting down VNC server: 1:tchung [ OK ]
Starting VNC server: 1:tchung [ OK ]
[tchung@tchung101 tchung]$

我們如何連上vncserver? 用vncviewer 指令來達成目的

[tchung@tchung101 tchung]$ vncviewer localhost:1

————————————————–
防火牆  打開port 5901

UPDATE:
如果要讓遠端能夠連過來,防火牆的port 5901需要打開(放行)
加入下列紅色部分來打開port 5901,然後重新啟動iptables 服務

[tchung@tchung101 tchung]$ sudo vi /etc/sysconfig/iptables

# Firewall configuration written by redhat-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT – [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp –icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 5901 -j ACCEPT
(↑加入這行)
-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited
COMMIT

[tchung@tchung101 tchung]$ sudo /sbin/service iptables restart
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading iptables modules: [ OK ]
Applying iptables firewall rules: [ OK ]
[tchung@tchung101 tchung]$

————————————————–
當使用二個使用者時
TIP by Forrest Taylor

如果要在vncservers使用二個使用者,照以下部驟

VNCSERVERS="1:tchung 2:thomasc"
當然你也要執行vncpasswd 去分別設定這二個使用者,當然你的防火牆要改動設定
你可能要確定VNC的 連線1 是不是用 port 5901,如果你有二個使用者以上的話
你可能要開啟(放行) port 5901port 5902
閱讀全文〈[筆記]Fedora 9下的VNC的設定〉