Я беру курс на C++ и было дано задание следующим образом:Программа счетчик слов в C++
Эта программа вводит рукопись в текстовой форме от стандартного устройства ввода и анализирует длины всех слова встретились. Только буквы, цифры и апострофы в слове вносят вклад в длину слова.
Профессор дал мне небольшую помощь со структурой, но я все еще борется. Главным образом, чтобы получить правильное расположение массива, чтобы увеличивать каждый раз, когда соответствующая длина возвращается из функции WordLength. План состоит в использовании элементов массива 1-15, и любые слова длиной более 15 символов будут просто входить в 15 элементов массива. Мой код выглядит следующим образом:
#include <iostream>
#include <ctype.h>
using namespace std;
int WordLength();
void DisplayCount(int wordCount[]);
void main()
{
int L;
int Num_of_Char[16]={0};
L=WordLength();
while (L)
{
L=WordLength();
Num_of_Char[L]+=1;
}
DisplayCount(Num_of_Char);
}
/***************************************WordLength*******************************************
* Action: Analyzes the text that has been entered and decides what is and isn't *
* a word (mainly by separating words by whitespaces and not accepting *
* most punctuation as part of a word with the exception of hyphens which *
* carry a partial word to the next line as well as apostrophes. *
* *
* Parameters: *
* IN: *
* *
* OUT: *
* *
* *
* Returns: The length of each word. *
* *
* Precondition: *
*********************************************************************************************/
int WordLength()
{
char ch[500];
int End_Of_Word=0, Length=0, i=0;
cout<<"Please enter some text:\n";
cin.get(ch,500);
while((!cin.eof)&&(!End_Of_Word))
{
if((i==0)&&(isspace(ch[i])))
{
++i;
}
else if(isalnum(ch[i]))
{
++Length;
++i;
}
else if ((ch[i]=='\'')&&((ch[i-1]=='s')||(ch[i-1]=='S'))&&(isspace(ch[i+1]))) //accounts for plural possessive of a word
{
++Length;
++i;
}
else if ((ch[i]=='\'')&&((ch[i+1]=='s')||(ch[i+1]=='S'))) //accounts for single possessive of a word and keeps the hyphen as part of the word
{
++Length;
++i;
}
else if((isspace(ch[i]))||(ispunct(ch[i]))||(ch[i]=='\0'))
{
++End_Of_Word;
}
return Length;
}
}
/***************************************DisplayCount*****************************************
* Action: Displays how many words have a specific character count between 1 and *
* 15 characters. Then displays the average word character size. *
* *
* Parameters: *
* IN: wordArray, which points to the array that holds the count of each word's*
* character size. *
* *
* OUT: Displays the array contents in a grid style as well as an average *
* word size based on the contents of the array. *
* *
* Returns: *
* *
* Precondition: wordArray points to an int array *
*********************************************************************************************/
void DisplayCount(int wordArray[])
{
double sum = 0;
cout<<"\tWord Length\t\t"<<"Frequency\n";
cout<<"\t-----------\t\t"<<"---------\n";
for(int i=1; i<16; i++)
{
cout<<"\t "<<i<<"\t\t\t "<<wordArray[i]<<endl; //Displays the contents of each element
sum+=(i*wordArray[i]); //Keeps a running total of contents of array
}
cout<<"\tAverage word length: "<<sum/(15)<<endl; //Displays the average word length
}
Любая помощь, была бы очень признательна!
@ bikram990: Это неправильное восприятие. C++ предназначен не только для ООП; [это язык с несколькими парадигмами] (http://www.stroustrup.com/bs_faq.html#multiparadigm), который может использоваться для разных стилей программирования. – legends2k
@ legendends2k Я согласен с этим, но красота C++ - это OOP. – bikram990