0

我一直在做一个从文件中取出一个句子并输出文件中出现的字符数并将其从高到低排序的作业。文件中的句子是“奥利弗是一只金毛猎犬,它的毛又长又金。”。文件中的句点用作标记值。对于将排序功能放在哪里,我有点迷茫。该程序编译虽然它只显示一个空白屏幕。谁能给我一些关于如何调试这些问题的提示?

#include <iostream>
#include <fstream>

using namespace std;

void swap_values(int& v1, int& v2);
int index_of_smallest(int list[],int start_index, int number_used);
void initialize(int list[]);
void Sort(int list[],int&number_used);
void characterCount(char ch, int list[]);
void readText(ifstream& intext, char& ch, int list[]);
void totalCount(int list[]);

int main()
{
    int letterCount[26], number_used;
    char ch;
    ifstream inFile;

    inFile.open("Data_Chapter_7_8.txt");

    if (!inFile)
    {
       cout << " Cannot open file." <<endl;
    }
    initialize(letterCount);

    while (inFile.get(ch))
    {
        readText(inFile,ch,letterCount);
    }

    totalCount(letterCount);

    inFile.close();

    system("PAUSE");
    return 0;
}

void initialize(int list[])
{
    for(int x = 0;x<26;x++)
    list[x] = 0;
}

void characterCount (char ch, int list[])
{
    ch = tolower(ch);
    if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122))
    list[static_cast<int>(ch)-97]++;
}

void readText(ifstream& intext, char& ch, int list[])
{ 
    if (ch != '.')
    {
        characterCount (ch,list);
    }
}

void totalCount(int letterCount[])
{
    for(int x=0;x<26;x++)
        if(letterCount[x]>0)
            cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl;
}

void Sort(int list[], int number_used)
{
    int index_of_next_smallest;
    int index;
    for(index= 0; index<number_used -1; index++)
        index_of_next_smallest = index_of_smallest(list, index,number_used);
    swap_values(list[index],list[index_of_next_smallest]);
}

int index_of_smallest(int list[], int start_index, int number_used)
{
    int index_of_min;
    int min = list[start_index];
    index_of_min = start_index;
    for (int index= start_index + 1; index < number_used; index++)
        if (list[index]>min)
        {
            min = list[index];
            index_of_min = index;
        }
    return index_of_min;
}

void swap_values(int& v1, int& v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}
4

0 回答 0