2015年12月30日 星期三

國小第一堂數學課

各位小朋友大家好!今天你們國小的第一堂數學課上的是計算a+b=?

請大家輸入下列程式,然後計算兩個數的合。

#‎include ‬<stdio.h>

int main()

{

int a, b, c;

printf("請輸入兩個整數,然後它會幫你相加。\n");

scanf("%d%d",&a,&b);

c = a + b;

printf("相加的答案 = %d\n",c);

return 0;

}

p.s.各位小朋友,我們的老祖宗曾經用手指算數學,也用過算盤,還有那個小小的電子計算機算的,甚至用什麼Apple II 算的,這些都落伍了!我們今天是用最快速的 C 語言來計算,在這變動的世界環境裡,隨時改變程式設計,才可以適應不同的需求。

查查看2016是否為閏年(leap year)?

‪#include <stdio.h>

int main()

{

int year;

printf("Enter a year to check if it is a leap year\n");

scanf("%d", &year);

if ( year%400 == 0)

printf("%d is a leap year.\n", year);

else if ( year%100 == 0)

printf("%d is not a leap year.\n", year);

else if ( year%4 == 0 )

printf("%d is a leap year.\n", year);

else

printf("%d is not a leap year.\n", year);

return 0;

}

p.s. 在Windows系統玩C語言

2015年8月4日 星期二

用 || (或) 來判斷英文字母是否為母音

#include <stdio.h>

int main()

{

char ch;

printf("Enter a character\n");

scanf("%c", &ch);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.\n", ch);

else

printf("%c is not a vowel.\n", ch);

return 0;

}

p.s.每次執行程式的時候,只能判斷一次,不能重複判斷

首頁

2015年7月19日 星期日

列印字串 Hello World! 的解說

#‎include ‬<stdio.h> //#include:引入stdio.h檔案     stdio.h:系統的引入檔

int main() //int : 宣告其回傳值為整數    main : 函數(本程式的主要函數)

{

printf("Hello, World! \n"); //printf:輸出到螢幕,列印出Hello world!   \n : 換行字元

return 0; //將整數0回傳給呼叫main的函數

}

首頁

2015年7月9日 星期四

一次做完加減乘除

#include <stdio.h>

int main()

{

int first, second, add, subtract, multiply;

float divide;

printf("Enter two integers\n");

scanf("%d%d", &first, &second);

add = first + second;

subtract = first - second;

multiply = first * second;

divide = first / (float)second; //typecasting

printf("Sum = %d\n",add);

printf("Difference = %d\n",subtract);

printf("Multiplication = %d\n",multiply);

printf("Division = %.2f\n",divide);

return 0;

}

首頁

2015年7月5日 星期日

判斷偶數和奇數

使用if...else來判斷偶數(Even)和奇數(Odd)

#include <stdio.h>

int main()

{

int n;

printf("Enter an integer\n");

scanf("%d", &n);

if (n%2 == 0)

printf("Even\n");

else printf("Odd\n");

return 0;

}

首頁

2015年6月30日 星期二

在Freebsd中操作

在Freebsd中,已內建cc功能,不必再設定C語言環境,只要遵照下列步驟即可

一、建立檔案

1.在home目下建一c目錄,方更統一管理

%mkdir c

2.進入c目錄

%cd c

3.利用文字編輯器建立一個xxx.c的檔案

%vi xxx.c

4.將程式鍵入或貼入

a.用鍵盤輸入

b.如果有X-window ,則可由網路將程式複製貼上

c.若無X-window ,則可由usb將程式貼入,或用ssh將程式傳入

5.存檔離開

wq

二、編譯執行.c檔案

1.在c目錄下

%cd c

2.編譯xxx.c

%cc xxx.c

3.檢查看看多了一個a.out (此步驟可不做)

%ls

%a.out xxx.c

4.執行

./a.out

5.如果在同一目錄再建另一個.c檔案,cc後a.out已經變成新的執行檔了。

6.再./a.out即可執行新的.c檔案。

首頁