메시지의 문자 빈도를 내림차순으로 정렬하는 시저 암호 해독 프로그램을 작성했습니다. 내 문제는 배열의 주파수 위치가 더 이상 내가 설정 한 문자와 일치하지 않는 결과를 인쇄 할 때입니다. 이 문제를 어떻게 해결할 수 있습니까? 구두점과 대문자를 제거하는 다른 코드, 디코딩중인 메시지의 공백과 소문자 이외의 모든 문자가 있습니다.정렬하는 동안 배열의 위치를 어떻게 동일하게 유지할 수 있습니까?
질문을 받고있는 코드를 정리했습니다.
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void sortArray(int*, int);
int main()
{
string fileContent = "a coded message which is several hundreds of characters long is being passed into the program";
int count[26];
// This code is skipping over spaces and other characters
for(int f = 0; f < fileContent.length(); f++)
{
if(fileContent[f] == 32)
{
continue;
}
if(fileContent[f] >= 48 && fileContent[f] <= 57)
{
continue;
}
count[(fileContent[f]-'a')%26]++;
}
// Here is where my issue begins. In sortArray, the position of the characters are being changed.
cout << "Letter frequency: Most common to least common" << endl;
sortArray(count, 26);
for(int p = 0; p < 26; p++)
{
cout << char(p + 97) << ": " << count[p] << endl;
}
return 0;
}
void sortArray(int* srcArray, int numElements)
{
for(int x = 0; x < numElements; x++)
{
int max = srcArray[x];
int maxIndex = x;
int hold;
for(int y = x + 1; y < numElements; y++)
{
if(srcArray[y] > max)
{
max = srcArray[y];
maxIndex = y;
}
}
hold = srcArray[x];
srcArray[x] = max;
srcArray[maxIndex] = hold;
hold = 0;
}
}
친절 내가, 내가 이론화 봤는데이 문제를 해결할 수 있지만이 가능한 솔루션을 알아낼 수없는 방법을 알려 주시기 바랍니다.
출력물을 게시 해주십시오. 예상되는 내용은 무엇입니까? – Valgrind1691
주파수 또는 단지 표준 :: pair을위한 문자와 정수를 포함하는 구조체를 생성하고 그에 따라 배열을 만들고 이에 따라 정렬하십시오. –
lamandy
@ Valgrind1691 예."count [(fileContent [f] - 'a') % 26] ++;"각 문자를 배열 int count [26]의 위치로 저장하고 있습니다. 그런 다음이 배열을 sortArray에 전달하고 있습니다. 이 질문에 대한 대답이 있습니까? 더 많은 프로그램이나 실행 후 리턴되는 코드를 포함하도록이 제공된 코드를 편집하고 싶다면 알려주십시오. – hammonak