如何以简单的方式编写此代码
How to write this code in simple way?
我在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;
}
相关文章:
- 在c代码之间共享数据的最佳方式
- 不同/较旧的处理器运行c++代码的方式是否不同
- 如何以静态代码分析友好的方式使用 #define 防护?
- 从排序数组中删除重复项,具有不同代码方式的相同解决方案具有不同的输出
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 为什么我的代码在添加不相关的代码行后工作方式不同?
- 在Qt Creator中应用代码更改的快捷方式是什么?
- 如何在运行时以选择性方式排除代码块
- 如何以这种方式在 C++ 中打印 ASCII 代码值
- 在计算机级别,应用程序以二进制方式运行,然后当发生运行时错误时,它如何呈现回错误代码
- 以有效的方式从 Node .js运行 C 或C++代码
- 如何在现有 c++ 代码中以功能方式实现遥测
- 视觉工作室代码远程调试C 类似Netbeans的方式
- 在某些代码中覆盖方法的方式是什么?
- 以简单且更少的代码方式将字符串添加在一起
- 以迭代方式编写递归代码
- 为什么相同的代码在Visual Studio和Dev-C++中运行方式不同
- 为什么这段代码在Mac和Red Hat中的运行方式如此不同
- Opencv 函数只能以 C 代码方式调用,而不能以C++方式调用