0

'q' だけが入力されるまで、一度に 1 語ずつ入力を読み取るプログラムを作成しました。プログラムは、母音で始まる単語の数、子音で始まる単語の数、およびこれらのカテゴリのいずれにも当てはまらない数を報告します。

#include <iostream>
#include <cstdlib>

int main()
{
char ch;
bool cont = true; //for controlling the loop
bool space = false; //see if there is a space in the input
    int i = 0; //checking if the input is the first word
int consta, vowel, others;
consta = vowel = others = 0;
std::cout<<"Enter words (q to quit)\n";

while (cont && std::cin>>ch) //continue while cont is true and the input succeded
{
    if (i == 0) //check if this is the first word
    {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch== 'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;
        ++i; //add 1 to i so this if statement wont run again
    }


    if (space == true) //check if the last input was a space
    {
        if (!isspace(ch)) //check if the current input is not a space
        {
         if (ch != 'q') //and ch is not 'q'
         {
        if (isalpha(ch))
            if ((ch == 'a' ||ch == 'e' ||ch== 'i' ||ch== 'o' ||ch==   'u') || (ch == 'A' ||ch== 'E' ||ch== 'I' ||ch== 'O' ||ch== 'U'))
                ++vowel;
            else
                ++consta;
        else
            ++others;

        space = false;
        }

        }
        else
            cont = false;
    }
    if (isspace(ch)) //check if ch is a space
        space = true;
}

std::cout<<"\n"<<consta<<" words beginnig with constants\n";
std::cout<<vowel<<" words beginnig with vowels\n";
std::cout<<others<<" words beginning with others\n";

system("pause");
return 0;
}

しかし、スペースと「q」を入力しても終了しません。しかし、'^Z' を入力すると終了しますが、コンスタンタンは常に 1 で、その他は常に 0 です。

4

3 に答える 3

0

これがお役に立てば幸いです

#include<iostream>
#include<cstdlib>
#include<string>
int main()
{
    char ch[50];
    int consta, vowel, others;
    consta = vowel = others = 0;
    bool flag = false;
    std::cout<<"Enter words (q to quit)\n";
    while(1)
    {
        if(flag == true)
        {
            break;
        }
        gets(ch);
        char* pch;
        pch = strtok(ch," ");
        if(strcmp("q",pch)==0)
        {
            break;
        }
        while (pch != NULL)
        {
            if(strcmp("q",pch)==0)
            {
                flag = true;
                break;
            }
            if(isalpha(pch[0]))
            {
                if ((pch[0] == 'a' ||pch[0] == 'e' ||pch[0]== 'i' ||pch[0]== 'o' ||pch[0]== 'u') || (pch[0] == 'A' ||pch[0]== 'E' ||pch[0]== 'I' ||pch[0]== 'O' ||pch[0]== 'U'))
                    ++vowel;
                else
                    ++consta;
            }
            else
                ++others;
            pch = strtok (NULL, " ");
        }
    }
    std::cout<<"\n"<<consta<<" words beginnig with constants\n";
    std::cout<<vowel<<" words beginnig with vowels\n";
    std::cout<<others<<" words beginning with others\n";
    std::cin.ignore();
    return 0;
}

strtokについてはこちらを参照してください。これは、スペースで区切られているか、別の行にあるかに関係なく、すべての単語をカウントします。

于 2013-07-28T06:27:38.903 に答える
0

同じコード構造を使用すると仮定すると、while ループで使用します。

while (cont && std::cin>> std::noskipws >>ch)

std::noskipws : これにより、cin ストリームの動作が変更され、スペースが無視されなくなります。また、ストリームの最後にもスペースを追加します。エンドスペースに注意してください。幸いなことに、コードはスペースを処理するため、その影響はわかりません。

コード内のスペースを処理しているため、これは機能します。また、デフォルトでは、cin の動作はスペースを無視することです。

ただし、コードの複雑さを軽減できると思います。このステートメントは必要ないかもしれません

于 2013-07-28T06:40:55.733 に答える