程序設計基礎(C)第05講例程

1 shoes1.c

/* shoes1.c -- 把鞋碼轉換成英寸 */
#include <stdio.h>
#define ADJUST 7.31              // 字符常量
int main(void)
{
    const double SCALE = 0.333;  //const變量
    double shoe, foot;
    
    shoe = 9.0;
    foot = SCALE * shoe + ADJUST;
    printf("Shoe size (men's)    foot length\n");
    printf("%10.1f %15.2f inches\n", shoe, foot);
    
    return 0;
}

2 shoes2.c

/* shoes2.c --計算多個不一樣鞋碼對應的腳長 */
#include <stdio.h>
#define ADJUST 7.31              // 字符常量
int main(void)
{
    const double SCALE = 0.333;  // const變量
    double shoe, foot;
    
    printf("Shoe size (men's)    foot length\n");
    shoe = 3.0;
    while (shoe < 18.5)      /* while循環開始 */
    {                        /* 塊開始*/
        foot = SCALE * shoe + ADJUST;
        printf("%10.1f %15.2f inches\n", shoe, foot);
        shoe = shoe + 1.0;
    }                        /* 塊結束            */
    printf("If the shoe fits, wear it.\n");
    
    return 0;
}

3 golf.c

/* golf.c -- golf錦標賽計分卡 */
#include <stdio.h>
int main(void)
{
    int jane, tarzan, cheeta;
    
    cheeta = tarzan = jane = 68;
    printf("                  cheeta   tarzan    jane\n");
    printf("First round score %4d %8d  %8d\n",cheeta,tarzan,jane);
    
    return 0;
}

4 squares.c

/* squares.c -- 計算1-20的平方 */
#include <stdio.h>
int main(void)
{
    int num = 1;
    
    while (num < 21)
    {
        printf("%4d %6d\n", num, num * num);
        num = num + 1;
    }
    
    return 0;
}

5 wheat.c

/* wheat.c -- 指數增加 */
#include <stdio.h>
#define SQUARES 64             //棋盤中的方格數
int main(void)
{
    const double CROP = 2E16;  //世界小麥年產穀粒數
    double current, total;
    int count = 1;
    
    printf("square     grains       total     ");
    printf("fraction of \n");
    printf("           added        grains    ");
    printf("world total\n");
    total = current = 1.0; /*從第一粒谷開始 */
    printf("%4d %13.2e %12.2e %12.2e\n", count, current,total, total/CROP);
    while (count < SQUARES)
    {
        count = count + 1;
        current = 2.0 * current;     /*下一個方格穀粒翻倍 */
        total = total + current;     /* 更新總數 */
        printf("%4d %13.2e %12.2e %12.2e\n", count, current,total, total/CROP);
    }
    printf("That's all.\n");
    
    return 0;
}

6 divide.c

/* divide.c -- 除法
#*/
#include <stdio.h>
int main(void)
{
    printf("integer division:  5/4   is %d \n", 5/4);
    printf("integer division:  6/3   is %d \n", 6/3);
    printf("integer division:  7/4   is %d \n", 7/4);
    printf("floating division: 7./4. is %1.2f \n", 7./4.);
    printf("mixed division:    7./4  is %1.2f \n", 7./4);
    
    return 0;
}

7 rules.c

/* rules.c -- 優先級測試 */
#include <stdio.h>
int main(void)
{
    int top, score;
    
    top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));
    printf("top = %d, score = %d\n", top, score);
    
    return 0;
}

8 sizeof.c

// sizeof.c -- 使用sizeof運算符
// 使用 C99 %z 轉換 -- 若是編譯器不支持 %zd將其改爲%u or %lu 
#include <stdio.h>
int main(void)
{
    int n = 0;
    size_t intsize;
    
    intsize = sizeof (int);
    printf("n = %d, n has %zd bytes; all ints have %zd bytes.\n", n, sizeof n, intsize );
    
    return 0;
}

9 min_sec.c

// min_sec.c -- 把秒數轉換成分和秒
#include <stdio.h>
#define SEC_PER_MIN 60            // 1分鐘60秒
int main(void)
{
    int sec, min, left;
    
    printf("Convert seconds to minutes and seconds!\n");
    printf("Enter the number of seconds (<=0 to quit):\n");
    scanf("%d", &sec);            // 讀取秒數
    while (sec > 0)
    {
        min = sec / SEC_PER_MIN;  // 截斷分鐘數
        left = sec % SEC_PER_MIN; // 剩下的秒數
        printf("%d seconds is %d minutes, %d seconds.\n", sec,min, left);
        printf("Enter next value (<=0 to quit):\n");
        scanf("%d", &sec);
    }
    printf("Done!\n");
    
    return 0;
}

10 add_one.c

/* add_one.c --  遞增:前綴和後綴 */
#include <stdio.h>
int main(void)
{
    int ultra = 0, super = 0;
    
    while (super < 5)
    {
        super++;
        ++ultra;
        printf("super = %d, ultra = %d \n", super, ultra);
    }
    
    return 0;
}

11 post_pre.c

/* post_pre.c -- 前綴和後綴 */
#include <stdio.h>
int main(void)
{
    int a = 1, b = 1;
    int a_post, pre_b;
    
    a_post = a++;  // 後綴遞增
    pre_b = ++b;   // 前綴遞增
    printf("a  a_post   b   pre_b \n");
    printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
    
    return 0;
}

12 bottles.c

#include <stdio.h>
#define MAX 100
int main(void)
{
    int count = MAX + 1;
    
    while (--count > 0) {
        printf("%d bottles of spring water on the wall, ""%d bottles of spring water!\n", count, count);
        printf("Take one down and pass it around,\n");
        printf("%d bottles of spring water!\n\n", count - 1);
    }
    
    return 0;
}

13 addemup.c

/* addemup.c -- 幾種常見的語句 */
#include <stdio.h>
int main(void)                /* 計算前20個整數的和*/
{
    int count, sum;           /* 聲明 */
    
    count = 0;                /* 表達式語句 */
    sum = 0;                  /* 表達式語句 */
    while (count++ < 20)      /* 迭代語句  */
        sum = sum + count;    /*     */
    printf("sum = %d\n", sum);/* 表達式語句*/
    
    return 0;                 /* 跳轉語句*/
}

14 convert.c

/* convert.c -- 自動類型轉換 */
#include <stdio.h>
int main(void)
{
    char ch;
    int i;
    float fl;
    
    fl = i = ch = 'C';                        /* line 9  */
    printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 10 */
    ch = ch + 1;                              /* line 11 */
    i = fl + 2 * ch;                          /* line 12 */
    fl = 2.0 * ch + i;                        /* line 13 */
    printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 14 */
    ch = 1107;                                /* line 15 */
    printf("Now ch = %c\n", ch);              /* line 16 */
    ch = 80.89;                               /* line 17 */
    printf("Now ch = %c\n", ch);              /* line 18 */
    
    return 0;
}

15 pound.c

/* pound.c -- 定義一個帶一個參數的函數   */
#include <stdio.h>
void pound(int n);   // ANSI 函數原型聲明
int main(void)
{
    int times = 5;
    char ch = '!';   // ASCII 碼是 33
    float f = 6.0f;
    
    pound(times);    // int 類型參數
    pound(ch);       // 和 pound((int)ch);相同
    pound(f);        //和 pound((int)f);相同
    
    return 0;
}

void pound(int n)    // ANSI-風格函數頭
{                    // 代表該函數接受一個int類型的參數
    while (n-- > 0)
        printf("#");
    printf("\n");
}

16 running.c

// running.c -- A useful program for runners
#include <stdio.h>
const int S_PER_M = 60;         // 1分鐘的秒數
const int S_PER_H = 3600;       // 1小時的秒數
const double M_PER_K = 0.62137; //1千米的英里數
int main(void)
{
    double distk, distm;  // 跑過的距離
    double rate;          // 平均速度
    int min, sec;         //跑步用時(以分鐘和秒爲單位)
    int time;             // 跑步用時(以秒爲單位)
    double mtime;         // 跑1英里須要的時間(以秒爲單位)
    int mmin, msec;       // 跑1英里須要的時間(以分鐘和秒爲單位)
    
    printf("This program converts your time for a metric race\n");
    printf("to a time for running a mile and to your average\n");
    printf("speed in miles per hour.\n");
    printf("Please enter, in kilometers, the distance run.\n");
    scanf("%lf", &distk);  // %lf for type double
    printf("Next enter the time in minutes and seconds.\n");
    printf("Begin by entering the minutes.\n");
    scanf("%d", &min);
    printf("Now enter the seconds.\n");
    scanf("%d", &sec);
    // 把時間轉換成秒
    time = S_PER_M * min + sec;
    // 把千米轉換成英里
    distm = M_PER_K * distk;
    // miles per sec x sec per hour = mph
    rate = distm / time * S_PER_H;
    // time/distance = time per mile
    mtime = (double) time / distm;
    mmin = (int) mtime / S_PER_M; // 求出分鐘數
    msec = (int) mtime % S_PER_M; // 求出剩餘的秒數
    printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n",distk, distm, min, sec);
    printf("That pace corresponds to running a mile in %d min, ", mmin);
    printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,rate);
    
    return 0;
}
相關文章
相關標籤/搜索