如何以简单的方式编写此代码?

时间:2016-03-04 15:56:15

标签: c++

我在cpp中开发了一个代码,它可以读取文本文件并对其中的单词进行计数并将输出存储在txt文件中。 喜欢 - 1个字母的单词,2个字母的单词,3个字母的单词_等等。

我得到了输出,但我的代码并不整齐,我的教授告诉我你应该编写一个每个人都能理解的代码。我试图改变它但不能以更好的方式做到这一点。请建议我在Cpp中以简短的方式完成这项任务的方法。

#include <fstream>
#include <iostream>

using namespace std;

void count_word();
void output_table(int&, int&, int&, int);

int main() {
    ifstream infile("input.txt");
    if (infile.fail()) {
        cout << "Input file not found" << endl;
        return 1;
    }
    count_word();
    infile.close();
    return 0;
}

void count_word() {
    ifstream infile("input.txt");
    int l = 0, x = 0, y = 0, z = 0; // l = letter count, x=1 letter word, y=2 letter        word, z=3+letter word;
    char c;                         // c = get command use from file;
    while (!infile.eof() || infile >> ws) {
        infile.get(c);
        if (c != ' ') // To remove the counter from counting whitespace  as characters
            l++;
        if (c == ' ' || infile.eof()) // Counter to implement the kind of words -> 1 word,2 word or 3 words
        {
            if (l == 1)
                x++;
            else if (l == 2)
                y++;
            else if (l >= 3)
                z++;
            if (infile.eof())
                l = l - 1;            // removing the extra space from the last word due to end of file
            output_table(x, y, z, l); // calling the output statements
            l = 0;                    // resetting the letter count to 0 for the next word
        }
    }
    infile.close();
}

void output_table(int& x, int& y, int& z, int l) {
    ofstream outfile("output.txt");
    outfile << "1 letter words  :" << x << endl;
    outfile << "2 letter words  :" << y << endl;
    outfile << "3+ letter words :" << z << endl;
    outfile << endl;
    cout << "Frequency of the word :\t" << l << endl;
    cout << "1 letter words :" << x << endl;
    cout << "2 letter words :" << y << endl;
    cout << "3 letter words :" << z << endl;
    cout << endl;
    outfile.close();
}

2 个答案:

答案 0 :(得分:0)

这是一种简单的方法,使用 cstring 函数:

#include <cstring>
#include <fstream>

using namespace std;

ifstream fin("file.txt");
ofstream fout("file2.txt");

void write(int length, char word[255])
{
  fout << length << " letter word: " << word << endl;
}

void read()
{
  char word[255];
  while(fin >> word)
  {
    write(strlen(word), word);
  }
}

int main()
{
  read();
  fin.close();
  fout.close();
  return 0;
}

答案 1 :(得分:0)

代码审查:

您在main中打开输入文件,然后在count_word功能中再次打开它。您可能会从操作系统收到错误,指出该文件已经打开。

一个好主意是在再次打开文件之前关闭文件,或者将文件指针传递给函数。您可以将文件指针设为全局变量,但全局变量是邪恶的。

变量名称。如果您必须在评论中解释变量的用途,那么您的命名很差。不要担心变量名称的长度,因为长度不会影响性能,在构建时间内可忽略不计。

在EOF之前阅读的正确方法是:

while ( infile >> ws)

BTW,变量ws未在全局或count_word函数中定义。编译器应该为此发出错误消息。

可以使用std::string类型变量输入单词:

std::string word;
//...
infile >> word; // Read a word.

除非您的应用程序必须一次只读取一个字符,否则不要使用getc方法。

使用std::string::length确定单词中的字符数。

const unsigned word_length = word.length();
if (word_length == 1)
{
  ++words_of_length_1;
}

通过引用传递大变量或函数是否会修改变量。更喜欢通过小变量的副本和较大变量的常量引用。

您的output_table函数不会修改其变量,变量很小,因此请按副本传递:

void output_table(int x, int y, int z, int l) {