CppEval 乙個 C 的表示式分析庫

2021-03-31 19:56:34 字數 3957 閱讀 2239

平時編寫程式的時候,會在不少地方用到表示式分析,譬如說:使用者介面,系統配置之類的。雖然對大多數程式設計師來說,這都不是一件難事,但是,要保證每次寫出的表示式分析完整無錯並且功能強大,不是一件易事。

你想在你的程式中可以處理類似於「+ aa * aa / b - b / -2 /2/2 -- 5 ** 2 + -100 + ( aa * aa / b - b / -2 /2/2 -- 5 ** 2 + -100 ) + return1() + negative( 1 ) + sum( 1, 2, 3, sum( 1 + 2 *5-7, 555, 111 ) )

這樣的**的表示式嗎?(上面這個表示式中,不僅有常數,變數,函式,而且某些函式的引數數目還是可變的)

使用cppeval,你只需要 include 乙個標頭檔案就可以做到這點!

介面

template< typename number >

number eval( const char* expression );

這個直接計算乙個表示式,支援+-*/()以及一元+-,並且支援**作為乘方

template< typename number >

number eval( const char* expression, const ::std::map< ::std::string, number >& variables );

這個除了支援上面的功能以外,還支援變數。你提供的 variables 裡面就是變數到數的對映。

template< typename number, typename functions >

number eval( const char* expression, const ::std::map< ::std::string, number >& variables, functions& funcs );

這個除了支援上面的功能以外,還支援函式。函式具體的定義方法請參見示例

cppeval.h

#ifndef cpp_eval_h_dff520db406edcf31ab9a538f7e1c3bd_20040721__

#define cpp_eval_h_dff520db406edcf31ab9a538f7e1c3bd_20040721__

#include

#include

#include

#include

#include

#include

namespace cpp_eval

};template< typename number >

class dummy_functions

};template< typename number >

number eval( const char* expression )

template< typename number >

number eval( const char* expression, const ::std::map< ::std::string, number >& variables )

template< typename number, typename functions >

class evaler

;type mtype;

std::string midentifier;

number mvalue;

void look_ahead()

else if( *mcurrent == '*' && *( mcurrent + 1 ) == '*' )

mtype = power, mcurrent += 2;

else if( *mcurrent == add_or_positive || *mcurrent == subtract_or_negative ||

*mcurrent == multiply || *mcurrent == divide ||

*mcurrent == left_bracket || *mcurrent == right_bracket ||

*mcurrent == parameter_seperator )

mtype = ( type )*mcurrent, ++mcurrent;

else if( isalpha( *mcurrent ) )

else if( *mcurrent == 0)

mtype = finished;

else

break;}}

void match( type type )

number expression()

number expression_r( const number& left )

number higher_expression()

number higher_expression_r( const number& left )

number sign_expression()

number power_expression()

number factor()

number lang_structure()

number lang_tail( const std::string& id )

else

return result;

}std::vectorparameter_list()

result.push_back( expression() );

parameter_tail( result );

}return result;

}void parameter_tail( std::vector& param )

public:

evaler( const ::std::map< ::std::string, number >& variables, functions& funcs )

: mvariables( variables ), mfuncs( funcs )

{}number operator()( const char* expr )

};template< typename number, typename functions >

number eval( const char* expression, const ::std::map< ::std::string, number >& variables, functions& funcs )

};#endif//cpp_eval_h_dff520db406edcf31ab9a538f7e1c3bd_20040721__

用於測試的 c++ 程式:

#include "cppeval.h"

#include

#include

using namespace std;

class functions

else if( stricmp( name, "negative" ) == 0 )

else if( stricmp( name, "sum" ) == 0 )}};

int main()

{map< string, int > variables;

variables[ "aa" ] = 100;

variables[ "b" ] = 200;

functions f;

cout<( " + aa * aa / b - b / -2 /2/2 -- 5 ** 2 + -100 "

" + ( aa * aa / b - b / -2 /2/2 -- 5 ** 2 + -100 )"

" + return1() + negative( 1 ) + sum( 1, 2, 3, sum( 1 + 2 *5-7, 555, 111 ) )", variables, f )<

給出乙個表示式,求取表示式的值

include include include include include using namespace std 思路 1.字串預處理,針對可能出現的 等特殊情況進行替換,判斷 是負號還是減號,負號前面 0,轉變成減法運算 2.將中綴字串轉變為字尾字串陣列 3.對字尾字串陣列進行求解 int ...

c 實現乙個小型算術表示式

輸入表示式 反覆 讀取乙個運算元存到運算元陣列中 讀取乙個運算子存到運算子陣列中 如果運算子是 就跳出反覆 計算 在運算子陣列中找優先順序最低的運算子 從右向左找有沒有 如果沒有再從右向左找有沒有 如果沒有運算子,運算元陣列第乙個元素就是結果,計算完畢 以它為界把運算子陣列分成兩半 位址相減確定元素...

C Lamda表示式的乙個妙用

在專案程式設計中,經常會遇到類似這樣的需求 當verbosity設定大於等於1時,開啟debug列印 否則關閉列印。以下是一種常見的實現方法,因為log可能需要進行一些拼接或者計算,故在乙個print log函式中實現。但這樣做有乙個問題,即使m verbosity配置為0,print log 這個...