C++ 从结果中删除重复项

C++ 从结果中删除重复项,c++,duplicates,C++,Duplicates,我有一个创建范围词的代码,我的问题是当我想从'abcd'(最小范围=4,最大范围=4)创建范围词时,它会创建'cdaa'或'ccca',并且。。。我想删除重复的“aa”或“cc”,然后。。。有办法吗?这是否可以向我的程序中添加一个代码,以便像这样删除副本 霉菌代码: #include <string.h> #include <windows.h> #include <stdio.h> #include <math.h> typedef unsi

我有一个创建范围词的代码,我的问题是当我想从'abcd'(最小范围=4,最大范围=4)创建范围词时,它会创建'cdaa'或'ccca',并且。。。我想删除重复的“aa”或“cc”,然后。。。有办法吗?这是否可以向我的程序中添加一个代码,以便像这样删除副本


霉菌代码:

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

typedef unsigned long long int64;


// config
char configfile[] = "settings.ini";
char outputfile[ 1<<7 ];
char charset[ 1 << 9 ];
int maxPWLength = 5;

// local vars
int cursorPosition = 0;
int offsetCursor = 0;
int currentLength = 1;
char currentword[ 1<<7 ];
int64 estfilesize = 0;
int64 charswritten = 0;
int lastpercentage = 0;
FILE *fp;

int64 pow64(unsigned int base, unsigned int exp)
{
    int64 result = 1;
    for (unsigned int v=0; v < exp; v++)
        result *= base;

    return result;
}


void Processbar()
{
    int percentage = (charswritten * 100) / estfilesize;
    if (percentage > 100) percentage = 100;
    char bar[51] = ""; ZeroMemory(bar, sizeof(bar));
    if (percentage > lastpercentage)
    {
        lastpercentage = percentage;
        int bars = percentage / 2;
        for (int z=0; z <= bars; z++)
            bar[z] = '|';

        printf("\r       [ %-50.50s ]  [ %d%% ]", bar, percentage);
    }
}
void Writeword(char* word)
{
    fprintf(fp, "%s\n", currentword);
    charswritten += strlen(word) + 2;
    Processbar();
}

BOOL GeneratePosition(int position)
{
    for (unsigned int x=0; x < strlen(charset); x++)
    {
        currentword[position] = charset[x];
        if (position > 0) GeneratePosition(position-1);
        if (((position > 0) && (currentword[position] != charset[0])) || (position == 0))   Writeword(currentword);
    }
    if (cursorPosition == maxPWLength-1) return FALSE;

    return TRUE;
}

int main(int argc, char* argv[])
{
    // -> header
    printf("                                                                         \n");
    printf("      #################################################################  \n");
    printf("     #                                                                 # \n");
    printf("     #             word List Generator by sh4d0w` v1.8             # \n");
    printf("     #                                                                 # \n");
    printf("      #################################################################  \n");
    printf("                                                                         \n");
    printf("                                                                         \n");


    // -> main routine

    // parse configuration file
    char path[ 1<<9 ]; GetCurrentDirectory( sizeof(path)-1, path);
    char absconfigfile[ 1<<10 ]; sprintf(absconfigfile, "%s\\%s", path, configfile);
    GetPrivateProfileString("PWListGen", "outputfile", "words.txt", outputfile, sizeof(outputfile)-1, absconfigfile);
    GetPrivateProfileString("PWListGen", "charset", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", charset, sizeof(charset)-1, absconfigfile);
    maxPWLength = GetPrivateProfileInt("PWListGen", "maxPWLength", 4, absconfigfile);
    cursorPosition = GetPrivateProfileInt("PWListGen", "minPWLength", 1, absconfigfile)-1;

    if (maxPWLength < cursorPosition+1) maxPWLength = cursorPosition+1;

    for (int x=0; x<=cursorPosition; x++)
        currentword[x] = charset[0]; // set startword

    // calculate estimated filesize
    /*double estfilesize = 0;
    for (int x=1; x <= maxPWLength; x++)
        estfilesize += (pow( strlen(charset), x) * (x+2));*/ // inaccurate

    for (int x=1; x <= maxPWLength; x++)
        estfilesize += pow64(strlen(charset), x) * (x+2); // combinations * length of combination and \r\n

    char estfilesizestr[ 1<<7 ] = "";
    if (estfilesize < 1024LL)
        sprintf(estfilesizestr, "%.2f Bytes", (double)estfilesize);
    else if (estfilesize < pow64(1024,2)) {
        sprintf(estfilesizestr, "%.2f kBytes", (double)estfilesize / 1024LL); }
    else if (estfilesize < pow64(1024,3))
        sprintf(estfilesizestr, "%.2f MBytes", (double)estfilesize / pow64(1024,2));
    else if (estfilesize < pow64(1024,4))
        sprintf(estfilesizestr, "%.2f GBytes", (double)estfilesize / pow64(1024,3));
    else if (estfilesize < pow64(1024,5))
        sprintf(estfilesizestr, "%.2f TBytes", (double)estfilesize / pow64(1024,4));
    else sprintf(estfilesizestr, "too big");

    // open filehandle
    if (fp = fopen(outputfile, "w"))
    {
        printf(" + Config:\n\n");
        printf("   -  output file = %.48s\n", outputfile);
        printf("   -  charset = [%.256s]\n", charset);
        printf("   -  min. word length = %d\n", cursorPosition+1);
        printf("   -  max. word length = %d\n", maxPWLength);
        printf(" \n");


        printf(" + Estimated file size: %.48s\n\n", estfilesizestr);

        printf(" + Opened %.48s successfully... Attempting to write now...\n\n", outputfile);

        // doing that generating process
        while (GeneratePosition(cursorPosition))
            cursorPosition++;

        // close filehandle
        fclose(fp);
        printf("\n\n + Done. \n\n");

        getchar();

    }


    return 0;
}

只需添加QP函数,在运行void Writeword(char*word)之前,检查currentword, 使用

有些代码是这样的

 bool QP(char A[],int n)  //delete duplicate 
{
   for(int i=0;i<n-1;i++)
    { 
        if(A[i+1]==A[i])
           return false;
    }
   return true;
}

void Writeword(char* word)
{
  if(QP(currentword,strlen(currentword)))
    {
     fprintf(fp, "%s\n", currentword);
     charswritten += strlen(word) + 2;
     Processbar();
    } 
}
boolqp(chara[],int n)//删除重复项
{

对于这是什么代码?一个Brutter的密码生成器?这个代码涉及C++,我添加了它,但没有用Brad和Lccu编译,可能需要在QueWord函数前面放置QP函数,我用MINW编译它。你需要再次检查你的代码。我下载了MINW并使用G++编译,它只给了我这个ER。ror:wordgen.cpp:在函数“void Writeword(char*)”:wordgen.cpp:79:9:错误:“currentword”未在此范围内声明OK我编译它;)但它与我在问题中提出的结果相同,没有更改您需要检查woid Writeword(char*word),是否添加了if(QP(currentword,strlen(currentword)))和“{}”而且,您可以在结果文本中搜索aa或bb。控制台中显示的大小没有更改,但副本已被删除。。
if(QP(currentword,strlen(currentword))) 
 bool QP(char A[],int n)  //delete duplicate 
{
   for(int i=0;i<n-1;i++)
    { 
        if(A[i+1]==A[i])
           return false;
    }
   return true;
}

void Writeword(char* word)
{
  if(QP(currentword,strlen(currentword)))
    {
     fprintf(fp, "%s\n", currentword);
     charswritten += strlen(word) + 2;
     Processbar();
    } 
}