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

📄 aidlg.cpp

📁 1、采用(1)盲目搜索(2)启发式搜索对凯撒密文进行自动解密。 2、对替代法加密的程序进行解密(未完成
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AIDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AI.h"
#include "AIDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

//
#include <iostream.h>
#include <fstream.h>
ifstream fin("input.in");
ofstream fout("output.out");
#include"time.h"
clock_t start,finish;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAIDlg dialog

CAIDlg::CAIDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAIDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAIDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAIDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAIDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAIDlg, CDialog)
	//{{AFX_MSG_MAP(CAIDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAIDlg message handlers

BOOL CAIDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization her
	
	//CStringList plist = new CStringList;
	//int iNumber,len;
	//iNumber=0;
	CString str;
	char ch[28];
	int len;
	//int kkkk=0;
	while( !fin.eof() )
	{	
		fin.getline(ch,28);
		str=ch;
		len=str.GetLength();
		//if(len>kkkk)
		//{
		//	kkkk=len;
	//		fout<<kkkk<<"----"<<str<<endl;
		//}
		str.MakeUpper();
		plist[len].AddTail(str);
		//fout<<str<<endl;
		//iNumber++;
		memset(ch,0,28);
	}
	//fout<<kkkk<<endl;
	str ="班级: 计算机3班                  ";
	str+="学号: 030300532                  ";
	str+="姓名: 刘友辉                     ";
	str+="1、代入法启发式搜索,执行会出错。";
	str+="故屏蔽掉了。                     ";
	str+="2、文件夹中有“凯撒测试数据.txt”";
	str+="3、词库“input.in”中包含约30万个单词。输入的格式可以任意的,但是不支持A.M.和P.M.。";
	str+="                                 ";
	str+="                 郁闷啊! 代入法不知道错在哪里!";
	GetDlgItem(IDC_EDIT5)->SetWindowText(str);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAIDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAIDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

		/*
		for(pos = pWordList.GetHeadPosition();pos != NULL;pWordList.GetNext(pos))
		{
			temp=pWordList.GetAt(pos);
			fout<<temp<<endl;
		}
		fout<<"ok"<<endl;
		*/
//测试 bcpvu
void CAIDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	//csTime="";
	POSITION pos;
	//clock_t start=0,finish=0;
	int i,isNotFind=1;
	int add;
	CString temp,cstime="";
	CString  csCharacterMap,csShowMingwen;
	if(GetWord())
	{
		start=clock();
		for(i=1;i<=25&&isNotFind==1;i++)
		{
			KaiSa(i);
			for(pos = pKaiSaList.GetHeadPosition();pos != NULL;pKaiSaList.GetNext(pos))
			{
				temp=pKaiSaList.GetAt(pos);
				if( FindWord(temp) ){
					//fout<<temp<<"----ok"<<endl;
					isNotFind=0;
					continue;}
				else{
					//fout<<temp<<"----fault"<<endl;
					isNotFind=1;
					break;}
			}
		}
		if(isNotFind){
			MessageBox("单词信息不足!");
		}
		else{
			finish=clock();
			int fTime=finish-start;
			cstime.Format("%d%s",fTime," ms");
			GetDlgItem(IDC_EDIT4)->SetWindowText(cstime);
			//输出明文
			add=i-1;//必须减去1
			csShowMingwen=ShowMingwen(add);
			GetDlgItem(IDC_EDIT2)->SetWindowText(csShowMingwen);
			//关系
			csCharacterMap=CharacterMap(add);
			GetDlgItem(IDC_EDIT3)->SetWindowText(csCharacterMap);
		}
		pWordList.RemoveAll();
		pKaiSaList.RemoveAll();
	}
}

void CAIDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	//clock_t start,finish;
	start=clock();
	CString str,cstime="",csCharacterMap;
	CString ch("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	GetDlgItem(IDC_EDIT1)->GetWindowText(str);
	str.MakeUpper();
	int length=str.GetLength();
	int i,j=0,e;
	int iWordTime[26];
	memset(iWordTime,0,26);
	for(i=0;i<length;i++){
		if(str[i]<='Z'&&str[i]>='A'){
			j=str[i]-'A';
			iWordTime[j]++;
		}
	}
	/*
	//测试 bcpvu	
	int add;
	e=Max(iWordTime);
	fout<<e<<endl;
	add=(ch[e]-'E'+26)%26;
	CString kk;
	kk.Format("%d",add);
	GetDlgItem(IDC_EDIT2)->SetWindowText(kk);
	*/
	
	int add;
	CString csShowMingwen,temp;
	POSITION pos;
	int isNotFind=1;
	if(GetWord())
	{
		while(isNotFind>0&&isNotFind<=26)
		{
			e=Max(iWordTime);
			add=(ch[e]-'E'+26)%26;
			KaiSa(add);
			for(pos = pKaiSaList.GetHeadPosition();pos != NULL;pKaiSaList.GetNext(pos))
			{
				temp=pKaiSaList.GetAt(pos);
				if( FindWord(temp) ){
					//fout<<temp<<"----ok"<<endl;
					isNotFind=0;
					continue;
				}
				else{
					//fout<<temp<<"----fault"<<endl;
					isNotFind+=1;
					break;
				}
			}
		}
		if(isNotFind){
			MessageBox("单词信息不足!");
		}
		else{
			//add不要减去1
			finish=clock();
			int fTime=finish-start;
			cstime.Format("%d%s",fTime," ms");
			GetDlgItem(IDC_EDIT4)->SetWindowText(cstime);
			//不要减去1
			csShowMingwen=ShowMingwen(add);
			GetDlgItem(IDC_EDIT2)->SetWindowText(csShowMingwen);
			csCharacterMap=CharacterMap(add);
			GetDlgItem(IDC_EDIT3)->SetWindowText(csCharacterMap);
		}
		pWordList.RemoveAll();
		pKaiSaList.RemoveAll();
	}
}




int CAIDlg::GetWord()
{
	CString str, str_temp; 
	str_temp.Empty();
	GetDlgItem(IDC_EDIT1)->GetWindowText(str);
	int length=str.GetLength();
	if(length==0){
		MessageBox("请输入密文!");
		return 0;
	}
	else
	{
		for(int i=0;i<length;i++)
		{
			if( (str[i]<='z' && str[i]>='a') || (str[i]<='Z' && str[i]>='A') ){
				str_temp+=str[i];
			}
			else if(str[i]==' '||'\n'){
				if(!str_temp.IsEmpty()){
					pWordList.AddTail(str_temp);
					str_temp.Empty();
				}
			}
			else if(str[i]=='\''){
				while(++i<length)
				{
					if(str[i]==' '){
						if(!str_temp.IsEmpty()){
							if(TestWordLength(str_temp)){
							pWordList.AddTail(str_temp);
							str_temp.Empty();
							break;}
						}
					}
					else 
						continue;
				}
			}
			else
				continue;
		}
		if(!str_temp.IsEmpty()){
			if(TestWordLength(str_temp)){
			pWordList.AddTail(str_temp);
			str_temp.Empty();}
		}
		return 1;
	}
}
int CAIDlg::TestWordLength(CString str)
{
	int length=str.GetLength();
	if(length>30)
	{
		MessageBox("单词太长");
		return 0;
	}
	return 1;
}
int CAIDlg::FindWord(CString csTemp)
{
	POSITION pos;
	int i=csTemp.GetLength();
	for( pos = plist[i].GetHeadPosition(); pos != NULL; ) {
		if( csTemp.Compare(plist[i].GetAt(pos))==0 )
			return 1;
		else
			plist[i].GetNext( pos );
	}
	return 0;
}

int CAIDlg::KaiSa(int add)
{
	POSITION pos;
	CString temp,ok;
	int length=0;
	int i;
	if(!pKaiSaList.IsEmpty())
	{
		pKaiSaList.RemoveAll();
	}
	for( pos = pWordList.GetHeadPosition(); pos != NULL;pWordList.GetNext(pos)) {
		temp=pWordList.GetAt(pos);
		temp.MakeUpper();
		length=temp.GetLength();
		ok.Empty();
		for(i=0;i<length;i++)
			ok+=( ((temp[i]-'A')-add+26)%26 +'A');
		pKaiSaList.AddTail(ok);
	}
	if(!pKaiSaList.IsEmpty())
		return 1;
	else 
		return 0;
}

CString CAIDlg::CharacterMap(int add)
{
	//字符映射关系
	CString str("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	CString temp;
	for(int i=0;i<26;i++){
		//temp+=str[i];
		//
		temp+=(((str[i]-'A')+add+26)%26 +'A'); 
		temp+="--";
		//temp+="\n";
	}
	return temp;
}

CString  CAIDlg::ShowMingwen(int add)

⌨️ 快捷键说明

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