Created
November 19, 2020 03:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include <stdio.h> | |
int main() | |
{ | |
const static int ARRAY_SIZE = 10; //配列のサイズ(配列数)を定義 | |
int ndigit[ARRAY_SIZE]; //数字キーのカウントを格納 | |
//配列番号が数字キーに対応 | |
//例 | |
// ndigit[0] 0キー | |
// ndigit[1] 1キー | |
// ndigit[2] 2キー | |
// 以降同様 | |
//---------------------------------- | |
//ndigit配列のクリア | |
//---------------------------------- | |
for (int i = 0; i < ARRAY_SIZE; i++) { | |
ndigit[i] = 0; | |
} | |
//---------------------------------- | |
//EOFが入るまで各キーの数をカウント | |
//---------------------------------- | |
int nwhite = 0; //空白、改行、タブの数(どれかがきたらカウントアップ) | |
int nother = 0; //数字や空白、改行、タブ以外の数 | |
int c; | |
while((c = getchar()) != EOF) { | |
if (c >= '0' && c <= '9') { //数字キーか? | |
//数字キーが入った | |
++ndigit[c-'0']; //対応する配列をカウント | |
//0~9のASCIIコードは下記のように順に並んでいる | |
//従って入力されたキーのアスキーコートから0のASCIIコードを引くと | |
//ndigitの配列番号になる | |
//ASCIIコード(16進表記) | |
// 0 = 0x30 | |
// 1 = 0x31 | |
// 2 = 0x32 | |
// 3 = 0x33 | |
// 4 = 0x34 | |
// 5 = 0x35 | |
// 6 = 0x36 | |
// 7 = 0x35 | |
// 8 = 0x38 | |
// 9 = 0x39 | |
} else if (c == ' ' || c == '\n' || c == '\t') { //空白、改行、タブを検出 | |
//空白、改行、タブのどれかのキーが入った | |
++nwhite; | |
} else { | |
//上記以外のキー | |
++nother; | |
} | |
} | |
//---------------------------------- | |
//カウントした数を表示 | |
//---------------------------------- | |
//数字キーの数を表示 | |
printf("0~9のキー数は\n"); | |
for (int i = 0; i < ARRAY_SIZE; i++) { | |
printf("%d: %d個\n", i, ndigit[i]); | |
} | |
printf("空白数 %d\n", nwhite); //空白数を表示 | |
printf("その他 %d\n", nother); //その他の数を表示 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment