zoukankan      html  css  js  c++  java
  • 【C Primer Plus】编程练习第七章

    1、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        char ch;
        int space_1 = 0;
        int break_1 = 0;
        int other_1 = 0;
        printf("请输入字符:");
        while ((ch=getchar()) != '#')
        {
            if (ch == ' ')
                space_1++;
            else if (ch == '
    ')
                break_1++;
            else
                other_1++;
            
        }
        printf("空格有%d个,换行符有%d个,其他字符有%d个", space_1, break_1, other_1);
    
        getchar();
        getchar();
        return 0;
    }

    2、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        char ch = 0;
        int i = 0;
    
        printf("请输入字符:");
        while ((ch = getchar()) != '#')
        {
            if ((i % 8 == 0) && (i != 0))
            {
                putchar('
    ');
            }
            i++;
            if (ch != '
    ')
                printf("%c:%d ", ch, ch);
        }
        getchar();
        getchar();
        return 0;
    }

    3、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        int i = 0;
        int odd = 0;
        int even = 0;
        int sum_e = 0;
        int sum_o = 0;
    
        printf("请输入整数:");
        while (scanf("%d",&i))
        {
            if (i == 0) {
                break;
            }
            else if ((i % 2) == 0) {    
                even++;
                sum_e = i + sum_e;
            }
            else {
                odd++;
                sum_o = i + sum_o;
            }
        }
        printf("输入的偶数个数有%d,偶数平均值为%f,输入的奇数个数有%d,奇数平均值为%f", even, (float)(sum_e / even), odd, (float)(sum_o / even));
    
        getchar();
        getchar();
        return 0;
    }

     

    4、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        char ch = { 0 };
        printf("请输入字符:");
        while ((ch = getchar()) != '#')
        {
            if (ch == '.') {
                putchar('!');
            }
            else if (ch == '!') {
                putchar('!');
                putchar('!');
            }
            else
                putchar(ch);
        }
    
        getchar();
        getchar();
        return 0;
    }

     

    5、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        char ch = { 0 };
        printf("请输入字符:");
        while ((ch = getchar()) != '#')
        {
            switch (ch)
            {
            case '.':
                putchar('!');
                continue;
            case '!':
                putchar('!');
                putchar('!');
                continue;
    
            default:
                putchar(ch);
                continue;
            }
        }
    
        getchar();
        getchar();
        return 0;
    }

     

    6、

    #include <stdio.h>
    #include <string>
    
    int main()
    {
        int n = 0;
        char ch = 0;
        char ch_p = 0;
        printf("请输入字符:");
        while ((ch = getchar()) != '#')
        {
            if (ch == 'i') {
                if (ch_p == 'e') {
                    n++;
                }
            }
            ch_p = ch;
        }
        printf("ei出现了%d次", n);
        getchar();
        getchar();
        return 0;
    }

     

    7、

    #include <stdio.h>
    #include <string>
    #define BASIC 10
    #define PLUS 1.5
    #define TAX_1 0.15
    #define TAX_2 0.2
    #define TAX_3 0.25
    
    int main()
    {
        int t = 0;
        double wage, tax, income;
        printf("请输入一周工作小时数:");
        while (scanf("%d", &t)) {
            if (t <= 40) {
                wage = BASIC * t;
                if (wage <= 300) {
                    tax = wage * TAX_1;
                    income = wage - tax;
                }
                else {
                    tax = 300 * TAX_1 + (wage - 300) * TAX_2;
                    income = wage - tax;
                }
            }
            else {
                wage = 400 + (t - 40)*PLUS*BASIC;
                if (wage <= 450) {
                    tax = 300 * TAX_1 + (wage - 300)*TAX_2;
                    income = wage - tax;
                }
                else {
                    tax = 300 * TAX_1 + 150 * TAX_2 + (wage - 450)*TAX_3;
                    income = wage - tax;
                }
            }
            printf("你工作了%d小时,工资总额为%lf,税金为%lf,净收入为%lf
    ", t, wage, tax, income);
            printf("请输入一周工作小时数:");
        }
        
        getchar();
        getchar();
        return 0;
    }

     

    8、

    #include <stdio.h>
    #include <string>
    #define PLUS 1.5
    #define TAX_1 0.15
    #define TAX_2 0.2
    #define TAX_3 0.25
    
    int main()
    {
        int choice = 0;
        double basic = 0;
        int t = 0;
        double wage = 0;
        double tax = 0; 
        double income = 0;
        printf("******************************************************************
    ");
        printf("Enter the number corresponding to the desired pay rate or action:
    ");
        printf("1)$8.75/hr                              2)$9.33/hr
    ");
        printf("3)$10.00/hr                             4)$11.20/hr
    ");
        printf("5)quit
    ");
        printf("******************************************************************
    ");
        printf("请输入工资等级:");
        while (scanf("%d", &choice)&&(choice<=4)) {
            switch (choice)
            {
            case 1:
                basic = 8.75;
                break;
            case 2:
                basic = 9.33;
                break;
            case 3:
                basic = 10.00;
                break;
            case 4:
                basic = 11.20;
                break;
            case 5:
                break;
            default:
                printf("请输入数字1到5,谢谢!");
                break;
            }
            printf("请输入一周工作小时数:");
            scanf("%d", &t);
    
            if (t <= 40) {
                wage = basic * t;
                if (wage <= 300) {
                    tax = wage * TAX_1;
                    income = wage - tax;
                }
                else {
                    tax = 300 * TAX_1 + (wage - 300) * TAX_2;
                    income = wage - tax;
                }
            }
            else {
                wage = 400 + (t - 40)*PLUS*basic;
                if (wage <= 450) {
                    tax = 300 * TAX_1 + (wage - 300)*TAX_2;
                    income = wage - tax;
                }
                else {
                    tax = 300 * TAX_1 + 150 * TAX_2 + (wage - 450)*TAX_3;
                    income = wage - tax;
                }
            }
            printf("你工作了%d小时,工资总额为%lf,税金为%lf,净收入为%lf
    ", t, wage, tax, income);
            printf("请输入工资等级:");
        }
        
        getchar();
        getchar();
        return 0;
    }

     

    9、

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int isprimer(int n);
    int main() {
        int n;
        while (1)
        {
            printf("请输入一个数:
    ");
            scanf("%d", &n);
            if (isprimer(n))
                printf("%d
    ", n);
            else
            {
                printf("这不是一个素数!
    ");
            }
        }
        system("pause");
        return 0;
    }
    int isprimer(int n) {
        for (int i = 2; i < n; i++) {
            if (n%i == 0)
                return 0;
        }
        return 1;
    }

     

  • 相关阅读:
    装箱拆箱操作
    《设计原本》试读:什么是设计
    代码大全中英文要点
    JSON对象和字符串之间的相互转换
    《设计原本》试读:何为真实?设计的概念
    Facebook后台技术探秘 世界最大的PHP网站
    重构模式:用异步计算替代长计算
    生命不息编程不止 Facebook CEO还在写代码
    万能的js复制按钮
    《网站设计解构》试读:1.1 可重用策略
  • 原文地址:https://www.cnblogs.com/roscangjie/p/11811872.html
Copyright © 2011-2022 走看看