- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个控制台应用程序,我可以在其中输入复数。一切正常,但我希望输入格式更加灵活。现在它只接受 a+bi,我想这样做,如果我只输入 3 或 -2i,它仍然有效。
Complex console::parseComplex(std::string cmp) {
float a, b;
char i;
std::stringstream ss(cmp);
if (ss >> a >> b >> i && i == 'i')
return { a, b };
else return { true };
据我所知,如果我添加以下内容
else if (ss >> a >> i && i == '\0')
return { a, 0 };
else if (ss >> b >> i && i == 'i')
return { 0, i };
在我的最后一行之前,它不会工作,因为 >> 通过 ss。字符串中不会有任何空格。我不确定如何在不重铸 ss 的情况下更改它(我认为这不是最好的方法)
而且我不确定如何过滤掉任何被转换为整数的随机字符并发送出去,检查 '\0' 感觉不对。
谢谢!
(顺便说一句,return {true} 会在被检查的类上转到 m_fail,这是我能想到的最简单的错误处理方法,但我是新手,所以如果你有任何关于该主题的文档我想阅读更多)
最佳答案
我已经建议编写一个小型解析器(例如,基于 SO: How to rearrange a string equation? 中所示的语法图)。
下面我按照OP尝试的方式做了一个更简单的方法(没有画语法图)。
即我的助手类型 Complex
的输入流运算符尝试读取两个数字。
如果第一个数字后跟一个 i
这实际上是第二个数字并且输入结束。
如果第二个数字后面没有跟 i
它实际上不是第二个但丢失了,流将重置到之前的位置。
示例代码:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
struct Complex {
float re, im;
};
std::istream& operator >> (std::istream &in, Complex &value)
{
value.im = 0;
// read 1st value
if (!(in >> value.re)) return in; // ERROR!
// check whether next char is 'i'
char c; if (!(in >> c)) return in; // End of Input
if (c == 'i') {
value.im = value.re; value.re = 0.0;
return in;
}
in.unget();
// remember start of imaginary
std::streampos pos = in.tellg();
if (!(in >> value.im)) return in; // ERROR or end of input
if (!(in >> c) || c != 'i') { // ERROR or premature end of input
value.im = 0.0;
in.clear();
in.seekg(pos);
return in;
}
return in;
}
std::ostream& operator << (std::ostream &out, const Complex &value)
{
return out << value.re << (std::signbit(value.im) ? "" : "+") << value.im << 'i';
}
int main()
{
// samples:
std::string samples[] = {
"1.23+45i",
" 1.23 +45i",
" 1.23E3 +45e-1i",
"Completely wrong!",
// multiple numbers in one line
"1.23+45i 1.23 +45i 1.23E3 +45e-1i",
"1.23 2.34-1i -2i"
};
int line = 1;
for (const std::string &sample : samples) {
std::cout << "Parse line " << line++ << " '" << sample << "':\n";
std::istringstream in(sample);
std::cout << "Got:";
Complex value;
while (in >> std::skipws >> value) std::cout << ' ' << value;
std::cout << '\n'
<< "Stopped with "
<< (in.eof() ? "End of Input" : !in.good() ? "Error" : "???")
<< '\n';
}
return 0;
}
编译和测试:
g++ (GCC) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Parse line 1 '1.23+45i':
Got: 1.23+45i
Stopped with End of Input
Parse line 2 ' 1.23 +45i':
Got: 1.23+45i
Stopped with End of Input
Parse line 3 ' 1.23E3 +45e-1i':
Got: 1230+4.5i
Stopped with End of Input
Parse line 4 'Completely wrong!':
Got:
Stopped with Error
Parse line 5 '1.23+45i 1.23 +45i 1.23E3 +45e-1i':
Got: 1.23+45i 1.23+45i 1230+4.5i
Stopped with End of Input
Parse line 6 '1.23 2.34-1i -2i':
Got: 1.23+0i 2.34-1i 0-2i
Stopped with End of Input
虽然摆弄我仍然意识到一些弱点,例如1.23 + 45i
无法读取,因为 +
和 45
之间有空格。所以,有了更多的代码,这肯定会得到改进。另一方面...... OP 没有具体说明哪里应该接受空格,哪里不接受空格。
关于c++ - 从字符串中解析复数,考虑语法变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54413316/
我一直认为使用“if”比捕获异常要好得多(就性能而言)。例如,这样做: User u = Users.getUser("Michael Jordan"); if(u!=null) System.
我正在尝试使用下一个格式将字符转换为日期。我有下一个数据框 i (我在最后添加了 dput() 版本的数据框): Date 1 Dec_28_2012_9:
考虑到 PHP 中的日期戳,我如何计算持续时间?我在日期之间使用的日期格式是“Y-m-d H:i:s”, 我的工作代码只能计算时间之间的持续时间而不考虑日期。 下面是我的代码: $assigned_t
我正在尝试解释 GLMM 中的自相关。我的响应变量是 bool 值,它表示一组 hive 的生命周期中是否存在 en 事件。我试图用一组描述每个巢状态的数值变量来预测此类事件的概率。因此,我在广义模型
我对如何解释 undefined variable 感到有点困惑(我不确定你现在是否可以)。我正在尝试使用以下代码底部附近的 if else 语句(已注释掉的行)。 这个想法是,如果请求歌曲的人不是与
Bjarne Stroustrup 的 The C++ Programming Language Fourth Edition 中的以下内容是什么意思? "Consider . (dot) suspe
我想要一个主元素,边 block 漂浮在它的右侧。我不知道边 block 的数量,也不知道它们的最终总高度。但是我的主要元素应该具有相同的高度(请参阅以下示例以更好地理解),而无需使用列。 (虚线部分
我在每个 TextView 上都有以下警告(来自 Lint),在我的 XML 中有一个 ID。 Consider making the text value selectable by specify
目前,我有 6 条曲线,以 6 种不同的颜色显示,如下所示。 这 6 条曲线实际上是由 一个相同实验 的 6 次试验生成的。这意味着,理想情况下它们应该是相同的曲线,但由于噪声和不同的试验参与者,它们
winner of a recent Wikipedia vandalism detection competition建议可以通过“检测考虑到 QWERTY 的随机键盘点击来改进检测键盘布局”。 示
多年来,我一直在编写 C 语言,主要是在嵌入式环境中,并且对指针有一个非常好的心智模型——我不必明确地考虑如何使用它们,我对指针算法 100% 感到满意,指针数组,指针指针等。 我写的 C++ 很少,
我正在使用 Boost.Date_time 来获取两个日期之间的时差。我希望代码在这些天也考虑夏令时的变化,并给我正确的时间间隔。 考虑这个例子。 2015 年 11 月 1 日,美国的 DST 将发
我有一个(人类)名字的向量,全部用大写字母表示: names <- c("FRIEDRICH SCHILLER", "FRANK O'HARA", "HANS-CHRISTIAN ANDERSEN")
我想呈现一个表单小部件。这是我要生成的原始 HTML: 使用这个: {{ form_row(form.email, { 'type' : 'email', 'attr' : { 'class' :
我正在开发一个 python 项目,它使用 pythonnet 和几个 C# dll 作为依赖项。 由于我不想将 dll 推送到 git 存储库,因此我调整了 .gitignore 文件。但是,现在
考虑到上午/下午,我想将字符串转换为 php 数据时间。 我想将 '03/06/2015 12:17 am' 转换为 php datatime。 我试过了, $myDateTime = DateTim
我想排除那些具有相同标题和同一年份的实例。 title votes ranking year 0 Wonderland 19 7.9 1931 1
例如对于一个 EditText,通常指定 android:inputType="numberDecimal"用于文本字段应该包含十进制数。但这假设“。”用作小数点分隔符,在某些国家/地区使用“,”代替
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
作为练习,我决定学习 Java 中的 lambda 表达式。我想重写我发现笨拙且冗长的旧代码。它检查命令行参数是否是(1)文件路径或(2)目录路径。在(1)场景中,它将命令行参数传递给方法。在 (2)
我是一名优秀的程序员,十分优秀!