C primer plus编程练习-第7章

本文提供了一系列C语言编程练习,涉及字符输入处理、ASCII码显示、整数统计、字符串替换、if-else与switch语句、素数计算、税务计算、订单总价计算等实际问题。通过这些练习,旨在提升C语言编程技能和逻辑思维能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.编写一个程序读取输入, 读到#字符停止, 然后报告读取的空格数、换行符数和所有其他字符的数量

#include<stdio.h>
int main(void)
{
   
        char ch;
        int space, linebreak, other;
        space = linebreak = other = 0;
        printf("Enter something, # to quit\n");
        while ((ch = getchar()) != '#')
        {
   
                if (ch == ' ')
                        space++;
                else if (ch == '\n')
                        linebreak++;
                else
                        other++;
        }
        printf("There are %d blank %s, %d line %s and %d other %s.\n",
                        space, (space>1? "spaces":"space"), linebreak, (linebreak>1? "linebreaks":"linebreak"),
                                other, (other>1? "characters":"character"));
        return 0;
}

2.编写一个程序读取输入, 读到#字符停止。 程序要打印每个输入的字符以及对应的ASCII码(十进制) 。 一行打印8个字符。 建议:使用字符计数和求模运算符(%) 在每8个循环周期时打印一个换行符。

#include<stdio.h>
int main(void)
{
   
        char ch;
        int sum = 0;
        printf("Enter some characters, # to quit\n");
        while ((ch = getchar()) != '#')
        {
   
                 sum++;
                 printf("%c-%d\t", ch, ch);
                 if (sum % 8 == 0)
                         printf("\n");
        }
        return 0;
}

3.编写一个程序, 读取整数直到用户输入 0。 输入结束后, 程序应报告用户输入的偶数(不包括 0) 个数、 这些偶数的平均值、 输入的奇数个数及其奇数的平均值。

#include<stdio.h>
int main(void)
{
   
        int num;
        int even, odd;
        float even_sum, odd_sum;
        even = odd= 0;
        even_sum = odd_sum = 0.0;
        printf("Enter some integers, 0 to quit\n");
        while ((scanf("%d", &num) == 1) && (num != 0))
        {
   
                if (num % 2)
                {
   
                        odd++;
                        odd_sum += num;
                }
                else
                {
   
                        even++;
                        even_sum += num;
                }
        }
        printf("even numbers:%d, average:%g, odd numbers:%d, average:%g\n", even, even_sum/even, odd, odd_sum/odd);
        return 0;
}

4.使用if else语句编写一个程序读取输入, 读到#停止。 用感叹号替换句号, 用两个感叹号替换原来的感叹号, 最后报告进行了多少次替换。

#include<stdio.h>
int main(void)
{
   
        char ch;
        int sub = 0;
        printf("Enter some sentences, # to quit\n");
        while ((ch = getchar()) != '#')
        {
   
                if (ch == '.')
                {
   
                        putchar('!');
                        sub++;
                }
                else if (ch == '!')
                {
   
                        putchar('!');
                        putchar('!');
                        sub++;
                }
                else
                        putchar(ch);
        }
        printf("\nsubstitution: %d\n", sub);
        return 0;
}

5.使用switch重写练习4

#include<stdio.h>
int main(void)
{
   
        char ch;
        int sub = 0;
        printf("Enter some sentences, # to quit\n");
        while ((ch = getchar()) != '#')
        {
   
                switch (ch)
                {
   
                        case '.':
                                putchar('!');
                                sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值