C練習
C語言練習 |
看完了上節的說明,相信對C語言有了初步的認識,以下舉出幾個範例,測試看看自己是否能知道以下程式的執行結果
◆基本練習
(1)
#include<stdio.h>intmain(void){ printf("Hello How are you!!!\n"); return(0);} |
(2)
#include<stdio.h>intmain(void){ printf("Hi! C!\n"); printf("This is my 2nd C program...."); printf("OK?"); return(0);} |
◆進階練習(此部分有興趣同學可參考C語言的書,在此不加以說明)
(1)分數相加減
// Fraction Example Program// by Lu// 2000/4/20 AM 10:45 #include<stdio.h>intmain(void){ int a, b, c, d; // 分別為兩個分數的分子和分母 int e, f; // 兩數相加結果的分子和分母 int g, h; // 兩數相加結果的分子和分母 printf("請輸入第一個分數的分子:"); scanf("%d", &a); printf("請輸入第一個分數的分母:"); scanf("%d", &b); printf("請輸入第二個分數的分子:"); scanf("%d", &c); printf("請輸入第二個分數的分母:"); scanf("%d", &d); e=a*d+c*b; f=b*d; printf("這兩個分數相加為%d/%d\n", e, f); g=a*d-c*b; h=b*d; printf("這兩個分數相減為%d/%d\n", g, h); return(0);} |
(2)換鈔程式
// Revised Bill Program: bill_revised.c// input: 1, 5, 10, 50, 100, 500, 1000// output: 1000, 100, 10, 1// by Jack Hung// 2000/2/14 AM 11:08 #include<stdio.h>intmain(void){ int one; // 一元的個數 int five; // 五元的個數 int ten; // 十元的個數 int fifty; // 五十元的個數 int hundred; // 一百元的個數 int five_hundred; // 五百元的個數 int thousand; // 一千元的個數 int total; // 錢的總額 int give_thousand; // 應付的一千元的個數 int give_hundred; // 應付的一百元的個數 int give_ten; // 應付的十元的個數 int give_one; // 應付的一元的個數 printf("這是一個換新鈔的程式!\n"); printf("請輸入款項,我們會把您的錢換成新的1000元,100元,10元,和1元的 組合!\n"); printf("您有幾個一元:"); scanf("%d", &one); printf("您有幾個五元:"); scanf("%d", &five); printf("您有幾個十元:"); scanf("%d", &ten); printf("您有幾個五十元:"); scanf("%d", &fifty); printf("您有幾個一百元:"); scanf("%d", &hundred); printf("您有幾個五百元:"); scanf("%d", &five_hundred); printf("您有幾個一千元:"); scanf("%d", &thousand); total=one*1+five*5+ten*10+fifty*50+hundred*100+five_hundred*500+ thousand*1000; printf("您一共有%d元\n", total); give_thousand=total/1000; give_hundred=(total%1000)/100; give_ten=(total%100)/10; give_one=total%10; printf("您可以兌換%d張一千元,%d張一百元,%d個十元銅板,和%d個一元銅板!! ! 祝您新年快樂\n", give_thousand, give_hundred, give_ten, give_one); return(0);} |