<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 評(píng)論 :: 0 Trackbacks
    復(fù)習(xí)題
    1、putchar(getchar())是一個(gè)有效的表達(dá)式,它實(shí)現(xiàn)什么功能?getchar(putchar())也有效嗎?
    答:
    語句putchar(getchar())使程序讀取下一個(gè)輸入字符并打印它,getchar()的返回值作為putchar()的參數(shù)。getchar(putchar())則不是合法的,因?yàn)間etchar()不需要參數(shù)而putchar()需要一個(gè)參數(shù)。
    2、下面的每個(gè)語句實(shí)現(xiàn)什么功能?
        a.putchar('H');
        b.putchar('\007');
        c.putchar('\n');
        d.putchar('\b');
    答:
    a. 顯示字符H
    b.如果系統(tǒng)使用ASCII字符編碼,則發(fā)出一聲警報(bào)
    c.把光標(biāo)移動(dòng)到下一行的開始
    d.退后一格
    3、假設(shè)您有一個(gè)程序count,該程序?qū)斎氲淖址M(jìn)行統(tǒng)計(jì)。用count程序設(shè)計(jì)一個(gè)命令行命令,對文件essay中的字符進(jìn)行計(jì)數(shù)并將結(jié)果保存在名為essayct的文件中。
    答:
    count < essay > essayct
    4、給定問題3中的程序和文件,下面哪個(gè)命令是正確的?
    答:
    a.essayct <essay
    b.count essay
    c.essay >count
    答:
    c是正確的。
    5、EOF是什么?
    答:
    它是由getchar()和scanf()返回的信號(hào)(一個(gè)特定的值),用來表明已經(jīng)到達(dá)了文件的結(jié)尾。
    6、對給出的輸入,下面每個(gè)程序段的輸出是什么(假定ch是int類型的,并且輸入是緩沖的)?
    a. 輸入如下所示:
        If you quit, I will.[enter]
        程序段如下所示:
        while ((ch = getchar()) != 'i')
                putchar(ch);
    b. 輸入如下所示:
        Harhar[enter]
        程序段如下所示:
        while ((ch = getchar()) != '\n')
        {
                   putchar(ch++);
                   putchar(++ch);
        }
    答:
    a.If you qu
    b.HJacrthjacrt
    7、C如何處理具有不同文件和換行約定的不同計(jì)算機(jī)系統(tǒng)?
    答:
    C的標(biāo)準(zhǔn)I/O庫把不同的文件形式映射為統(tǒng)一的流,這樣就可以按相同的方式對它們進(jìn)行處理。
    8、在緩沖系統(tǒng)中把數(shù)值輸入與字符輸入相混合時(shí),您所面臨的潛在問題是什么?
    答:
    數(shù)字輸入跳過空格和換行符,但是字符輸入并不是這樣。假設(shè)您編寫了這樣的代碼:
        int score;
        char grade;
        printf("Enter the score.\n");
        scanf("%d", &score);
        printf("Enter the letter grade.\n");
        grade = getchar();
    假設(shè)您輸入分?jǐn)?shù)98,然后按下回車鍵來把分?jǐn)?shù)發(fā)送給程序,您同時(shí)也發(fā)送了一個(gè)換行符,它會(huì)成為下一個(gè)輸入字符被讀取到grade中作為等級(jí)的值。如果在字符輸入之前進(jìn)行了數(shù)字輸入,就應(yīng)該添加代碼以在獲取字符輸入之前剔除換行字符。
    編程練習(xí)
    1、
    #include <stdio.h>
    int main(void)
    {
        int ch;
        int count = 0;
        while((ch = getchar()) != EOF) // 包括換行符
            count++;
        printf("The number of characters is %d\n", count);
        return 0;
    }
    2、(覺得這題超難的?。?!看了一些他人寫的例子,簡直胡說八道?。?!不過還是完美解決了)
    #include <stdio.h>
    int main(void)
    {
        int ch;
        int i = 0;

        while((ch = getchar()) != EOF)
        {
            if(ch >= 32)  // 可打印字符
            {
                putchar(ch);
                printf("/%d  ", ch);
                i++;
            }
            else if(ch == '\n')  // 打印換行符
            {
                printf("\\n");
                printf("/%d  ", ch);
                putchar(ch); // 清除輸入緩沖區(qū)里面的換行符
                = 0// i置為0重新開始計(jì)數(shù),因?yàn)轭}目要求每次遇到一個(gè)換行符時(shí)就要開始打印一個(gè)新行
            }
            else if(ch == '\t')  // 打印制表符
            {
                printf("\\t");
                printf("/%d  ", ch);
                i++;
            }
            else // 打印控制字符
            {
                putchar('^');
                putchar(ch + 64);
                printf("/%d  ", ch);
            }
            if(i == 10)
            {
                putchar('\n');
                i = 0;
            }
        }
        return 0;
    }
    運(yùn)行結(jié)果如下:
    I love you!
    I/73   /32  l/108  o/111  v/118  e/101   /32  y/121  o/111  u/117(每行打印10個(gè)值)
    !/33  \n/10(每次遇到一個(gè)換行符時(shí)就開始一個(gè)新行)
    My hello world^A
    M/77  y/121   /32  h/104  e/101  l/108  l/108  o/111   /32  w/119(每行打印10個(gè)值)
    o/111  r/114  l/108  d/100  ^A/1  \n/10(每次遇到一個(gè)換行符時(shí)就開始一個(gè)新行)
    ^Z
    3、
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
        int ch;
        int low_count = 0, up_count = 0;

        while((ch = getchar()) != EOF)
        {
            if(islower(ch))
                low_count++;
            if(isupper(ch))
                up_count++;
        }
        printf("A number of capital letters: %d\n", up_count);
        printf("A number of lower case letters: %d\n", low_count);
        return 0;
    }
    4、
    #include <stdio.h>
    #include <ctype.h>
    #include <stdbool.h>
    int main(void)
    {
        char ch;
        long chars = 0L; // 統(tǒng)計(jì)單詞的字符數(shù)
        int words= 0; // 單詞數(shù)
        bool inword = false// 如果ch在一個(gè)單詞中,則inword為true

        printf("Enter text to be analyzed: \n");
        while((ch = getchar()) != EOF)
        {
            if(!isspace(ch) && !ispunct(ch))
                chars++;
            if(!isspace(ch) && !inword)
            {
                inword = true;
                words++;
            }
            if(isspace(ch) && inword)
                inword = false;
        }
        printf("The average number of words per word: %ld\n", chars / words);
        return 0;
    }
    5、(二分搜索算法第一次碰見,搞了大半天了,借鑒的是CSDN-----vs9841作者的做法,不過稍微加了下工)
    #include <stdio.h>
    char get_choice(void);
    char get_first(void);
    int main(void)
    {
        int low = 1, high = 100, guess = 50;
        char ch;

        printf("Pick an integer from 1 to 100. I will try to guess it\n");
        printf("Unis your number %d?\n", guess);
        while((ch = get_choice()) != 'q')
        {
            if(ch == 'a')
            {
                printf("I knew I could do it!\n");
                break;
            }
            else if(ch == 'b')
            {
                printf("It is too small!\n");
                low = guess + 1;
            }
            else if(ch == 'c')
            {
                printf("It is too big!\n");
                high = guess - 1;
            }
            guess = (low + high) / 2;
            printf("Unis your number %d?\n", guess);
        }
        printf("Done!\n");
        return 0;
    }
    char get_choice(void)
    {
        int ch;

        printf("Enter the letter of your choice: \n");
        printf("a. right       b. too small\n");
        printf("c. too big     q. quit\n");
        ch = get_first();
        while((ch < 'a' || ch > 'c') && ch != 'q')
        {
            printf("Please respond with a, b, c, or q.\n");
            ch = get_first();
        }
        return ch;
    }
    char get_first(void)
    {
        int ch;

        ch = getchar();
        while(getchar() != '\n')
            continue;
        return ch;
    }
    6、
    char get_first(void)
    {
        int ch;

        while((ch = getchar()) == '\n')
            continue;
        while(getchar() != '\n')
            continue;
        return ch;
    }
    7、
    #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)
    char get_choice(void);
    char get_first(void);
    int main(void)
    {
        int hour, choise;
        double total, tax, net_pay;
        double base_pay; // 基本工資等級(jí)不能用#define來定義了,因?yàn)樗S著程序而改變了,書上真是胡說八道

        while((choise = get_choice()) != 'q')
        {
            switch(choise)
            {
            case 'a':
                base_pay = 8.15;
                break;  // break只是導(dǎo)致程序脫離switch語句,跳到switch之后的下一條語句!??!
            case 'b':
                base_pay = 9.33;
                break;
            case 'c':
                base_pay = 10.00;
                break;
            case 'd':
                base_pay = 11.20;
                break;
            default:
                printf("Program error!\n");
                break;
            }
            printf("Please enter the hour used: ");
            scanf("%d", &hour); // 獲取每周工作小時(shí)數(shù)時(shí)沒有像書上那樣判斷,我偷懶了!??!
            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("Bye!\n");
        return 0;
    }
    char get_choice(void)
    {
        int ch;

        printf("*****************************************************************\n");
        printf("Enter number corresponding to the desired pay rate or action:\n");
        printf("a) $8.75/hr\tb) $9.33/hr\n");
        printf("c) $10.00/hr\td) $11.20/hr\n");
        printf("q) quit\n");
        printf("*****************************************************************\n");
        printf("Please enter your choise: ");
        ch = get_first();
        while((ch < 'a' || ch > 'd') && ch != 'q')
        {
            printf("Please respond with a, b, c, d, or q.\n");
            ch = get_first();
        }
        return ch;
    }
    char get_first(void)
    {
        int ch;

        while((ch = getchar()) == '\n')
            continue;
        while(getchar() != '\n')
            continue;
        return ch;
    }
    8、
    #include <stdio.h>
    char get_choice(void);
    char get_first(void);
    float get_float(void);
    int main(void)
    {
        char choise;
        float first_number, second_number;

        while((choise = get_choice()) != 'q')
        {
            printf("Enter first number: ");
            first_number = get_float();
            printf("Enter second number: ");
            second_number = get_float();
            switch(choise)
            {
            case 'a':
                printf("%.1f + %.1f = %.1f\n", first_number, second_number, first_number + second_number);
                break;
            case 's':
                printf("%.1f - %.1f = %.1f\n", first_number, second_number, first_number - second_number);
                break;
            case 'm':
                printf("%.1f * %.1f = %.1f\n", first_number, second_number, first_number * second_number);
                break;
            case 'd':
                if(second_number == 0)
                {
                    printf("Enter a number other than 0: ");
                    second_number = get_float();
                    printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
                }
                else
                    printf("%.1f / %.1f = %.1f\n", first_number, second_number, first_number / second_number);
                break;
            default:
                printf("Program error!\n");
                break;
            }
        }
        printf("Bye.\n");
        return 0;
    }
    char get_choice(void)
    {
        int ch;

        printf("Enter the operation of your choice: \n");
        printf("a. add\ts. subtract\n");
        printf("m. multiply\td. divide\n");
        printf("q. quit\n");
        ch = get_first();
        while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
        {
            printf("Please respond with a, s, m, d, or q.\n");
            ch = get_first();
        }
        return ch;
    }
    char get_first(void)
    {
        int ch;

        while((ch = getchar()) == '\n')
            continue;
        while(getchar() != '\n')
            continue;
        return ch;
    }
    float get_float(void)
    {
        float input;
        char ch;

        while((scanf("%f", &input)) != 1)
        {
            while((ch = getchar()) != '\n')
                putchar(ch);
            printf(" is not a number.\nPlease enter a ");
            printf("number, such as 2.5, -1.78E8, or 3: ");
        }
        return input;
    }

    posted on 2015-11-21 20:12 李阿昀 閱讀(428) 評(píng)論(0)  編輯  收藏 所屬分類: C Primer Plus 復(fù)習(xí)題與編程練習(xí)
    主站蜘蛛池模板: 亚洲国产日韩综合久久精品| 国产精品亚洲av色欲三区| 中文字幕天天躁日日躁狠狠躁免费| 亚洲视频一区在线| 暖暖日本免费在线视频| 国产精品福利在线观看免费不卡| 亚洲好看的理论片电影| 毛片基地免费视频a| 一级毛片在播放免费| 亚洲一区二区三区亚瑟 | 99视频免费观看| 久久亚洲AV成人无码国产最大| 国产亚洲av人片在线观看| 亚洲免费观看在线视频| 免费播放国产性色生活片| 中文字幕亚洲综合久久| 免费永久看黄在线观看app| 7m凹凸精品分类大全免费| 国产精品亚洲天堂| 亚洲综合小说久久另类区| 亚洲人成网站色在线入口 | 亚洲AⅤ优女AV综合久久久| 99视频在线免费| 一级做a爰片久久毛片免费陪| 亚洲高清无在码在线电影不卡| 免费在线观看污网站| 精品女同一区二区三区免费站| 一级一片免费视频播放| 亚洲一级高清在线中文字幕| 亚洲精品夜夜夜妓女网| 国产精品高清全国免费观看| 18女人腿打开无遮掩免费| 男女一边摸一边做爽的免费视频| 亚洲熟妇少妇任你躁在线观看| 亚洲精品免费在线观看| 亚洲精品一级无码鲁丝片| 国产精品另类激情久久久免费| 国产精品免费观看| 国产免费一区二区视频| 精品久久久久久国产免费了| 亚洲精品无码久久久久秋霞|