c prime plus第八章

编程练习

1.

#include <stdio.h>
int main(void)
{
    int count = 0;

    while(getchar() != '\n')
    {
        count++;
    }
    printf("There are %d chars.\n", count);
    return 0;
}

2.

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

    for(n = 0; (ch = getchar()) != EOF; n++)
    {
        if(ch >= " " || ch == "\n" || ch == "\t")
            printf("%-5c", ch);
        else 
            printf("%-4c", ch + 64);
        printf("%-5d", ch);
        if(n % 10 == 0)
            printf("\n");
    }
    return 0;
}

3.

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

int main(void)
{
    int u = 0, l = 0;
    char ch;

    while((ch = getchar()) != '\n')
    {
        if(isupper(ch))
            l++;
        if(islower(ch))
            u++;
    }
    printf("There are %d uppers and %d lowers.\n", u, l);
    return 0;
}

4.

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
    int word = 0, sum = 0;
    char ch;
    bool inword = false;

    while((ch = getchar()) != '\n')
    {
        if(!ispunct(ch) && !isspace(ch))
            sum++;
        if(!isspace(ch) && !inword)
        {
            inword = true;
            word++;
        }
        if(isspace(ch))
            inword = false;
    }
    printf("%d\n", word);
    printf("%d\n", sum);
    printf("The average of per words is %d.\n", sum / word);
    return 0;
}

使用inword主要是用来统计单词数量。

5.

#include <stdio.h>
int main(void)
{
    int guess, max = 100, min = 1;
    char respond;

    printf("Pick an integer from 1 to 100. I will try to guess ");
    printf("it.\nRespond with a g if my guess is great and with ");
    printf("\nan l if it is little.\n");
    printf("If my guess is right, respond y.\n");
    printf("If my guess is wrong, respond n.\n");
    printf("Is it your number is %d.\n", guess = (max + min) / 2);

    while((respond = getchar()) != 'y')
    {
        if(respond == 'g')
        {
            max = guess - 1;
            printf("Well, is it %d.\n", guess = (max + min) / 2);
        }
        else if(respond == 'l')
        {
            min = guess + 1;
            printf("Well, is it %d.\n", guess = (max + min) /2); 
        }
        else
            printf("Sorry, I can't understand it.\n");
            while(getchar() != '\n');
    }
    printf("I did it.\n");
    return 0;

}

6.

char get_first(void)
{
    int ch;

    ch = getchar();
    while(isspace(ch = getchar()));
    while(getchar() != '\n');
    return ch;
}

利用isspace确定。

7.

#include <stdio.h>
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
char get_first(void);
int main(void)
{ 
    float pay, hours, tax, HOURPER;

    while(1)
    {
        printf("Enter the number corresponding to the desired pay rate or action:\n");
        printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
        printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
        printf("5) quit\n");
        switch(get_first())
        {
            case 1: HOURPER = 8.75;
                    break;
            case 2: HOURPER = 9.33;
                    break;
            case 3: HOURPER = 10.00;
                    break;
            case 4: HOURPER = 11.20;
                    break;
            case 5: printf("quit.\n");
                    return 0;
        }
        printf("Pleas enter the hours:\n");
        scanf("%f", &hours);
        if(hours > 40.0 )
        {
            hours = 40 + (hours - 40) *1.5;
            pay = hours * HOURPER;
            printf("Pay is %f.\n", pay);
        }
        else
                pay = hours * HOURPER;
        if(pay <= 300)
            tax = pay * TAX1;
        else if(pay <= 450)
            tax = 300 * TAX1 + (pay - 300) * TAX2;
        else
            tax = 300 * TAX1 + 150 * TAX2 + (pay - 450) * TAX3; 
        printf("pay is %f,tax is %f, income is %f\n", pay, tax, pay - tax);
        return 0;
    }
}

char get_first(void)
{
    char ch;
    while(isspace(ch = getchar()));
    while(getchar() != '\n');
    return ch;
}

其实就是把get_int函数改成get_first函数。。。。。。。。。

8.

#include <stdio.h>
#include <ctype.h>
float get_float(void);
char get_char(void);

int main(void)
{
    float first_number, second_number;
    char ch;
    while(1)
    {
        printf("Enter the operation of your choice:\n");
        printf("a. add         s. subtract\n");
        printf("m. multiply    d. divide\n");
        printf("q. quit\n");
        ch = get_char();
        if(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')
        {
            printf("Bye.\n");
            break;
        }
        printf("Enter first number:");
        first_number = get_float();
        printf("Enter second number:");
        second_number = get_float();
        while(ch == 'd' && second_number)
        {
            printf("Enter another nunmber:");
            second_number = get_float();
        }
        switch(ch)
        {
            case 'a':
                    printf("%.2f + %.2f = %.2f\n", first_number, second_number, first_number + second_number);
                    break;
            case 's':
                    printf("%.2f - %.2f = %.2f\n", first_number, second_number, first_number - second_number);
                    break;
            case 'm':
                    printf("%.2f * %.2f = %.2f\n", first_number, second_number, first_number * second_number);
                    break;
            case 'd':
                    printf("%.2f / %.2f = %.2f\n", first_number, second_number, first_number / second_number);
                    break;
            default:
                    break;
        }
    }
    return 0;
}

float get_float(void)
{
    float num;
    char str[40];
    while(scanf("%f", &num) != 1)
    {
        gets(str);
        printf("%s is not a number.\n", str);
        printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
    }
    while(getchar() != '\n')
    return num;
}

char get_char(void)
{
    int ch;

    while(isspace(ch = getchar()));
    while(getchar() != '\n');
    return ch;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值