84669 Lernen von Personen
152542 Lernen von Personen
20005 Lernen von Personen
5487 Lernen von Personen
7821 Lernen von Personen
359900 Lernen von Personen
3350 Lernen von Personen
180660 Lernen von Personen
48569 Lernen von Personen
18603 Lernen von Personen
40936 Lernen von Personen
1549 Lernen von Personen
1183 Lernen von Personen
32909 Lernen von Personen
除了逆波兰式求解 字符串表达式之外,还有其他什么方法来求值?
学习是最好的投资!
不少方法啊 表达式树,两个栈(操作数栈,运算符栈),文法等等
直接贴个以前写过的给你吧。
#include <iostream> #include <boost/algorithm/string/trim.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_operator.hpp> typedef double value_t; template <typename Iterator> struct calc_parser : boost::spirit::qi::grammar<Iterator, value_t(), boost::spirit::ascii::space_type> { calc_parser() : calc_parser::base_type(expression) { using boost::spirit::qi::double_; using boost::spirit::qi::_val; using boost::spirit::qi::_1; expression = term [_val=_1] >> *( ('+' >> term [_val=_val+_1]) | ('-' >> term [_val=_val-_1]) ) ; term = factor [_val=_1] >> *( ('*' >> factor [_val=_val*_1]) | ('/' >> factor [_val=_val/_1]) ) ; factor = double_ [_val=_1] | '(' >> expression [_val=_1] >> ')' | ('+' >> factor [_val=_1]) | ('-' >> factor [_val=-_1]) ; } boost::spirit::qi::rule<Iterator, value_t(), boost::spirit::ascii::space_type> expression; boost::spirit::qi::rule<Iterator, value_t(), boost::spirit::ascii::space_type> term; boost::spirit::qi::rule<Iterator, value_t(), boost::spirit::ascii::space_type> factor; }; int main() { using boost::spirit::ascii::space; typedef std::string::const_iterator iterator_type; calc_parser<iterator_type> calc; std::string line; while (std::getline(std::cin, line)) { boost::trim(line); if(line=="quit") break; iterator_type first=line.cbegin(); iterator_type last=line.cend(); value_t v; bool r=phrase_parse(first, last, calc, space, v); if (r && first==last) { std::cout << v << std::endl; } else { std::cout << "Parse error" << std::endl; } } return 0; }
这个我也不懂,帮不了你
不少方法啊 表达式树,两个栈(操作数栈,运算符栈),文法等等
直接贴个以前写过的给你吧。
这个我也不懂,帮不了你