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

📄 推理机view.cpp

📁 一个基于H.wang的谓词演算公式的机器证明的vc实现
💻 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 + -