<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、確定哪個表達式為true,哪個為false。
        a.100 > 3 && 'a' > 'c'
        b.100 > 3 || 'a' > 'c'
        c.!(100>3)
    答:
    a.false
    b.true
    c.false
    2、構造一個表達式來表示下列條件:
    a.number等于或大于90,但是小于100
    b.ch不是字符q也不是字符k
    c.number界于1到9之間(包括1和9),但是不等于5
    d.number不在1到9之間
    答:
    a.number >= 90 && number < 100
    b.ch != 'q' && ch != 'k'
    c.number >= 1 && number <= 9 && number != 5
    d.number < 1 || number > 9
    3、下面程序中的關系表達式過于復雜,并有些錯誤,請簡化并改正它。
     #include <stdio.h> 
     1
     int main(void)
     2 {
     3     int weight, height; /* weight以磅為單位,height以英寸為單位 */
     4 
     5     scanf("%d, weight, height);
     6     if(weight < 100 && height > 64)
     7         if(height >= 72)
     8             printf("You are very tall for your weight.\n");
     9         else if(height < 72 && > 64)
    10             printf("You are tall for your weight.\n");
    11     else if(weight > 300 && !(weight <= 300)
    12             && height < 48)
    13         if(!(height >= 48))
    14             printf(" You are quite short for your weight.\n");
    15     else
    16         printf("Your weight is ideal.\n");
    17 
    18     return 0;
    19 }
    答:
    第5行:應該是scanf("%d %d", &weight, &height);。在scanf()中不要忘記使用&運算符。這一行前面也應該有提示輸入的語句。但第6行已經保證height>64,因此,不需要任何測試,并且
    else if應該是else。
    第9行:它的意思是(height<72&&height>64)。但是表達式的第一部分是不必要的,因為既然程序已經到達了這個else if,那么height必然小于72。因此一個簡單的(height>64)就可以了。
    第11行:條件冗余;第二個表達式(weight不是小于或等于300的)與第一個子表達式意義相同。所需要的只是一個簡單的(weight>300)。但是這里還有更多的問題。第11行屬于一個不正確的if!很明顯,這個else是與第6行中的if相匹配的。但是根據if的“最就近原則”,它會與第7行的if相匹配。(因此會在weight小于100并且height小于或等于64時到達第11行。這就使得在到達該語句時weight不可能超過300。)
    第7到10行:應該用花括號括起來。這樣第11行就會是第6行而不是第7行的可選情況。而如果到第9行的else if由一個簡單的else替代了,就不再需要花括號了。
    第13行:應該簡化為if(height<48)。其實這一行完全可以忽略,因為第12行已經作了這種測試。
    第15行:這個else與第13行的if相匹配。把第13行和第14行括在花括號中可以強制使這個else與第6行的if相匹配。或者,按照建議,簡單地刪除第13行。
    下面是一個正確的版本:
    #include <stdio.h>
    int main(void)
    {
        int weight, height; /* weight以磅為單位,height以英寸為單位 */
        printf("Enter your weight in pounds and ");
        printf("your height in inches.\n");
        scanf("%d %d", &weight, &height);
        if(weight < 100 && height > 64)
            if(height >= 72)
                printf("You are very tall for your weight.\n");
            else
                printf("You are tall for your weight.\n");
        else if(weight > 300 && height < 48)
                printf(" You are quite short for your weight.\n");
        else
            printf("Your weight is ideal.\n");

        return 0;
    }
    4、下列每個表達式的數值是多少?
    a.5 > 2
    b.3 + 4 > 2 && 3 < 2
    c.x >= y || y > x
    d.d = 5 + (6 > 2)
    e.'X' > 'T' ? 10 : 5
    f. x > y ? y > x : x > y
    答:
    a.1
    b.0
    c.1(如果第一個表達式為假則第二個為真,反之亦然;只需要一個為真的表達式,結果就為真。)
    d.6
    e.10
    f.0
    5、下列程序將打印出什么?
    #include <stdio.h>
    int main(void)
    {
        int num;
        for(num = 1; num <= 11; num++)
        {
            if(num % 3 == 0)
                putchar('$');
            else
                putchar('*');
                putchar('#');
            putchar('%');
        }
        putchar('\n');
        return 0;
    }
    答:
    *#%*#%$#%*#%*#%$#%*#%*#%$#%*#%*#%
    6、下列程序將打印出什么?
    #include <stdio.h>
    int main(void)
    {
        int i = 0;
        while(i < 3)
        {
            switch(i++)
            {
                case 0: printf("fat");
                case 1: printf("hat");
                case 2: printf("cat");
                default: printf("Oh no!");
            }
            putchar('\n');
        }
        return 0;
    }
    答:
    fathatcatOh no!
    hatcatOh no!
    catOh no!
    7、下列程序有什么錯誤?
     1 #include <stdio.h>
     2 int main(void)
     3 {
     4     char ch;
     5     int lc = 0;    /*統計小寫字符
     6     int uc = 0;    /*統計大寫字符
     7     int oc = 0;    /*統計其他字符
     8 
     9     while((ch = getchar()) != '#')
    10     {
    11         if('a' <= ch >= 'z')
    12             lc++;
    13         else if(!(ch < 'A') || !(ch > 'Z')
    14             uc++;
    15         oc++;
    16     }
    17     printf("%d lowercase, %d uppercase, %d other, lc, uc, oc");
    18     return 0;
    19 }
    答:
    第5行到第7行的注釋應該以*/結尾,或者用//來代替/*。表達式'a' <= ch >= 'z'應該被寫成這樣:ch >= 'a' && ch <= 'z'。或者用一種更簡單也更通用的方法:包含ctype.h文件并使用islower()。順便提一下,'a' <= ch >= 'z'在C中是合法的,只是不具有正確的意義。因為關系運算符是從左到右結合的,所以這個表達式被解釋為('a' <= ch) >= 'z'。圓括號中表達式的值為1或0(真或假),然后檢查這個值來看它是否大于或等于'z'的數值編碼。0和1都不能滿足這個條件,所以整個表達式的值總是為0(假)。在第二個判斷表達式中,在第二個表達式中,||應該為&&,盡管!(ch < 'A')是合法的,而且意義也正確,但ch >= 'A'更為簡單。'Z'后面需要有兩個結束圓括號,而不是一個。再一次更簡單的方法是使用isuper()。應該在oc++;語句前面放置一個else,否則。每輸入一個字符,它都會加1。在printf()調用中的控制表達式應該用雙引號引起來。
    下面是一個正確的版本:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
        char ch;
        int lc = 0;    //統計小寫字符
        int uc = 0;    //統計大寫字符
        int oc = 0;    //統計其他字符

        while((ch = getchar()) != '#')
        {
            if(islower(ch))
                lc++;
            else if(isupper(ch))
                uc++;
            else
                oc++;
        }
        printf("%d lowercase, %d uppercase, %d other", lc, uc, oc);
        return 0;
    }
    8、下列程序將打印出什么?
    /* retire.c*/
    #include <stdio.h>
    int main(void)
    {
        int age = 20;

        while(age++ <= 65)
        {
            if((age % 20) == 0)      /* age能被20整除嗎?*/
                printf("You are %d. Here is a raise.\n", age);
            if(age = 65)
                printf("You are %d. Here is your gold watch.\n", age);
        }
        return 0;
    }
    答:
    無休止地打印同一行:
    You are 65. Here is your gold watch.
    9、當給定下述輸入時,下列程序將打印出什么?
    q
    c
    g
    b
    #include <stdio.h>
    int main(void)
    {
        char ch;

        while((ch = getchar()) != '#')
        {
            if(ch == '\n')
                continue;
            printf("Step 1\n");
            if(ch == 'c')
                continue;
            else if(ch == 'b')
                break;
            else if(ch == 'g')
                goto laststep;
            
    printf("Step 2\n")
    ;
            laststep: printf("Step 3\n");
        }
        printf("Done!\n");
        return 0;
    }
    答:
    q
    Step 1
    Step 2
    Step 3  (當ch == 'q'時,laststep: printf("Step 3\n");語句也要打印出來)
    c
    Step 1
    g
    Step 1
    Step 3 (當ch == 'g'時,通過goto語句跳轉到laststep: printf("Step 3\n");,前面的語句
    printf("Step 2\n")
    ;就不要打印出來了
    b
    Step 1
    Done!
    10、重寫題目9的程序,以使它表現相同的行為但不使用continue或goto。
    #include <stdio.h>
    int main(void)
    {
        char ch;

        while((ch = getchar()) != '#')
        {
            if(ch != '\n')
            {
                printf("Step 1\n");
                if(ch != 'c')
                {
                    if(ch == 'b')
                        break;
                    if(ch != 'g')
                        printf("Step 2\n");
                    printf("Step 3\n");
                }
            }
        }
        printf("Done!\n");
        return 0;
    }
    編程練習
    1、
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
        int s_ct = 0; //統計空格數
        int l_ct = 0; //統計換行數
        int o_ct = 0; //統計除空格和換行符之外的其他字符
        char ch;

        printf("Please enter text to be analyzed(# to terminate): \n");
        while((ch = getchar()) != '#')
        {
            if(ch == ' ')
                s_ct++;
            if(ch == '\n')
                l_ct++;
            if(ch != ' ' && ch != '\n')
                o_ct++;
        }
        printf("The number of spaces,lines,others: %d, %d, %d", s_ct, l_ct, o_ct);
        return 0;
    }
    2、
    #include <stdio.h>
    int main(void)
    {
        char ch;
        int count = 0;

        printf("Please enter text to be analyzed(# to terminate): \n");
        while((ch = getchar()) != '#')  // 當讀取\n時,又是一種什么情況,比如說要從幾行來鍵入???
        {
            count++;
            printf("%c/%d ", ch, ch);
            if(count % 8 == 0)
                printf("\n");
        }
        //printf("Each character and its ASCII code value:");
        return 0;
    }
    3、
    #include <stdio.h>
    int main(void)
    {
        int even_count = 0;
        float even_sum = 0;
        int odd_count = 0;
        float odd_sum = 0;
        int number;
        printf("Please enter text to be analyzed(# to terminate): \n");
        while(scanf("%d", &number) == 1)
        {
            if(number == 0)
                break;
            if(number % 2 == 0)
            {
                even_count++;
                even_sum += number;
            }
            else
            {
                odd_count++;
                odd_sum += number;
            }
        }
        printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
        printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
        return 0;
    }
    4、
    #include <stdio.h>
    int main(void)
    {
        char ch;
        int count = 0;

        printf("Please enter text to be analyzed(# to terminate): \n");
        while((ch = getchar()) != '#')
        {
            if(ch == '!')
            {
                count++;
                printf("!!");
            }
            else if(ch == '.')
            {
                count++;
                putchar('!');
            }
            else
                putchar(ch);
        }
        printf("\nNumber of alternatives: %d", count);
        return 0;
    }
    5、
    #include <stdio.h>
    int main(void)
    {
        int even_count = 0;
        float even_sum = 0;
        int odd_count = 0;
        float odd_sum = 0;
        int number;
        printf("Please enter text to be analyzed(# to terminate): \n");
        while(scanf("%d", &number) == 1)
        {
            // 此判斷如何才能弄到switch里去呢???
            if(number == 0)
                break;
            switch(number % 2)
            {
                case 0: even_count++;
                        even_sum += number;
                        break;
                case 1: odd_count++;
                        odd_sum += number;
                        break;
            }
        }
        printf("The number of odd: %d; The average of odd: %.2f\n", odd_count, odd_sum / odd_count);
        printf("The number of even: %d; The average of even: %.2f\n", even_count, even_sum / even_count);
        return 0;
    }
    6、
    #include <stdio.h>
    #include <stdbool.h>
    int main(void)
    {
        char prev = 0; // 前一個字符
        char current = 0, ch;   // 當前字符
        int count = 0;

        printf("Please enter text to be analyzed(# to terminate): \n");
        while((ch = getchar()) != '#')
        {
            prev = current; // 每次都要更新前一個字符,我怎么就沒想到呢???
            current = ch;
            if(prev == 'e' && current == 'i')
                count++;
        }
        printf("The number of ei: %d\n", count);
        return 0;
    }
    7、
    #include <stdio.h>
    #define BASE_PAY 10.00
    #define WORK_OVERTIME 40
    #define MULTIPLE 1.5
    #define RATE1 0.15
    #define RATE2 0.20
    #define RATE3 0.25
    #define BREAK1 300
    #define BREAK2 450
    #define BASE1 (BREAK1 * RATE1)
    #define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
    int main(void)
    {
        int hour;
        double total, tax, net_pay;

        printf("Please enter the hour used.\n");
        scanf("%d", &hour);
        if(hour <= WORK_OVERTIME)
        {
            total = hour * BASE_PAY;
            if (total <= BREAK1)
            {
                tax = total * RATE1;
                net_pay = total - tax;
            }
            else
            {
                tax = BASE1 + (total - BREAK1) * RATE2;
                net_pay = total - tax;
            }
        }
        else
        {
            total = BASE_PAY * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * BASE_PAY;
            if(total <= BREAK2)
            {
                tax = BASE1 + (total - BREAK1) * RATE2;
                net_pay = total - tax;
            }
            else
            {
                tax = BASE2 + (total - BREAK2) * RATE3;
                net_pay = total - tax;
            }
        }
        printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
        return 0;
    }
    8、
    #include <stdio.h>
    #define WORK_OVERTIME 40
    #define MULTIPLE 1.5
    #define RATE1 0.15
    #define RATE2 0.20
    #define RATE3 0.25
    #define BREAK1 300
    #define BREAK2 450
    #define BASE1 (BREAK1 * RATE1)
    #define BASE2 (BASE1 + (BREAK2 - BREAK1) * RATE2)
    int main(void)
    {
        int hour, choise;
        double total, tax, net_pay;
        double base_pay; // 基本工資等級不能用#define來定義了,因為它要隨著程序而改變了,書上真是胡說八道
        printf("*****************************************************************\n");
        printf("Enter number corresponding to the desired pay rate or action:\n");
        printf("1) $8.75/hr\t2) $9.33/hr\n");
        printf("3) $10.00/hr\t4) $11.20/hr\n");
        printf("5) quit\n");
        printf("*****************************************************************\n");
        printf("Please enter your choise: ");
        while(scanf("%d", &choise) == 1)
        {
            if(choise == 5)
                break;
            else
                switch(choise)
                {
                case 1:
                    base_pay = 8.15;
                    break;  // break只是導致程序脫離switch語句,跳到switch之后的下一條語句!!!
                case 2:
                    base_pay = 9.33;
                    break;
                case 3:
                    base_pay = 10.00;
                    break;
                case 4:
                    base_pay = 11.20;
                    break;
                default:
                    printf("default choices to the user: 1  2  3  4  5\n");
                    printf("Please enter your choise: ");
                    continue// continue導致程序跳過該循環的其余部分,其中包括switch的其余部分!!!
                }
            printf("Please enter the hour used: ");
            scanf("%d", &hour);
            if(hour <= WORK_OVERTIME)
            {
                total = hour * base_pay;
                if (total <= BREAK1)
                {
                    tax = total * RATE1;
                    net_pay = total - tax;
                }
                else
                {
                    tax = BASE1 + (total - BREAK1) * RATE2;
                    net_pay = total - tax;
                }
            }
            else
            {
                total = base_pay * WORK_OVERTIME + (hour - WORK_OVERTIME) * MULTIPLE * base_pay;
                if(total <= BREAK2)
                {
                    tax = BASE1 + (total - BREAK1) * RATE2;
                    net_pay = total - tax;
                }
                else
                {
                    tax = BASE2 + (total - BREAK2) * RATE3;
                    net_pay = total - tax;
                }
            }
            printf("The total pay: %.2f; tax: %.2f; net pay: %.2f\n", total, tax, net_pay);
            printf("Please enter your choise: ");
        }
        return 0;
    }
    9、(接受一個整數輸入,然后顯示所有大于或等于該數的素數,我覺得這是第7章編程練習里最難的了!!!借鑒創十三的做法終于做出來了!!!
    #include <stdio.h>
    #include <stdbool.h>
    #include <math.h>
    bool isPrime(int a);    // 判斷一個數a是否為素數
    void printPrime(int a); // 輸出所有小于或等于a的素數
    int main(void)
    {
        int num;

        printf("Please input a number: ");
        scanf("%d", &num);
        printPrime(num);
        return 0;
    }
    bool isPrime(int a)
    {
        int i;
        int j = sqrt(a);

        for(i = 2; i <= j; i++)
        {
            if(a % i == 0)
                return false;
        }
        return true;
    }
    void printPrime(int a)
    {
        if(a == 1)
            printf("1是一個無效的輸入數!!!");
        else
            while(a != 1)
            {

                if(isPrime(a))
                    printf("%d  ", a);
                a--;
            }
    }
    10、(與練習8類似)
    #include <stdio.h>
    #define RATE1 0.15
    #define RATE2 0.28
    int main(void)
    {
        int choise, break_point;
        double income, tax;

        printf("*****************************************************************\n");
        printf("Enter number corresponding to the desired tax type or action:\n");
        printf("1) 單身\t2) 戶主\n");
        printf("3) 已婚,共有\t4) 已婚,離異\n");
        printf("5) 退出\n");
        printf("*****************************************************************\n");
        printf("Please enter your choise: ");
        while(scanf("%d", &choise) == 1)
        {
            if(choise == 5)
                break;
            else
                switch(choise)
                {
                case 1:
                    break_point = 17850;
                    break;
                case 2:
                    break_point = 23900;
                    break;
                case 3:
                    break_point = 29750;
                    break;
                case 4:
                    break_point = 14875;
                    break;
                default:
                    printf("default choices to the user: 1  2  3  4  5\n");
                    printf("Please enter your choise: ");
                    continue;
                }
            printf("Please enter your income: ");
            scanf("%lf", &income);
            if(income <= break_point)
                tax = income * RATE1;
            else
                tax = break_point * RATE1 + (income - break_point) * RATE2;
            printf("your tax is %.2f\n", tax);
            printf("Please enter your choise: ");
        }
        return 0;
    }
    11、
    #include <stdio.h>
    #define SALE1 1.25
    #define SALE2 0.65
    #define SALE3 0.89
    #define DISCOUNT_MONEY 100
    #define DISCOUNT 0.05
    #define WEIGHT1 5
    #define WEIGHT2 20
    #define TRANSPORTATION_COST1 3.50
    #define TRANSPORTATION_COST2 10.00
    #define BASE 8
    #define COST3 0.1
    int main(void)
    {
        int a = 0, b = 0, c = 0;
        int total_pounds;
        char choise;
        double total, discount, transportation_cost;

        printf("*****************************************************************\n");
        printf("Enter number corresponding to the desired action:\n");
        printf("a) 輸入所需朝鮮薊的磅數\n");
        printf("b) 輸入所需甜菜的磅數\n");
        printf("c) 輸入所需胡蘿卜的磅數\n");
        printf("q) 退出訂購過程\n");
        printf("*****************************************************************\n");
        printf("Please enter your choise: ");
        while((choise = getchar()) != 'q')
        {
            switch(choise)
            {
            case 'a':
                printf("輸入所需朝鮮薊的磅數: ");
                scanf("%d", &a);
                break;
            case 'b':
                printf("輸入所需甜菜的磅數: ");
                scanf("%d", &b);
                break;
            case 'c':
                printf("輸入所需胡蘿卜的磅數: ");
                scanf("%d", &c);
                break;
            default:
                printf("輸入有誤,您有合適的選項:a b c q\n");
                break;
            }
            while(getchar() != '\n')
                continue;
            printf("Please enter your choise: ");
        }
        total = SALE1 * a + SALE2 * b + SALE1 * c;
        total_pounds = a + b + c;
        if(total < DISCOUNT_MONEY)
            discount = 0;
        else
            discount = total * DISCOUNT;
        if(total_pounds <= WEIGHT1)
            transportation_cost = TRANSPORTATION_COST1;
        else if(total_pounds > 5 && total_pounds < 20)
            transportation_cost = TRANSPORTATION_COST2;
        else
            transportation_cost = BASE + COST3 * total_pounds;
        printf("所需朝鮮薊的磅數: %d\n", a);
        printf("所需甜菜的磅數: %d\n", b);
        printf("所需胡蘿卜的磅數: %d\n", c);
        printf("訂購的總磅數:%d\n", total_pounds);
        printf("訂購的朝鮮薊蔬菜的費用:%.2f\n", a * SALE1);
        printf("訂購的甜菜蔬菜的費用:%.2f\n", b * SALE2);
        printf("訂購的胡蘿卜蔬菜的費用:%.2f\n", c * SALE3);
        printf("訂單的運輸和裝卸費用:%.2f\n", transportation_cost);
        printf("訂單的折扣:%.2f\n", discount);
        printf("訂單的總費用(包括運輸費用,而且還要減去折扣):%.2f\n", total - discount + transportation_cost);
        return 0;
    }
    posted on 2015-11-20 00:02 李阿昀 閱讀(587) 評論(0)  編輯  收藏 所屬分類: C Primer Plus 復習題與編程練習
    主站蜘蛛池模板: 日韩免费一区二区三区| 黄页免费在线观看| 在线观看免费宅男视频| 国产v亚洲v天堂a无| 成年网站免费视频A在线双飞| 亚洲黑人嫩小videos| 中文无码成人免费视频在线观看| 三年片在线观看免费观看大全动漫| 日韩精品无码免费一区二区三区| 中文字幕无码播放免费| 亚洲一卡2卡4卡5卡6卡在线99| 国产亚洲欧美日韩亚洲中文色| 国产亚洲免费的视频看| 亚洲av成人无码久久精品| 日韩精品极品视频在线观看免费| 国产在线国偷精品产拍免费| 亚洲熟妇自偷自拍另欧美| 国产免费人成在线视频| 二个人看的www免费视频| 亚洲国产一区二区a毛片| 蜜桃视频在线观看免费视频网站WWW | 免费看少妇作爱视频| 亚洲高清一区二区三区| 日本人护士免费xxxx视频| 亚欧乱色国产精品免费视频| 亚洲嫩模在线观看| 成人免费淫片在线费观看| 有码人妻在线免费看片| 久久亚洲AV成人无码电影| 成人无遮挡毛片免费看| 4hu四虎免费影院www| 亚洲第一页中文字幕| 国产成人无码免费视频97| 99久久国产精品免费一区二区| 免费高清小黄站在线观看| 国产99久久久久久免费看| 亚洲AV无码精品色午夜果冻不卡 | 亚洲精品国产综合久久一线| 特级无码毛片免费视频尤物| 亚洲精品天堂无码中文字幕| 亚洲人成图片小说网站|