C语言第一章导言中的06部分,你能详细解释一下吗?

2026-04-11 21:360阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计296个文字,预计阅读时间需要2分钟。

C语言第一章导言中的06部分,你能详细解释一下吗?

第一章 导言-06 1.6 数组// 所有的输入字符可以分为12类,因为这样可以利用一个数组来存储各个数字字符出现的次数,这样比使用10个独立的变量更方便。

第1章 导言 -06

1.6 数组

// 所有的输入字符可以分成12类,因此可以用一个数组存放各个数字出现的次数,这样比使用10个独立的变量更方便。 # include <stdio.h> /* 统计各个数字,空白符及其他字符出现的次数 */ int main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (int i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if(c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits = "); for (int i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } // 将变量 ndigit 声明为由10个整型数构成的数组。

阅读全文

本文共计296个文字,预计阅读时间需要2分钟。

C语言第一章导言中的06部分,你能详细解释一下吗?

第一章 导言-06 1.6 数组// 所有的输入字符可以分为12类,因为这样可以利用一个数组来存储各个数字字符出现的次数,这样比使用10个独立的变量更方便。

第1章 导言 -06

1.6 数组

// 所有的输入字符可以分成12类,因此可以用一个数组存放各个数字出现的次数,这样比使用10个独立的变量更方便。 # include <stdio.h> /* 统计各个数字,空白符及其他字符出现的次数 */ int main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (int i = 0; i < 10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if(c >= '0' && c <= '9') ++ndigit[c-'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits = "); for (int i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } // 将变量 ndigit 声明为由10个整型数构成的数组。

阅读全文