我正在为我的班级编写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;
}

在执行之后,它显示出完美的结果;星号按原样放置到位。
但是当我开始输入长句时,我的问题就出现了,比如我输入
输入一行文字:asd asd asd asd asd asd asd asd asd asd asd asd asd
在我输入“DONE”之后,它会给出垃圾结果,星号被弄乱了,在执行结束时,它会显示消息
分段故障(堆芯转储)
在这种情况下,我做错了什么?

最佳答案

问题出在代码的这一部分字母数组长度为26个项目,当您输入长文本时,堆栈已损坏。它在main()函数调用结束时导致segfault。

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]++;
    }
}

关于c++ - 执行结束时出现段错误(核心转储),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32432955/

10-11 19:09