我必须以这种格式读取文件中的输入:
12 + 56
9 divided by -3
45 minus 15
Min of 2 and 1
Max of 3 and 5
34 plus 33
我必须以这种格式输出到另一个文件:
12 + 56 is 68
9 divided by -3 is -3
45 minus 15 is 30
Min of 2 and 1 is 1
Max of 3 and 5 is 5
34 plus 33 is 67
我已经以这种方式接近它,但却陷入了困境。我将每行读入一个字符串但是如何访问字符串中的操作数,因为它们位于不同位置的不同位置?
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int compute(string s) {
//Not sure how to do this
return 0;
}
int main() {
ifstream in;
ofstream out;
string s1 = "Input.txt";
string s2 = "CalculationResults.txt";
string s;
int operand1, operand2, result;
in.open(s1);
out.open(s2);
if (in) {
while (getline(in, s)) {
result = compute(s);
out << s << " is " << result << endl;
}
in.close();
out.close();
} else {
cout << "\nCouldn't find the input file\n\n";
}
system("pause");
return 0;
}
答案 0 :(得分:2)
我认为最简单的方法是读取第一个空格中的字符串,将其与命令进行比较,如果它不匹配,则将其转换为数字:
if (in1 >> firstthing) {
out << firstthing << " ";
if (firstthing == "Min") {
result = compute_min(in1, out); //reads in " of " and then the numbers. Also writes.
} else if (firstthing == "Max") {
result = compute_max(in1, out); //reads in " of " and then the numbers. Also writes.
} else {
int first_operand = atoi(firstthing.c_str());
//reads in an operator, then the second operand. Also writes.
result = compute_expression(in1, out, first_operand);
}
out << " is " << result << endl;
}
各种功能体相对简单:
int compute_min(std::istream& in1, std::ofstream& out) {
std::string ofstring;
int first_operand;
std::string andstring;
int second_operand;
in1 >> ofstring >> first_operand >> andstring >> second_operand;
out << ofstring << first_operand << andstring << second_operand;
return std::min(first_operand, second_operand);
}
int compute_expression(std::istream& in1, std::ofstream& out, int first_operand) {
std::string operation;
int second_operand;
in1 >> operation >> second_operand;
out << operation << second_operand;
if (operation=="+" || operation=="plus")
return first_operand + second_operand;
else if
//more
else
throw std::runtime_error("Invalid operation "+operation);
}
答案 1 :(得分:2)
我被邀请在这方面打击不可避免的提升精神方法。
这是中心部分:
auto value_ = qi::create_parser<V>();
start = eval;
eval = no_case [
as_string [ raw [ value_[_a=_1] >> ('+' | lit("plus")) >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a+_b) << "\n" ]
| as_string [ raw [ value_[_a=_1] >> ('-' | lit("minus")) >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a-_b) << "\n" ]
| as_string [ raw [ value_[_a=_1] >> ('/' | lit("divided") >> "by") >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a/_b) << "\n"]
| as_string [ raw [ value_[_a=_1] >> ('*' | lit("multiplied") >> "by") >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a*_b) << "\n" ]
| as_string [ raw [ lit("min") >> "of" >> value_[_a=_1] >> "and" >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << min_(_a,_b) << "\n" ]
| as_string [ raw [ lit("max") >> "of" >> value_[_a=_1] >> "and" >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << max_(_a,_b) << "\n" ]
]
;
请注意,语法对于值类型是通用的,这意味着如果您愿意,可以使用double
,甚至cpp_dec_float
。即使std::complex<T>
也可能有用。
源迭代器也是如此。这里的驱动程序显示了如何将它与直接std::cin
流迭代器一起使用:
<强> Live On Coliru 强>
int main() {
using It = boost::spirit::istream_iterator;
It f(std::cin >> std::noskipws), l;
bool ok = qi::phrase_parse(f, l, verbiage<It>() % qi::eol, qi::blank);
std::cout << "Success: " << std::boolalpha << ok << "; Remaining: '" << std::string(f,l) << "'\n";
}
打印
12 + 56 is 68
9 divided by -3 is -3
45 minus 15 is 30
Min of 2 and 1 is 1
Max of 3 and 5 is 5
34 plus 33 is 67
Success: true; Remaining: '
'
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/phoenix/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
template <typename It, typename V = long, typename Skipper = qi::blank_type>
struct verbiage : qi::grammar<It, Skipper> {
verbiage() : verbiage::base_type(start) {
using namespace qi;
auto value_ = qi::create_parser<V>();
start = eval;
eval = no_case [
as_string [ raw [ value_[_a=_1] >> ('+' | lit("plus")) >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a+_b) << "\n" ]
| as_string [ raw [ value_[_a=_1] >> ('-' | lit("minus")) >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a-_b) << "\n" ]
| as_string [ raw [ value_[_a=_1] >> ('/' | lit("divided") >> "by") >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a/_b) << "\n"]
| as_string [ raw [ value_[_a=_1] >> ('*' | lit("multiplied") >> "by") >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << (_a*_b) << "\n" ]
| as_string [ raw [ lit("min") >> "of" >> value_[_a=_1] >> "and" >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << min_(_a,_b) << "\n" ]
| as_string [ raw [ lit("max") >> "of" >> value_[_a=_1] >> "and" >> value_[_b=_1] ] ] [ std::cout << _1 << " is " << max_(_a,_b) << "\n" ]
]
;
}
private:
struct min_f {
using result_type = V;
template <typename A, typename B> V operator()(A a, B b) const { return std::min(a,b); }
};
struct max_f {
using result_type = V;
template <typename A, typename B> V operator()(A a, B b) const { return std::max(a,b); }
};
phx::function<min_f> min_;
phx::function<max_f> max_;
qi::rule<It, Skipper, qi::locals<V,V> > eval;
qi::rule<It, Skipper> start;
};
int main() {
using It = boost::spirit::istream_iterator;
It f(std::cin >> std::noskipws), l;
bool ok = qi::phrase_parse(f, l, verbiage<It>() % qi::eol, qi::blank);
std::cout << "Success: " << std::boolalpha << ok << "; Remaining: '" << std::string(f,l) << "'\n";
}
答案 2 :(得分:0)
您所寻找的内容似乎是std::strtok
。读完行后,可以使用std :: strtok将其解析为字符串向量。 Here是描述该功能的页面的链接,以及使用示例。一旦你有了一个标记向量,你就可以遍历它们并分别检查每个单独的元素。