?? 推理機view.cpp
字號:
// 推理機View.cpp : C推理機View 類的實現
//
#include "stdafx.h"
#include "推理機.h"
#include "推理機Doc.h"
#include "inDlg.h"
#include "help.h"
#include "mystack.h"
#include "推理機View.h"
#include "FormulaTree.h"
#include "Illation_machine.h"
#include "myset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// C推理機View
IMPLEMENT_DYNCREATE(C推理機View, CScrollView)
BEGIN_MESSAGE_MAP(C推理機View, CScrollView)
// 標準打印命令
ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CScrollView::OnFilePrintPreview)
ON_COMMAND(ID_32771, &C推理機View::On32771)
ON_COMMAND(ID_32772, &C推理機View::On32772)
ON_COMMAND(ID_32777, &C推理機View::On32777)
END_MESSAGE_MAP()
// C推理機View 構造/析構
C推理機View::C推理機View()
{
// TODO: 在此處添加構造代碼
f=0;
m_pCurFont=new CFont;
m_pCurFont->CreateFont(20,0,0,0,400,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH&FF_SWISS,_T("Aerial"));
height=20;
}
C推理機View::~C推理機View()
{
}
BOOL C推理機View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此處通過修改
// CREATESTRUCT cs 來修改窗口類或樣式
return CScrollView::PreCreateWindow(cs);
}
// C推理機View 繪制
void C推理機View::OnDraw(CDC* pDC)
{
C推理機Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機數據添加繪制代碼
CFont *pold;
int k,i=20;
pold=(CFont *)pDC->SelectObject(m_pCurFont);
pDC->SetTextColor(color);
pDC->SetBkMode(TRANSPARENT);
proof_procedure.MakeEmpty();
if(f==0)return;
if(formu.GetLength()==0){ pDC->TextOutW(20,20,_T("輸入為空!")); return; }
char *str=new char[formu.GetLength()+1];
for(k=0;k<formu.GetLength();k++) str[k]=(char)formu[k]; str[k]='\0';
if(!IsLegal(str,0,strlen(str)-1)){ pDC->TextOutW(20,20,_T("公式不合法!")); return; }
proof_procedure.push(formu);
Illation_machine m(str);
proof_procedure.push(_T("推理證明過程為"));
int istrue=m.Illation(proof_procedure);
if(f==1){
pDC->TextOutW(20,20,formu);
if(istrue==1){ pDC->TextOutW(20,20+height,_T("為重言式")); return; }
FormulaTree<char> ft(str);
char *cnf=ft.ToCNF();
int flag=SimplifyCnf(cnf);
if(flag==-1){ pDC->TextOutW(20,20+height,_T("為矛盾式")); return;}
else{
pDC->TextOutW(20,20+height,_T("合取范式為"));
CString temp(cnf);
pDC->TextOutW(20,20+2*height,temp);
}
delete []cnf;
}
else if(f==2){
proof_procedure.push(_T("析取范式為"));
pDC->TextOutW(20,20,formu);
if(istrue==1){ pDC->TextOutW(20,20+height,_T("為重言式")); return; }
FormulaTree<char> ft(str);
char *dnf=ft.ToDNF();
int flag=SimplifyDnf(dnf);
if(flag==-1){ pDC->TextOutW(20,20+height,_T("為矛盾式")); return;}
else{
pDC->TextOutW(20,20+height,_T("析取范式為"));
CString temp(dnf);
pDC->TextOutW(20,20+2*height,temp);
}
delete []dnf;
}
else if(f==4){
if(istrue==1){
while(!proof_procedure.IsEmpty()){
pDC->TextOutW(20,i,proof_procedure.top());
proof_procedure.pop();
i+=height;
}
}
else{
pDC->TextOutW(20,i,proof_procedure.top());i+=height;
pDC->TextOutW(20,i,_T("該命題不成立"));i+=height;
}
}
delete []str;
}
void C推理機View::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: 計算此視圖的合計大小
sizeTotal.cx =5000;
sizeTotal.cy = 100000;
SetScrollSizes(MM_TEXT, sizeTotal);
}
// C推理機View 打印
BOOL C推理機View::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默認準備
return DoPreparePrinting(pInfo);
}
void C推理機View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加額外的打印前進行的初始化過程
}
void C推理機View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后進行的清理過程
}
// C推理機View 診斷
#ifdef _DEBUG
void C推理機View::AssertValid() const
{
CScrollView::AssertValid();
}
void C推理機View::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
C推理機Doc* C推理機View::GetDocument() const // 非調試版本是內聯的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C推理機Doc)));
return (C推理機Doc*)m_pDocument;
}
#endif //_DEBUG
// C推理機View 消息處理程序
void C推理機View::On32771()
{
// TODO: Add your command handler code here
inDlg in;
in.construct(formu);
in.DoModal();
UpdateData(FALSE);
if(in.f==5)Invalidate();
else{
f=in.f;
formu=in.form();
Invalidate();
}
}
void C推理機View::On32772()
{
// TODO: Add your command handler code here
CFontDialog dlg;
if(dlg.DoModal()==1){
delete m_pCurFont;
m_pCurFont=new CFont;
m_pCurFont->CreateFont(dlg.GetSize(),0,0,0,dlg.GetWeight(),dlg.IsItalic(),dlg.IsUnderline(),dlg.IsStrikeOut(),ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH&FF_SWISS,dlg.GetFaceName());
color=dlg.GetColor();
height=dlg.GetSize();
}
Invalidate();
}
bool C推理機View::IsLegal(char *s,int start,int end){
int count=0,flag=0;
//判斷括號是否匹配
for(int i=start;i<=end;i++)
if(count<0)
return false;
else if(s[i]=='('){
count++; flag=1;
}
else if(s[i]==')')
count--;
//不匹配返回假
if(count!=0)return false;
//flag==0意味著式中未出現括號,則只可能呈形!!!a,a+b,a*b,a>b,a=b,!!!!!a+!!!b等情況
if(flag==0){
int i=start;
while(s[i]=='!')i++;
if(Isalpha(s[i]))
i++;
else
return false;
if(i==end+1)return true;
if(IsOperator(s[i])&&s[i]!='!')
i++;
else
return false;
while(s[i]=='!')i++;
if(Isalpha(s[i]))
i++;
else
return false;
if(i==end+1)
return true;
else
return false;
}
else{
bool f1=true,f2=true;
int substart,i=start;
while(s[i]=='!')i++;
if(s[i]=='('){
i++;substart=i;count=1;
while(count!=0){ if(s[i]=='(')count++;else if(s[i]==')')count--;i++;}
f1=IsLegal(s,substart,i-2);
}
else if(Isalpha(s[i]))
i++;
else
return false;
if(i==end+1)return f1;
if(IsOperator(s[i])&&s[i]!='!')
i++;
else
return false;
while(s[i]=='!')i++;
if(s[i]=='('){
i++;substart=i;count=1;
while(count!=0){ if(s[i]=='(')count++;else if(s[i]==')')count--;i++;}
f2=IsLegal(s,substart,i-2);
}
else if(Isalpha(s[i]))
i++;
else
return false;
if(i!=end+1)
return false;
if(f1&&f2)
return true;
else
return false;
}
}
int C推理機View::SimplifyCnf(char *s){
int i=0,j=0,len=strlen(s);
while(i<len){
myset temp;
while(s[i]!='*'&&s[i]!='\0'){
if(s[i]=='!'){
i++;
atomic at;
at.flag=1;at.c=s[i];
temp.push(at);
}
else if(Isalpha(s[i])){
atomic at;
at.flag=0;at.c=s[i];
temp.push(at);
}
i++;
}
int t=temp.cnf();
if(!temp.IsEmpty()&&t!=1){
temp.SetFirst();
if(!temp.Isat())s[j++]='(';
while(!temp.IsEnd()){
if(temp.GetData().flag==1){
s[j++]='!';
s[j++]=temp.GetData().c;
s[j++]='+';}
else if(temp.GetData().flag==0){
s[j++]=temp.GetData().c;
s[j++]='+';
}
temp.Next();}
j--;
if(!temp.Isat())s[j++]=')';
s[j++]='*';
}
i++;
temp.MakeEmpty();
}
if(j!=0)s[j-1]='\0';
else s[0]='\0';
i=0; myset set;
if(strlen(s)==0)
return 1;
int p[58]={0};
while(i<(int)strlen(s)){
if(i==0||s[i]=='*'){
if(i!=0)i++;
if(s[i]=='('){ while(s[i]!=')') i++;continue;}
if(s[i]=='!'){
i++;
if(p[int(s[i]-'A')]==0)p[int(s[i]-'A')]=1;
else if(p[int(s[i]-'A')]==2)
return -1;
else{
for(int k=i+1;k<=(int)strlen(s);k++)
s[k-3]=s[k];
i=i-3;
}
}
else if(Isalpha(s[i])){
if(p[int(s[i]-'A')]==0)p[int(s[i]-'A')]=2;
else if(p[int(s[i]-'A')]==1)
return -1;
else{
for(int k=i+1;k<=(int)strlen(s);k++)
s[k-2]=s[k];
i=i-2;
}
}
}
i++;
}
return 0;
}
int C推理機View::SimplifyDnf(char *s){
int i=0,j=0,len=strlen(s);
while(i<len){
myset temp;
while(s[i]!='+'&&s[i]!='\0'){
if(s[i]=='!'){
i++;
atomic at;
at.flag=1;at.c=s[i];
temp.push(at);
}
else if(Isalpha(s[i])){
atomic at;
at.flag=0;at.c=s[i];
temp.push(at);
}
i++;
}
if(temp.dnf()){
temp.SetFirst();
if(!temp.Isat())s[j++]='(';
while(!temp.IsEnd()){
if(temp.GetData().flag==1){
s[j++]='!';
s[j++]=temp.GetData().c;
s[j++]='*';}
else if(temp.GetData().flag==0){
s[j++]=temp.GetData().c;
s[j++]='*';
}
temp.Next();}
j--;
if(!temp.Isat())s[j++]=')';
s[j++]='+';
}
i++;
temp.MakeEmpty();
}
if(j!=0)s[j-1]='\0';
else s[0]='\0';
i=0; myset set;
if(strlen(s)==0)
return -1;
int p[58]={0};
while(i<(int)strlen(s)){
if(i==0||s[i]=='+'){
if(i!=0)i++;
if(s[i]=='('){ while(s[i]!=')') i++;continue;}
if(s[i]=='!'){
i++;
if(p[int(s[i]-'A')]==0)p[int(s[i]-'A')]=1;
else if(p[int(s[i]-'A')]==2)
return 1;
else{
for(int k=i+1;k<=(int)strlen(s);k++)
s[k-3]=s[k];
i=i-3;
}
}
else if(Isalpha(s[i])){
if(p[int(s[i]-'A')]==0)p[int(s[i]-'A')]=2;
else if(p[int(s[i]-'A')]==1)
return 1;
else{
for(int k=i+1;k<=(int)strlen(s);k++)
s[k-2]=s[k];
i=i-2;
}
}
}
i++;
}
return 0;
}
void C推理機View::On32777()
{
// TODO: Add your command handler code here
help he;
he.DoModal();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -