-3

我正在尝试创建一个程序来计算用户给出的单词中元音和辅音的数量。我使用 strlen() 之类的函数来获取用户输入数组的长度以进行迭代。我还使用了 bits/stdc++.h,所以我可以为用户输入中出现的任何元音调用 count() 函数。

为了检查元音的出现,我尝试了几个函数,从 count 到 find_first_of,再到 find(),再到 count()。我的第一个错误首先是说它无法识别对 strlen() 的调用。我检查以确保我包含了正确的包以使用 strlen(),但这似乎不是问题。我在装有 High Sierra 的 MacBook Pro 上运行这个程序。

  #include <iostream>//std::cout
  #include <string>//std::string
  #include <cstdlib>
  #include <set>// std::size_t
  #include "/Users/Desktop/stdc++.h"

  using namespace std;

  int main(){
      string input = "hello";
      cout << " The input is " << input << endl;
      std::size_t vowelsFound = 0;

      /*cout << " Enter a word, and we will count the vowels and    consonants: ";
      cin >> input;
      cout << " You entered " << input << endl;*/

      char cons[] =    {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};

      char vows[] = {'a','e','i','o','u','y'};

      std::size_t n = strlen(input);
      std::size_t v = strlen(vows);

      for(int i = 0; i < v; i++){
          if(count(input,input + n,vows[i]) != 0){
              vowelsFound += count(input,input + n, vows[i]);//count of vowels in vows[] encountered in input
              cout << " The vowel(s) found is " << vowelsFound << endl;
    }
          else{
              cout << " The vowel " << vows[i] << " is not found in input " << endl;
    }
}

  cout << " The amount of vowels found is " << vowelsFound << endl;
  cout << " The expected amount of vowels found is 2 " << endl;
}

我硬编码了短语“hello”作为输入,所以当一切都说完后,元音计数应该是 2。

4

3 回答 3

1

您的代码中存在许多问题:

  1. 我不知道包含中的内容,"/Users/richardlopez/Desktop/stdc++.h"但包含它不太可能是一件好事。你可能#include <cstring>想得到strlen
  2. 您不能strlen在 a 上使用std::string,它仅适用于以空字符结尾的字符数组。你应该input.size()改用。
  3. 你也不应该使用strlenon vows,因为它是一个字符数组,所以编译它不是空终止的,所以返回值strlen是未定义的。您可以使用sizeof(vows)或只vows使用std::stringorstd::vector<char>来代替。
  4. count(input,input + n,vows[i])是不正确的。input + n不编译。你大概的意思是count(input.begin(),input.end(),vows[i])

纠正上述问题并使用现代 c++ 会产生简化的代码:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
  std::string input = "hello";
  std::cout << " The input is " << input << "\n";
  std::size_t vowelsFound = 0;

  std::string cons = "bcdfghjklmnpqrstvwxyz";
  std::string vows = "aeiou";

  for ( auto v : vows )
  {
      size_t c = std::count(input.begin(), input.end(), v);
      if ( c != 0 )
      {
          vowelsFound += c;
          std::cout << "found " << c << " " << v << "\n";
      }
      else
      {
          std::cout << " The vowel " << v << " is not found in input\n";              
      }
  }

  std::cout << " The amount of vowels found is " << vowelsFound << "\n";
  std::cout << " The expected amount of vowels found is 2\n";
}

如果你只需要你可以使用的元音总数:

std::cout << " The amount of vowels found is " << std::count_if( input.begin(), input.end(), [&](char c)
  {
    return vows.find(c) != std::string::npos;
  }) << "\n";
于 2019-01-04T08:13:07.123 回答
0

几个问题,在 "/Users/richardlopez/Desktop/stdc++.h" 之上。如果您需要特定功能,请查看参考并包含该功能的相应官方 C++ 标头。

首先strlen是 0-terminated char*。它不起作用input。为此,只需使用:

auto n = input.size();

然后同样的vows

std::vector<char> vows{'a','e','i','o','u','y'};
auto v = vows.size();

那么count也是不同的:

std::count(std::begin(input) ,std::end(input), vows[i])

如果您有 C++17,请执行以下操作:

if(auto count = std::count(std::begin(input), std::end(input), vows[i]); count > 0)
{
    vowelsFound += count;
    std::cout << count << std::endl;
}
于 2019-01-04T08:00:38.580 回答
0

您可以使用分区算法。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main() {

    std::string input = "hello";
    std::cout << " The input is " << input << std::endl;

    std::vector<char> vows = {'a','e','i','o','u','y'};
    auto it_vows_end = std::partition(input.begin(), input.end(), [vows](char c){return find(vows.begin(), vows.end(), c) != vows.end();});
    std::cout << " The amount of vowels found is " << std::distance(input.begin(), it_vows_end) << std::endl;
    std::cout << " The expected amount of vowels found is 2 " << std::endl;


    // If you also want to count consonants (for example if the input can also have digits or punctuation marks)
    std::vector<char> cons = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
    auto it_cons_end = std::partition(it_vows_end, input.end(), [cons](char c){return find(cons.begin(), cons.end(), c) != cons.end();});
    std::cout << " The amount of consonants found is " << std::distance(it_vows_end, it_cons_end) << std::endl;

}

输出:

输入是 hello
找到的元音数量是 2 找到
的预期元音数量是 2 找到
的辅音数量是 3


std::size_t 是 sizeof 的返回类型。所以使用它来迭代容器是很自然的。此外,由于它是某些库使用的标准类型,因此它使代码更易于阅读。

于 2019-01-04T08:24:05.993 回答