<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    BeautifulMan

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      16 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
    復習題
    1、哪一存儲類生成的變量對于包含他們的函數來說是局部變量?
    答:自動存儲類、寄存器存儲類和靜態空鏈接存儲類
    2、哪一存儲類的變量在包含它們的程序運行時期內一直存在?
    答:靜態空鏈接存儲類、靜態內部鏈接存儲類和靜態外部鏈接存儲類
    3、哪一存儲類的變量可以在多個文件中使用?哪一存儲類的變量只限于在一個文件中使用?
    答:靜態外部鏈接存儲類和靜態內部鏈接存儲類
    4、代碼塊作用域變量具有哪種鏈接?
    答:空鏈接
    5、關鍵字extern的用處是什么?
    答:在聲明中使用關鍵字extern表明一個變量或函數已經在其他地方被定義過了
    6、考慮如下代碼段:
    int * p1 = (int *)malloc(100 * sizeof(int));
    考慮到最終的結果,下面的語句有何不同?
    int * p1 = (int *)calloc(100, sizeof(int));
    答:都分配一個具有100個int值的數組。使用calloc()的語句還把每個元素設置為0
    7、下列每個變量對哪些函數是可見的?程序有什么錯誤嗎?
    /* 文件1 */
    int daisy;
    int main(void)
    {
        int lily;
        ;
    }
    int petal()
    {
        extern int daisy, lily;
        ;
    }
    /* 文件2 */
    extern int daisy;
    static int lily;
    int rose;
    int stem()
    {
        int rose;
        ;
    }
    void root()
    {
        ;
    }
    答:
    daisy對main()是默認可見的,對petal()、stem()和root()是通過extern聲明可見的。文件2中的聲明extern int daisy;使得daisy對該文件中的所有函數可見。第一個lily是main()的局部變量。petal()中對lily的引用是錯誤的,因為兩個文件中都沒有lily的外部聲明。有一個外部的靜態lily,但是它只對第二個文件中的函數可見。第一個外部rose對root()可見,但是stem()使用它自己的局部rose覆蓋了外部的rose。
    8、下面程序會打印出什么?
    #include <stdio.h>
    char color = 'B';
    void first(void);
    void second(void);

    int main(void)
    {
        extern char color;

        printf("color in main() is %c\n", color);
        first();
        printf("color in main() is %c\n", color);
        second();
        printf("color in main() is %c\n", color);
        return 0;
    }
    void first(void)
    {
        char color;

        color = 'R';
        printf("color in first() is %c\n", color);
    }
    void second(void)
    {
        color = 'G';
        printf("color in second() is %c\n", color);
    }
    答:
    color in main() is B
    color in first() is R
    color in main() is B
    color in second() is G
    color in main() is G
    9、文件開始處做了如下聲明:
    static int plink;
    int value_ct(const int arr[], int value, int n);
    a.這些聲明表明了程序員的什么意圖?
    b.用const int value和const int n代替int value和int n會增強對調用程序中的值的保護嗎?
    答:
    a.它告訴我們程序將使用一個變量plink,該變量局部于包含該函數的文件。value_ct()的第一個參數是一個指向整數的指針,并假定它指向具有n個元素的數組的第一個元素。這里重要的一點是不允許程序使用指針arr來修改原始數組的值。
    b.不會。value和n已經是原始數據的拷貝,所以函數不能改變調用程序中的對應值。這樣聲明起到的作用只是防止在函數中改變value和n的值。例如,如果const限定n,那么函數就不能使用n++表達式。
    編程練習
    1、
    #include <stdio.h>
    int critic(void);
    int main(void)
    {
        int units;

        printf("How many pounds to a firkin of butter?\n");
        scanf("%d", &units);
        while(units != 56)
            units = critic();
        printf("You must have looked it up!\n");
        return 0;
    }
    int critic(void)
    {
        int units;

        printf("No luck, chummy. Try again.\n");
        scanf("%d", &units);
        return units;
    }
    2、
    /* pe12-2b.c */
    #include <stdio.h>
    #include "pe12-2a.h"
    int main(void)
    {
        int mode;

        printf("Enter 0 for metric mode, 1 for US mode: ");
        scanf("%d", &mode);
        while(mode >= 0)
        {
            set_mode(mode);
            get_info();
            show_info();
            printf("Enter 0 for metric mode, 1 for US mode");
            printf(" (-1 to quit): ");
            scanf("%d", &mode);
        }
        printf("Done.\n");
        return 0;
    }
    /* pe12-2a.c */
    #include <stdio.h>
    #include <stdlib.h>

    static int mode; // 代表模式
    static int distance; // 代表距離
    static double fuel_consumption; // 代表消耗的燃料
    // 設置模式
    void set_mode(int m)
    {
        mode = m;
    }
    // 根據模式設置提示輸入相應的數據
    void get_info(void)
    {
        if(mode == 0)  // 公制
        {
            printf("Enter distance traveled in kilometers: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in liters: ");
            scanf("%lf", &fuel_consumption);
        }
        else if(mode == 1) // 美制
        {
            printf("Enter distance traveled in miles: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in gallons: ");
            scanf("%lf", &fuel_consumption);
        }
        else
        {
            printf("Invalid mode specified. Mode 1 (US) used.\n");
            printf("Enter distance traveled in miles: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in gallons: ");
            scanf("%lf", &fuel_consumption);
        }
    }
    // 根據所選的模式計算并顯示燃料消耗值
    void show_info(void)
    {
        if(mode == 0)
            printf("Fuel consumption is %.2f liters per 100 km.\n", fuel_consumption / (distance / 100));
        else
            printf("Fuel consumption is %.1f miles per gallon.\n", distance / fuel_consumption);
    }
    /* pe12-2a.h */

    void set_mode(int mode);
    // 根據模式設置提示輸入相應的數據
    void get_info(void);
    // 根據所選的模式計算并顯示燃料消耗值
    void show_info(void);
    3、
    /* pe12-2b.c */
    #include <stdio.h>
    #include "pe12-2a.h"
    int main(void)
    {
        int mode;

        printf("Enter 0 for metric mode, 1 for US mode: ");
        scanf("%d", &mode);
        while(mode >= 0)
        {
            //set_mode(mode);
            get_info(mode);
            //show_info(mode);
            printf("Enter 0 for metric mode, 1 for US mode");
            printf(" (-1 to quit): ");
            scanf("%d", &mode);
        }
        printf("Done.\n");
        return 0;
    }
    /* pe12-2a.c */
    #include <stdio.h>
    #include <stdlib.h>

    // 根據模式設置提示輸入相應的數據
    void get_info(int mode)
    {
        int distance; // 代表距離
        double fuel_consumption; // 代表消耗的燃料
        double result; // 計算油耗

        if(mode == 0)  // 公制
        {
            printf("Enter distance traveled in kilometers: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in liters: ");
            scanf("%lf", &fuel_consumption);
            result = fuel_consumption / (distance / 100);
            printf("Fuel consumption is %.2f liters per 100 km.\n", result);
        }
        else if(mode == 1) // 美制
        {
            printf("Enter distance traveled in miles: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in gallons: ");
            scanf("%lf", &fuel_consumption);
            result = distance / fuel_consumption;
            printf("Fuel consumption is %.1f miles per gallon.\n", result);
        }
        else
        {
            printf("Invalid mode specified. Mode 1 (US) used.\n");
            printf("Enter distance traveled in miles: ");
            scanf("%d", &distance);
            printf("Enter fuel consumed in gallons: ");
            scanf("%lf", &fuel_consumption);
            result = distance / fuel_consumption;
            printf("Fuel consumption is %.1f miles per gallon.\n", result);
        }
    }
    /* pe12-2a.h */

    // 根據模式設置提示輸入相應的數據
    double get_info(int mode);
    4、
    #include <stdio.h>
    int helloworld(void);
    static int count = 0;
    int main(void)
    {
        char ch;
        printf("Please enter a character (q to quit): ");
        while((ch = getchar()) != 'q')
        {
            count = helloworld();
            while(getchar() != '\n')
                continue;
            printf("Please enter a character (q to quit): ");
        }
        printf("Number of times the function helloworld() itself is called: %d\n", count);
        return 0;
    }
    int helloworld(void)
    {
        count++;
        return count;
    }
    程序應盡量寫的短一點,第二次更新如下:
    #include <stdio.h>
    void sayhello(void);
    static int count = 0;
    int main(void)
    {
        for(int n = 0; n < 7; n++)
            sayhello();
        printf("函數sayhello()被調用的次數為:%d\n", count);
        return 0;
    }
    void sayhello(void)
    {
        printf("Hello World!\n");
        count++;
    }

    5、
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    // 產生1~10范圍內的隨機數
    int rollem(void);
    void sort(int *array);
    int main(void)
    {
        int arr[100];
        int i;

        srand((unsigned) time(0)); // 隨機化種子
        for(i = 0; i < 100; i++)
        {
            arr[i] = rollem();
        }
        sort(arr);
        printf("The result is a descending sort: \n");
        for(i = 0; i < 100; i++)
        {
            printf("%d ", arr[i]);
            // 每行打印10個數,然后換行
            if(i % 10 == 9)
                putchar('\n');
        }
        return 0;
    }
    // 產生1~10范圍內的隨機數
    int rollem(void)
    {
        int roll;

        roll = rand() % 10 + 1;
        return roll;
    }
    void sort(int *array)
    {
        int i, j, temp;
        for(i = 0; i < 100 - 1; i++)
        {
            for(j = i + 1; j < 100; j++)
            {
                if(array[i] < array[j])
                {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
    一個可能的運行結果:

    6、
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    // 產生1~10范圍內的隨機數
    int rollem(void);

    int main(void)
    {
        int i, random_number;
        int arr[10] = { arr[0] = 0 };
        srand((unsigned) time(0)); // 隨機化種子
        for(i = 0; i < 1000; i++)
        {
            random_number = rollem();
            switch(random_number)
            {
                case 1: arr[0]++;
                        break;
                case 2: arr[1]++;
                        break;
                case 3: arr[2]++;
                        break;
                case 4: arr[3]++;
                        break;
                case 5: arr[4]++;
                        break;
                case 6: arr[5]++;
                        break;
                case 7: arr[6]++;
                        break;
                case 8: arr[7]++;
                        break;
                case 9: arr[8]++;
                        break;
                case 10: arr[9]++;
                        break;
            }
        }
        for(i = 0; i < 10; i++)
            printf("Numbers of random number %d is %d\n", i+1, arr[i]);
        return 0;
    }
    // 產生1~10范圍內的隨機數
    int rollem(void)
    {
        int roll;

        roll = rand() % 10 + 1;
        return roll;
    }
    一個可能的運行結果為:


    7、(看來第一次就做的挺好的,沒必要再次更新!!!)
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "diceroll.h"
    // 產生1~10范圍內的隨機數
    int rollem(void);

    int main(void)
    {
        char ch;
        int dice, index = 0;
        int sides, sets;
        int * pti;

        srand((unsigned) time(0)); // 隨機化種子
        printf("Enter the number of sets: enter q to stop.\n");
        while(scanf("%d", &sets))
        {
            pti = (int *)malloc(sets * sizeof(int));
            if(pti == NULL)
            {
                puts("Memory allocation failed. Goodbye.");
                exit(EXIT_FAILURE);
            }

            printf("How many sides and how many dice?\n");
            scanf("%d %d", &sides, &dice);
            for(index = 0; index < sets; index++)
                // pti現在指向有sets個元素的數組
                pti[index] = roll_n_dice(dice, sides);
            printf("Here are %d sets of %d %d-sided throws.\n", sets, dice, sides);
            for(index = 0; index < sets; index++)
            {
                printf("%d ", pti[index]);
                if(index % 15 == 14)
                    putchar('\n');
            }
            if(index % 15 != 0)
                putchar('\n');
            while((ch = getchar()) != '\n')
                continue;
            printf("How many sets? Enter q to stop.\n");

            free(pti);
        }

        return 0;
    }
    #include "diceroll.h"
    #include <stdio.h>
    #include <stdlib.h>

    int roll_count = 0;

    static int rollem(int sides)
    {
        int roll;

        roll = rand() % sides + 1;
        ++roll_count;
        return roll;
    }

    int roll_n_dice(int dice, int sides)
    {
        int d;
        int total = 0;
        if(sides < 2)
        {
            printf("Need at least 2 sides.\n");
            return -2;
        }
        if(dice < 1)
        {
            printf("Need at least 1 die.\n");
            return -1;
        }
        for(d = 0; d < dice; d++)
            total += rollem(sides);
        return total;
    }
    extern int roll_count;

    int roll_n_dice(int dice, int sides);
    一個可能的運行結果為:

    8、
    // pe12-8.c
    #include <stdio.h>
    #include <stdlib.h>
    int *make_array(int elem, int val);
    void show_array(const int arr[], int n);
    int main(void)
    {
        int *pa;
        int size;
        int value;

        printf("Enter the number of elements: ");
        scanf("%d", &size);
        while(size > 0)
        {
            printf("Enter the initialization value: ");
            scanf("%d", &value);
            pa = make_array(size, value);
            if(pa)
            {
                show_array(pa, size);
                free(pa);
            }
            printf("Enter the number of elements (<1 to quit): ");
            scanf("%d", &size);
        }
        printf("Done.\n");
        return 0;
    }
    int *make_array(int elem, int val)
    {
        int *pti;
        pti = (int *)malloc(elem * sizeof(int));
        if(pti == NULL)
        {
            puts("Memory allocation failed. Goodbye.");
            exit(EXIT_FAILURE);
        }
        // 為數組中的每個值賦val值
        for(int i = 0; i < elem; i++)
            pti[i] = val;
        return pti;
    }
    void show_array(const int arr[], int n)
    {
        int i ;

        for(i = 0; i < n; i++)
        {
            printf("%d ", arr[i]);
            if(i % 8 == 7)
                putchar('\n');
        }
        if(i % 8 != 0)
            putchar('\n');
    }
    一個可能的運行結果為:

    總結:這一章的編程練習還算是很簡單的,自己一個人硬是磨出來的,沒有借鑒任何人的做法,雖然做的慢,還算是給自己一個交代?。?!


    posted on 2015-12-04 20:03 李阿昀 閱讀(514) 評論(0)  編輯  收藏 所屬分類: C Primer Plus 復習題與編程練習
    主站蜘蛛池模板: 亚洲国产精品尤物yw在线| 午夜毛片不卡免费观看视频| 久久久久亚洲爆乳少妇无| 亚洲妇女无套内射精| 德国女人一级毛片免费| 国产成人精品日本亚洲专区6| 91黑丝国产线观看免费| 亚洲啪啪免费视频| 最近最新中文字幕完整版免费高清| 亚洲网站在线播放| 男女免费观看在线爽爽爽视频 | 久久亚洲精品成人777大小说| 久久久久久久久久免免费精品| 精品亚洲成α人无码成α在线观看 | 亚洲色欲一区二区三区在线观看| 一级人做人爰a全过程免费视频| 亚洲阿v天堂在线2017免费| 黄色一级毛片免费| 亚洲中文字幕无码永久在线| 在线毛片片免费观看| 78成人精品电影在线播放日韩精品电影一区亚洲 | 爱情岛亚洲论坛在线观看| 免费大片在线观看网站| a级毛片高清免费视频就| 亚洲综合无码一区二区| 香蕉97超级碰碰碰免费公| 亚洲色大成网站www永久网站| 日本一道本高清免费| 一级特黄色毛片免费看| 久久久久亚洲精品影视| 麻豆高清免费国产一区| www亚洲精品久久久乳| 久久久久久A亚洲欧洲AV冫| 最刺激黄a大片免费网站| 亚洲精品乱码久久久久久V| 亚洲一区二区三区免费| 99久9在线|免费| 激情婷婷成人亚洲综合| 无码乱人伦一区二区亚洲| 国产精品va无码免费麻豆| 一个人免费视频在线观看www|