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

📄 bianyidlg.cpp

📁 分析for i=E step E until E do i=E
💻 CPP
字号:
// bianyiDlg.cpp : implementation file
//
#include "StrOper.h"
#include "stdafx.h"
#include "bianyi.h"
#include "bianyiDlg.h"
#include "LlFenxi.h"
#include<iostream>
#include<string>
#include<stack>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
char * key[]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","return","short","signed","static","struct","switch","typedef","union","void","while","new","true","false"};
char * oprate[]={"<",">","=","+","-","*","/","<=","=<","<>","!=","[","]","(",")",">=","=>","{","}",";"};
char *oper[]={"+","-","*","/"};


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()

/////////////////////////////////////////////////////////////////////////////
// CBianyiDlg dialog

CBianyiDlg::CBianyiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBianyiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBianyiDlg)
		// 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 CBianyiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBianyiDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBianyiDlg, CDialog)
	//{{AFX_MSG_MAP(CBianyiDlg)
	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_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBianyiDlg message handlers

BOOL CBianyiDlg::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 here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBianyiDlg::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 CBianyiDlg::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 CBianyiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CBianyiDlg::OnButton1() //保存文件的语句
{

	CFileDialog fd(false);
	fd.m_ofn.lpstrTitle="请选择你要save的文件夹";  
	fd.m_ofn.lpstrInitialDir="E:\\";            
	if(fd.DoModal())
	{
		filename=fd.GetPathName();
	}
	//SetDlgItemText(IDC_EDIT1,filename);
	FILE *pfile;
	pfile=fopen(filename,"w");
	char buffer[300];
	memset(buffer,0,300);
	GetDlgItem(IDC_EDIT1)->GetWindowText(buffer,300);
	fwrite(buffer,1,strlen(buffer),pfile);
	fflush(pfile);
	fclose(pfile);
	// TODO: Add your control notification handler code here
	
}

void CBianyiDlg::OnButton2() //对保存的文件进行分析
{

	FILE* write=fopen("out.txt","w");//对文件进行操作的指针
	FILE* read=fopen(filename,"rw");
	int i=0;
	int j=1;
	char buf[20];
	int readlen=querylen(read);
	char c=fgetc(read);//取出文件中的一个字符
	while(j<=readlen)//
	{
		if(c==' '||c==10||c==13)
		{
			c=fgetc(read);
			j++;
		}
		else if(isdigit(c)&&j<=readlen)
		{
			while(isdigit(c)&&j<=readlen)
			{
				buf[i]=c;
				i++;
				c=fgetc(read);
				j++;
			}
			buf[i]='\0';
			i=0;
			fprintf(write,"%s\t%d\r\n",buf,32);
			
		}
    	else if(isalpha(c)&&j<=readlen)
		{		
			while((isalpha(c)||isdigit(c))&&j<=readlen)
			{
				buf[i]=c;
				i++;
				j++;
				c=fgetc(read);
			}			
			buf[i]='\0';
			i=0;
			if(strcmp(buf,"step")==0)
			{
				fprintf(write,"%s\t%d\r\n",buf,50);
			}
			else if(strcmp(buf,"until")==0)
			{
				fprintf(write,"%s\t%d\r\n",buf,51);

			}
	
			else if(find(buf)!=0)
			{
				fprintf(write,"%s\t%d\r\n",buf,find(buf));
						
			}
		    else
			{
				fprintf(write,"%s\t%d\r\n",buf,33);
				
			}

		}
		else if(c=='+')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","+",34);

		}
		else if(c=='-')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","-",35);

		}
		else if(c=='*')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","*",36);

		}
		else if(c=='/')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","/",37);

		}
		else if(c=='<')
		{
			c=fgetc(read);
			j++;
			if(c=='=')
			{
				fprintf(write,"%s\t%d\r\n","<=",38);
				c=fgetc(read);
				j++;
			}
			else
				fprintf(write,"%s\t%d\r\n","<",39);


		}
		else if(c=='>')
		{
			c=fgetc(read);
			j++;
			if(c=='=')
			{
				fprintf(write,"%s\t%d\r\n",">=",40);
				c=fgetc(read);
				j++;
			}
			else
				fprintf(write,"%s\t%d\r\n",">",41);

			
		}
		else if(c=='=')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","=",42);

		}
		else if(c=='(')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","(",43);

		}
		else if(c==')')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n",")",44);

		}
		else if(c=='[')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","[",45);

		}
		else if(c==']')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","]",46);

		}
		else if(c=='{')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","{",47);

		}
		else if(c=='.')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n",".",50);

		}
		else if(c=='#')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","#",51);

		}
		else if(c=='}')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n","}",48);

		}
		else if(c==';')
		{
			c=fgetc(read);
			j++;
			fprintf(write,"%s\t%d\r\n",";",49);

		}
		else 
		{
			//fprintf(write,"没有此字符 %c\r\n",c);
			j++;
		}
		
	}

	fclose(read);
	fflush(write);
	fclose(write);
	FILE *fwr=fopen("out.txt","r");
	fseek(write,0,SEEK_END);
	int len=ftell(write);	
	char *pbuf=new char[len+1];
	rewind(write);
	memset(pbuf,0,len);
	fread(pbuf,1,300,write);
	SetDlgItemText(IDC_EDIT2,pbuf);
	fclose(fwr);
	
}

int CBianyiDlg::find(char *a)
{
	int j;
	for(int i=0;i<=30;i++)
	{
		if(!strcmp(key[i],a))
		{
			j=i+1;
			return j;
		}		
			
	}
	return 0;
}

int CBianyiDlg::querylen(FILE* f)
{
	fseek(f,0,SEEK_END);
	int len=ftell(f);
	rewind(f);
	return len;


}

int CBianyiDlg::findother(char *a)
{
	int j;
	for(int i=0;i<=20;i++)
	{
		if(!strcmp(oprate[i],a))
		{
			j=i+34;
			return j;
		}
		
			
	}
	return 0;

}

void CBianyiDlg::OnButton3() 
{
	danci w[50];
	char *abuf=new char[10];
	char *bbuf=new char[10];	
	memset(abuf,0,10);
	memset(bbuf,0,10);
	char bufa;
	FILE* read=fopen("out.txt","r");
	FILE* write=fopen("bianyi.txt","w");
	//int n=querylen(read);
		int jishu=0;
		int wenjian=0;
		fseek(read,0,SEEK_END);
		int len=ftell(read);	
		char *pbuf=new char[len+1];
		rewind(read);
		memset(pbuf,0,len);
		fread(pbuf,1,len,read);
		fclose(read);
		int n=strlen(pbuf);
	/*	while(wenjian<n)
		{
			int i=0;
			int j=0;
			bufa=fgetc(read);
			wenjian++;
			while(bufa!='\t'&&wenjian<n)
			{
				
				abuf[i]=bufa;		
				i++;
				bufa=fgetc(read);
					wenjian++;
				
				
			}
			while((bufa=fgetc(read))!='\n'&&wenjian<n)
			{
				bbuf[j]=bufa;
				j++;
				wenjian++;
			}
			w[jishu].key=abuf;
			w[jishu].kind=atoi(bbuf);
			jishu++;
			memset(abuf,0,6);
			memset(bbuf,0,6);
		
		}*/
		while(wenjian<n)
		{
			int i=0;
			int j=0;
			bufa=pbuf[wenjian];
			wenjian++;
			while(bufa!='\t'&&wenjian<n)
			{
				
				abuf[i]=bufa;		
				i++;
				bufa=pbuf[wenjian];
				wenjian++;
				
				
			}
			while((bufa=pbuf[wenjian])!='\n'&&wenjian<n)
			{
				bbuf[j]=bufa;
				j++;
				wenjian++;
			}
			wenjian++;
			w[jishu].key=abuf;
		//	MessageBox(abuf);
		//	StrOper str1;
		//	MessageBox(str1.inttostr(strlen(abuf)).c_str());

			w[jishu].kind=atoi(bbuf);
			jishu++;
			memset(abuf,0,10);
			memset(bbuf,0,10);
		
		}
		
			w[jishu].key="#";
		char a[50];
		
	for(int i=0;i<=jishu;i++)
		{
			if(w[i].kind==33||w[i].kind==32)
			{
				a[i]='i';
			}
			else if(w[i].kind==8)
			{
				a[i]='d';
			}
			else if(w[i].kind==34||w[i].kind==35||w[i].kind==36||w[i].kind==37)
			{
				a[i]='r';

			}
			else if(w[i].kind==50)
			{
				a[i]='s';
			}
			else if(w[i].kind==51)
			{
				a[i]='u';
			}
			else
			{
				char * kge=(char *)w[i].key.c_str();
				a[i]=kge[0];
			}
			
		}
		a[jishu+1]=0;

	//	MessageBox(a);
	//	FenXi fenxi;
		LlFenxi ll;
	//	MessageBox(a);
		int k=ll.zong(a,write,w);
	//	int k=fenxi.digui(a,write,w);	
	if(k==0)
		{	
			SetDlgItemText(IDC_EDIT3,"failed");
		}
		else
		{	
			fclose(write);
			FILE *fwr=fopen("bianyi.txt","r");
			fseek(fwr,0,SEEK_END);
			int len=ftell(fwr);	
			char *pbuf1=new char[len+1];
			rewind(fwr);
			memset(pbuf1,0,len);
			fread(pbuf1,1,len,fwr);
			SetDlgItemText(IDC_EDIT3,pbuf1);
			fclose(fwr);

		}

	
	
	
}

⌨️ 快捷键说明

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