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

新建对话框按钮及dll调用代码示例
博客介绍在新建对话框上放置2个按钮(隐式和显式调用dll)和1个编辑框,还给出按钮响应代码,同时提到隐式调用时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工程):
None.gif//Calc.cpp
None.gif
#include <math.h>
None.gif
#define N 20
None.gif
//函数格式如下
None.gif
extern "C" __declspec(dllexport) double Calc(const char exp[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
char ch;    
InBlock.gif    
//cout<<exp<<endl;
InBlock.gif
    int add(2),sub(3),mul(4),div(5),op[20],nc(0),oc(0);
InBlock.gif    
double num[N],rel(0);            
InBlock.gif    
for(int j=0;j<N;j++)
InBlock.gif        num[j]
=op[j]=0;
InBlock.gif    j
=0;
InBlock.gif    
int len=strlen(exp),i=-1;    
InBlock.gif    
while(++i<len)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ch
=*(exp+i);    
InBlock.gif        
if(ch>='0' &&ch<='9' || ch=='+' || ch=='-' || ch=='*' ||ch=='/' ||ch=='(' ||ch==')')
InBlock.gif            
continue;        
InBlock.gif        
else                 
InBlock.gif            
return -99999999.99999;
InBlock.gif    
ExpandedSubBlockEnd.gif    }

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

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

InBlock.gif        
//cout<<op[oc-1]<<" ";
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
if(oc>nc)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if(op[oc]%10==2)dot.gif{
InBlock.gif                op[oc
--]=0;
ExpandedSubBlockEnd.gif                
break;    }

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

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

InBlock.gif        temp
=j;
InBlock.gif        
for(j;j<19;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                
InBlock.gif            op[j]
=op[j+1];
InBlock.gif            num[j]
=num[j+1];
ExpandedSubBlockEnd.gif        }

InBlock.gif        num[temp]
=rel;        
ExpandedSubBlockEnd.gif    }

InBlock.gif
//    cout<<"The result is "<<num[0]<<endl;    
InBlock.gif
    return num[0];
InBlock.gif
ExpandedBlockEnd.gif}
这个程序运行后可以进行四则表达式运算(含括号、负号),更多的格式暂时没时间做~~

转载于:https://www.cnblogs.com/yjm0105/archive/2005/05/19/158693.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值