我需要帮助完成课堂任务,基本上我需要从文本文件中读取字母,对每个字母的出现进行计数,并按大多数出现的次数对它们进行排序。我在排序后记住每个字母的原始计数时遇到问题(因为索引更改) 问题是结果字母出现的次数多于一次,所以我丢失了一些字母
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
char letter, b[26] = {};
int number[26] = {0}, temp, i, j;
ofstream outfile ("test.txt");
srand (time(NULL));
for(int i=0;i<20;i++){
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile <<c<<endl;
}
outfile.close();
ifstream file( "test.txt");
while(file >> letter){
if (letter >= 'a' && letter <= 'z') number[letter - 'a']++;
if (letter >= 'A' && letter <= 'Z') number[letter - 'A']++;
}
for(i=0; i<26; i++)
{
for(j=i+1; j<26; j++)
{
if(number[j] > number[i])
{
temp = number[i];
b[i] = char(97+j);
number[i] = number[j];
number[j] = temp;
}
}
}
for(i=0;i<26;i++){
if(number[i] != 0)cout<<b[i]<<" "<<number[i]<<endl;
}
return 0;
}
l 3
m 3
m 2
q 2
w 2
j 1
l 1
m 1
o 1
q 1
t 1
v 1
w 1
答案 0 :(得分:0)
要扩大我在使用结构的评论中所说的话,请考虑以下问题:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct CharInstance {
int count;
char letter;
};
int main()
{
char letter;
int i, j;
CharInstance temp;
CharInstance charInst[26] = { 0 };
// create test data randomly
{
ofstream outfile("test.txt");
srand(time(NULL));
for (int i = 0; i < 500; i++) {
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile << c << endl;
}
outfile.close();
}
// read in data and store in array
{
ifstream file("test.txt");
while (file >> letter) {
// force lowercase to uppercase so we count both lowercase and uppercase as the same
if (letter >= 'a' && letter <= 'z') letter -= 32;
// since all input is now uppercase, ignore anything that isn't in uppercase range
if (letter < 'A' || letter > 'Z') continue; // skip non-alpha chars
const int ind = letter - 'A';
++charInst[ind].count;
charInst[ind].letter = letter;
}
file.close();
}
// bubble sort
for (i = 0; i < 26; i++)
{
for (j = i + 1; j < 26; j++)
{
if (charInst[j].count > charInst[i].count)
{
temp = charInst[i];
charInst[i] = charInst[j];
charInst[j] = temp;
}
}
}
for (i = 0; i < 26; i++) {
cout << charInst[i].letter << ": " << charInst[i].count << " " << endl;
}
return 0;
}
答案 1 :(得分:0)
如果您不介意C ++ 11。
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
int main() {
std::ifstream in("sort.cpp");
// Map to store counts of each character.
// Note: this won't work with some international characters (i.e. Chinese).
std::map<char, int> histogram;
char ch = 0;
while (in.get(ch)) {
// TODO
// Filter out characters you don't want, as this would also pick whitespaces
// and newlines.
// Increment the count for |ch|.
// This would create new map element if the count was zero.
++histogram[ch];
}
// Move map contents into an array of pairs character->count.
std::vector<std::pair<char, int>> pairs(histogram.begin(), histogram.end());
// Sort the array. The third parameter is a function used as a custom comparator.
std::sort(pairs.begin(), pairs.end(),
[](const std::pair<char, int>& left, const std::pair<char, int>& right) {
// Use the count to compare |left| and |right|.
return left.second > right.second;
});
for (const auto& pair : pairs) {
std::cout << pair.first << " " << pair.second << std::endl;
}
}
g++ -std=c++11 sort.cpp
./a.out
207
t 79
a 62
e 61
r 61
i 53
s 51
o 50
n 50
c 46
...