0
//Loop to initialize the array of structs; set count to zero

for(int i = 0; i < 26; i++)
{
    //This segment sets the uppercase letters
    letterList[i].letter = static_cast<char>(65 + i);
    letterList[i].count = 0;

    //This segment sets the lowercase letters
    letterList[i + 26].letter = static_cast<char>(97 + i);
    letterList[i + 26].count = 0;
}

//これはうまくいかないようです!!!

プログラム全体は、テキストファイルを取得し、それを読み取り、使用された各文字、使用された回数、および出現率を出力します...ただし、私の出力は次のように出力され続けます。

文字数 出現率

0 0.00%

52回……

私はあちこちを検索しましたが、これを取得できないようです...

4

2 に答える 2

1

このコード出力に問題はありません

    letterType letterList[52];
    for(int i = 0; i < 26; i++)
    {
        //This segment sets the uppercase letters
        letterList[i].letter = static_cast<char>('A' + i);
        letterList[i].count = 0;

        //This segment sets the lowercase letters
        letterList[i + 26].letter = static_cast<char>('a' + i);
        letterList[i + 26].count = 0;
    }
    for (int i = 0; i < 26 * 2; i++)
        cout<<letterList[i].letter<<" "<<letterList[i].count<<endl;
于 2013-04-15T04:55:03.473 に答える
0
for(int i=0, c='a'; i<26; i++)
{
        letterList[i] = c++;
}
for(int i=26,c='a'; i<52; i++)
{
        letterList[i] = toupper(c++);
}

または、2 番目の for ループを次のように置き換えることもできます。

for(int i=26,c='A'; i<52; i++)
{
        letterList[i] = c++;
}

編集

あなたの要件に基づいて、メンバーがあり、各インスタンスが各文字を運ぶとstruct
仮定すると、これはコードです:structcharstruct

struct letterType
{
    char letter;
};

int main()
{
    struct letterType letterList[52];
    char c;
    for(int i=0, c='a'; i<26; i++)
    {
            letterList[i].letter = c++;
    }
    for(int i=26,c='a'; i<52; i++)
    {
            letterList[i].letter = toupper(c++);
    }
    for(int i=0; i<52; i++)
    {
            cout<< letterList[i].letter << endl;
    }
}
于 2013-04-15T04:51:01.747 に答える