0

だからここに私が作ろうとしているプログラムがあります:辞書と一致ファイルを使用して、辞書の単語の文字を一致する文字に置き換えてパスワードを生成するパスワードジェネレーターを作成します。例: 辞書ファイル: apple loop 一致ファイル: a 4 e 3 o 0 出力: 4pple appl3 4ppl3 l0op lo0p l00p

そして、これが私の解決策です:

#include <stdio.h>
#include <string.h>

/* function prototypes */
int read_source(char* ,char* ,char* );
char* shift_string(char* ,char* );
int write_shifted(char* ,char* );

int main(int argc, char *argv[])
{
/* argv[1]: the source file with the original strings
 * argv[2]: the shift file which indicates which characters are to be
 * replaced with what
 * argv[3]: the file in which the new strings are to be stored*/

if(!argv[1])
{
    printf("No source file, can do nothing. Exiting.\n");
    return 1;
}
else if (!argv[2])
{
    printf("No shift file given, can do nothing. Exiting.\n");
    return 2;
}
else if (!argv[3])
{
    /* If no output file has been specified, print the
     * output directly to the console. */
    read_source(argv[1],argv[2],NULL);
    return 0;
}

if (read_source(argv[1],argv[2],argv[3])!=0)
{
    printf("There has been an error. Exiting.\n");
    return 3;
}

if (argv[3])
{
    printf("Everything seems to have gone according to plan.\n");
    printf("Your output has been stored in \"%s\"\n",argv[3]);
}

return 0;
}

int read_source(char* source_file,char* shift_file,char* out_file)
{
FILE *file_pointer;
file_pointer=fopen(source_file, "r");

/* Exit gracefully if source_file cannot be found. */
if (file_pointer == NULL) 
{
     printf("Couldn't open \"%s\" for reading.\n",source_file);
     return 1;
}

char* new_line;

    /* maximum line length, can be changed if needed */
char line[256];

/* Go through the source file. */
while (fgets(line,sizeof line,file_pointer) != NULL)
{
    new_line = shift_string(shift_file,line);
    if (new_line==NULL)
    {
        printf("There has been an error with replacing the characters.\n");
        return 2;
    }
    write_shifted(new_line,out_file);
}
int fclose(FILE *file_pointer);

return 0;
 }


 char* shift_string(char* shift_file,char* source_string)
 {
/* This function replaces certain characters in a given source_string as
 * specified by shift_file */


/* Open shift file. */
FILE *file_pointer;
char i,j;
file_pointer=fopen(shift_file, "r");

/* Exit gracefully if shift_file cannot be found. */
if (file_pointer == NULL) 
{
     printf("Couldn't open \"%s\" for reading.\n",shift_file);
     return NULL;
}

int k;

/* Determine how long the source_string is for the loop below. */
int length = strlen(source_string);

while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)
{

    /* This loop actually does the replacing. */
    for (k=0;k<length;k++)
    {
        if (source_string[k]==i) 
        {
            source_string[k]=j;
        }
    }
}
int fclose(FILE *file_pointer);

return source_string;
}

int write_shifted(char* new_line,char* out_file)
{
/* This function writes the new strings to out_file
 * If no out_file has been given, it will write the
 * output to the console.*/

if (out_file==NULL)
{
    printf("%s",new_line);
    return 0;
}

FILE *file_pointer; 

/* Open in a+ mode. If out_file does not yet exist, it will be created.
 * If it does exist, it will be appended to instead of overwritten. */
file_pointer = fopen(out_file,"a+"); 

/* Write the new line */
fprintf(file_pointer,"%s",new_line);

fclose(file_pointer);
return 0; 
} 

したがって、問題は、最終状態 (4ppl3 l00p) のみを出力するようになりましたが、中間状態 (4pple appl3 l0op lo0p) も必要であることです。誰か作り方の手がかりを教えてください。前もって感謝します。:)

4

1 に答える 1

0

再帰は、このタイプの問題に適しています。基本的には、最初の文字を置換するかどうかを判断し、それぞれのケースで単語の残りの部分を再帰的に使用します (置換する場合と置換しない場合)。文字がなくなるまでこれを行うと、すべての組み合わせができあがります。

// terribly pseudo-codey, but should give the idea
string='abc';
stringfunction("", string);

// simplified, just shifts each letter by one
stringfunction(processed, unprocessed)
  if unprocessed is empty
    print processed
    return

  stringfunction(strcat(processed, unprocessed[0]), unprocessed[1]);
  stringcunction(strcat(processed, unprocessed[0]+1), unprocessed[1]);

これにより、abc、abd、acc、acd、bbc、bbd、bcc、bcd が出力されます。

もちろん、あなたの場合、文字をシフトする代わりに、置換を行うか行わないかのどちらかです。

于 2013-05-07T07:46:42.560 に答える