文件是这样的:
[dn][dn]
我写了以下代码:
public void calcButtonClick(View view) {
Button b = (Button) view;
String buttonText = b.getText().toString();
// TODO: Case: dn[extraneous numbers]
// TODO: Case: d[n]d[n]
codeStr = codeStr + buttonText;
etCalc.setText(codeStr);
}
}
输出是:
A(20,50)(40,50)(45,67)
B(20,60)(40,50)(45,67)
C(30,70)(40,50)(45,67)
等等。
答案 0 :(得分:0)
由于您将问题标记为C ++,我将提供C ++答案。
使用std::string
和std::getline
读取一行文字,然后使用std::string
中的函数来解析和隔离字段。
示例:
std::ifstream input("test2file");
if (!input)
{
std::cerr << "Error opening data file\n";
return EXIT_FAILURE;
}
std::string text_row;
std::string::size_type number_position = 0;
int sum = 0;
while (std::getline(input, text_row)
{
static const char digits[] = "0123456789";
std::string::size_type number_position = text_row.find_first_of(digits, number_position);
std::string::size_type number_end_position =
text_row.find_first_not_of(digits, number_position);
std::string number_text = text_row.substr(number_position,
number_end_position - number_position);
std::istringstream number_stream(number_text);
int number = 0;
number_stream >> number;
sum += number;
number_position = number_end_position + 1;
}