C++字母出现

C++ Letter Occurrences

本文关键字:C++      更新时间:2023-10-16

>编写一个C++程序,该程序从文本文件中读取输入并计算从输入中读取的字符数。如果读取的字符是字母 ('a'-'z'),则计算该字母在输入中 [使用数组] 出现的次数(大写和小写都应计为同一个字母)。输出输入文本中每个字母的百分比,以及输入中非字母字符的百分比。

是的,这是一个家庭作业问题,我有大部分,但由于某种原因,它没有像我希望的那样添加。

#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;
}

你的代码比它需要的要复杂一些,但更糟糕的是它有几个错误。

  • 您正在使用 2 个单独的循环来完成 1 个循环可以完成的工作。

  • 在执行读取操作之前调用input.eof()。 流的状态直到尝试读取后才会更新,因此在第一次读取之前调用eof()是未定义的行为。

  • 在将流通读一次到 EOF 只是为了计算其字符数之后,您不会将流返回到开头,以便您可以再次阅读字符。

  • 您没有计算第一个循环中的换行符
  • 字符,但您正在读取第二个循环中的换行符字符,因此第二个循环(可能)不会读取与第一个循环计数的字符数一样多。

  • 您没有正确测试大写和小写字母,
  • 并且没有考虑到在 ASCII 中,大写字母集和小写字母集之间有 6 个非字母字符的事实。 在计算字符数时,narray[]数组的索引都是错误的。

  • 您没有考虑文件可能完全为空的可能性,或者其中只有换行符而没有非换行符。 如果发生上述任一情况,则length变量将为 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;
}
相关文章:
  • 没有找到相关文章