안녕하세요. 편지 집합을 해독하고 해당 문자 집합에서 만들 수있는 모든 단어를 출력하는 프로그램을 만들고 있습니다. 예를 들어, "vlei"문자를 입력하면 프로그램 "생", "사악", "사악"을 출력합니다.Word Unscrambling Program - C++
지금까지 인터넷을 통해이 quiiiite에 대해 조금 훑어 보았고이 시점에서 내 스킬 레벨과 관련된 구체적인 질문 (레벨 2 멍청한 답변)을 찾을 수 없습니다.
지금까지 나는 주어진 문자들로부터 모든 가능한 조합을 만들었습니다. 7 자 미만의 문자는 제외됩니다.
이
내가 지금까지 가지고있는 코드 :string letter;
char newWord[7];
int main()
{
cout << "Type letters here: ";
cin >> letter;
for(int i = 0 ; i < 7 ; i++)
{
for(int j = 0 ; j < 7 ; j++)
{
for(int k = 0 ; k < 7 ; k++)
{
for(int l = 0 ; l < 7 ; l++)
{
for(int m = 0 ; m < 7 ; m++)
{
for(int n = 0 ; n < 7 ; n++)
{
for(int o = 0 ; o < 7 ; o++)
{
sprintf(newWord, "%c%c%c%c%c%c%c", letter[i], letter[j], letter[k], letter[l], letter[m], letter[n], letter[o]);
}
}
}
}
}
}
}
return 0;
}
나는 사람이 이런 일에 경험이 있는지 궁금하고, 힌트 또는 조언을 제공 할 수 있습니다.
특히 내가 어려움을 겪고있는 것은 .txt 파일을 단어를 비교하기위한 사전으로 사용하는 방법입니다. 또한 나는 strcmp()
을 사용하는 데 어려움을 겪고 있었는데, 이는 내가 스크램블 된 단어를 사전과 비교하는 데 사용할 계획이었습니다. 그래서 두 개의 문자열을 비교하는 다른 방법이 더 간단하다면 크게 도움이 될 것입니다.
미리 감사드립니다.
안녕하세요. 제 프로그램을 끝내고 다른 사람을 도울 수 있기를 바랍니다. 모든 도움에 감사드립니다.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
//declaring variables
int i;
int scores[531811]; //array for scores of found words
string wordlist[531811]; //array for found matched words
string word[531811]; //array of strings for dictionary words about to be read it
string tester;//string for scrambled letters that will be read in
int scorefinder(string scrab) //SCORE FINDER FUNCTION
{
int score = 0;
int x = 0;
int j = 0;
while (scrab[j])
{
char ltr = toupper(scrab[j]); //converts to all caps
//assings values to each letter and adds it to itself
if(ltr == 'A' || ltr == 'E' || ltr == 'I' || ltr == 'L' || ltr == 'N' || ltr == 'O' || ltr == 'R' || ltr == 'S' || ltr == 'T' || ltr == 'U')
x += 1;
else if(ltr == 'D' || ltr == 'G')
x += 2;
else if(ltr == 'B' || ltr == 'C' || ltr == 'M' || ltr == 'P')
x += 3;
else if(ltr == 'F' || ltr == 'H' || ltr == 'V' || ltr == 'W' || ltr == 'Y')
x += 4;
else if(ltr == 'K')
x += 5;
else if(ltr == 'J' || ltr == 'X')
x += 8;
else if(ltr == 'Q' || ltr == 'Z')
x += 10;
++j;
}
score = x;
return score;
}
int main() {
//READS IN DICTIONARY
ifstream file("words.txt"); //reads in dictionary
if (!file.is_open()){ //checks if file is being NOT read correctly
cout << "BROEKN \n"; //prints error message if so
}
if(file.is_open()){ //checks if file IS being read correctly
for(int i = 0; i < 531811; i++){
file >> word[i]; //read in each word from the file and
} //assigns each to it's position in the words array
}
//END OF READ IN DICTIONARY
cout << "Enter scrambled letters: ";
cin >> tester; //reads in scrambled letters
sort(tester.begin(),tester.end()); //sorts scrambled letters for next_permutation
while (next_permutation(tester.begin(),tester.end())){ //while there are still permutations available
for(i=0;i<531811;i++){
if (is_permutation (word[i].begin(),word[i].end(), tester.begin())){
wordlist[i] = word[i]; //assigns found word to foundword array
scores[i] = scorefinder(word[i]); //assigns found word score to foundscore array
}
}
}
//PRINTS OUT ONLY MATCHED WORDS AND SCORES
for(i=0;i<531811;i++){
if(scores[i]!=0){
cout << "Found word: " << wordlist[i] << " " << scores[i] << "\n";
}
}
}
무엇이 문제입니까? –
단어의 글자 수가 10 개라면 프로그램이 어떻게 보이나요? 그리고 당신의 코드는 작동합니까? –
@AndreiAlexandruAgape 죄송합니다. 실수로 너무 일찍 게시했습니다. 방금 내 게시물을 편집했습니다. 대부분 그냥 스크램블 된 단어와 사전 단어를 비교하는 방법과 사전에서 내 프로그램을 읽는 방법에 대한 조언을 구하는 것입니다. –