?? jisuanqidlg.cpp
字號(hào):
// jisuanqiDlg.cpp : implementation file
//
#include "stdafx.h"
#include "jisuanqi.h"
#include "jisuanqiDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h> //其中有個(gè)isdigit(char)函數(shù),判斷是否為數(shù)字字符
#define N 40 //棧的空間大小
#define OK 1
#define ERROR 0
//數(shù)據(jù)的棧結(jié)構(gòu)定義
typedef short status;
typedef struct{
int top;
double array[N];
}NumStack;
//操作符的結(jié)構(gòu)定義
typedef struct{
int top;
char array[N];
}OpStack;
void InitNumStack(NumStack &S)
{
int i;
for(i=0;i<N;i++)
{
S.array[i]=-111111.111111;
}
S.top=0;
}
void InitOpStack(OpStack &O)
{
int i;
for(i=0;i<N;i++)
{
O.array[i]=' ';
}
O.top=0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CJisuanqiDlg dialog
CJisuanqiDlg::CJisuanqiDlg(CWnd* pParent /*=NULL*/)
: CDialog(CJisuanqiDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CJisuanqiDlg)
m_TXT = _T("");
m_S3 = _T("");
m_S2 = _T("");
m_S4 = _T("");
m_S5 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CJisuanqiDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CJisuanqiDlg)
DDX_Control(pDX, IDC_SLIDER1, m_Slider1);
DDX_Text(pDX, IDC_EDIT1, m_TXT);
DDX_Text(pDX, IDC_STATIC3, m_S3);
DDX_Text(pDX, IDC_STATIC2, m_S2);
DDX_Text(pDX, IDC_STATIC4, m_S4);
DDX_Text(pDX, IDC_STATIC5, m_S5);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CJisuanqiDlg, CDialog)
//{{AFX_MSG_MAP(CJisuanqiDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_SLIDER1, OnOutofmemorySlider1)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_STATIC3, OnStatic3)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJisuanqiDlg message handlers
BOOL CJisuanqiDlg::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
m_Slider1.SetRange(20,1000,true); //設(shè)置控制條的取值范圍
m_Slider1.SetTicFreq(4);
m_Slider1.SetLineSize(1);
m_Slider1.SetPageSize(4);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CJisuanqiDlg::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 CJisuanqiDlg::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 CJisuanqiDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//數(shù)字進(jìn)棧的函數(shù)
status PushNum(NumStack &numstack,double num)
{
if(numstack.top<N)
{
numstack.top++;
numstack.array[numstack.top-1]=num;
return OK;
}
else return ERROR;
}
//數(shù)字出棧的函數(shù)
status PopNum(NumStack &numstack,double &num)
{
if(numstack.top>0)
{
num=numstack.array[numstack.top-1];
numstack.array[numstack.top-1]=-111111.111111;
numstack.top--;
return OK;
}
else return ERROR;
}
status GetTopNum(NumStack numstack,double &num)
{
if(numstack.top>0)
{
num=numstack.array[numstack.top-1];
return OK;
}
else return ERROR;
}
//操作符進(jìn)棧的函數(shù)
status PushOp(OpStack &opstack,char op)
{
if(opstack.top<N)
{
opstack.top++;
opstack.array[opstack.top-1]=op;/**/
return OK;
}
else return ERROR;
}
//操作符出棧的函數(shù)
status PopOp(OpStack &opstack,char &op)
{
if(opstack.top>0)
{
op=opstack.array[opstack.top-1];
opstack.array[opstack.top-1]=' '; //清除當(dāng)前內(nèi)容
opstack.top--;
return OK;
}
else return ERROR;
}
status GetTopOp(OpStack opstack,char &op)
{
if(opstack.top>0)
{
op=opstack.array[opstack.top-1];
return OK;
}
else return ERROR;
}
//運(yùn)算函數(shù) c 為操作符
double Operation(double a,double b,char c)
{
double result;
switch(c)
{
case '+':result=a+b;break;
case '-':result=a-b;break;
case '*':result=a*b;break;
case '/':result=a/b;break;
case '^':result=pow(a,b);break;
default:break;
}
return result;
}
//優(yōu)先級(jí)判斷
char Precede(char y,char x)
{
char priority='<'; //以“?!北硎咀址Y(jié)束
switch(x)
{
case '+':
case '-':if(y=='(' || y=='#')priority='>';break;
case '*':
case '/':if(y=='(' || y=='#'|| y=='+' || y=='-') priority='>';break;
case '^':if(y=='(' || y=='#'|| y=='+' || y=='-'||y=='*'||y=='/') priority='>';break;
case '(':priority='>';break;
case ')':if(y=='(')priority='=';break;
case '#':if(y=='#')priority='=';break;
default:priority='E';
}
return priority;
}
//錄入表達(dá)式并轉(zhuǎn)換!分別入棧 判斷優(yōu)先級(jí)并出棧計(jì)算,將結(jié)果入棧!
static double NUMBER=0,a,b; //NUMBER 是取出的一整個(gè)實(shí)數(shù),a是當(dāng)前從字符串得到的數(shù)
static bool dot=FALSE; //小數(shù)點(diǎn)標(biāo)記
static long length=1; //記錄小數(shù)位數(shù)
static int flag=0; //控制是否push一個(gè)新的實(shí)數(shù),避免重復(fù)push
static int next;
static short ERROR1=0; //判斷除數(shù)是否為0
void EvaluateExpression(char c,NumStack &OPND,OpStack &OPTR)//::CJisuanqiDlg()
{
//取得字符串內(nèi)容并將其進(jìn)棧
char fuhao;
if(isdigit(c)||c=='.') //判斷是否為數(shù)字
{
flag=1;
if(c=='.')
{
dot=TRUE;
}
else
{
a=c-48;
if(dot) //如果已經(jīng)使實(shí)數(shù)
{
length*=10;
NUMBER+=a/length;
}
else //還不是實(shí)數(shù)
{
NUMBER=NUMBER*10+a;
}
}
} //當(dāng)前的NUMBER已經(jīng)完成一個(gè)實(shí)數(shù)的錄入
else //如果是操作符
{
if(flag)
{
PushNum(OPND,NUMBER);dot=FALSE;NUMBER=0;length=1;flag=0; //push一個(gè)實(shí)數(shù),還原初始值
}
switch(Precede(OPTR.array[OPTR.top-1],c))
{
case '<':PopOp(OPTR,fuhao);
PopNum(OPND,b);
if(b==0&&fuhao=='/') {ERROR1=1;return;}
PopNum(OPND,a);
PushNum(OPND,Operation(a,b,fuhao));
next--;
break;
case '>':PushOp(OPTR,c);GetTopOp(OPTR,c);break;
case '=':PopOp(OPTR,fuhao);break;
}
}
}
static int speed; //設(shè)置執(zhí)行速度??!
void CJisuanqiDlg::OnButton1()
{
UpdateData(TRUE);
NumStack OPND; //運(yùn)算數(shù)
OpStack OPTR; //運(yùn)算符
double value;
char *optr;
int lenth=m_TXT.GetLength(); //輸入的字符串的長度!
InitNumStack(OPND);
InitOpStack(OPTR);
PushOp(OPTR,'#'); //棧頂用‘?!硎窘Y(jié)束
for(next=0;next<=lenth;next++)
{
if(ERROR1==1)
{
MessageBox("輸入的表達(dá)式中或計(jì)算過程中,除數(shù)為0");
break;
}
char c;
if(next==lenth)
{
c='#'; //以“?!北硎窘Y(jié)尾
}
else
{
c=m_TXT.GetAt(next);
}
EvaluateExpression(c,OPND,OPTR);
m_S3.Format("%d C='%c'",next,c);
Sleep(speed);
m_S4.Format("%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t",OPND.array[0],OPND.array[1],OPND.array[2],
OPND.array[3],OPND.array[4],OPND.array[5],OPND.array[6],OPND.array[7]);
optr=OPTR.array;
m_S5.Format("%s",optr);
UpdateData(FALSE);
}
PopNum(OPND,value);
m_S2.Format("%f",value); //輸出表達(dá)式的值
m_S4.Format("%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t",OPND.array[0],OPND.array[1],OPND.array[2],
OPND.array[3],OPND.array[4],OPND.array[5],OPND.array[6],OPND.array[7]);
optr=OPTR.array;
m_S5.Format("%s",optr);
UpdateData(FALSE);
}
void CJisuanqiDlg::OnButton2()
{
MessageBox("于2005年6月3日課程設(shè)計(jì)!版權(quán)所有:03級(jí)計(jì)科2班楊欽洪!");
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void CJisuanqiDlg::OnOutofmemorySlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CJisuanqiDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CSliderCtrl*slider=(CSliderCtrl*)pScrollBar;
speed=slider->GetPos();
// UpdateData(FALSE); //不能更新,不然表達(dá)式里面的內(nèi)容就被更新了。沒有了
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CJisuanqiDlg::OnStatic3()
{
// TODO: Add your control notification handler code here
}
void CJisuanqiDlg::OnButton3()
{
MessageBox("程序已經(jīng)在表達(dá)式的頭和尾加上'#',所以O(shè)PTR是以'#'開始和結(jié)尾的!");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -