C Primer Plus第七章课后答案

其他章节答案

第七章课后答案
/*Project 1*/
#include 
int main(void)
{
    int space = 0;
    int low = 0;
    int chars = 0;
    char ch;
    printf("Please enter:");
    while((ch = getchar()) != '#')
    {
        if (ch == ' ')
            space++;
        else if (ch == '\n')
            low++;
        else
            chars++;
    }
    printf("space:%d\nlow:%d\nchars:%d\n",space,low,chars);
    return 0;
 } 
/*Project 2*/
#include 
int main(void)
{
    int n = 0;
    char ch;
    printf("Please enter:");
    while((ch = getchar()) != '#')
    {
        n++;
        if(n == 9)
        {
            printf("\n");
            n = 0;
        }
        else
            printf("%c%d ",ch,ch);
    }
    return 0;
 } 
/*Project 3*/
#include
int main(void) 
{
    int num,n1,n2,sum1,sum2;
    printf("请输入整数:");
    scanf("%d",&num);
    while(num != 0)
    {
        if(num % 2 == 0)
        {
            n1++;
            sum1 = sum1 + num;
        }
        else
        {
            n2++;
            sum2 = sum2 + num;
        }
        printf("请输入整数(输入0结束):");
        scanf("%d",&num);
     } 
     printf("偶数个数:%d.\n",n1);
     printf("偶数平均值:%d.\n",sum1 / n1);
     printf("奇数个数:%d.\n",n2);
     printf("奇数平均值:%d.\n",sum2 / n2);
     return 0;
}
/*Project 4*/
#include
int main(void) 
{
    char ch;
    int n = 0;
    printf("请输入:");
    while((ch = getchar()) != '#')
    {
        if(ch == '.')
        {
            ch = '!';
            n++;
        }
        else if(ch == '!')
        {
            ch = '!';
            n++;
            printf("%c",ch);
        }
        printf("%c",ch);
    }
    printf("\n共进行了%d次替换。\n",n);
     return 0;
}
/*Project 5*/
#include
int main(void) 
{
    char ch;
    int n = 0;
    printf("请输入:");
    while((ch = getchar()) != '#')
    {
        switch(ch)
        {
            case '.':
                ch = '!';
                n++;
                break;
            case '!':
                ch = '!';
                n++;
                printf("%c",ch);
                break;
            default:
                break;
        }
        printf("%c",ch);
    }
    printf("\n共进行了%d次替换。\n",n);
     return 0;
}
/*Project 6*/
#include
int main(void) 
{
    char ch;
    int n = 0;
    printf("请输入:");
    while((ch = getchar()) != '#')
    {
        if(ch == 'e')
        {
            if((ch = getchar()) == 'i')
                n++;
        }
    }
    printf("\nei共出现了%d次。\n",n);
     return 0;
}
/*Project 7*/
#include
#define RATE 1000
#define OVERTIME 1.5
#define TAX11 300
#define TAX12 0.15
#define TAX21 150
#define TAX22 0.2
#define TAX3 0.25
#define TIME 40
int main(void)
{
    float time,pay,tax;
    printf("请输入一周工作小时数:");
    scanf("%f",&time);
    while(time < 0 || time > 168)
    {
        printf("请输入正确的时间!\n");
        scanf("%f",&time);
    }
    if(time <= TIME)
        {
            pay = time * RATE;
            if(pay <= TAX11)
                tax = pay * TAX12;
            else if(pay <= (TAX11 + TAX21) && pay > TAX11)
                tax = TAX11 * TAX12 + (pay - TAX11) * TAX22;
            else if(pay > (TAX11 + TAX21))
                tax = TAX11 * TAX12 + TAX21 * TAX22 + (pay - TAX11 - TAX21) * TAX3;
        } 
        else if(time > TIME)
        {
            pay = RATE * OVERTIME * time;
            tax = TAX11 * TAX12 + TAX21 * TAX22 + (RATE * TIME - TAX11 - TAX21) * TAX3;
        }
        printf("工资总额:%.2f.\n税金:%.2f.\n净收入:%.2f.\n",pay,tax,pay - tax);  
    return 0;
}

你可能感兴趣的:(C Primer Plus第七章课后答案)