Skip to content

Latest commit

 

History

History
188 lines (161 loc) · 4.66 KB

编程题.md

File metadata and controls

188 lines (161 loc) · 4.66 KB

第七章 分支和跳转控制语句 编程练习题

  1. 编写程序,读取输入,读到#字符就停止然后报告读取的空格数、换行符数和所有其他字符的数量。
#include<stdio.h>
#define STOP '#' // 停止信号
#define SPACE ' ' // 声明空格

int main(void)
{
    char ch;
    int lines = 0; // 换行符数
    int spaces = 0; // 空格数
    int others = 0; // 其他字符的数量

    printf("请输入内容(输入#即可停止):\n");

    while((ch =getchar())!=STOP) // 注意点:换成scanf()函数读取,即不能结束
    {
        if(ch == '\n')
            lines++;
        else if(ch == SPACE)
            spaces++;
        else
            others++;
    } // while循环结束
    printf("空格数为:%d ;换行符数为: %d ;其他的字符数为: %d\n",spaces,lines,others);

    return 0;
}
  1. 编写一个程序读取输入,读到#字符就停止程序要打印每个输入的字符以及对应的ASCII码(十进制),一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。
#include<stdio.h>
#define STOP '#'

const int NUM = 8;

int main(void)
{
    char ch;
    int count = 0; // 统计数量
    printf("请输入要打印的字符(输入#就停止):");

    while((ch = getchar()) !=STOP) // 判断读取的字符
    {
        count++;
        putchar(ch); // 输出
        printf(" : %d ",ch); // 把字符转换成ASCII码形式
        if(count%8 == 0) // 不需要加花括号
            printf("\n");
    } // while循环结束
    printf("\n");
    printf("内容结束!拜拜!\n");

    return 0;
}
  1. 编写一个程序,读取整数直到用户输入0,输入结束后,程序应报告用户输入的偶数(不包括0)的个数,这些偶数的平均值、输入的奇数个数及其奇数的平均值。
#include<stdio.h>
int main(void)
{
    int num;
    int even_Count = 0; // 偶数的数量
    int odd_Count = 0; // 奇数的数量
    int Sum_even = 0; // 偶数的和
    int Sum_odd = 0; // 奇数的和

    printf("请输入数字,以空格作为间隔,以0作为结尾:");

    while ((scanf("%d",&num)) == 1)
    {
        if(num == 0)
            break; // break 直接结束循环
        else if(num % 2==0)
        {
            even_Count++;
            Sum_even += num;
        }  // 偶数的个数统计和计算偶数的总和
        else
        {
            odd_Count++;
            Sum_odd += num;
        } // 奇数的个数统计和计算偶数的总和
    } // while循环结束
    printf("偶数的个数为:%d ; 所有的偶数的平均数为: %d \n",even_Count,Sum_even/even_Count);
    printf("奇数的个数为:%d ; 所有的奇数的平均数为: %d \n",odd_Count,Sum_odd/odd_Count);

    return 0;
}
  1. 使用if else语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后输出进行了多少次替换。
#include<stdio.h>
#define STOP '#'
#define FULL_STOP '.'

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

    printf("请输入一句话,包含句号和感叹号(输入#就停止):");

    while((ch = getchar()) !=STOP)
    {
        if(ch == FULL_STOP)
        {
            putchar('!');
            count++;
        }
        else if(ch =='!')
        {
            putchar('!!');
            count++;      
        }
        else
            putchar(ch);
    }
    printf("一共转换了 %d 次\n",count);

    return 0;
}
  1. 使用switch重写练习4
#include<stdio.h>

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

    printf("请输入一句话,包含句号和感叹号(输入#就停止):\n");

    while((ch = getchar())!='#')
    {
        switch(ch)
        {
            case '.':
                putchar('!');
                count++;
                break;
            case '!':
                putchar('!!');
                count++;
                break;
            default:
                putchar(ch);
        }
    }
    printf("一共转换了 %d 次\n",count);

    return 0;
}
  1. 编写程序读取输入,读到#停止,输出ei出现的次数。

可以使用 “Receive your eieio award” 来进行测试

#include<stdio.h>
int main(void)
{
    char ch;
    int count = 0;
    char first_char;

    printf("请输入一些内容(输入#停止):\n");

    while((ch = getchar()) !='#')
    {
        if(ch=='e'){
            first_char = ch; // 先让程序记住一个
        }
        if(ch=='i'&&first_char=='e') 
        {
            count++;
        }
    }
    printf("内容中ei出现的次数是:%d \n",count);
    return 0;
}