- #include "stdafx.h"
- #include "YuyinTree.h"
- #include "YuyinTreeDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
-
- CStoredPinyin* m_storedPinyin[30000];
- CWordTree* curTree=new CWordTree;
- CString MaxLenghci='/'';
- CString Tempci;
- int StoredNum=0;
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
-
-
- enum { IDD = IDD_ABOUTBOX };
-
-
-
-
- protected:
- virtual void DoDataExchange(CDataExchange* pDX);
-
-
-
- protected:
-
-
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
-
-
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
-
-
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
-
-
-
- END_MESSAGE_MAP()
-
-
-
-
- CYuyinTreeDlg::CYuyinTreeDlg(CWnd* pParent )
- : CDialog(CYuyinTreeDlg::IDD, pParent)
- {
-
- m_inputTongyin = _T("");
-
-
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CYuyinTreeDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
-
- DDX_Text(pDX, IDC_PINYIN_EQUAL, m_inputTongyin);
-
- }
-
- BEGIN_MESSAGE_MAP(CYuyinTreeDlg, CDialog)
-
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BTN_READFILE, OnBtnReadfile)
- ON_BN_CLICKED(IDC_BTN_TEST_PANDCHI, OnBtnTestPandchi)
- ON_BN_CLICKED(IDC_BTN_TEST_TONGYINCI, OnBtnTestTongyinci)
- ON_BN_CLICKED(IDC_YUYIN_QUERY, OnTongyinQuery)
- ON_NOTIFY(TVN_SELCHANGED, IDC_YUYIN_TREE, OnSelchangedYuyinTree)
-
- END_MESSAGE_MAP()
-
-
-
-
- BOOL CYuyinTreeDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
-
-
-
- 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);
- }
- }
-
-
-
- SetIcon(m_hIcon, TRUE);
- SetIcon(m_hIcon, FALSE);
-
-
-
-
-
- for(int i=0;i<30000;i++)
- {
- m_storedPinyin[i]=NULL;
- }
- FILE *InputFile=NULL;
- CTreeNode* CurNode=NULL;
- CTreeNode* FatherNode=NULL;
- TCHAR NodeItem[100];
- TCHAR NodePinyin[51];
- TCHAR NodeHanzi[50];
- int ZiMuNum,j,k;
- int WordNum=0;
- BOOL IsFirst=TRUE;
- CTreeNode* TempfatherNode=NULL;
- CTreeNode* TempfatherNode1=NULL;
- InputFile=fopen("LexiconTree.txt","r");
-
- while(!feof(InputFile))
- {
- WordNum++;
- fscanf(InputFile,"%s",NodeItem);
- ZiMuNum=0;
- while(NodeItem[ZiMuNum]!=',')
- {
- NodeHanzi[ZiMuNum]=NodeItem[ZiMuNum];
- ZiMuNum++;
- }
- NodeHanzi[ZiMuNum]='/0';
- ZiMuNum++;
-
-
- while(NodeItem[ZiMuNum]!=',')
- {
- ZiMuNum++;
- }
- ZiMuNum++;
- j=ZiMuNum;
- k=0;
- while(NodeItem[j]!='/0')
- {
- NodePinyin[k]=NodeItem[j];
- k++;
- j++;
- }
- NodePinyin[k-1]='/0';
- CString TempString;
- TempString=NodePinyin;
-
- CurNode=new CTreeNode;
- CurNode->m_Pinyin=TempString;
- CurNode->m_Word=NodeHanzi;
- CurNode->m_pParent=NULL;
- CurNode->m_pchild=NULL;
- CurNode->m_pneighbour=NULL;
- CurNode->m_pchar=NULL;
- CurNode->m_CurNum=0;
- BOOL Fviewed=FALSE;
- BOOL FParent=FALSE;
- BOOL FEqual=FALSE;
- for(int temp=0;temp<30000;temp++)
- {
- if(m_storedPinyin[temp]!=NULL)
- {
- int t=-1;
- t=TempString.Find(m_storedPinyin[temp]->m_StoredPinyin);
- if(t>=0)
- {
-
- if(TempString==m_storedPinyin[temp]->m_StoredPinyin)
- {
- Fviewed=TRUE;
- FEqual=TRUE;
- int TempCurNum=(m_storedPinyin[temp]->m_pcurPosition)->m_CurNum;
-
- CTongyinci* temp1=new CTongyinci;
- temp1->next=NULL;
- temp1->m_data=new char[50];
- strcpy(temp1->m_data,NodeHanzi);
-
- CTongyinci* temp2=(m_storedPinyin[temp]->m_pcurPosition)->m_pchar;
- CTongyinci* pre;
- while(temp2!=NULL)
- {
- pre=temp2;
- temp2=temp2->next;
- }
- pre->next=temp1;
-
- (m_storedPinyin[temp]->m_pcurPosition)->m_CurNum=(m_storedPinyin[temp]->m_pcurPosition)->m_CurNum+1;
- break;
- }
- else if(t==0)
- {
-
-
- Tempci=m_storedPinyin[temp]->m_StoredPinyin;
- int Strsize=Tempci.GetLength();
-
- if(TempString.GetAt(Strsize)==',')
- {
- FParent=TRUE;
- if(Strsize>=MaxLenghci.GetLength())
- {
- MaxLenghci=Tempci;
- TempfatherNode1=m_storedPinyin[temp]->m_pcurPosition;
- }
-
- Fviewed=TRUE;
- }
-
- }
-
- }
- }
- else
- continue;
- }
- if(FParent&&!FEqual)
- {
- CurNode->m_CurNum++;
-
-
-
-
-
-
-
-
- CTongyinci* temp=new CTongyinci;
- temp->next=NULL;
-
- temp->m_data=new char[50];
- strcpy(temp->m_data,NodeHanzi);
- CurNode->m_pchar=temp;
-
- curTree->InsertNode(&CurNode,&TempfatherNode1);
-
- CStoredPinyin* TempStorPinyin1=new CStoredPinyin;
-
- TempStorPinyin1->m_pcurPosition=CurNode;
- TempStorPinyin1->m_StoredPinyin=TempString;
- TempStorPinyin1->m_pchar=temp;
- m_storedPinyin[StoredNum++]=TempStorPinyin1;
- }
- MaxLenghci='/0';
- TempfatherNode1=NULL;
-
- if(!Fviewed)
- {
-
-
- if(IsFirst==TRUE)
- {
- TempfatherNode=curTree->m_Root;
- }
-
- CurNode->m_CurNum++;
-
-
-
-
-
-
-
-
-
-
-
-
-
- CTongyinci* temp=new CTongyinci;
- temp->next=NULL;
- temp->m_data=new char[50];
- strcpy(temp->m_data,NodeHanzi);
- CurNode->m_pchar=temp;
-
- TempfatherNode=curTree->InsertFirstNode(&CurNode,&TempfatherNode);
- IsFirst=FALSE;
-
- CStoredPinyin* TempStorPinyin;
- TempStorPinyin=new CStoredPinyin;
-
- TempStorPinyin->m_pcurPosition=CurNode;
- TempStorPinyin->m_StoredPinyin=NodePinyin;
- TempStorPinyin->m_pchar=temp;
-
-
- m_storedPinyin[StoredNum]=TempStorPinyin;
- StoredNum++;
- }
-
- }
- char buffer[50];
- sprintf(buffer,"已读取单词: %d",WordNum);
-
- AfxMessageBox(buffer);
- BrowseYuyin();
-
- return TRUE;
- }
-
- void CYuyinTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
-
-
-
-
- void CYuyinTreeDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this);
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
-
- 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;
-
-
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
-
-
- HCURSOR CYuyinTreeDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
-
-
- void CYuyinTreeDlg::OnBtnReadfile()
- {
-
- CTreeNode* root=curTree->m_Root;
- CTreeNode* Temp=root->m_pchild;
- while(Temp!=NULL)
- {
- AfxMessageBox(Temp->m_Pinyin);
- Temp=Temp->m_pneighbour;
- }
- }
-
-
-
- CWordTree::CWordTree()
- {
- m_Root=new CTreeNode;
- m_Root->m_Pinyin="";
- m_Root->m_pParent=NULL;
- m_Root->m_pchild=NULL;
- m_Root->m_pneighbour=NULL;
- m_Root->m_CurNum=0;
- m_Root->m_Word="";
- m_Root->m_pchar=NULL;
- }
-
- void CWordTree::CreateTree()
- {
-
- }
- CTreeNode* CWordTree::InsertFirstNode(CTreeNode** newNode,CTreeNode** parent)
- {
-
- CTreeNode* WilladNode=*newNode;
- CTreeNode* FatherNode=*parent;
- if(FatherNode==curTree->m_Root)
- {
- FatherNode->m_pchild=WilladNode;
- WilladNode->m_pParent=curTree->m_Root;
- }
- else
- {
- FatherNode->m_pneighbour=WilladNode;
- WilladNode->m_pParent=curTree->m_Root;
- }
-
- return WilladNode;
- }
- void CWordTree::InsertNode(CTreeNode** newNode,CTreeNode** parent)
- {
-
- CTreeNode* WilladNode=*newNode;
- CTreeNode* FatherNode=*parent;
- CTreeNode* Temp=NULL;
-
- if(FatherNode->m_pchild==NULL)
- {
- FatherNode->m_pchild=WilladNode;
- WilladNode->m_pParent=FatherNode;
- }
- else
- {
- Temp=FatherNode->m_pchild;
- while(Temp->m_pneighbour!=NULL)
- {
- Temp=Temp->m_pneighbour;
- }
- Temp->m_pneighbour=WilladNode;
- WilladNode->m_pParent=FatherNode;
- }
-
- }
-
- void CYuyinTreeDlg::OnBtnTestPandchi()
- {
- CTreeNode* root=curTree->m_Root;
- CTreeNode* Temp=root->m_pchild;
- AfxMessageBox(Temp->m_Pinyin);
- Temp=Temp->m_pchild;
- while(Temp!=NULL)
- {
- AfxMessageBox(Temp->m_Pinyin);
- Temp=Temp->m_pneighbour;
- }
- }
-
- void CYuyinTreeDlg::OnBtnTestTongyinci()
- {
- CTreeNode* NodeA=curTree->m_Root->m_pchild;
- CTongyinci* temp=NodeA->m_pchar;
- while(temp!=NULL)
- {
- AfxMessageBox(temp->m_data);
- temp=temp->next;
- }
-
- }
- void CYuyinTreeDlg::BrowseYuyin()
- {
- CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_YUYIN_TREE);
- ASSERT(pCtrl != NULL);
-
- TVINSERTSTRUCT tvInsert;
- tvInsert.hParent = NULL;
- tvInsert.hInsertAfter = NULL;
- tvInsert.item.mask = TVIF_TEXT;
- tvInsert.item.pszText = _T("词拼音");
-
- HTREEITEM hRoot = pCtrl->InsertItem(&tvInsert);
-
- CTreeNode* root=curTree->m_Root;
- CTreeNode* Temp=root->m_pchild;
-
- HTREEITEM hPA=NULL;
- HTREEITEM hPB=NULL;
- HTREEITEM hPC=NULL;
- while(Temp!=NULL)
- {
- hPA=pCtrl->InsertItem(TVIF_TEXT,
- _T(Temp->m_Pinyin), 0, 0, 0, 0, 0, hRoot, NULL);
- CTreeNode* TempNode1=Temp->m_pchild;
- while(TempNode1!=NULL)
- {
- hPB=pCtrl->InsertItem(TVIF_TEXT,
- _T(TempNode1->m_Pinyin), 0, 0, 0, 0, 0, hPA, NULL);
- CTreeNode* TempNode2=TempNode1->m_pchild;
- while(TempNode2!=NULL)
- {
- hPC=pCtrl->InsertItem(TVIF_TEXT,
- _T(TempNode2->m_Pinyin), 0, 0, 0, 0, 0, hPB, NULL);
- CTreeNode* TempNode3=TempNode2->m_pchild;
- while(TempNode3!=NULL)
- {
- pCtrl->InsertItem(TVIF_TEXT,
- _T(TempNode3->m_Pinyin), 0, 0, 0, 0, 0, hPC, NULL);
- TempNode3=TempNode3->m_pneighbour;
- }
- TempNode2=TempNode2->m_pneighbour;
- }
- TempNode1=TempNode1->m_pneighbour;
- }
- Temp=Temp->m_pneighbour;
- }
-
- }
-
- void CYuyinTreeDlg::OnTongyinQuery()
- {
-
-
- UpdateData(TRUE);
- CString StringWilqury=m_inputTongyin;
- CString Tempci;
- BOOL FExit=FALSE;
- int TempNum;
- CComboBox* tongyinc=(CComboBox*)GetDlgItem(IDC_PINYIN_RESULT);
- ASSERT(tongyinc != NULL);
- tongyinc->ResetContent();
- ASSERT(tongyinc->GetCount() == 0);
-
-
- for(int temp=0;temp<30000;temp++)
- {
- if(m_storedPinyin[temp]!=NULL&&m_storedPinyin[temp]->m_StoredPinyin==StringWilqury)
- {
- TempNum=m_storedPinyin[temp]->m_pcurPosition->m_CurNum;
-
-
-
-
- CTongyinci* temp1=m_storedPinyin[temp]->m_pcurPosition->m_pchar;
- while(temp1!=NULL)
- {
- Tempci=temp1->m_data;
- temp1=temp1->next;
- tongyinc->AddString(Tempci);
- }
-
-
- tongyinc->SetWindowText(m_storedPinyin[temp]->m_pcurPosition->m_pchar->m_data);
- FExit=TRUE;
- }
- }
- if(!FExit)
- tongyinc->SetWindowText("该拼音在树中不存在!");
-
- }
-
-
-
- void CYuyinTreeDlg::OnSelchangedYuyinTree(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
-
-
- TV_ITEM item=pNMTreeView->itemNew;
- HTREEITEM hItem=item.hItem;
- CTreeCtrl* pCtrl = (CTreeCtrl*) GetDlgItem(IDC_YUYIN_TREE);
- ASSERT(pCtrl != NULL);
- CString Tempci=pCtrl->GetItemText(hItem);
- if(Tempci=="词拼音")
- return;
-
-
- BOOL FExit=FALSE;
- int TempNum;
- CComboBox* tongyinc=(CComboBox*)GetDlgItem(IDC_PINYIN_RESULT);
- ASSERT(tongyinc != NULL);
- tongyinc->ResetContent();
- ASSERT(tongyinc->GetCount() == 0);
-
-
- for(int temp=0;temp<30000;temp++)
- {
- if(m_storedPinyin[temp]!=NULL&&m_storedPinyin[temp]->m_StoredPinyin==Tempci)
- {
- TempNum=m_storedPinyin[temp]->m_pcurPosition->m_CurNum;
-
-
-
-
- CTongyinci* temp1=m_storedPinyin[temp]->m_pcurPosition->m_pchar;
- while(temp1!=NULL)
- {
- Tempci=temp1->m_data;
- temp1=temp1->next;
- tongyinc->AddString(Tempci);
- }
-
- tongyinc->SetWindowText(m_storedPinyin[temp]->m_pcurPosition->m_pchar->m_data);
- FExit=TRUE;
- }
- }
- if(!FExit)
- tongyinc->SetWindowText("该拼音在树中不存在!");
- *pResult = 0;
- }
posted on
2008-12-30 11:05
Yincheng
阅读(
273)
评论()
编辑
收藏
举报