动态链接库DLL的编写和调用(四则表达式计算)

在新建对话框上放置2个按钮(隐式和显式调用dll)和1个编辑框(连接变量m_exp),添加按钮响应代码如下(隐式调用时:dll文件要放在对应目录[系统目录或者程序目录]下):

void  CCalcTestDlg::OnButtonYinshi() 
{
    
//  TODO: Add your control notification handler code here
    UpdateData( true );
    
if (m_exp.IsEmpty())
    {
        AfxMessageBox(
" 请先输入四则运算表达式: " );
        
return ;
    }
    
if ( ! TestExp(m_exp))
    {
        AfxMessageBox(
" 格式错,请输入类似 \n -(-5+3*2)/2+1 \n这样的四则表达式 " );
        
return ;
    }
    HMODULE hDll
= ::LoadLibrary( " .\\Calc.dll " ); // 加载链接库 
     if (hDll == NULL){ 
        MessageBox(
" 找不到Calc.dll,加载动态链接库失败 " , " Warning " ,MB_OK | MB_ICONWARNING); 
        
return ;} 
    typedef 
double  ( * pShow)( const   char   * ); 
    pShow Show
= (pShow)::GetProcAddress(hDll, " Calc " ); 
    
if (Show == NULL)
        MessageBox(
" 函数调用失败 " , " Warning " ,MB_OK | MB_ICONWARNING);  // 调用加载动态链接库失败
     else                  
        m_exp.Format(
" %f " ,Show(m_exp));     

    FreeLibrary(hDll);
    UpdateData(
false );
}

void  CCalcTestDlg::OnButtonXian() 
{
    
//  TODO: Add your control notification handler code here
    UpdateData( true );
    
if (m_exp.IsEmpty())
    {
        AfxMessageBox(
" 请先输入四则运算表达式: " );
        
return ;
    }
    
if ( ! TestExp(m_exp))
    {
        AfxMessageBox(
" 格式错,请输入类似 \n -(-5+3*2)/2+1 \n这样的四则表达式 " );
        
return ;
    }
    CString filter,strPath;
    filter
= " Dll(*.dll)|*.dll|| "
    CFileDialog dlg(TRUE,NULL,
" Calc.dll " ,OFN_HIDEREADONLY,filter); 

    
if (dlg.DoModal() == IDOK) 
        strPath
= dlg.GetPathName(); 

    HMODULE hDll
= ::LoadLibrary(strPath); // 加载链接库 
     if (hDll == NULL){ 
        MessageBox(
" 加载动态链接库失败 " , " Warning " ,MB_OK | MB_ICONWARNING); 
        
return ;} 
    typedef 
double  ( * pShow)( const   char  []); 
    pShow Show
= (pShow)::GetProcAddress(hDll, " Calc " ); 
    
if (Show == NULL)
        MessageBox(
" 函数调用失败 " , " Warning " ,MB_OK | MB_ICONWARNING);  // 调用加载动态链接库失败
     else  
        m_exp.Format(
" %f " ,Show(m_exp)); 

    FreeLibrary(hDll);
    
    UpdateData(FALSE); 
}

bool  CCalcTestDlg::TestExp( const   char   * str)
{
    
char  ch;
    
int  len = strlen(str),i = 0 ;    
    
while (len)
    {
        ch
=* (str + i ++ );
        
if (i == len + 1 )
                
return   true ;
        
if (ch >= ' 0 '   && ch <= ' 9 '   ||  ch == ' + '   ||  ch == ' - '   ||  ch == ' * '   || ch == ' / '   || ch == ' ( '   || ch == ' ) ' )
            ;
        
else
            
return   false ;
    }
    
return   true ;        
}
这是用来做四则运算的,dll的编写如下(用MFC建立DLL工程):
// Calc.cpp
#include  < math.h >
#define  N 20
// 函数格式如下
extern   " C "  __declspec(dllexport)  double  Calc( const   char  exp[])
{
    
char ch;    
    
//cout<<exp<<endl;
    int add(2),sub(3),mul(4),div(5),op[20],nc(0),oc(0);
    
double num[N],rel(0);            
    
for(int j=0;j<N;j++)
        num[j]
=op[j]=0;
    j
=0;
    
int len=strlen(exp),i=-1;    
    
while(++i<len)
    
{
        ch
=*(exp+i);    
        
if(ch>='0' &&ch<='9' || ch=='+' || ch=='-' || ch=='*' ||ch=='/' ||ch=='(' ||ch==')')
            
continue;        
        
else                 
            
return -99999999.99999;
    
    }

    
//cout<<strlen(exp)<<endl;
    for(i=0;i<strlen(exp);i++)
    
{    
        
        
if(exp[i]<58 && exp[i]>47)    
        
{            
            num[nc]
=num[nc]*10+int(exp[i])-48;
            
//cout<<num[nc]<<' ';
        }

        
else 
        
{
            
//cout<<exp[i]<<' ';
            switch(exp[i])
        
{
            
case '+':
                op[oc
++]=add;                
                
break;
            
case'-':
                op[oc
++]=sub;                
                
break;
            
case'*':
                op[oc
++]=mul;                
                
break;
            
case'/':
                op[oc
++]=div;                
                
break;
            
case'(':
                add
+=10;
                sub
+=10;
                mul
+=10;
                div
+=10;
                
break;
            
case')':
                add
-=10;
                sub
-=10;
                mul
-=10;
                div
-=10;
                
break;            
        }

        
//cout<<op[oc-1]<<" ";
        }

        
if(oc>nc)
        
{
            
if(op[oc]%10==2){
                op[oc
--]=0;
                
break;    }


            
if(op[oc]%10==3)
                num[nc]
=0;//"-x" => "0-x"
            nc++;
        }
    
    }

    
    
for(int w=0;w<20;w++)
    
{
        j
=0;
        
if(op[0]==0)
            
break;
        
int temp=op[0]/2;            
        
for(int i=0;i<20;i++)            
            
if(op[i]/2>temp)
            
{    
                j
=i;
                temp
=op[i]/2;
            }
    
        
switch(op[j]%10)
        
{
        
case 2:            
            rel
=num[j]+num[j+1];        
            
break;
        
case 3:
            rel
=num[j]-num[j+1];
            
break;
        
case 4:
            rel
=num[j]*num[j+1];
            
break;
        
case 5:
            rel
=num[j]/num[j+1];                    
            
break;
        }

        temp
=j;
        
for(j;j<19;j++)
        
{                
            op[j]
=op[j+1];
            num[j]
=num[j+1];
        }

        num[temp]
=rel;        
    }

//    cout<<"The result is "<<num[0]<<endl;    
    return num[0];

}
这个程序运行后可以进行四则表达式运算(含括号、负号),更多的格式暂时没时间做~~

你可能感兴趣的:(dll)