<sub id="1djzl"><dfn id="1djzl"><ins id="1djzl"></ins></dfn></sub>

    <sub id="1djzl"><delect id="1djzl"></delect></sub>

    <address id="1djzl"></address>
    <address id="1djzl"><dfn id="1djzl"></dfn></address><address id="1djzl"></address><form id="1djzl"></form>

    <address id="1djzl"><dfn id="1djzl"></dfn></address>
    <address id="1djzl"><listing id="1djzl"></listing></address>
    <thead id="1djzl"><var id="1djzl"><mark id="1djzl"></mark></var></thead>

        <form id="1djzl"><listing id="1djzl"></listing></form>
        <address id="1djzl"><var id="1djzl"><output id="1djzl"></output></var></address>

          <sub id="1djzl"></sub>

            <address id="1djzl"><var id="1djzl"></var></address>

              <sub id="1djzl"><dfn id="1djzl"></dfn></sub><sub id="1djzl"><var id="1djzl"><ins id="1djzl"></ins></var></sub>

                  <sub id="1djzl"><var id="1djzl"><output id="1djzl"></output></var></sub>

                    C Primer Plus 第六版 第八章節課后編程練習答案

                    首頁    學習交流    C語言    C Primer Plus 第六版 第八章節課后編程練習答案

                    編程環境Visual Studio 2017    (根據自己編程環境自行修改)

                    #include <stdio.h>#include <stdlib.h>#include <math.h>#include <ctype.h>// practice 1void p8_1(void){    char ch = 0;    int n_char = 0;    while ((ch = getchar()) != EOF)
                        {
                            n_char++;
                        }    printf("There are %d characters in the file!\n", n_char);    return;
                    }// practice 2void p8_2(void){    char ch = 0;    int n_char = 0;    while ((ch = getchar()) != EOF)
                        {
                            n_char++;        if (ch < ' ')
                            {            if ('\t' == ch)
                                {                putchar('\\');                putchar('t');                printf(":%d ", ch);
                                }            else if ('\n' == ch)
                                {                putchar('\\');                putchar('n');                printf(":%d ", ch);
                                }            else
                                {                putchar('^');                putchar(ch+64);                printf(":%d ", ch);
                                }
                            }        else
                            {            putchar(ch);            printf(":%d ", ch);
                            }        if (n_char % 10 == 0)
                            {            printf("\n");
                            }
                    
                        }
                    }// practice 3void p8_3(void){    char ch = 0;    int n_upper = 0;    int n_lower = 0;    while ((ch = getchar()) != EOF)
                        {        if (islower(ch))
                            {
                                n_lower++;
                            }        else if (isupper(ch))
                            {
                                n_upper++;
                            }
                        }    printf("There are %d upper character and %d lower charecter.\n", n_upper, n_lower);    return; 
                    }// practice 4void p8_4(void){    char ch = 0;    char ch_pre = 0;    int n_word = 0;    int total_chr = 0;    int word_chr = 0;    double word_chr_avg = 0;    while (ch = getchar()) // 清除文件開頭處的空格、標點和換行符等
                        {        if (isalpha(ch))
                            {            break;
                            }
                        }    while (ch != EOF)
                        {        if ((' ' == ch || '\n' == ch) && !isspace(ch_pre)) // 排除連續空格情況
                            {
                                n_word++;            printf("ch_pre = %c\n", ch_pre);
                                total_chr += word_chr;            printf("word_chr = %d, total_chr = %d\n", word_chr, total_chr);
                                word_chr = 0;
                            }        else if (isspace(ch))  // 清除連續的空格
                            {
                                ch_pre = ch;
                                ch = getchar();            continue;
                            }        if (!ispunct(ch) && !isspace(ch))
                            {
                                word_chr++;
                            }
                            ch_pre = ch;
                            ch = getchar();
                        }    if (!isspace(ch_pre))
                        {
                            n_word++;        printf("ch_pre = %c\n", ch_pre);
                            total_chr += word_chr;
                        }
                    
                        word_chr_avg = (double)total_chr / n_word;    printf("Total words: %d, Total characters: %d, Characters per word: %.2lf", n_word, total_chr, word_chr_avg);
                    }// practice 5void p8_5(void){    char ch = 0;    int upper = 100;    int lower = 0;    int guest = 50;    printf("I guest the number is 50. Is it right(Y/N)?");
                        scanf_s("%c", &ch);
                        getchar();    while ('Y' != ch)
                        {        printf("Ok, the number you chosen is bigger or smaller than I guest?(B/S)");
                            scanf_s("%c", &ch);
                            getchar();        if ('B' == ch)
                            {
                                lower = guest;
                                guest = (lower + upper) / 2;
                            }        else
                            {
                                upper = guest;
                                guest = (lower + upper) / 2;
                            }        printf("I guest the number is %d, Is it right(Y/N)?", guest);
                            scanf_s("%c", &ch);
                            getchar();
                        }    printf("HAHA, I win!");    return;
                    }// practice 6char get_first(void){    int ch = 0;    while (!isalpha(ch = getchar()))
                        {        continue;
                        }    return ch;
                    }void p8_6(void){    char ch = get_first();    putchar(ch);
                    }// practice 7#define RATE1 0.15#define RATE2 0.20#define RATE3 0.25void p8_7(void){    char chooice = 0;    double work_hours = 0;    double total_income = 0;    double tax = 0;    double net_income = 0;    double hourly_wage = 0;    while (1)
                        {        printf("Enter the number corresponding to the desired pay rate to action:\n");        printf("%-20s%-20s\n%-20s%-20s\n%-20s\n", "a) $8.75/hr", "b) $9.33/hr", "c) $10.00/hr", "d) $11.20/hr", "q) quit");
                            scanf_s("%c", &chooice);
                            getchar();        switch (chooice)
                            {        case 'a':
                                hourly_wage = 8.75;            break;        case 'b':
                                hourly_wage = 9.33;            break;        case 'c':
                                hourly_wage = 10.00;            break;        case 'd':
                                hourly_wage = 11.20;            break;        case 'q':            return;        default:            printf("Please enter the choice between a to d and q\n");            continue;
                            }        printf("How long have you worked:");
                            scanf_s("%lf", &work_hours);
                            getchar();        if (work_hours > 40)
                            {
                                work_hours = (work_hours - 40) * 1.5 + 40;
                            }
                    
                            total_income = work_hours * hourly_wage;        if (total_income <= 300)
                            {
                                tax = total_income * RATE1;
                                net_income = total_income - tax;
                            }        else if (300 < total_income <= 450)
                            {
                                tax = 300 * RATE1 + (total_income - 300) * RATE2;
                                net_income = total_income - tax;
                            }        else
                            {
                                tax = 300 * RATE1 + 150 * RATE2 + (total_income - 450) * RATE3;
                                net_income = total_income - tax;
                            }        printf("total income = %.2lf, tax = %.2lf, net income = %.2lf\n", total_income, tax, net_income);
                        }    return;
                    }// practice 8// 浮點數無法和0.0進行"=="判斷,設置一個判斷閥值#define ABS_VAL 0.000001void p8_8(void){    float number1 = 0.0;    float number2 = 0.0;    char choice = 0;    do
                        {        printf("Enter the operation of your choice:\n");        printf("%-20s%-20s\n%-20s%-20s\n%-20s\n", "a. add", "s. subtract", "m. multuply", "d. divide", "q. quit");        // 如果scanf的返回值為1并且輸入值是a、s、m、d、q中之一代表輸入成功,這里取反剔除不符合條件的輸入
                            if (!(scanf("%c", &choice) == 1 && (('a' == choice) || ('s' == choice) || ('m' == choice) || ('d' == choice) || ('q' == choice))))
                            {            printf("Please enter the correct operation, please enter again!\n");            while (getchar() != '\n')
                                {                continue;
                                }            continue;
                            }
                            getchar();        if ('q' == choice)
                            {            printf("Bye!\n");            return;
                            }        printf("Enter first number:");        while (scanf_s("%f", &number1) != 1)
                            {            char ch = 0;            while ((ch = getchar()) != '\n')
                                {                putchar(ch);
                                }            printf(" is not an number.\n");            printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                            }
                            getchar();        printf("Enter second number:");        while (scanf_s("%f", &number2) != 1)
                            {            char ch = 0;            while ((ch = getchar()) != '\n')
                                {                putchar(ch);
                                }            printf(" is not an number.\n");            printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                            }
                            getchar();        switch (choice)
                            {        case 'a':            printf("%f + %f = %f\n", number1, number2, (number1 + number2));            break;        case 's':            printf("%f - %f = %f\n", number1, number2, (number1 - number2));            break;        case 'm':            printf("%f * %f = %f\n", number1, number2, (number1 * number2));            break;        case 'd':            //在閾值區間內,將輸入值判斷為零,要求重新輸入
                                while (number2 > -ABS_VAL && number2 < ABS_VAL)
                                {                printf("Enter a number other than 0:");                while (scanf_s("%f", &number2) != 1)
                                    {                    char ch = 0;                    while ((ch = getchar()) != '\n')
                                        {                        putchar(ch);
                                        }                    printf(" is not an number.\n");                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                                    }
                                    getchar();
                                }            printf("%f / %f = %f\n", number1, number2, (number1 / number2));            break;        default:            break;
                            }
                        } while (1);    return;
                    }int main(int argc, char **argv){
                        p8_8();    while (getchar());    return 0;
                    }

                                

                        本文章轉自博主  Jimmy Chen 

                        轉載請標明出處



                    2018年8月12日 21:45
                    ?瀏覽量:0
                    ?收藏
                    国产偷拍欧洲日韩亚洲 - 视频 - 在线观看 - 影视资讯 - 新赏网