1

我知道缺少声明,代码编译得很好,但是输出没有正确输出......而不是一个字母,我得到的是 ¿ 。我相信问题出在初始化函数中,我似乎无法弄清楚它是什么......

void printResult(ofstream& outFile, letterType letterList[], int listSize)
{
    int i;
    int sum = 0;
    double Percentage = 0;

    cout << "PRINT" << endl;

    for (i = 0; i < 52; i++)
    sum += letterList[i].count;

    outFile << fixed << showpoint << setprecision(2) << endl;

    outFile << "Letter  Count   Percentage of Occurrence" << endl;

    for (i = 0; i < 52; i++)
    {
    outFile << "  " << letterList[i].letter << "     "
    << setw(5) << letterList[i].count;

    if (sum > 0)
        Percentage = static_cast<double>(letterList[i].count) /
        static_cast<double>(sum) * 100;
    /*
     Calculates the number of Occurrence by dividing by the total number of
     Letters in the document.
     */
    outFile << setw(15) << Percentage << "%" << endl;
    }

    outFile << endl;
}


void openFile(ifstream& inFile, ofstream& outFile)
{
    string inFileName;
string outFileName;

cout << "Enter the path and name of the input file (with extension): ";
getline(cin, inFileName);

    inFile.open(inFileName);
cout << endl;

    cout << "Your input file is " << inFileName << endl;
    cout << endl;

cout << "Enter the path and name of the output file (with extension): ";
getline(cin, outFileName);

outFile.open(outFileName);
cout << endl;

    cout << "The name of your output file is " << outFileName << endl;
    cout << endl;

}

void initialize(letterType letterList[])
{

    //Loop to initialize the array of structs; set count to zero
    for(int i = 0; i < 26; i++)
    {
    //This segment sets the uppercase letters
    letterList[i].letter = static_cast<char>('A' + i);
    letterList[i].count = 0;

    //This segment sets the lowercase letters
    letterList[i + 26].letter = static_cast<char>('a' + i);
    letterList[i + 26].count = 0;
    }

}

void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{

cout << "COUNT WORKING" << endl;
char ch;
//read first character
inFile >> ch;

//Keep reading until end of file is reached
while( !inFile.eof() )
{
    //If uppercase letter or lowercase letter is found, update data
    if('A' <= ch && ch <= 'Z')
    {
        letterList[static_cast<int>(ch) - 65].count++;
    }
    else if('a' <= ch && ch <= 'z')
    {
        letterList[static_cast<int>(ch) - 97].count++;
    }

    //read the next character

    inFile >> ch;


} //end while
} //end function

================

驱动程序代码

int main()
{
    struct letterType letterList[52]; //stores the 52 char we are going to track stats on

    int totalBig = 0; //variable to store the total number of uppercase
    int totalSmall = 0; //variable to store the total number of lowercase

    ifstream inFile;
    //defines the file pointer for the text document
    ofstream outFile;
    //the file pointer for the output file
    cout << "MAIN WORKING" << endl;

    openFile(inFile, outFile);
    //allow the user to specify a file for reading and outputting the stats

    /*if (!inFile || !outFile)
    {
        cout << "***ERROR*** /n No such file found" << endl;
        return 1;
    }
    else
        return 1;
    *///Check if the files are valid

    initialize(&letterList[52]);
    //initalizes the letter A-Z, and a-z */


    count(inFile, &letterList[52], totalBig, totalSmall);
    // counts the letters

    printResult(outFile, letterList, 52);
    //writes out the stats

    //Close files
    inFile.close();
    outFile.close();



    return 0;
}

======================

整个计数功能

void count(ifstream& inFile, letterType letterList[], int& totalBig, int& totalSmall)
{

cout << "COUNT WORKING" << endl;
char ch;
//read first character
inFile >> ch;

//Keep reading until end of file is reached
while( !inFile.eof() )
{
    //If uppercase letter or lowercase letter is found, update data
    if('A' >= ch && ch <= 'Z')
    {
        letterList[ch - 'A'].count++;
    }
    else if('a' >= ch && ch <= 'z')
    {
        letterList[(ch - 'a') + 26].count++;
    }

    //read the next character

    inFile >> ch;


} //end while
} //end function
4

2 回答 2

1

计数逻辑写得很混乱,这掩盖了一个错误(它折叠了案例):

if('A' <= ch && ch <= 'Z')
{
    letterList[static_cast<int>(ch) - 65].count++;
}
else if('a' <= ch && ch <= 'z')
{
    letterList[static_cast<int>(ch) - 97].count++;  // <--- a bug here
}

这会'a'通过增加第一个元素的计数来做出反应,这看起来像是打算作为'A'. 这很容易通过偏移小写来解决,也可以重写它,以便更清楚正在做什么:

if ('A' <= ch  &&  ch <= 'Z')
{
    letterList[static_cast<int>(ch - 'A')].count++;  // count uppercase
}
else if ('a' <= ch  &&  ch <= 'z')
{
    letterList[static_cast<int>(ch - 'a') + 26].count++;  // count lowercase
}

至于主要的错误,initialize()没有在任何地方调用。

初始化被错误地调用,因为initialize(&letterList[52]); 这试图初始化条目 52、53、... 103。我很惊讶它没有段错误。

它应该被称为

initialize(letterList);
于 2013-04-15T05:26:05.810 回答
0

你的功能做得恰到好处。

在您的驱动程序代码中,您使用错误的参数调用这些函数

检查这些行,它们应该是这样的

initialize(&letterList[0]);
//initalizes the letter A-Z, and a-z */


count(inFile, &letterList[0], totalBig, totalSmall);

在将letterList数组传递给函数时,您应该将指针传递给数组中的第一个元素。你应该通过&letterList[0]或干脆letterList

于 2013-04-15T06:39:30.267 回答