Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 产生时髦输出的程序_C++ - Fatal编程技术网

C++ 产生时髦输出的程序

C++ 产生时髦输出的程序,c++,C++,我正在编写一个程序,它从std::cin接收一个输入流,然后用输入中的所有单词(删除所有标点符号并使其小写)以及它们的频率填充一个映射容器 这是我的密码 #include "prog4.h" void clean_entry(const string& s1, string& s2) { for (int i = 0; i < s2.size(); i++) {//loop through the string s2[i] = tolower(s

我正在编写一个程序,它从std::cin接收一个输入流,然后用输入中的所有单词(删除所有标点符号并使其小写)以及它们的频率填充一个映射容器

这是我的密码

#include "prog4.h"


void clean_entry(const string& s1, string& s2) {
    for (int i = 0; i < s2.size(); i++) {//loop through the string
        s2[i] = tolower(s2[i]);
    }
}

void get_words(map < string, int >& map1) {
    string input;
    getline(cin, input);
    string s1;
    for (int i = 0; i < input.size(); i++ , s1 = "") {//loop through 
 entire input
        if (isalnum(input[i]) == 0) {//if its a alphanumeric char
            for (int d = i; isalnum(input[d]) == 0;d++) {//make s1 the 
next set of characters between punctuation
                s1 += input[d];

                if (isalnum(input[d]) != 0)//update i to the next non 
alfanumeric character position
                    i = d;
            }
         }
        clean_entry(s1, s1);
        map1[s1]++;
    }

}

void print_words(const map < string, int >& m1) {
    map<string, int>::const_iterator it;
    cout << "Number of non-empty words: " << m1.size() << '\n';
    int count = 0;
    for (it = m1.begin(); it != m1.end(); it++) {
        if (it->second == 1)
            count++;
    }
    cout << "Number of distinct words: " << count << '\n';
    it = m1.begin();
    for (int y = 0; it != m1.end(); it++,y++) {
        if (y % 3 == 0) {
            cout << '\n';
        }
        cout << setw(20) << it->first << setw(10) << it->second;
     }
}

int main() {
    map <string, int> m1;
    get_words(m1);
    print_words(m1);

    return 0;
}

我不确定是什么原因导致了这种情况,在查看代码后,我似乎找不到问题所在,这就是我在这里发布的原因。

让我列出一些我可以在代码中观察到的问题

  • isalnum
    函数返回字母数字字符的非零值。 这意味着声明 函数
    get_words
    中的if(isalnum(输入[i])==0){应更改为
    if(isalnum(输入[i])!=0){
  • 另外,(intd=i;isalnum(input[d])==0;d++{的语句
    应该更改为(intd=i;isalnum(input[d])!=0;d++{

  • 另一个问题是语句
    i=d;
    将不会执行,因为当
    for
    循环中断时,它将跳过这些行,因为条件正好相反。您可以通过将
    d
    的声明移出
    for
    循环来解决此问题。修改后的函数应为:
  • =>

    void get_单词(map&map1)
    {
    字符串输入;
    getline(cin,输入);
    字符串s1;
    对于(int i=0;i
    让我列出一些我可以在您的代码中观察到的问题

  • isalnum
    函数返回字母数字字符的非零值。 这意味着声明 函数
    get_words
    中的if(isalnum(输入[i])==0){应更改为
    if(isalnum(输入[i])!=0){
  • 另外,(intd=i;isalnum(input[d])==0;d++{的语句
    应该更改为(intd=i;isalnum(input[d])!=0;d++{

  • 另一个问题是语句
    i=d;
    将不会执行,因为当
    for
    循环中断时,它将跳过这些行,因为条件正好相反。您可以通过将
    d
    的声明移出
    for
    循环来解决此问题。修改后的函数应为:
  • =>

    void get_单词(map&map1)
    {
    字符串输入;
    getline(cin,输入);
    字符串s1;
    对于(int i=0;i
    for(int d=i;isalnum(input[d])==0;d++)
    不考虑
    输入的大小。
    for(int d=i;isalnum(input[d])==0;d++)
    不考虑
    输入的大小。
    
    Number of non-empty words: 2
    Number of distinct words: 0
    
    
                               16                           3
    
    void get_words(map < string, int >& map1) 
    {
        string input;
        getline(cin, input);
        string s1;
        for (int i = 0; i < input.size(); i++ , s1 = "") 
        {
            if (isalnum(input[i]) != 0) 
            {//if its a alphanumeric char
            int d;
                for (d = i; isalnum(input[d]) != 0;d++) 
                {
                    s1 += input[d];
                }
                if (isalnum(input[d]) == 0)
                    i = d;
            }
            clean_entry(s1, s1);
            map1[s1]++;
        }
    }