C++字母出现 编写一个C++程序,从文本文件读取输入,并计算从输入读取的字符数。如果读取的字符是字母“a”-“z”,则计算该字母出现的次数[使用数组],大写和小写都应在输入中计算为同一个字母。输出输入文本中每个字母的百分比,以及输入文本中非字母字符的百分比
是的,这是一个家庭作业问题,我有大部分,但由于某种原因,它并不像我希望的那样增加C++字母出现 编写一个C++程序,从文本文件读取输入,并计算从输入读取的字符数。如果读取的字符是字母“a”-“z”,则计算该字母出现的次数[使用数组],大写和小写都应在输入中计算为同一个字母。输出输入文本中每个字母的百分比,以及输入文本中非字母字符的百分比,c++,C++,是的,这是一个家庭作业问题,我有大部分,但由于某种原因,它并不像我希望的那样增加 #include <iostream> #include <fstream> #include <string> #include <cctype> using namespace std; int main() { // make array with size of 26 // make array with all letter of alp
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
// make array with size of 26
// make array with all letter of alphabet
const int size = 26;
int narray[size];
char larray[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
// all variables
int count = 0;
char character;
int length = 0;
int amount = 0;
int sum = 0;
double percent = 0;
// open file user inputs
ifstream input;
string file;
cout << "Please enter the file to be read" << endl;
cin >> file;
input.open(file);
if (input.fail())
{
cout << "Can't open file successfully." << endl;
return 1;
}
// count amount of characters and spaces in while loop
while (!input.eof()) //loop until the file ends
{
getline(input, file); // read every character
int c = file.length(); // count length
length += c;
}
// make every variable in array equal to 0
for (count = 0; count < size; count++)
{
narray[count] = amount;
}
// make for loop to read every character
for (int i = 0; i < length; i++)
{
input.get(character); // read characters
if (character <= 'A' && character >= 'z')
{
narray[tolower(character)-'a']++; // find out which variable of the array it is and add 1 to the amount
sum++;
}
}
// make for loop to print out array percentages
for (int j = 0; j < size; j++)
{
percent = (narray[j] / length) * 100;
cout << larray[j] << " " << percent << "%" << endl;
}
int non = (((length - sum) / length) * 100);
cout << "Non letter characters " << non << "%" << endl;
input.close();
return 0;
}
您的代码比需要的复杂一点,但更糟糕的是,它有几个bug 您正在使用2个单独的循环来完成1个循环可以完成的工作 在执行读取操作之前,您正在调用input.eof。在尝试读取之前,流的状态不会更新,因此在第一次读取之前调用eof是未定义的行为 在您将流读取一次到EOF只是为了计算其字符数之后,您不会将流返回到开头,这样您就可以重新读取所有字符 您没有在第一个循环中计数换行字符,但在第二个循环中读取换行字符,因此第二个循环可能不会读取第一个循环计数的字符数 您没有正确测试大写字母和小写字母,也没有考虑在ASCII中,大写字母集和小写字母集之间有6个非字母字符。计算字符数时,对narray[]数组的索引完全错误 您没有考虑文件可能完全为空,或者文件中只有换行符而没有非换行符。如果出现这两种情况中的任何一种,那么长度变量将为0,当除以0计算百分比时,将出现错误 话虽如此,请尝试类似的方法:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// make array with size of 26
const int size = 26;
int narray[size] = {}; // make every variable in array equal to 0
// all variables
char character;
int count = 0;
int sum_letters = 0;
int sum_non = 0;
double percent;
string file, line;
// prompt user for filename
cout << "Please enter the file to be read" << endl;
getline(cin, file);
// open file
ifstream input(file);
if (!input.is_open())
{
cout << "Can't open file." << endl;
return 1;
}
//loop until the file ends
while (getline(input, line))
{
count += line.size(); // count every character
for (int j = 0; j < line.size(); ++j)
{
character = line[j];
// find out which variable of the array it is and add 1 to the amount
if (character >= 'A' && character <= 'Z')
{
narray[character-'A']++;
++sum_letters;
}
else if (character >= 'a' && character <= 'z')
{
narray[character-'a']++;
++sum_letters;
}
else
++sum_non;
}
}
input.close();
if (count != 0)
{
// make for loop to print out array percentages
for (int j = 0; j < size; ++j)
{
percent = (double(narray[j]) / count) * 100.0;
cout << ('a'+j) << " " << percent << "%" << endl;
}
percent = (double(sum_non) / count) * 100.0;
cout << "Non letter characters " << percent << "%" << endl;
}
else
{
cout << "File has no characters" << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// make array with size of 26
const int size = 26;
int narray[size] = {}; // make every variable in array equal to 0
// all variables
char character;
int count = 0;
int sum_letters = 0;
int sum_non = 0;
double percent;
string file, line;
// prompt user for filename
cout << "Please enter the file to be read" << endl;
getline(cin, file);
// open file
ifstream input(file);
if (!input.is_open())
{
cout << "Can't open file." << endl;
return 1;
}
//loop until the file ends
while (input.get(character))
{
++count; // count every character
// find out which variable of the array it is and add 1 to the amount
if (character >= 'A' && character <= 'Z')
{
narray[character-'A']++;
++sum_letters;
}
else if (character >= 'a' && character <= 'z')
{
narray[character-'a']++;
++sum_letters;
}
else
++sum_non;
}
input.close();
if (count != 0)
{
// make for loop to print out array percentages
for (int j = 0; j < size; ++j)
{
percent = (double(narray[j]) / count) * 100.0;
cout << ('a'+j) << " " << percent << "%" << endl;
}
percent = (double(sum_non) / count) * 100.0;
cout << "Non letter characters " << percent << "%" << endl;
}
else
{
cout << "File is empty" << endl;
}
return 0;
}
如果要在非字母字符的百分比中包含换行符,请改为使用:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// make array with size of 26
const int size = 26;
int narray[size] = {}; // make every variable in array equal to 0
// all variables
char character;
int count = 0;
int sum_letters = 0;
int sum_non = 0;
double percent;
string file, line;
// prompt user for filename
cout << "Please enter the file to be read" << endl;
getline(cin, file);
// open file
ifstream input(file);
if (!input.is_open())
{
cout << "Can't open file." << endl;
return 1;
}
//loop until the file ends
while (getline(input, line))
{
count += line.size(); // count every character
for (int j = 0; j < line.size(); ++j)
{
character = line[j];
// find out which variable of the array it is and add 1 to the amount
if (character >= 'A' && character <= 'Z')
{
narray[character-'A']++;
++sum_letters;
}
else if (character >= 'a' && character <= 'z')
{
narray[character-'a']++;
++sum_letters;
}
else
++sum_non;
}
}
input.close();
if (count != 0)
{
// make for loop to print out array percentages
for (int j = 0; j < size; ++j)
{
percent = (double(narray[j]) / count) * 100.0;
cout << ('a'+j) << " " << percent << "%" << endl;
}
percent = (double(sum_non) / count) * 100.0;
cout << "Non letter characters " << percent << "%" << endl;
}
else
{
cout << "File has no characters" << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// make array with size of 26
const int size = 26;
int narray[size] = {}; // make every variable in array equal to 0
// all variables
char character;
int count = 0;
int sum_letters = 0;
int sum_non = 0;
double percent;
string file, line;
// prompt user for filename
cout << "Please enter the file to be read" << endl;
getline(cin, file);
// open file
ifstream input(file);
if (!input.is_open())
{
cout << "Can't open file." << endl;
return 1;
}
//loop until the file ends
while (input.get(character))
{
++count; // count every character
// find out which variable of the array it is and add 1 to the amount
if (character >= 'A' && character <= 'Z')
{
narray[character-'A']++;
++sum_letters;
}
else if (character >= 'a' && character <= 'z')
{
narray[character-'a']++;
++sum_letters;
}
else
++sum_non;
}
input.close();
if (count != 0)
{
// make for loop to print out array percentages
for (int j = 0; j < size; ++j)
{
percent = (double(narray[j]) / count) * 100.0;
cout << ('a'+j) << " " << percent << "%" << endl;
}
percent = (double(sum_non) / count) * 100.0;
cout << "Non letter characters " << percent << "%" << endl;
}
else
{
cout << "File is empty" << endl;
}
return 0;
}
欢迎来到堆栈溢出!听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:查看一个ASCII表,然后问自己字符='z'是否正确。你能详细说明一下它没有像我希望的那样添加吗。?你得到的与你期望的相比是什么?请阅读Fred Larson我所有字母得到0%,非字符得到100%,这就是为什么我相信当阅读字符时,它们不会添加到数组中。非换行字符是什么意思?谢谢你的帮助!换行符也是字符-\r 13,0x0D和\n 10,0x0A。std::getline不会输出这些字符,但std::istream::get会输出这些字符。在您展示的代码中,您没有计算用于换行符的字符,因此即使文件不是空的,如果文件中的所有字符都是换行符,而没有其他字符,那么长度变量也有可能为0。如果要使用单独的循环进行计数和读取,请确保它们作用于相同的数据。1个计数/读取两个循环中的换行符,或2个忽略两个循环中的换行符。