C++ 如何防止频率表计数'';空盒子'';在c++;

C++ 如何防止频率表计数'';空盒子'';在c++;,c++,C++,我写了下面的代码(用于家庭作业),计算字母和数字,并生成频率表。 我的问题是:当字母或数字不存在时,如何停止频率生成 在我编写的代码中,程序会计算输入到它的每个字母,但也会为ASCII代码中每个可能的字母/数字发布一行 我希望我的问题问得对,我感谢任何帮助或建议 #include <iostream> #include <string> using namespace std; int countingLetters(string someWords); int c

我写了下面的代码(用于家庭作业),计算字母和数字,并生成频率表。 我的问题是:当字母或数字不存在时,如何停止频率生成

在我编写的代码中,程序会计算输入到它的每个字母,但也会为ASCII代码中每个可能的字母/数字发布一行

我希望我的问题问得对,我感谢任何帮助或建议

#include <iostream>
#include <string>

using namespace std;

int countingLetters(string someWords);

int countingNumbers(string someWords);

int frequency(string someWords, double totalChar);

int main() {
    string someWords;
    cout << "Write a some words: " << endl;
    getline(cin, someWords);
    cout << "You wrote:" << someWords << '\n';

    cout << "Your sentence has " << someWords.length() << " characters." << '\n';

    double totalChar = someWords.length();

    cout << "Your sentence has " << countingLetters(someWords) << " letters." << '\n';

    cout << "Your sentence has " << countingNumbers(someWords) << " numbers." << '\n';

    cout << "Frequency of signs and letters :" << endl;

    frequency(someWords, totalChar);


    return 0;
}

int countingLetters(string someWords) {
    int count = 0;
    for (int i = 0; i < someWords.length(); i++) {
        if (someWords[i] >= 'a' && someWords[i] <= 'z')
            count++;
    }
    return count;
}

int countingNumbers(string someWords) {
    int count = 0;
    for (int i = 0; i < someWords.length(); i++) {
        if (isdigit(someWords[i]) != 0)
            count++;
    }
    return count;
}

int frequency(string someWords, double totalChar) {
    cout << " Letter" << '\t' << "Antal" << '\t' << "Procent" << endl;
    int frequency[255]={0};
    for (int i = 0; i < someWords.length(); i++) {
        char c = someWords[i];
        if (isdigit(c) != 0)
            frequency[c]++;
        if (isalpha(c) != 0)
            frequency[c]++;
    }
    for (int i = 0; i < sizeof(frequency); i++) {
        if(frequency[i]>0)
        cout << '\t' << static_cast<char>(i) << '\t' << frequency[i] << '\t' << frequency[i]/totalChar << endl;
    }

    return 0;
}
#包括
#包括
使用名称空间std;
int计数字母(字符串someWords);
int countingNumbers(字符串someWords);
int频率(字符串someWords,双totalChar);
int main(){
字串;
cout
sizeof(frequency)
是以字节为单位的
frequency
数组的大小。如果数组除以一个数组元素的字节大小,则需要以字节为单位的元素数:

这是
频率
数组的元素数:

sizeof(frequency) / sizeof(frequency[0])

但是,当你使用C++时,你不应该使用原始数组,但是<代码> STD::数组< /C> >:

#include <array>
...
int frequency(string someWords, int totalChar) {
  cout << " Letter" << '\t' << "Antal" << '\t' << "Procent" << endl;
  std::array<int, 255> frequency{0};

  for (int i = 0; i < someWords.length(); i++) {
    char c = someWords[i];
    if (isdigit(c) != 0)
      frequency[c]++;
    if (isalpha(c) != 0)
      frequency[c]++;
  }

  int x = frequency.max_size();

  for (int i = 0; i < frequency.max_size(); i++) {
    if (frequency[i]>0)
      cout << '\t' << static_cast<char>(i) << '\t' << frequency[i] << '\t' << frequency[i] / totalChar << endl;
  }

  return 0;
}
#包括
...
int频率(字符串someWords,int totalChar){

这里的问题不是很严重,但在使用整数类型时不要使用
double
sizeof(frequency)
是数组的大小(以字节为单位)。我不清楚“当字母或数字不存在时如何停止频率生成”是什么意思下面一段。在你的代码中,你在计算总数时考虑了所有字符,这会影响百分比。你是在问如何只计算字母数字字符,还是在发现不同字符时如何停止循环?在我的输出中,我得到了频率表,但也得到了有关所有字母和数字的信息我不在那里。我希望这能让事情变得更清楚。。。