C Language Logo
C语言二级在线二级在线
首页选择题程序题成为会员帮助其他科目

统计和数量

请修改编辑器中的代码,完成以下功能:实现函数 fun,统计一个 14×14 的字符矩阵中字符 'A' 和字符 'C' 各出现了多少次,分别通过指针 *a 和 *c 传出。 主函数先随机生成一个 14×14 的字母矩阵并打印出来,调用 fun 统计后输出结果;随后已经写好的 NONO 函数从 in.txt 读入一个固定的字符矩阵,再次调用 fun 统计,并把结果按 A=? 和 C=? 两行写入 out.txt。程序不需要输入。 注意屏幕上那个矩阵是随机生成的,程序没有调用srand,相同实现下重新运行通常会重复同一伪随机序列;不同实现的结果可能不同,评测看的是 out.txt 里对 in.txt 的统计结果。

#include  <stdio.h>
#include  <stdlib.h>
#define  M  14

void NONO();

void fun(char (*t)[M], int *a, int *c)
{
    // 请实现统计二维数组中字符'A'和'C'的个数
}

void get(char (*s)[M])
{
    int i, j;
    for(i=0; i<M; i++)
    {
        for(j=0; j<M; j++)
        {
            s[i][j] = 65 + rand() % 12;
            printf("%c ", s[i][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    char a[M][M];
    int x, y;
    get(a);
    fun(a, &x, &y);
    printf("A = %d   C = %d\n", x, y);
    NONO();
    return 0;
}

void NONO()
{
    FILE *fp, *wf;
    int i, j, x, y;
    char a[M][M];

    fp = fopen("in.txt", "r");
    wf = fopen("out.txt", "w");
    for(i=0; i<M; i++)
    {
        for(j=0; j<M; j++)
        {
            fscanf(fp, "%c ", &a[i][j]);
        }
    }
    fun(a, &x, &y);
    fprintf(wf, "A=%d\n", x);
    fprintf(wf, "C=%d\n", y);
    fclose(fp);
    fclose(wf);
}

控制台

调试终端

准备就绪