📄 dlg_replace.cpp
字号:
// Dlg_Replace.cpp : implementation file
//
#include "stdafx.h"
#include "Decode.h"
#include "Dlg_Replace.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Dlg_Replace dialog
#define Max 99999999
char **cipher;
char result[26],answer[26],solution[26];
int Success,Apha1,apha1[26],All,*lrst,*An,**Aa,table1[26],bear1[26],ciph[200];
bool first;
//ciph[i]表示1表示已搜索,1表示未搜索;
Dlg_Replace::Dlg_Replace(CWnd* pParent /*=NULL*/)
: CDialog(Dlg_Replace::IDD, pParent)
{
//{{AFX_DATA_INIT(Dlg_Replace)
m_s = 0;
m_cfpath = _T("");
m_sc = _T("");
m_sp = _T("");
m_time = 0;
m_pfpath = _T("");
m_begin = _T("");
m_end = _T("");
//}}AFX_DATA_INIT
}
BOOL Dlg_Replace::OnInitDialog()
{
int i;
CDialog::OnInitDialog();
Dict.Setdict();
UpdateData(true);
for(i=0;i<25;i++)
{
m_begin+='A'+i;
m_begin+=" ";
m_end+='*';
m_end+=" ";
}
m_begin+='A'+i;
m_end+='*';
UpdateData(false);
return TRUE;
}
void Dlg_Replace::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Dlg_Replace)
DDX_Control(pDX, IDC_STATIC_String, m_string);
DDX_Control(pDX, IDC_STATIC_SP, m_ssp);
DDX_Control(pDX, IDC_STATIC_SC, m_ssc);
DDX_Control(pDX, IDC_STATIC_FP, m_fp);
DDX_Control(pDX, IDC_STATIC_File, m_file);
DDX_Control(pDX, IDC_STATIC_FC, m_fc);
DDX_Control(pDX, IDC_PLAIN_EDIT, m_PFP);
DDX_Control(pDX, IDC_OPEN_PLAIN_BUTTON, m_openp);
DDX_Control(pDX, IDC_OPEN_CIPHER_BUTTON, m_openc);
DDX_Control(pDX, IDC_CIPHER_EDIT, m_CFP);
DDX_Control(pDX, IDC_EDIT_SP, m_SP);
DDX_Control(pDX, IDC_EDIT_SC, m_SC);
DDX_Radio(pDX, IDC_RADIO_String, m_s);
DDX_Text(pDX, IDC_CIPHER_EDIT, m_cfpath);
DDX_Text(pDX, IDC_EDIT_SC, m_sc);
DDX_Text(pDX, IDC_EDIT_SP, m_sp);
DDX_Text(pDX, IDC_EDIT_Time, m_time);
DDX_Text(pDX, IDC_PLAIN_EDIT, m_pfpath);
DDX_Text(pDX, IDC_EDIT_BEGIN, m_begin);
DDX_Text(pDX, IDC_EDIT_END, m_end);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(Dlg_Replace, CDialog)
//{{AFX_MSG_MAP(Dlg_Replace)
ON_BN_CLICKED(IDC_BUTTON_Exit, OnBUTTONExit)
ON_BN_CLICKED(IDC_RADIO_String, OnRADIOString)
ON_BN_CLICKED(IDC_RADIO_File, OnRADIOFile)
ON_BN_CLICKED(IDC_DECIPHER_BUTTON, OnDecipherButton)
ON_BN_CLICKED(IDC_OPEN_CIPHER_BUTTON, OnOpenCipherButton)
ON_BN_CLICKED(IDC_OPEN_PLAIN_BUTTON, OnOpenPlainButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Dlg_Replace message handlers
void Dlg_Replace::OnBUTTONExit()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void Dlg_Replace::OnRADIOString()
{
// TODO: Add your control notification handler code here
m_s=0;
m_ssc.EnableWindow(true);
m_ssp.EnableWindow(true);
m_SC.EnableWindow(true);
m_SP.EnableWindow(true);
m_string.EnableWindow(true);
m_file.EnableWindow(false);
m_PFP.EnableWindow(false);
m_CFP.EnableWindow(false);
m_fp.EnableWindow(false);
m_fc.EnableWindow(false);
m_openc.EnableWindow(false);
m_openp.EnableWindow(false);
UpdateData(true);
m_pfpath="";
m_cfpath="";
UpdateData(false);
}
void Dlg_Replace::OnRADIOFile()
{
// TODO: Add your control notification handler code here
m_s=1;
m_ssc.EnableWindow(false);
m_ssp.EnableWindow(false);
m_SC.EnableWindow(false);
m_SP.EnableWindow(false);
m_string.EnableWindow(false);
m_file.EnableWindow(true);
m_PFP.EnableWindow(true);
m_CFP.EnableWindow(true);
m_fp.EnableWindow(true);
m_fc.EnableWindow(true);
m_openc.EnableWindow(true);
m_openp.EnableWindow(true);
UpdateData(true);
m_sp="";
m_sc="";
UpdateData(false);
}
bool Dlg_Replace::Possible(int lr,char * a,char * b)
//判断是否有可能成为密文单词的明文单词
{
int i,j;
for(i=1;i<lr;i++){
for(j=0;j<i;j++)
if((a[i]==a[j]&&b[i]!=b[j])||(a[i]!=a[j]&&b[i]==b[j]))
return false;
else
;
}
return true;
}
void Dlg_Replace::PSearch(int st)//寻找每个密文单词在字典中找到的可能的对应词
{
int i,lr,k=0;
lr=strlen(cipher[st]);
for(i=0;i<Dict.num[lr-1];i++)
if(Possible(lr,cipher[st],Dict.c[lr-1][i]))
{
An[st]++;
Aa[st][k++]=i;
}
}
bool Dlg_Replace::Test(int lr,char * a,char * b)
{
int i;
for(i=0;i<lr;i++){
if((table1[a[i]-'a']!=-1&&table1[a[i]-'a']!=b[i]-'a')||
(table1[a[i]-'a']==-1&&bear1[b[i]-'a']==1))
return false;
}
return true;
}
int Dlg_Replace::Select()
{
int i,j,k,min=Max,next=0,num1,num2;
for(i=0;i<All;i++)
{
if(ciph[i]==0)
{
num1=0;
for(k=0;k<26;k++){
if(apha1[k]==0){
for(j=0;j<lrst[i];j++)
{
if((cipher[i][j]-'a')==k){
num1++;
break;
}
}
}
}
num2=(Apha1-num1)*100000+An[i];
if(num2<min)
{
min=num2;
next=i;
}
}
}
return next;
}
void Dlg_Replace::Search(int deep)//deep表示搜索深度
{
if(Success>=200)
return ;
int i;
if(deep>=All){
Success++;
for(i=0;i<26;i++)
{
if(table1[i]!=-1)
result[table1[i]]='A'+i;
}
if(first){
for(i=0;i<26;i++){
answer[i]=result[i];
solution[i]=table1[i];
}
first=false;
}
memset(result,'*',sizeof(result));
return ;
}
int next,lr,j,k,table2[26],bear2[26],apha2[26],Apha2,num;//几种可能
bool fear;
for(j=0;j<26;j++){
table2[j]=table1[j];
bear2[j]=bear1[j];
apha2[j]=apha1[j];
}
Apha2=Apha1;
next=Select();
ciph[next]=1;
for(i=0;i<An[next];i++)
{
k=Aa[next][i];
lr=lrst[next];
fear=Test(lr,cipher[next],Dict.c[lr-1][k]);
if(fear){
num=0;
for(j=0;j<lrst[next];j++){
if(table1[cipher[next][j]-'a']==-1)
{
table1[cipher[next][j]-'a']=Dict.c[lr-1][k][j]-'a';
bear1[Dict.c[lr-1][k][j]-'a']=1;
}
if(apha1[cipher[next][j]-'a']==0){
apha1[cipher[next][j]-'a']=1;
num++;
}
}
for(k=0;k<26;k++){
if(apha1[k]==0){
for(j=0;j<lrst[next];j++)
{
if(cipher[next][j]-'a'==k){
apha1[k]=1;
num++;
}
break;
}
}
}
Apha1-=num;
Search(deep+1);
//回朔
for(j=0;j<26;j++){
table1[j]=table2[j];
bear1[j]=bear2[j];
apha1[j]=apha2[j];
}
Apha1=Apha2;
}
}
ciph[next]=0;
return ;
}
void Dlg_Replace::OnDecipherButton()
{
// TODO: Add your control notification handler code here
int lr,i,j,n,t,s=0,k=0;
bool fear;
char **x;
clock_t start,finish;
start=clock();
x=new char * [100];
cipher=new char * [100];
for(i=0;i<100;i++){
x[i]=new char[20];
cipher[i]=new char[20];
}
UpdateData(true);
m_cipher="";
if(m_s==0){
m_plain="";
m_cipher=m_sc;
}
else
{
if(!file.Fread(m_cipher,m_cfpath))
return;
}
i=0;
m_plain="";
n=strlen(m_cipher);
All=0;
Apha1=0;
memset(table1,-1,sizeof(table1));
memset(result,'*',sizeof(result));
memset(answer,'*',sizeof(answer));
memset(bear1,0,sizeof(bear1));
memset(ciph,0,sizeof(ciph));
memset(apha1,-1,sizeof(apha1));
while(i<n) //从输入文本中分解出单词
{
if(fear&&isalpha(m_cipher[i])){
All++;
x[k][s]='\0';
s=0;
k++;
strcpy(x[k],"");
x[k][s]=tolower(m_cipher[i]);
s++;
fear=false;
}
else if(isalpha(m_cipher[i])){
x[k][s]=tolower(m_cipher[i]);
s++;
}
else{
fear=true;
}
i++;
}
x[k][s]='\0';
t=All;
for(i=0;i<All;i++)
strcpy(cipher[i],x[i+1]);
An=new int [All+1];
lrst=new int [All+1];
Aa=new int * [All+1];
for(i=0;i<All;i++)
{
char ch=cipher[i][0];
An[i]=0;
lr=strlen(cipher[i]);
Aa[i]=new int [Dict.num[lr-1]];
for(j=0;j<Dict.num[lr-1];j++)
Aa[i][j]=0;
}
k=0;
for(i=0;i<All;i++){
lrst[i]=strlen(cipher[i]);
for(j=0;j<lrst[i];j++)
{
if(apha1[cipher[i][j]-'a']==-1){
Apha1++;
k++;
apha1[cipher[i][j]-'a']=0;
}
}
PSearch(i);
}
first=true;
Success=0;
Search(0);
m_end="";
for(i=0;i<25;i++)
{
m_end+=answer[i];
m_end+=" ";
}
m_end+=answer[i];
if(Success==0)
AfxMessageBox("所给密文无相应明文!");
if(Success>1){
CString str,nstr;
str+="存在";
nstr.Format("%d",Success);
str+=nstr;
str+="组解!";
AfxMessageBox(str);
}
if(Success>=1)
{
for(i=0;i<=n-1;i++)
{
if(m_cipher[i]>='a'&&m_cipher[i]<='z')
m_plain+='a'+solution[m_cipher[i]-'a'];
else if(m_cipher[i]>='A'&&m_cipher[i]<='Z')
m_plain+='A'+solution[m_cipher[i]-'A'];
else
m_plain+=m_cipher[i];
}
if(m_s==0)
{
m_sp=m_plain;
}
else
{
if(!file.Fwrite(m_plain,m_pfpath))
return;
}
}
finish=clock();
m_time=finish-start;
UpdateData(false);
}
void Dlg_Replace::OnOpenCipherButton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_cfpath);
UpdateData(false);
}
void Dlg_Replace::OnOpenPlainButton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_pfpath);
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -