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

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

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

    BeautifulMan

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      16 隨筆 :: 0 文章 :: 0 評論 :: 0 Trackbacks
    問題:編寫一個函數(shù)將一個整數(shù)轉(zhuǎn)換成二進制形式?(擴展請移步編程練習(xí)9)
    #include <stdio.h>
    void to_binary(unsigned long n);
    int main(void)
    {
        unsigned long number;
        printf("Enter an integer (q to quit): \n");
        while(scanf("%lu", &number) == 1)
        {
            printf("Binary equivalent: ");
            to_binary(number);
            putchar('\n');
            printf("Enter an integer (q to quit): \n");
        }
        printf("Done!\n");
        return 0;
    }
    void to_binary(unsigned long n)
    {
        int r;
        r = n % 2;
        if(n >= 2)
            to_binary(n / 2);
        putchar('0' + r);
        return;
    }
    復(fù)習(xí)題
    1、實際參數(shù)和形式參量有何不同?
    答:
    形式參量(也被稱為形式參數(shù))是一個變量,它在被調(diào)函數(shù)中進行定義。實際參數(shù)是在函數(shù)調(diào)用中出現(xiàn)的值,它被賦值給形式參量??梢园褜嶋H參數(shù)認為是在函數(shù)被調(diào)用時用來初始化形式參量的值。
    2、寫出下面所描述的各個函數(shù)的ANSI函數(shù)頭。注意:只寫出函數(shù)頭即可,不需要實現(xiàn)。
    a.donut()接受一個int類型的參數(shù),然后輸出若干個0,輸出0的數(shù)目等于參數(shù)的值。
    b.gear()接受兩個int類型的參數(shù)并返回int類型的值。
    c.stuff_it()的參數(shù)包括一個double類型的值以及一個double類型變量的地址,功能是把第一個數(shù)值存放到指定的地址中。
    答:
    a.void donut(int n)
    b.int gear(int n, int m)
    c.void stuff_it(double n, double * d)
    3、只寫出下列函數(shù)的ANSI C函數(shù)頭,不需要實現(xiàn)函數(shù)。
    a.n_to_char()接受一個int類型的參數(shù)并返回一個char類型的值。
    b.digits()接受的參數(shù)是一個double類型的數(shù)值和一個int類型的數(shù)值,返回值類型是int。
    c.random()不接受參數(shù),返回int類型的數(shù)值。
    答:
    a.char n_to_char(int n)
    b.int digits(double n, int m)
    c.int random(void)
    4、設(shè)計一個實現(xiàn)兩整數(shù)相加并將結(jié)果返回的函數(shù)。
    答:
    int plus(int n, int m)
    {
        return n + m;
    }
    5、假如問題4中的函數(shù)實現(xiàn)兩個double類型的數(shù)值相加,那么應(yīng)該如何修改原函數(shù)?
    答:
    double plus(double n, double m)
    {
        return n + m;
    }
    6、設(shè)計函數(shù)alter(),其輸入?yún)?shù)是兩個int類型的變量x和y,功能是分別將這兩個變量的數(shù)值改為它們的和以及它們的差。
    答:(注意:下面這種寫法是錯誤的?。。。?/span>
    void alter(int x, int y)
    {
        x = x + y;
        y = x - y;
    }
    正確的寫法如下:
    void alter(int * u, int * v)
    {
        int temp;

        temp = *u;
        *u = *u + *v;
        *v = temp - *v;
    }
    7、判斷下面的函數(shù)定義是否正確。
    void salami(num)
    {
        int num, count;

        for(count = 1; count <= num; num++)
            printf("O salami mio!\n");
    }
    答:
    有錯誤。num應(yīng)該在salami()的參數(shù)列表中而不是在花括號之后聲明,而且應(yīng)該是count++而不是num++。
    8、編寫一個函數(shù),使其返回3個整數(shù)參數(shù)中的最大值。
    答:
    int max(int x, int y, int z)
    {
        int max;
        if(x > y)
            if(x > z)
                max = x;
            else
                max = z;
        else
            if(y > z)
                max = y;
            else
                max = z;
        return max;
    }
    or (更簡潔一點)
    int max(int x, int y, int z)
    {
        int max = x;
        if(y > max)
            max = y;
        if(z > max)
            max = z;
        return max;
    }
    9、給定下面的輸出:
    Please choose one of the following:
    1)copy files 2)move files
    3)remove files 4)quit
    Enter the number of your choice:
    a.用一個函數(shù)實現(xiàn)菜單的顯示,且該菜單有4個用數(shù)字編號的選項并要求你選擇其中之一(輸出應(yīng)該如題中所示)。
    b.編寫一個函數(shù),該函數(shù)接受兩個int類型的參數(shù):一個上界和一個下界。在函數(shù)中,首先從輸入終端讀取一個整數(shù),如果該整數(shù)不在上下界規(guī)定的范圍內(nèi),則函數(shù)重新顯示菜單(使用本題目a部分中的函數(shù))以再次提醒用戶輸入新值。如果輸入數(shù)值在規(guī)定的范圍內(nèi),那么函數(shù)應(yīng)該將數(shù)值返回給調(diào)用函數(shù)。
    c.使用本題目a和b部分中的函數(shù)編寫一個最小的程序。最小的意思是該程序不需要實現(xiàn)菜單中所描述的功能;它只需要顯示這些選項并能獲取正確的響應(yīng)即可。
    答:(參考課后答案)
    #include <stdio.h>
    void menu(void);
    int get_input(intint);
    int main(void)
    {
        int res;

        menu();
        while((res = get_input(1, 4)) != 4)
            printf("I like choice %d.\n", res);
        printf("Bye!\n");
        return 0;
    }
    void menu(void)
    {
        printf("Please choose one of the following: \n");
        printf("1)copy files          2)move files\n");
        printf("3)remove files        4)quit\n");
        printf("Enter the number of your choice: \n");
    }
    int get_input(int min, int max)
    {
        int number;

        scanf("%d", &number);
        while(number < min || number > max)
        {
            printf("%d is not a valid choice; try again.\n", number);
            menu();
            scanf("%d", &number);
        }
        return number;
    }
    編程練習(xí)
    1、
    #include <stdio.h>
    double min(doubledouble);
    int main(void)
    {
        printf("One of the smaller of the two numbers is %.2f", min(23.34, 12.11));
        return 0;
    }
    double min(double x, double y)
    {
        return x < y ? x : y;
    }
    2、
    #include <stdio.h>
    void chline(char ch, int i, int j);
    int main(void)
    {
        chline('$', 3, 5);
        return 0;
    }
    void chline(char ch, int i, int j)
    {
        int index;

        for(index = 1; index < i; index++)
            putchar(' ');
        for(index = 1; index <= j - i + 1; index++)
            putchar(ch);
    }
    3、
    #include <stdio.h>
    void chline(char ch, int col, int row);
    int main(void)
    {
        chline('$', 3, 5);
        return 0;
    }
    void chline(char ch, int col, int row)
    {
        int i, j;

        for(i = 0; i < row; i++)
        {
            for(j = 0; j < col; j++)
               putchar(ch);
            putchar('\n');
        }
    }
    4、
    #include <stdio.h>
    double computer(double a, double b);
    int main(void)
    {
        printf("%.2f和%.2f的諧均值是:%.3f\n", 0.3, 0.5, computer(0.3, 0.5));
        return 0;
    }
    double computer(double a, double b)
    {
        double result;

        result = 1 / ((1/a + 1/b) / 2);
        return result;
    }
    5、
    #include <stdio.h>
    void larger_of(double *, double *);
    int main(void)
    {
        double x = 23.3;
        double y = 34.4;
        printf("Originally x = %.1f; y = %.1f\n", x, y);
        larger_of(&x, &y);
        printf("Now x = %.1f; y = %.1f\n", x, y);
        return 0;
    }
    void larger_of(double * u, double * v)
    {
        double temp;
        temp = *u > *v ? *u : *v;
        *u = temp;
        *v = temp;
    }
    6、(第一次碼的程序讀取到換行符的時候也會打印出來,會給人看不明白的感覺,索性按[Enter]鍵的時候就退出循環(huán),不要讀到EOF)
    #include <stdio.h>
    #include <ctype.h>
    void printchar(char ch);
    int main(void)
    {
        char ch;

        printf("請輸入要分析的東西:\n");
        while((ch = getchar()) != EOF)
        {
            printchar(ch);
        }
        return 0;
    }
    void printchar(char ch)
    {
        if(isalpha(ch))
        {
            printf("%c %d\n", ch, toupper(ch) % 'A' + 1);
        }
    }
    修改之后,程序如下:
    #include <stdio.h>
    #include <ctype.h>
    int show_c_location(char ch);

    int main(void)
    {
        char ch;

        printf("Please enter some characters: \n");
        while((ch = getchar()) != '\n')
            printf("%c-%d ", ch, show_c_location(ch));
        return 0;
    }
    int show_c_location(char ch)
    {
        int result;

        if(isalpha(ch))
            result = toupper(ch) - 'A' + 1;
        else
            result = -1;
        return result;
    }
    7、
    #include <stdio.h>
    double power(double n, int p);
    int main(void)
    {
        double x, xpow;
        int exp;

        printf("Enter a number and the positive integer power");
        printf(" to which\nthe number will be raised. Enter q");
        printf(" to quit.\n");
        while(scanf("%lf%d", &x, &exp) == 2)
        {
            xpow = power(x, exp);
            printf("%.3g to power %d is %.5g\n", x, exp, xpow);
            printf("Enter next pair of numbers or q to quit.\n");
        }
        printf("Hope you enjoyed this power trip -- bye!\n");
        return 0;
    }
    double power(double n, int p)
    {
        int i;
        double result = 1;

        if(n != 0)
        {
            if(p > 0)
            {
                for(i = 1; i <= p; i++)
                    result *= n;
            }
            else if(p < 0)
            {
                for(i = 1; i <= -p; i++)
                    result *= (1 / n);
            }
            else
                result = 1;
        }
        else
        {
            if(p == 0)
                result = 1;// 0的0次方是一個有爭議的數(shù),本題認為會得到1
            else
                result = 0;
        }
        return result;
    }
    8、
    #include <stdio.h>
    double power(double n, int p);
    int main(void)
    {
        double x, xpow;
        int exp;

        printf("Enter a number and the positive integer power");
        printf(" to which\nthe number will be raised. Enter q");
        printf(" to quit.\n");
        while(scanf("%lf%d", &x, &exp) == 2)
        {
            xpow = power(x, exp);
            printf("%.3g to power %d is %.5g\n", x, exp, xpow);
            printf("Enter next pair of numbers or q to quit.\n");
        }
        printf("Hope you enjoyed this power trip -- bye!\n");
        return 0;
    }
    double power(double n, int p)
    {
        double result = 1;

        if(n != 0)
        {
            if(p > 0)
                result = n * power(n, p-1);
            else if(p < 0)
                result = (1/n) * power(n, p+1);
            else
                result = 1;
        }
        else
        {
            if(p == 0)
                result = 1;// 0的0次方是一個有爭議的數(shù),本題認為會得到1
            else
                result = 0;
        }
        return result;
    }
    9、
    #include <stdio.h>
    void to_base_n(unsigned long n, int range);
    int main(void)
    {
        unsigned long number;
        int range;
        printf("請輸入要轉(zhuǎn)換的無符號整數(shù)和所規(guī)定的進制數(shù): \n");
        while(scanf("%lu %d", &number, &range) == 2)
        {
            if(range >= 2 && range <= 10)
            {
                printf("無符號整數(shù)%lu轉(zhuǎn)換成%d進制數(shù)為: ", number, range);
                to_base_n(number, range);
                putchar('\n');
                printf("請輸入要轉(zhuǎn)換的無符號整數(shù)和所規(guī)定的進制數(shù): \n");
            }
            else
                printf("所規(guī)定的進制數(shù)的范圍是2~10,請輸入正確的數(shù)字\n");
        }
        printf("Done!\n");
        return 0;
    }
    void to_base_n(unsigned long n, int range)
    {
        int r;

        r = n % range;
        if(n >= range)
            to_base_n(n / range, range);
        putchar('0' + r);
        return;
    }
    10、(題意理解不清楚,借鑒CSDN——vs9841原作者的做法,腦子太笨,實在想不出來)
    #include <stdio.h>
    int Fibonacci(int n);
    int main(void)
    {
        int n = 9;
        printf("當n為%d時,斐波納契數(shù)值為%d", n, Fibonacci(9));
        return 0;
    }
    int Fibonacci(int n)
    {
        int a, b, i;
        a = 0;
        b = 1;
        int sum;
        if(n == 0)
            return 0;
        if(n == 1)
            return 1;
        else
        {
            for(i = 2; i <= n; i++)
            {
                sum = a + b;
                a = b;
                b = sum;
            }
            return sum;
        }
    }
    總結(jié):總體來說編程練習(xí)相對以往來說要簡單了,但第10題沒明白什么意思,所以只能借鑒別人的了,真是天下文章一大抄!
    posted on 2015-11-22 23:03 李阿昀 閱讀(1060) 評論(0)  編輯  收藏 所屬分類: C Primer Plus 復(fù)習(xí)題與編程練習(xí)
    主站蜘蛛池模板: 亚洲老熟女@TubeumTV| 亚洲一区精品伊人久久伊人| 91亚洲一区二区在线观看不卡| aa在线免费观看| 亚洲综合AV在线在线播放| 人成午夜免费大片在线观看| 免费在线观看理论片| 手机永久免费的AV在线电影网| 大胆亚洲人体视频| 无码毛片一区二区三区视频免费播放 | 亚洲爆乳无码一区二区三区| 中文字幕不卡高清免费| 亚洲精品蜜桃久久久久久| 可以免费观看的毛片| 亚洲美女中文字幕| 国国内清清草原免费视频99| 亚洲国产系列一区二区三区| 日韩免费一级毛片| 窝窝影视午夜看片免费| 亚洲精品无码永久在线观看你懂的| 99re6在线视频精品免费| 亚洲AV日韩AV天堂一区二区三区| 最近免费中文字幕大全高清大全1| 亚洲六月丁香六月婷婷蜜芽| 全免费A级毛片免费看网站| 无码的免费不卡毛片视频| 久久噜噜噜久久亚洲va久| www.黄色免费网站| 真人无码作爱免费视频| 亚洲乱色熟女一区二区三区丝袜| 一区二区三区观看免费中文视频在线播放 | 免费国产一级特黄久久| 成人精品一区二区三区不卡免费看| 78成人精品电影在线播放日韩精品电影一区亚洲 | 污污视频免费观看网站| 久久久久亚洲AV成人无码| 黄瓜视频高清在线看免费下载 | 亚洲乱色伦图片区小说| 亚洲精品~无码抽插| 免费可以在线看A∨网站| 亚洲五月午夜免费在线视频|