⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subdedlg.cpp

📁 1、对于凯撒密文
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					besti = i;
					bestj = j;
				}
			}
		}
		if( bestadj > bestscore )//当前分数大于最高分
		{ 
			c = dec[besti];//更改当前排列两个字符
			dec[besti] = dec[bestj];
			dec[bestj] = c;
			bestscore = bestadj;//刷新当前临时排列最高分
			if( bestscore > bestoverall )//临时排列最高分大于总的最高分
			{ 
				bestoverall = bestscore;//刷新总的最高分
				smallperturb = 1;
				CString temp;
				//temp.Format("\n%.2lf %s\n",bestoverall,dec);
				temp.Format("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c" , 
					dec[0] , dec[1] , dec[2] , dec[3] , dec[4] ,
					dec[5] , dec[6] , dec[7] , dec[8] , dec[9] ,
					dec[10] , dec[11] , dec[12] , dec[13] , dec[14] ,
					dec[15] , dec[16] , dec[17] , dec[18] , dec[19] ,
					dec[20] , dec[21] , dec[22] , dec[23] , dec[24] ,
					dec[25]);
				temp.MakeUpper();
				SetDlgItemText( (HWND)lpParam , IDC_SUB , temp );
				//UpdateWindow();
				//printf("\n%.2lf %s\n",bestoverall,dec);//输出此时总的最高分,以及对应排列
				/*for(i=0;i<bpt;i++)//输出此对应排列的"明文"
					printf("%c",dec[buffer[i]-'a']);
				printf("\n");*/
				for ( i = 0 ; i < length ; i ++ )
				{
					if((m_cipherString[i]>='a')&&(m_cipherString[i]<='z'))
						buffout[i]=dec[m_cipherString[i]-'a'];
					else if((m_cipherString[i]>='A')&&(m_cipherString[i]<='Z'))
						buffout[i]=dec[m_cipherString[i]-'A']-'a'+'A';
					else
						buffout[i]=m_cipherString[i];
				}
				m_plainString.Format( "%s" , buffout );
				SetDlgItemText((HWND)lpParam,IDC_PLAIN,m_plainString);
				::UpdateWindow((HWND)lpParam);
				isCorrect = CheckString(m_plainString);

			}
		}
		else
		{ 
			if(smallperturb)//如果前一轮有变化过
			{ 
				smalladj(dec);//小调整一下
				smallperturb = 0;
			}
			else
			{ 
				rndstr(dec);//重新利用'a'-'z'生成一个更乱的随机排列
				smallperturb = 1;
			}
			bestscore = score(dec);
		}
		
	}
	::MessageBox(hWnd,"恭喜恭喜,终于找到答案了!","^o^",MB_OK);
	return 0;
}
DWORD WINAPI DecipherH(LPVOID lpParam)
{
	::SetTimer(hWnd,3,60000,(TIMERPROC)TimerProc);
	clock_t start=clock();
	clock_t finish;
	CString times = _T("");
//	bool isCorrect = 0;
	time_t t;
	int i , j , besti , bestj , smallperturb;
	char c , dec[27] , testd[26];
	dec[26] = '\0';
	double bestscore , sc , bestadj , bestoverall;
	srand( time(&t) );//根据时间生成随机数起始点
	bpt = 0;
	char str[100000];
	::GetDlgItemText((HWND)lpParam,IDC_CIPHER,str,100000);
	CString m_cipherString = str;
	int length = m_cipherString.GetLength();
	m_cipherString.GetBuffer(length);
	for( i = 0 ; i < length ; i ++ )
/*		if( ( m_cipherString[i] >= 'a' ) && ( m_cipherString[i] <= 'z' ) )
			buffer[bpt++] = m_cipherString[i];
		else if( ( m_cipherString[i] >= 'A' ) && ( m_cipherString[i] <= 'Z' ) )
			buffer[bpt++] = m_cipherString[i] - 'A' + 'a';*/
		if ( isalpha(m_cipherString[i]) != 0 )
			buffer[bpt++] = tolower(m_cipherString[i]);
	buffer[bpt] = '\0';
	buffout[length] = '\0';
	//strrev(buffer);//the end of buffer
	/*printf("密文初始化\n %s\n",buffer);
	printf("Analysing crypt:\n%.2lf %s\n",score("abcdefghijklmnopqrstuvwxyz"),buffer);//初始得分*/
//	rndstr(dec);//random sort
	freqsort(dec);	//sort by frequency first
	bestscore = score(dec);//score the random pemutation
	bestoverall = bestscore-1;//总体最高分初始化
	smallperturb = 1;
	while(1)//while(!isCorrect)
	{
		finish = clock();
		times.Format("%1.2f",(finish-start)/1000.0);
		times += _T("s");
		::SetDlgItemText((HWND)lpParam,IDC_TIME,times);
		bestadj = bestscore;
		for( i = 0 ; i < 26 ; i ++ )
		{
			for( j = i+1 ; j < 26 ; j ++ )
			{ 
				strcpy( testd , dec );//拷贝排列
				c = testd[i];
				testd[i] = testd[j];
				testd[j] = c;
				sc = score(testd);//改两个字母替换后的分数
				if( sc > bestadj )
				{ 
					bestadj = sc;
					besti = i;
					bestj = j;
				}
			}
		}
		if( bestadj > bestscore )//当前分数大于最高分
		{ 
			c = dec[besti];//更改当前排列两个字符
			dec[besti] = dec[bestj];
			dec[bestj] = c;
			bestscore = bestadj;//刷新当前临时排列最高分
			if( bestscore > bestoverall )//临时排列最高分大于总的最高分
			{ 
				bestoverall = bestscore;//刷新总的最高分
				smallperturb = 1;
				CString temp;
				//temp.Format("\n%.2lf %s\n",bestoverall,dec);
				temp.Format("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c" , 
					dec[0] , dec[1] , dec[2] , dec[3] , dec[4] ,
					dec[5] , dec[6] , dec[7] , dec[8] , dec[9] ,
					dec[10] , dec[11] , dec[12] , dec[13] , dec[14] ,
					dec[15] , dec[16] , dec[17] , dec[18] , dec[19] ,
					dec[20] , dec[21] , dec[22] , dec[23] , dec[24] ,
					dec[25]);
				temp.MakeUpper();
				SetDlgItemText( (HWND)lpParam , IDC_SUB , temp );
				//UpdateWindow();
				//printf("\n%.2lf %s\n",bestoverall,dec);//输出此时总的最高分,以及对应排列
				/*for(i=0;i<bpt;i++)//输出此对应排列的"明文"
					printf("%c",dec[buffer[i]-'a']);
				printf("\n");*/
				for ( i = 0 ; i < length ; i ++ )
				{
					if((m_cipherString[i]>='a')&&(m_cipherString[i]<='z'))
						buffout[i]=dec[m_cipherString[i]-'a'];
					else if((m_cipherString[i]>='A')&&(m_cipherString[i]<='Z'))
						buffout[i]=dec[m_cipherString[i]-'A']-'a'+'A';
					else
						buffout[i]=m_cipherString[i];
				}
				m_plainString.Format( "%s" , buffout );
				SetDlgItemText((HWND)lpParam,IDC_PLAIN,m_plainString);
				::UpdateWindow((HWND)lpParam);
			//	isCorrect = CheckString(m_plainString);

			}
		}
		else
		{ 
			if(smallperturb)//如果前一轮有变化过
			{ 
				smalladj(dec);//小调整一下
				smallperturb = 0;
			}
			else
			{ 
				rndstr(dec);//重新利用'a'-'z'生成一个更乱的随机排列
				smallperturb = 1;
			}
			bestscore = score(dec);
		}
		
	}
//	::MessageBox(hWnd,"恭喜恭喜,终于找到答案了!","^o^",MB_OK);
	return 0;
}
//DEL void CSubDeDlg::DecipherH()
//DEL {
//DEL if ( 0 == m_iMethod )
//DEL 	{
//DEL 		if(hThread)
//DEL 			TerminateThread(hThread,dwThreadId);
//DEL 		hThread = CreateThread(
//DEL 			NULL,
//DEL 			0,
//DEL 			pppp,
//DEL 			this->GetSafeHwnd(),
//DEL 			0,
//DEL 			&dwThreadId);
//DEL 		if(hThread==NULL)
//DEL 			AfxMessageBox("创建线程失败!");
//DEL 		
//DEL 	}
//DEL }

void CSubDeDlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	if(hThread)
	{
		SuspendThread(hThread);
		::KillTimer(hWnd,3);
		CWnd* poWnd;
		poWnd = GetDlgItem(IDC_PAUSE);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_DECIPHER);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_RESUME);
		poWnd->EnableWindow(true);
		poWnd = GetDlgItem(IDC_STOP);
		poWnd->EnableWindow(true);
	}
	
}

void CSubDeDlg::OnResume() 
{
	// TODO: Add your control notification handler code here
	if(hThread)
	{
		ResumeThread(hThread);
		::SetTimer(hWnd,3,60000,(TIMERPROC)TimerProc);
		CWnd* poWnd;
		poWnd = GetDlgItem(IDC_PAUSE);
		poWnd->EnableWindow(true);
		poWnd = GetDlgItem(IDC_DECIPHER);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_RESUME);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_STOP);
		poWnd->EnableWindow(true);
	}
	
}

void CSubDeDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	if(hTetraThread)
		TerminateThread(hTetraThread,dwTetraThreadId);
	if(hThread)
		TerminateThread(hThread,dwThreadId);
}

void CSubDeDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	if(hThread)
	{
		TerminateThread(hThread,dwThreadId);
		CloseHandle(hThread);
		::KillTimer(hWnd,3);
		CWnd* poWnd;
		poWnd = GetDlgItem(IDC_DECIPHER);
		poWnd->EnableWindow(true);
		poWnd = GetDlgItem(IDC_STOP);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_PAUSE);
		poWnd->EnableWindow(false);
		poWnd = GetDlgItem(IDC_RESUME);
		poWnd->EnableWindow(false);
	}
	
}

bool CheckString(CString &str)
{
	CString strRes;
	int iStart = 0,iEnd =0, iLen=0, iStrLen;
	bool isCorrect = true;
	iStrLen = str.GetLength();
	while ( iEnd < iStrLen )
	{
		while ( iStart < iStrLen 
			&& !( ( str.GetAt(iStart) >= 'a' && str.GetAt(iStart) <= 'z' )
			|| ( str.GetAt(iStart) >= 'A' && str.GetAt(iStart) <= 'Z' ) ) )
			iStart ++;
		if (iStart == iStrLen)
			break;
		iEnd = iStart;
		while ( iEnd < iStrLen
			&& ( ( str.GetAt(iEnd) >= 'a' && str.GetAt(iEnd) <= 'z' )
			|| ( str.GetAt(iEnd) >= 'A' && str.GetAt(iEnd) <= 'Z' ) ) )
			iEnd ++;
		strRes = str.Mid(iStart,iEnd-iStart); //Extract the word
		if(m_pDictionary->IsWordListed(strRes)==3 
			|| m_pDictionary->IsWordListed(strRes)==4)		//check for spelling
		{
			isCorrect = false;
			break;				//error
		}
		iStart = iEnd + 1;
	}
	return isCorrect;
}

VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
	if ( 3 == idEvent )
	{
		if(hTimerThread)
		{
			SuspendThread(hTimerThread);
			HWND poWnd;
			poWnd = ::GetDlgItem(hWnd,IDC_PAUSE);
			::EnableWindow(poWnd,false);
			poWnd = ::GetDlgItem(hWnd,IDC_DECIPHER);
			::EnableWindow(poWnd,false);
			poWnd = ::GetDlgItem(hWnd,IDC_RESUME);
			::EnableWindow(poWnd,true);
			poWnd = ::GetDlgItem(hWnd,IDC_STOP);
			::EnableWindow(poWnd,true);
			::KillTimer(hWnd,3);
			::KillTimer(hWnd,4);
			::MessageBox(
				hWnd,
				"真是不好意思,都过了这么久了。。。\n还要继续吗?\n要继续的话请点  继续  按钮。。。\n希望一分钟后你不会再次看到这个对话框。。。",
				"Sorry...:-(",
				MB_OK
				);
		}
	}
}

BOOL CSubDeDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	if ( hThread )
		CloseHandle(hThread);
	return CDialog::DestroyWindow();
}

BOOL CSubDeDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if ( pWnd == GetDlgItem(IDC_PAUSE) 
		|| pWnd == GetDlgItem(IDC_DECIPHER)
		|| pWnd == GetDlgItem(IDC_RESUME)
		|| pWnd == GetDlgItem(IDC_STOP)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_CIPHER)
		|| pWnd == GetDlgItem(IDC_PLAIN)
		|| pWnd == GetDlgItem(IDC_PLAIN_FILE)
		|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
		|| pWnd == GetDlgItem(IDC_KEY)
		)
		::SetCursor(m_hBeam);
	else if ( pWnd == GetDlgItem(IDC_RADIO_P)
		|| pWnd == GetDlgItem(IDC_RADIO_H)
		)
		::SetCursor(m_hPen);
	else if ( nHitTest == HTCAPTION )
		::SetCursor(m_hMove);
	else
		::SetCursor(m_hArrow);
	return true;
	//return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

BOOL CSubDeDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.	{		// Let the ToolTip process this message.		m_tooltip.RelayEvent(pMsg);	}	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -