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

📄 1dlg.cpp

📁 VC实现的用堆栈实现表达式求值!! 数据结构的一个小作业!! 大家可以参考一下
💻 CPP
字号:
// 1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "1.h"
#include "1Dlg.h"
#include <ctype.h>
#include <string.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy1Dlg dialog

CMy1Dlg::CMy1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy1Dlg)
	m_biaodashi = _T("");
	m_result = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy1Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_biaodashi);
	DDX_Text(pDX, IDC_EDIT2, m_result);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy1Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy1Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1Dlg message handlers

BOOL CMy1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
}

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

#define N 50
typedef struct{
 int top;
 double array[N];
}NumStack;
typedef struct{
 int top;
 char array[N];
}OpStack;
int Cint(char mychar){
 return (mychar-48);
}//?
void PushNum(NumStack *numstack,double num){
 numstack->top++;
 numstack->array[numstack->top-1]=num;
}//数字如栈
void PopNum(NumStack *numstack,double *num){
 *num=numstack->array[numstack->top-1];
 numstack->top--;
}
void PushOp(OpStack *opstack,char op){
 opstack->top++;
 opstack->array[opstack->top-1]=op;
}//符号入栈
void PopOp(OpStack *opstack,char *op){
 *op=opstack->array[opstack->top-1];
 opstack->top--;
}//符号出栈
double Calc(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;
 }
 return result;
}//-------------运算判断
char Priority(char y,char x){
  char priority='<';
  switch(x){
   case '+':
   case '-':if(y=='(' || y=='#')priority='>';break;
   case '*':
   case '/':if(y=='(' || y=='#'|| y=='+' || y=='-')priority='>';break;
   case '(':priority='>';break;
   case ')':if(y=='(')priority='=';break;
   case '{':if(y=='*'|| y=='+' || y=='-'|| y=='/')priority='>';break;
   case '}':if(y=='{')priority='=';break;
//********************怎么识别大括号啊??*************************  
   case '#':if(y=='#')priority='=';break;
   default:priority='E';
  }
  
  return priority;
}//判断算术符号优先级别
void Process(NumStack *numstack,OpStack *opstack,char x){
 double a,b;char c;
 static double tempnum=0.00000000;static int len=10;static int dot=0,flags=0;

 if(isdigit(x) || x=='.'){
  if(x=='.')dot=1;
  else{
   if(dot==0)
    tempnum=tempnum*10+Cint(x);
   else{
    tempnum=tempnum+(double)Cint(x)/len;
    len*=10;
   }
  }
 }
 else{
  if(flags==0 && x!='('){PushNum(numstack,tempnum);tempnum=0.00000000;len=10;dot=0;}
  switch(Priority(opstack->array[opstack->top-1],x)){
   case '>':PushOp(opstack,x);flags=0;break;
   case '<':
     PopOp(opstack,&c);
     PopNum(numstack,&b);
     PopNum(numstack,&a);
     PushNum(numstack,Calc(a,b,c));flags=1;
     Process(numstack,opstack,x);break;
   case '=':PopOp(opstack,&c);flags=1;break;
   default:printf("Wrong Express!");
	   
  }
 }
}

void CMy1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
NumStack numstack;
OpStack opstack;
char s[N];
int i=0;
CString str;
 numstack.top=0;
opstack.top=0;
 PushOp(&opstack,'#');
 UpdateData(true);
 str=m_biaodashi;
m_biaodashi=m_biaodashi+"#";
  for(i=0;i<strlen(m_biaodashi);i++)
 Process(&numstack,&opstack,m_biaodashi[i]);
 double kkkkk;
  kkkkk = numstack.array[numstack.top-1];
  m_result.Format ("%f",kkkkk);
  m_biaodashi=str;
 UpdateData(false);
}

⌨️ 快捷键说明

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