自学C第12天

C primer plus

第8章

第5题:

#include<stdio.h>

int main(void)

{

        int number;

        int high =100, low =0;

        int guess = (high + low) / 2; #注:中位数等于大数加小数除以2

        char answer;

        printf("Please put a integer from 0 to 100\n");

        printf("Is this number %d?\n",guess);

        printf("s means too small, b means too big, y means the answer is correct.");

        while ((answer=getchar()) != 'y')

        {

                switch (answer)  #注:除了switch语句还可以用if语句

                {

                case '\n': #注:getchar每次按下回车都会输入一个\n所以有必要添加这个case 不然会多出default中的语句。

                break;

                case 's':

                low = guess;

                guess = (high + low) / 2;

                printf("Well, is it %d?", guess);

                break;

                case 'b':

                high = guess;

                guess = (high + low) / 2;

                printf("Well, is it %d?", guess);

                break;

                default:

                printf("Please answer with 's', 'b' or 'y'");

                break;

                }

        }

        printf("Bingo!");

        return 0;

}

第6题:

一直无法理解这行代码

int ch;

ch = getchar();

while (getchar() != '\n')

continue;

return ch;

根据解释如果在 ch=getchar();中键入了 abc,return ch时 ch = a,其他输入会被清除。

根据搜索的资料可以这样理解:

当输入abc时,第一个字符会被直接赋值给ch 也就是 ch = a,其余的 bc和外加回车的 \n会进入缓存区。

当程序运行到 while (getchar() != '\n') 时 getchar()会从缓冲区读取字符 也就是剩下的 bc\n 用于判读并清除。

于是之后首字母a得以保留,这也是 为什么不用 while((ch=getchar()) != '\n')的原因,如果用了,a也应该会被清除。

#include <stdio.h>

#include <ctype.h>

int get_first(void);

int main(void)

{

        int ch;

        printf("Please enter some characters ('#' to quit):\n");

        while ((ch = get_first()) != '#')

        {

        printf("Result: %c\n", ch);

        printf("You can enter again ('#' to quit):\n");

        }

        printf("Done.\n");

        return 0;

}

int get_first(void)

{

        int ch;

        do

        {

                ch = getchar();

        } while (isspace(ch));  #注:do while循环一定会执行一次,如果输入的是空格,制表符,换行符,就会一直循环直到输入字符为止。

        while (getchar() != '\n')#注:用来去除除首字符外所有缓存中的数据。

                continue;

return ch;

}

第7题

# include <stdio.h>

#define rate_300 0.15

#define rate_150 0.2

#define rate_l 0.25

void show_menus(void);

void count(float a, float b);

int get_first(void); #注:如果需要用字符来表示选项,这步很重要,这个函数需要牢记

int main(void)

{

        int ch;

        float income_hour = 0.0;

        float hour = 0.0;

        float income = 0.0, tax = 0.0;

        show_menus();

        while ((ch=get_first()) !='q')

        {

                switch (ch)

                {

                case 'a':

                income_hour = 8.75;

                printf("Please enter how many hours you worked:");

                scanf("%g", &hour);

                count(income_hour, hour);

                show_menus();

                break;

                case 'b':

                income_hour = 9.33;

                printf("Please enter how many hours you worked:");

                scanf("%g", &hour);

                count(income_hour, hour);

                show_menus();

                break;

                case 'c':

                income_hour = 10.00;

                printf("Please enter how many hours you worked:");

                scanf("%g", &hour);

                count(income_hour, hour);

                show_menus();

                break;

                case 'd':

                income_hour = 11.20;

                printf("Please enter how many hours you worked:");

                scanf("%g", &hour);

                count(income_hour, hour);

                show_menus();

                break;

                default:

                printf("Please enter letter between a~q");

                break;

                }

        }

        printf("done!");

        return 0;

}

void count(float a, float b)

{

        float income = 0.0, tax = 0.0;

        if (a <= 40)

        {

        income = a * b;

        tax = 300 * rate_300 + 150 * rate_150 + (income - 450) * rate_l;

        }

        else

        income = a * (40 + (b - 40) * 1.5);

        tax = 300 * rate_300 + 150 * rate_150 + (income - 450) * rate_l;

        printf("This week your income is $%g, tax is $%g, net income $%g\n",

        income, tax, income - tax);

        return;

}

void show_menus(void)

{

        printf("*****************************************************************\n\n");

        printf("Enter the number corresponding to the desired pay rate or action:\n");

        printf("a) $8.75/hr                          b) $9.33/hr\n");

        printf("c) $10.00/hr                         d) $11.20/hr\n");

        printf("q) quit\n");

        printf("*****************************************************************\n\n");

}

int get_first(void)

{

        int ch;

        do

        {

                ch = getchar();

        } while (isspace(ch));

        while (getchar() != '\n')

                continue;

        return ch;

}

第8题:

#include <stdio.h>

#include <ctype.h>

int get_first(void);

void show_menus(void);

float get_number(void);

int main(void)

{

        int ch;

        float reslut = 0;

        float a, b;

        show_menus();

        while ((ch = get_first()) != 'q')

        {

                if (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')

                {

                        printf("Please enter the letter show on the menus\n");

                        continue;

         }

        printf("Enter first number:");

        a = get_number();

        printf("Enter second number:");

        b = get_number();

                switch(ch)

                {

                case 'a':

                reslut = a + b;

                printf("%g + %g = %g\n", a, b, reslut);

                show_menus();

                break;

                case 's':

                reslut = a - b;

                printf("%g - %g = %g\n", a, b, reslut);

                show_menus();

                break;

                case 'm':

                reslut = a * b;

                printf("%g * %g = %g\n", a, b, reslut);

                show_menus();

                break;

                case 'd':

                reslut = a / b;

                printf("%g / %g = %g\n", a, b, reslut);

                show_menus();

                break;

                }

        }

        printf("Done!");

        return 0;

}

int get_first(void)

{

        int ch;

        do {

                ch = getchar();

        } while (isspace(ch));

        while (getchar() != '\n')

                continue;

        return ch;

}

void show_menus(void)

{

         printf("Enter the operation of your choice:\n");

        printf("a. add            s. subtract\n");

        printf("m. multiply       d. divide\n");

        printf("q. quit\n");

}

float get_number(void)

{

        int ch;

        float input;

        while (scanf("%f", &input) != 1)

        {

                while ((ch = getchar()) != '\n') #注:如果上方是scanf,输入后getchar可以继承scanf存储在缓存中的值,从而无需输入直接判定,由于scanf输入后每次都会有回车,所以当getchar读取到末尾后,循环结束。

                putchar(ch);

                printf(" is not a number.\nPlease enter a number, such as 2.5, -1.78E8, or 3:");

         }

        return input;

}

加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值