我正在为我的类(class)在 Linux Ubuntu 上编写 C++ 程序。该程序应该找到用户输入的字符串的频率。频率的计算方法是每个字符出现在句子中的数量,然后除以句子中的最大字符数。之后,我根据每个字符出现的百分比打印出星号。这是我写的代码
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
string text;//partial text for each whitespace
string finalText;//String that has been concat
int totalLetter=0;//Total number of all letter
double storedValue[26];//Giving Result to each array
int i;//"for" statement counter declaring
int j;//"for" statement second counter declaring
int letters[26];//array to reducing ascii value on line 26
cout << "Enter the text at the prompt to calculate letter frequencies. " << endl <<
"Enter DONE when finished with the input."<< endl;
while(text!="DONE"){//check the input, if it's not "DONE" then continue adding string
cout<< "Enter a line of a text: ";
std::getline(std::cin,text);
if(text=="DONE")break;
finalText = finalText + text;
}
char *charArray = new char[finalText.length()+1];//Converting string input to char type
std::strcpy(charArray,finalText.c_str());//Copying string to char
for(i=0; i<finalText.length(); i++){//reduce to smaller case
if(charArray[i]>='A' && charArray[i]<='Z')
charArray[i]=charArray[i]+32;
}
for(i=0; i<finalText.length(); i++){//set default for charArray to be 0
if(charArray[i]<'a' && charArray[i]>'z'){
charArray[i]=0;
}
}
for(i=0; i<finalText.length(); i++){//calculate the total characters input
if(charArray[i]>='a' && charArray[i]<='z'){
totalLetter++;
}
}
for(i=0; i<26; i++){//Set all storeValue to be 0 from a - z
storedValue[i] = 0;
}
for(i=0; i<finalText.length(); i++){//convert letters to start at 0 as an 'a' from ascii code and so on
letters[i]=(int)charArray[i]-97;
storedValue[i] = 0;
}
for(i=0; i<finalText.length(); i++){//increment value in array for each letter
for(j=0; j<26; j++){
if(letters[i]==j)
storedValue[j]++;
}
}
cout << endl;
cout << "A total of " << totalLetter << " letters of the alphabet were processed" << endl;
cout << endl;
cout << "a b c d e f g h i j k l m n o p q r s t u v w x y z %"<<endl;
//calculate the percent
for(i=0; i<26; i++){
storedValue[i]=round((storedValue[i]/totalLetter)*100);
}
cout<<endl;
//Get Maximum Percent
int maxPercent=0;
int maxLetterPercent=0;
for(i=0; i<25; i++){
if(storedValue[i]>maxPercent){
maxPercent=storedValue[i];
maxLetterPercent=i;
}
}
//Printing asterisk
for(i=0; i<maxPercent; i++){
for(j=0; j<26; j++){
if(storedValue[j]>0)
cout<<"* ";
else if(storedValue[j]<=maxPercent)
cout<<" ";
}
cout<<" "<<i+1;
cout<<endl;
}
char finalCharMax = maxLetterPercent + 'a';
cout<<"The most common letter is " << finalCharMax << " with a frequency of " << maxPercent
<< " %";
cout<<endl;
return 0;
}
执行后,显示完美结果;星号在它们应该在的地方。但是当我开始输入长句时出现了问题,例如我输入
Enter a line of text: asd asd asd asd asd asd asd asd asd asd asd asd asd
在我输入“DONE”后,它给出了垃圾结果,星号被弄乱了,在执行结束时,它显示了消息
Segmentation fault (core dumped)
在这种情况下,我做错了什么?
问题出在这部分代码中。字母数组的长度为 26 个项目,当您输入长文本时,堆栈已损坏。它会在 main() 函数调用结束时导致段错误。
for(i=0; i<finalText.length(); i++){//convert letters to start at 0 as an 'a' from ascii code and so on
letters[i]=(int)charArray[i]-97;
storedValue[i] = 0;
}
for(i=0; i<finalText.length(); i++){//increment value in array for each letter
for(j=0; j<26; j++){
if(letters[i]==j)
storedValue[j]++;
}
}
我是一名优秀的程序员,十分优秀!