天天看點

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);
        }
    }
}

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

繼續閱讀