c primer plus 第七章 编程练习

//practice 7_1 ;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{

    char parse;
    int space, enter, other;
    space = enter = other = 0;  //空格、回车、其他字符

    printf("please input parse:\n");
    while((parse = getchar()) != '#')  //用getchar 比 scanf 要好,用scanf还需要在程序里进行判断
    {

        if (' ' == parse)
            space++;
        else if ('\n' == parse)
            enter++;
        else
            other++;
    }
    printf("This parse has %d space, %d enter, %d other char", space, enter, other);


    return 0;
}

/**************************************/

//practice 7_2 ;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{
    char ch;
    int count = 1;

    printf("Plese input text:\n");
    while((ch = getchar()) != '#')
    {
        printf("%c:%d  ", ch, ch);
        if(count % 8 == 0)       
            printf("\n");
        count++;
    }

    return 0;
}

/**************************************/
//practice 7_3 ;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{
    int even, odd;
    int sum_even, sum_odd;
    int number;
    even = odd = 0;
    sum_even = sum_odd = 0;


    printf("please input integer:\n");
    while(scanf("%d", &number))
    {
        if (0 == number )
            break;
        else if (0 == number % 2)
            {
                even++;
                sum_even += even;
            }
        else
            {
                odd++;
                sum_odd += odd;
            }
    }

    printf("%d %d",sum_even, sum_odd);
    printf("the average value of %d even is: %f; the average value of %d odd is: %f\n", even, (float)sum_even/even, odd, (float)sum_odd/odd);

    return 0;
}

/**************************************/
//practice 7_4;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{
    char ch;

    printf("please input text:\n");
    while((ch = getchar()) != '#')
    {
        if ('.' == ch)
        {
            putchar('!');
        }
        else if ('!' == ch)
        {
            putchar('!');
            putchar('!');
        }
        else
        {
            putchar(ch);
        }

    }
    return 0;
}

/**************************************/
//practice 7_5;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{
    char ch;

    printf("please input text:\n");
    while((ch = getchar()) != '#')
    {
        switch(ch)
        {
        case '!':
            putchar('!');
            putchar('!');
            break;
        case '.':
            putchar('!');
            break;
        default:
            putchar(ch);
            break;
        }
    }
    return 0;
}

/**************************************/
//practice 7_6;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>

int main(void)
{

    char ch, pre_ch;
    int count = 0;
    pre_ch = -1;

    printf("please input text:\n");
    while((ch = getchar()) != '#')
    {
        if ('i' == ch)
        {
            if ('e' == pre_ch)
                count++;
        }
        pre_ch = ch;
    }
    printf("this text has %d ei\n", count);

    return 0;
}

/**************************************/
//practice 7_7;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define BASIC_SALARY 10.00
#define OVER_WORK 15.00
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25

float acc_rate(float salary)
{
    /********************************************//**
         * \brief  计算税率
     *
     * \param  收入(浮点数)
     * \return 应交税费(浮点数)
     ***********************************************/

    float rate_money = 0;
    if (salary <= 300)
        rate_money = salary * RATE1;
    else if (salary <= 450)
        rate_money = 300 * RATE1 + (salary - 300) * RATE2;
    else
        rate_money = 300 * RATE1 + 150 * RATE2 + (salary - 450) * RATE3;

    return rate_money;
}

int main(void)
{
    float hours;
    float salary, rates, income;
    salary = rates = income = 0;

    printf("please input hours:_______\b\b\b\b");
    scanf("%f", &hours);
    if (hours <= 0)
        printf("you input hours is error\n");
    else if (hours <= 40)
    {
        salary = BASIC_SALARY * hours;
        rates = acc_rate(salary);
    }
    else
    {
        salary = BASIC_SALARY * 40 + OVER_WORK * (hours - 40);
        rates = acc_rate(salary);
    }

    printf("Salary:%f  Rates:%f  Income:%f", salary, rates, salary - rates);
}

/**************************************/
//practice 7_8;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25

float acc_rate(float salary)
{
    /********************************************//**
         * \brief  计算税率
     *
     * \param  收入(浮点数)
     * \return 应交税费(浮点数)
     ***********************************************/

    float rate_money = 0;
    if (salary <= 300)
        rate_money = salary * RATE1;
    else if (salary <= 450)
        rate_money = 300 * RATE1 + (salary - 300) * RATE2;
    else
        rate_money = 300 * RATE1 + 150 * RATE2 + (salary - 450) * RATE3;

    return rate_money;
}

int main(void)
{
    float BASIC_SALARY = 10.00;
    float OVER_WORK = 15.00;
    float hours;
    float salary, rates, income;
    int chage;
    salary = rates = income = 0;
    printf("****************************************************************\n");
    printf("ENter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr\t\t 2) $9.33/hr\t\t\n3) $10.00/hr\t\t 4) $11.20/hr\t\t\n");
    printf("****************************************************************\n");
    scanf("%d", &chage);
    switch(chage)
    {
        case 1:BASIC_SALARY = 8.75;break;
        case 2:BASIC_SALARY = 9.33;break;
        case 3:BASIC_SALARY = 10.00;break;
        case 4:BASIC_SALARY = 11.20;break;
    }


    printf("please input hours:_______\b\b\b\b");
    scanf("%f", &hours);
    if (hours <= 0)
        printf("you input hours is error\n");
    else if (hours <= 40)
    {
        salary = BASIC_SALARY * hours;
        rates = acc_rate(salary);
    }
    else
    {
        salary = BASIC_SALARY * 40 + OVER_WORK * (hours - 40);
        rates = acc_rate(salary);
    }

    printf("Salary:%f  Rates:%f  Income:%f", salary, rates, salary - rates);
    
    return 0;
   }

/**************************************/
//practice 7_9;
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>

int main(void)
{
    int input = 0;

    printf("please input the upper number:");
    scanf("%d", &input);

    int i = 0;
    int j = 0;

    for (i = 2; i <= input; i++)
    {
        for (j = 2; j < sqrt((double)i); j++)
        {
            if (i % j == 0)
            {
                break;
            }
        }
        if (j > sqrt(i))
        {
            printf("%d ", i);
        }
    }
}

/**************************************/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

St_up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值