对字符进行计数的算法不计算撇号

时间:2016-01-29 03:35:54

标签: c++ xcode terminal

这是更大程序的功能。此函数只能计算字母数字中的字符数。奇怪的是,它在Xcode中运行时会计算撇号,但在终端中编译并运行后却没有。有什么建议吗?

#include <iostream>
#include <iomanip>
#include <ctype.h>

using namespace std;

int WordLength();
void DisplayTable(int WF[]);

int main()
{
    int WordFrequency[16]={0};
    int TempLengthStorage=0;

    TempLengthStorage=WordLength();

    while (TempLengthStorage!=0)
    {
        if (TempLengthStorage==1)//skipping subscript 0 - not using it
            ++WordFrequency[1];

        else if (TempLengthStorage==2)
            ++WordFrequency[2];

        else if (TempLengthStorage==3)
            ++WordFrequency[3];

        else if (TempLengthStorage==4)
            ++WordFrequency[4];

        else if (TempLengthStorage==5)
            ++WordFrequency[5];

        else if (TempLengthStorage==6)
            ++WordFrequency[6];

        else if (TempLengthStorage==7)
            ++WordFrequency[7];

        else if (TempLengthStorage==8)
            ++WordFrequency[8];

        else if (TempLengthStorage==9)
            ++WordFrequency[9];

        else if (TempLengthStorage==10)
            ++WordFrequency[10];

        else if (TempLengthStorage==11)
            ++WordFrequency[11];

        else if (TempLengthStorage==12)
            ++WordFrequency[12];

        else if (TempLengthStorage==13)
            ++WordFrequency[13];

        else if (TempLengthStorage==14)
            ++WordFrequency[14];

        else if (TempLengthStorage>=15)
            ++WordFrequency[15];

        TempLengthStorage=WordLength();
    }

    DisplayTable(WordFrequency);
}

int WordLength()
{
    int LetterCount=0;
    int EndOfWord=0;
    char Input;
    char NextChar;

    cin.get(Input);

    while (EndOfWord!=1 && !cin.eof())
    {
        if (Input==' ' || Input=='\n' || Input=='.' || Input=='!' || Input=='?'  || Input==',')
        {//do nothing if whitespace or punctuation, essentially skips over them
        }

        else if (Input=='-')
        {
            NextChar=cin.peek();

            if ((isalpha(NextChar)) || (isnumber(NextChar)))//this includes the hyphen in words that have hyphens (refer to funtion notes)
                ++LetterCount;

            if (NextChar=='\n')//for cases where the hyphen doesn't add to word length (refer to function notes)
            {
                cin.get(Input);//dont count it and get next character
            }
        }

        else if (Input=='\'')
            ++LetterCount;

        else if ((isalpha(Input)) || (isnumber(Input)))
        {
            ++LetterCount;

            NextChar=cin.peek();

            if (NextChar==' ' || NextChar=='\n' || NextChar=='.' || NextChar=='!' || NextChar=='?'  || NextChar==',' || NextChar==')')//included ) as a condition to end words for cases such as (219)
            {
                EndOfWord=1;
            }
        }

        cin.get(Input);
    }

    return LetterCount;
}

void DisplayTable(int WF[])
{
    int TotalWords=0;
    float TotalLetters=0;//made a float for float based masth later on when finding average
    float Average=0;

    cout << endl;

    cout << "Word Length                 Frequency" << endl;
    cout << "-----------                 ---------" << endl;

    for (int i=1; i<=15; ++i)
    {
        /*This portion of the loop displays the table*/

        cout << "    ";
        cout << setw(2) << i;
        cout << "                          ";
        cout << WF[i] << endl;

        /*this portion of the loop does the calculations needed to find the average word length*/

        TotalWords += WF[i];
        TotalLetters += (i * WF[i]);
    }

    cout << "\nTotal Words = " << TotalWords << endl << endl;

    cout << "Total Letters = " << TotalLetters << endl << endl;

    Average = TotalLetters/TotalWords;

    cout << "Average Word Length: " << Average << endl;
}

我用过#34; Ain&#t; t&#34;在这两种情况下,仅用于测试目的。

enter image description here

enter image description here

0 个答案:

没有答案