| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
 
 
您现在的位置: 编程中国 >> 技术教程 >> 开发语言 >> C语言 >> C技术资料 >> 正文
  ►  一个计算四则表达式的模板
一个计算四则表达式的模板
作者:雨中飞燕    阅读人次:……    文章来源:本站原创    发布时间:2007-9-10    网友评论()条
 

原帖及讨论:http://bbs.bc-cn.net/dispbbs.asp?BoardID=56&ID=168693

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国  http://www.bc-cn.net
*/ 作者: 雨中飞燕        QQ:609754650
*/ 时间: 2007-9-9  编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------

在9月8日那天我特意编写的,给大家分享的,
一个很方便的函数模板,可以并且只可以计算含括号的四则表达式
只有一个函数接口:int GetExpValue(_Tstream& istrin, _T& nReturn)
参数解释:
istrin: 一个输入流,可以是标准IO流,可以是文件流,也可以是串流
nReturn:用于接收计算结果的变量,计算所使用的类型由这个变量确定
返回值:
返回非0表示计算成功,0表示计算失败有错误

 

程序代码:

namespace fy_Exp{
namespace {template <class _T>
inline _T GetExpValue(_T t[], char& csym){
    char c=csym; csym=0;
    switch(c){
    case '+':return t[0] += t[1];
    case '-':return t[0] -= t[1];
    case '*':return t[0] *= t[1];
    default: return t[0] /= t[1];//case '/':
    }
}}
template <class _T, class _Tstream>
/* _Tstream: inputstream, _T: get return value
* Return nonzero if get value successfully */
int GetExpValue(_Tstream& istrin, _T& nReturn){
    _T t[3] = {0}; //雨中飞燕之作
    char csym[3] = "++";
    int nLevel = 1, nERR = 0;
    if(!(istrin>>t[1]))istrin.clear();
    for(;;){
        if(istrin>>csym[2]){
            switch(csym[2]){
            case '(':
                if(!csym[1]){nLevel=0x100; nERR=1;}else
                if(!GetExpValue(istrin, t[2]))nLevel|=0x10;
                else{nLevel=0x100; nERR=1;}
                break;
            case ')':
                {nLevel = 0x100;}break;
            case '+':case '-':case '*':case '/':
                {csym[nLevel++] = csym[2];}break;
            case ' ':case '\r':case '\n':case '\t':continue;
            default:
                {nLevel=0x100; nERR=1;}
            }
            if(nLevel==0x100)break;
            if(nLevel&0x10 || istrin>>t[2]){
                nLevel &= 0xF;
                if(nLevel==1){t[1]=t[2];csym[1]=0;continue;}
                if(csym[1]=='*'||csym[1]=='/'){
                    GetExpValue(t+1, csym[1]);
                }
                else{
                    GetExpValue(t, csym[0]);
                    t[1]=t[2];csym[0]=csym[1];csym[1]=0;
                }
                nLevel = 1;
            }
            else istrin.clear();
        }
        else{nERR = -1; break;}
    }
    if(csym[1])t[2]=0,nReturn=GetExpValue(t+1, csym[1]);
    else nReturn=GetExpValue(t, csym[0]);
    return nERR==-1?1:0;
}}



函数模板使用示例:
在以上那段代码的后面加上以下代码:

 

程序代码:

#include<strstream>
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
    string s1;
    while(cin>>s1)
    {
        istrstream isin(s1.data());
        double d;
        if(fy_Exp::GetExpValue(isin, d))
        {
            cout<<d<<endl;
        }
        else
        {
            cout<<"ERROR"<<endl;
        }
    }
    return 0;
}


然后编译执行就可以了(*^_^*)
其它:TC++上一定编译错误,不保证在VC6上也能通过编译
      建议使用VC7或VC更高版本,或者使用GNU C++编译

如果对以上代码有疑问或者建议都可以找我。

 

 
文章录入:静夜思    责任编辑:静夜思 
  • 上一篇文章:

  • 下一篇文章:

  •  
    相关文章
    原创地带
    24小时热门帖子