2013-05-07 43 views
0

所以在這裏的所有變體是我正努力使程序: 創建密碼生成器,它使用字典和匹配文件的字典中的單詞與他們的比賽替換字母來生成密碼。 示例: 字典文件: 蘋果 環 匹配的文件: E 3 Ø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; 
} 

所以問題是,現在它只打印最終狀態(4 ppl3 l00p),但我還需要中間狀態(4pple appl3 l0op lo0p)。有人能給我一些線索怎麼做。提前致謝。 :)

+0

錯誤檢查'!的argv [N]' – BLUEPIXY 2013-05-07 09:41:18

+0

該標準要求的argv是'的argv [ARGC] == NULL',但沒有真正的情況。 – BLUEPIXY 2013-05-07 12:58:38

回答

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

當然

,爲您的情況,而不是轉移的信,你要麼做還是不做做替換。

+0

非常感謝您的幫助!對此,我真的非常感激。但我仍然不確定如何實現遞歸。你可以說得更詳細點嗎?抱歉,添麻煩了。 – 2013-05-07 09:35:01