【C Primer Plus第八章课后编程题】

水平有限

8.1

#include <stdio.h>
int main (void)
{
    char ch;   
    int count;

    count = 0;
    
    while((ch = getchar()) != EOF)
        count++;

    printf("Total %d characters.\n",count);
    return 0;
}



8.2

// 这一问需要查表
// 前31个是需要打印^
// 第32个三空格

#include <stdio.h>
int main (void)
{
    char ch;
    int count;

    count = 0;

    while((ch = getchar()) != EOF)
    {
        switch (ch)
        {
            case '\n':
                printf("\\n");
                printf("%3d\t",ch);
                break;
            
            case '\t':
                printf("\\t");
                printf("%3d\t",ch);
                break;
        
            default:
                if (ch < ' ')
                {
                    printf("^");
                    printf("%c",ch+64);
                    printf("%3d\t",ch);
                }
                else
                {
                    printf("%c",ch);
                    printf("%3d\t",ch);
                }
                break;
        }
        count++;
        if(count % 10 == 0)
            putchar('\n');
    }
    
    return 0;
}

8.3

#include <stdio.h>
#include <ctype.h>

int main (void)
{
    char ch;
    int count_l,count_up;

    count_l = count_up = 0;

    while((ch = getchar()) != EOF)
    {
        if (islower(ch))
        {
            count_l++;
        }
        if (isupper(ch))
        {
            count_up++;
        }
    }
    printf("%d lower character %d upper character.\n",count_l,count_up);

    return 0;
}

8.4

#include <stdio.h>
#include <ctype.h>

int main (void)
{
    char ch;
    float count_word,count_character;

    count_character = count_word = 0;

    while ((ch = getchar()) != EOF)
    {
        if (isalpha(ch))
        {
            count_character++;
        }
        else
        {
            count_word++;
        }
    }
    printf("%.2f worlds with %.2f mean characters.\n",count_word,count_character/count_word);

    return 0;
}

8.5

#include <stdio.h>
#include <time.h>
#include <stdlib.h>   

#define MAX 100

int main (void)
{
    int max,min,guess,value;

    max = MAX;
    min = 0;
    srand((unsigned int)time(NULL));
    value = rand()%(max + 1);

    printf("%d\n",value);

    printf("Pick an integer from 1 to 100. I will try to guess it.\n");
    scanf("%d",&guess);
    printf("Uh...is your number %d?\n",guess);
    while (guess != value)
    {
        if (guess > value)
        {
            max = guess;
            printf("The value you guessed is bigger than true.\n");
            printf("Try another number from %d to %d.\n",min,max);    
        }
        else
        {
            min = guess;
            printf("The value you guessed is smaller than true.\n");
            printf("Try another number from %d to %d.\n",min,max);    
        }
        scanf("%d",&guess);
    }
    printf("I knew I could do it!\n");

    return 0;
}


8.6

#include <stdio.h>

char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);

int main (void)
{
    int choice;
    void count(void);

    while ((choice = get_choice()) != 'q')
    {
        switch (choice)
        {
            case 'a':   printf("Buy low, sell high.\n");
                        break;
            case 'b':   putchar('\a');
                        break;
            case 'c':   count();
                        break;
            default:    printf("Program error!\n");
                        break;
        }
    }
    printf("Bye.\n");

    return 0;
}

void count(void)
{
    int n, i;

    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for(i = 1; i <= n;i++)
        printf("%d\n",i);
    while(getchar() != '\n')
        continue;
}

char get_choice(void)
{
    int ch;

    printf("Enter the letter of your choice:\n");
    printf("a. advice          b. bell\n");
    printf("c. count           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;

    while((ch = getchar()) == ' ');
    return ch;
}

int get_int(void)
{
    int input;
    char ch;

    while (scanf("%d",&input) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value, such as 25, -178, or 3: ");
    }
    
    return input;
}




8.7

#include <stdio.h>

#define level_1     8.75
#define level_2     9.33
#define level_3     10.00
#define level_4     11.20

#define over        40

#define base_rate   0.15
#define add_rate    0.20
#define left_rate   0.25

void show_menu (void);
float deal(float level,int time);

int main (void)
{
    int num,hours;
    float base;

    show_menu();
    while (scanf("%d",&num))
    {
        switch (num)
        {
            case 1:
                base = level_1;
                printf("Please enter time your worked this week:\n");
                scanf("%d",&hours);
                printf("The total income is %.2f\n",deal(base,hours));
                show_menu();
                break;
            
            case 2:
                base = level_2;
                printf("Please enter time your worked this week:\n");
                scanf("%d",&hours);
                printf("The total income is %.2f\n",deal(base,hours));
                show_menu();
                break;
            
            case 3:
                base = level_3;
                printf("Please enter time your worked this week:\n");
                scanf("%d",&hours);
                printf("The total income is %.2f\n",deal(base,hours));
                show_menu();
                break;
                            
            case 4:
                base = level_4;
                printf("Please enter time your worked this week:\n");
                scanf("%d",&hours);
                printf("The total income is %.2f\n",deal(base,hours));
                show_menu();
                break;
            
            case 5:
                printf("Bye!\n");
                return 0;
                break;
        
            default:
                printf("Please enter true choose.\n");
                show_menu();
                break;
        }
    }
    


    return 0;
}

void show_menu (void)
{
    printf("*****************************************************************\n\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr                          2) $9.33/hr\n");
    printf("3) $10.00/hr                         4) $11.20/hr\n");
    printf("5) quit\n");
    printf("*****************************************************************\n");
}

float deal(float level,int time)
{
    float money,tex;

    if (time > 40)
        money = over * level + (time -40) * 1.5 * level;
    else
        money = time * level;

    if (money <= 300)
        money *= 1 - base_rate;
    else if(money > 300 && money < 450)
    {
        tex = 300 * base_rate + (money - 300) * add_rate;
        money -= tex;
    }
    else
    {
        tex = 300 * base_rate + 150 * add_rate + (money - 450) * left_rate;
        money -= tex;
    }
    
    return money;
}
//我这个好像不用改……

8.8

#include <stdio.h>
#include <ctype.h>

#define STOP    'q'

void show_menu(void);
char get_choice(void);
float get_firstnumber(void);
float get_secondnumber(char choice);

int main (void)
{
    char ch;
    float first,second;

    show_menu();
    ch = get_choice();

    while (ch != STOP)
    {
        switch (ch)
        {
            case 'a':
                first = get_firstnumber();
                second = get_secondnumber(ch);
                printf("%f + %f = %f\n",first,second,first+second);
                show_menu();
                break;
            
            case 's':
                first = get_firstnumber();
                second = get_secondnumber(ch);
                printf("%f - %f = %f\n",first,second,first-second);
                show_menu();
                break;
                   
            case 'm':
                first = get_firstnumber();
                second = get_secondnumber(ch);
                printf("%f * %f = %f\n",first,second,first*second);
                show_menu();
                break;
                    
            case 'd':
                first = get_firstnumber();
                second = get_secondnumber(ch);
                printf("%f / %f = %f\n",first,second,first/second);
                show_menu();
                break;
        }
        ch = get_choice();
    }
    
    printf("Bye.\n");

    return 0;
}

void show_menu(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");
}

char get_choice(void)
{
    char choice;

    while (scanf("%c",&choice) != 1 || !(choice == 'a' || choice == 's' || choice == 'm' || choice == 'd' || choice == 'q'))
    {
        printf("Please enter character a, s, m, d or q\n");
    }
    while (getchar() != '\n')
        continue;

    return choice;
}


float get_firstnumber(void)
{
    char ch;
    float first;

    printf("Enter first number:");
    while(scanf("%f",&first) != 1)
    {
        while ((ch = getchar()) >= 32)
        {
            putchar(ch);
        }
        printf(" is not an number.\n");
        printf("Please enter a number, such as 2.5, -1,78E8, or 3:");
    }
    while (getchar() != '\n')
        continue;

    return first;

}

float get_secondnumber(char choice)
{
    char ch;
    float second;

    printf("Enter second number:");
    while(scanf("%f",&second) != 1)
    {
        while ((ch = getchar()) >= 32)
        {
            putchar(ch);
        }
        printf(" is not an number.\n");
        printf("Please enter a number, such as 2.5, -1,78E8, or 3:");
    }
    if(choice == 'd' && second == 0)
    {
        printf("Enter a number other than 0:");
        scanf("%f",&second);
    }
    while (getchar() != '\n')
        continue;

    return second;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值