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

How to write this code in simple way?

本文关键字:代码 方式编 简单      更新时间:2023-10-16

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

我得到了输出,但我的代码并不整洁,我的教授告诉我,你应该写一个每个人都能理解的代码。我试图改变它,但无法以更好的方式做到这一点。请建议我在 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();
}

代码审查:


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

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


变量名称。 如果你必须在注释中解释变量的用途,那么你的命名就很糟糕。 不要担心变量名称的长度,因为长度不会影响性能,并且在构建时可以忽略不计。


读取EOF的正确方法是:

while ( infile >> ws)

顺便说一句,变量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) {

这是一种简单的方法,通过使用 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;
}