【问题标题】:c++ program to check the frequencies of the lettersc ++程序检查字母的频率
【发布时间】:2016-07-24 00:20:03
【问题描述】:

我正在编写一个 C++ 程序来检查字母的频率。当输出没有像“aabbbsssd”这样的空格时,我的代码可以准确地检查字母的频率。但是,如果输入有空格,例如:“do be do bo”。我的输出只是 “d 1”和“o 1”,没有“b”和“e”的频率。

更新代码:

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

using namespace std;

struct Letters
{
  Letters() : freq(0){}
  Letters(char letter,int freq) {
    this->freq = freq;
    this->letter = letter;
}
char letter;
int freq;
};

bool Greater(const Letters& a, const Letters& b)
{
   if(a.freq == b.freq)
      return a.letter < b.letter;

   return a.freq > b.freq;
}

int main () {

cout<<"Enter text:" << endl;
string input;
getline(cin, input);

vector<Letters> count;
int letters[26]= {0};

for (int x = 0; x < input.length(); x++) {
    if (isalpha(input[x])) {
        int c = tolower(input[x] - 'a');
        letters[c]++;
    }
}

for (int x = 0; x < 26; x++) {
    if (letters[x] > 0) {
        char c = x + 'a';
        count.push_back(Letters(c, letters[x]));
    }
}

std::sort(count.begin(),count.end(),Greater);

cout<<"Frequencies:" << endl;
for (int x = 0 ; x < count.size(); x++) {
    cout<<count[x].letter <<  " " << count[x].freq<<"\n";
  }


return 0;
}

如果写得不好,请见谅,提前感谢您的帮助!

【问题讨论】:

  • 您在使用调试器检查代码时发现了什么?
  • 以前的if 声明if(a &gt;'a' &amp;&amp; a&lt;='z') 似乎没有害处,但似乎没有用。
  • 不仅如此,a - 'a' 还假定字母是连续的(即在'a''z' 之间没有其他字符)。这适用于某些字符集(例如 ASCII),但不适用于其他字符集。
  • 确定没有大写输入吗?

标签: c++


【解决方案1】:

cin &gt;&gt; text; 将只读取到第一个非空白字符之后的第一个空白字符。

您可以使用std::getline() 来读取整行,如下所示:

getline(cin, text);

【讨论】:

  • 谢谢!我已经修好了。当我提交它进行测试时,我遇到了一些错误,例如图像显示 {i.stack.imgur.com/ioV3j.png}。我不知道如何解决它。你能给我一些想法吗?
  • 规格是什么?不应该计算大写字母吗?字母和数字之间不应该有空格吗?输出不是排序的吗?要修复它,请修改您的代码以匹配规范。
【解决方案2】:

更新

假设测试你的程序的人也输入大写文本,你的程序应该是这样的:

#include <iostream>
#include <iomanip>

using namespace std;

int alpha[26] = {0};

int main(void)
{
    string text;

    cout << "Enter text:" << endl;
    getline(cin, text);

    for (int i = 0; i < text.length(); i++)
    {
        int a = text[i];

        if (a >= 'A' && a <= 'Z')
        {
            alpha[a - 'A']++;
        }
        else if (a >= 'a' && a <= 'z')
        {
            alpha[a - 'a']++;
        }
    }

    cout << "Frequencies:" << endl;

    for (int i = 0; i < 26; i++)
    {
        if (alpha[i])
        {
            cout << right << char('a' + i) << setw(2) << right << alpha[i] << endl;
        }
    }
    return 0;
}

【讨论】:

  • 当我提交测试时,它显示了这种错误{submit.cs.ucsb.edu/submission/203516}。是setw的问题吗?
  • 您只是在阅读“do be do be”的第一个“do”。这就是整个问题。阅读整行,然后开始数数。 Setw 不是你的问题。如果您已经调试过或者只是显示了您刚刚从 cin 读取的字符串,您可能已经发现了这一点。
  • i.stack.imgur.com/ioV3j.png,我把它做成图像。也许你可以看看它。
  • 这看起来像是一条评论。您可以在某处发布您更新的代码吗?
  • 但是当我提交测试时,它有一些错误,就像我给你看的图片一样。
【解决方案3】:

好吧,cin 只会读入直到空格分隔符并读入整行,你需要使用 getline(cin, v);其中 v 是您想要将输入放入其中的变量。

【讨论】:

    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多