📄 可编程计算器dlg.cpp
字号:
// 可编程计算器Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "可编程计算器.h"
#include "可编程计算器Dlg.h"
CString Result();
//#include "CAboutDlg.h"
//#include "stdafx.h"
#include "stdlib.h"
#include "conio.h"
// 这一段是记号的定义
#define ADD 0
#define MUL 1
#define LBRACE 2
#define RBRACE 3
#define NUM 4
#define END 5
#define OTHER 6
#define SUB 7
#define DIV 8
int yylex();
void Match(int t);
double T();
double E_();
long E();
double T_();
double F();
char *input=NULL; // 输入串。
int lookahead;
int pCur;//input[200]的下角标
int yylval;
// 词法分析器,读入一个记号
int yylex()
{
char num[20];
int temp = 0;
// 过滤掉空白
while ( input[pCur]==' ' ) pCur++;
// 如果是数字,那么把这个记号的数值计算出来放在 yylval 中
while (input[pCur] >= '0' && input[pCur] <= '9'){
num[temp++] = input[pCur++];
}
if (temp >0)
{
sscanf(num, "%d", &yylval);/*表头文件 #include<stdio.h>定义函数
int sscanf (const char *str,const char * format,........);
函数说明 sscanf()会将参数str的字符串根据参数format字符串来转换并格式化数据。
格式转换形式请参考scanf()。转换后的结果存于对应的参数内。*/
return NUM;
}
// 其他记号的处理
switch (input[pCur++]) // 注意:这里指针往前移了一位
{
case '+': return ADD;
case '*':return MUL;
case '(':return LBRACE;
case ')':return RBRACE;
case '\0': return END;
case '/':return DIV;
case '-':return SUB;
default: return OTHER;
}
}
// 匹配函数,若当前记号与参数相同,则读入下一个记号
void Match(int t)
{
if (lookahead == t) lookahead = yylex();
else
{
MessageBox(NULL,"ERROR",0,0);
exit(0);
}
}
// 处理 T-->FT'
double T()
{
switch (lookahead)
{
case LBRACE: // FIRST(FT')={(,num}
case NUM:
return F()*T_();
default:
MessageBox(NULL,"ERROR",0,0);
exit(0);
}
}
// 处理 E'-->+TE'|e
double E_()
{
switch (lookahead)
{
case ADD: // E'-->+TE' 的情况, FIRST(E')={+,e}
Match(ADD); return T() + E_(); break;
case SUB:
Match(SUB); return -(T()-E_());
case RBRACE:// E'-->e 的情况,这个时候需要处理 FOLLOW集合, FOLLOW(E')={), $}
case END:
return 0;
default:
MessageBox(NULL,"ERROR",0,0);
// exit(0);
}
}
// 处理 E-->TE'
long E()
{
switch (lookahead)
{
case LBRACE: // FIRST(TE')={(,num}
case NUM:
return T() + E_();
// case END: // FOLLOW(E)={),$}
// return 0;
default:
MessageBox(NULL,"ERROR",0,0);
// exit(0);
}
}
// 处理 T'-->*FT'|e
double T_()
{
switch (lookahead)
{
case MUL: // FIRST(*FT')={*}
Match(MUL);
return F() * T_(); break;
case DIV:
Match(DIV);
return 1/(F()/T_());
case ADD: // T'-->e 的情况,这个时候需要处理 FOLLOW集合, FOLLOW(T')={+,),$}
case RBRACE:
case END:
case SUB:
return 1;
default:
MessageBox(NULL,"ERROR",0,0);
// exit(0);
}
}
// 处理 F-->(E)|num
double F()
{
int temp;
switch(lookahead)
{
case LBRACE: // FIRST((E))={(}
Match(LBRACE);
temp = E();
Match(RBRACE);
return temp;
case NUM: // FIRST(num) = {num}
temp = yylval;
Match(NUM);
return temp;
default:
MessageBox(NULL,"ERROR",0,0);
// exit(0);
}
}
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_Edit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
DDX_Text(pDX, IDC_EDIT1, m_Edit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_0, OnButton0)
ON_BN_CLICKED(IDC_BUTTON_1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON_OFF, OnButtonOff)
ON_BN_CLICKED(IDC_BUTTON_2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON_3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON_4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON_5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON_6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON_7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON_8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON_9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_SUB, OnButtonSub)
ON_BN_CLICKED(IDC_BUTTON_MUL, OnButtonMul)
ON_BN_CLICKED(IDC_BUTTON_DIV, OnButtonDiv)
ON_BN_CLICKED(IDC_BUTTON_LEFT, OnButtonLeft)
ON_BN_CLICKED(IDC_BUTTON_RIGHT, OnButtonRight)
ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_C, OnButtonC)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_COMMAND(ID_ABORT, OnAbort)
ON_BN_CLICKED(IDC_BUTTON_RESULT, OnButtonResult)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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
lookahead=0;
yylval=0;
pCur=0;
m_Edit.Empty();
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
//m_Edit1.ReplaceSel("0");
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnButton0()
{
m_Edit+="0";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton1()
{
m_Edit+="1";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonOff()
{
OnCancel();
}
void CMyDlg::OnButton2()
{
m_Edit+="2";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton3()
{
m_Edit+="3";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton4()
{
m_Edit+="4";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton5()
{
m_Edit+="5";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton6()
{
m_Edit+="6";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton7()
{
m_Edit+="7";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton8()
{
m_Edit+="8";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButton9()
{
m_Edit+="9";
// CMyDlg::UpdateData(true);
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonAdd()
{
m_Edit+="+";
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonSub()
{
m_Edit+="-";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonMul()
{
m_Edit+="*";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonDiv()
{
m_Edit+="/";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonLeft()
{
m_Edit+="(";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonRight()
{
m_Edit+=")";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnButtonPoint()
{
m_Edit+=".";
CMyDlg::UpdateData(false);
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
//m_Edit1.SetSel(-1,0);
// m_Edit1.ReplaceSel(".");
}
void CMyDlg::OnButtonC()
{
// TODO: Add your control notification handler code here
lookahead=0;
yylval=0;
pCur=0;
m_Edit.Empty();
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
void CMyDlg::OnChangeEdit1()
{
;//my fault
}
void CMyDlg::OnAbort()
{
CAboutDlg a;
a.DoModal();
}
void CMyDlg::OnButtonResult()
{
GetDlgItem(IDC_EDIT1)->GetWindowText(m_Edit);
input=m_Edit.GetBuffer(m_Edit.GetLength());
m_Edit=Result() ;
GetDlgItem(IDC_EDIT1)->SetWindowText(m_Edit);
}
CString Result()
{
CMyDlg a;
lookahead = yylex();
char array[200];
if(a.search()==0)
MessageBox(NULL,"分母不能为零",0,0);
sprintf(array, "%.1d",E());
CString result=array;
return result;
}
//bool search() {
// int i=0;
// while(i<)
//}
BOOL CMyDlg::search()
{
int i=0;
while(i<m_Edit.GetLength()) {
i++;
if((m_Edit[i]=='/')&&(m_Edit[i+1]=='0')) {
return 0;
//break;
}
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -