如何用cin读取特定格式? 例如: - 为了读取一个复数,我希望用户像往常一样输入它:x + yi, 所以我想要这样的: c >> >> >> >> >> >> >> >> “; 但是这是一个错误。什么是正确的方式?非常感谢。用cin读取特定格式
回答
一个非常简单的解决方案:
char plus,img;
double x,y;
cin>> x >> plus >> y >> img;
if (plus!='+' || img!='i') ...error
在 “现实生活” 的代码你建立/使用class complex
,和过载操作>>.
我尝试在Ideone:http://ideone.com/ZhSprF
#include <iostream>
using namespace std;
int main()
{
char plus{},img{};
double x{},y{};
cin>> x >> plus >> y >> img;
if (plus!='+' || img!='i')
cout << "\nError: "<< "x=" << x <<", plus=" << plus <<", y=" << y <<", img=" << img;
else
cout << "\nComplex: " << x << plus << y << img;
return 0;
}
stdin:3 + 4i
- > stdout:Complex: 3+4i
标准输入:1E4L1e3g
- >标准输出:Error: x=10000, plus=L, y=1000, img=g
标准输入:a+3i
- >标准输出:Error: x=0, plus=, y=0, img=
标准输入:1e3+93E-2i
- >标准输出:Complex: 1000+0.93i
'1E4L1e3g'将是一个适当的复数做法。 – 2013-02-15 19:20:16
此解决方案在读取'x'失败时不处理该情况,例如读取一个字母而不是数字。 – 2013-02-15 19:30:22
@BartekBanachewicz我无法理解你的意思。 '1E4L1e3g'出现错误。 – qPCR4vir 2016-05-12 15:29:11
这将是一定要好只使用std::complex
。
这里是说明我的意见伪代码:
struct Complex { double real, imag; }
istream& operator>> (Complex& c, istream& str)
{
// read until whitespace
char C;
string Buffer;
enum { READING_REAL, READING_PLUS, READING_IMAG }
State = READING_REAL;
C = str.peek();
while (/* C is not whitespace */)
{
// actually read C
switch(State) {
case READ_REAL:
// check if it fits allowed double syntax
// continue until first double is read
/* when it is
c.real = parsedouble(Buffer);
Buffer.clear();
State = READ_PLUS;
*/
case READ_PLUS:
// accept plus sign
case READ_IMAG:
// read 2nd double
}
}
}
注意,该操作可能会失败。
按照my answer on a vaguely related question,与流解析通常是一个坏主意,但可以做到:
我编写了一段代码,可以在字符串和字符阅读。与普通的流读取一样,如果它获取无效数据,它将设置流的坏点。这应该适用于所有类型的流,包括宽流。在头坚持这四个功能:
#include <iostream>
#include <string>
#include <array>
#include <cstring>
template<class e, class t, int N>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e(&literal)[N]) {
std::array<e, N-1> buffer; //get buffer
in >> buffer[0]; //skips whitespace
if (N>2)
in.read(&buffer[1], N-2); //read the rest
if (strncmp(&buffer[0], literal, N-1)) //if it failed
in.setstate(in.rdstate() | std::ios::badbit); //set the state
return in;
}
template<class e, class t>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e& literal) {
e buffer; //get buffer
in >> buffer; //read data
if (buffer != literal) //if it failed
in.setstate(in.rdstate() | std::ios::badbit); //set the state
return in;
}
//redirect mutable char arrays to their normal function
template<class e, class t, int N>
std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, e(&carray)[N]) {
return std::operator>>(in, carray);
}
,它将使输入字符很容易:
if (cin>>x>>"+">>y>>"i";) {
// read correctly
}
PROOF OF CONCEPT。现在,您可以使用cin
字符串和字符文字,并且如果输入不完全匹配,则它的行为与任何其他无法正确输入的类型相同。请注意,这只能匹配不是第一个字符的字符串中的空格。它只有三个功能,所有这些功能都很简单。
- 1. 以特定格式读取文件
- 2. Matlab的 - 读取特定格式线
- 3. 如何让std :: cin读取空格?
- 4. 用std :: cin整行读取
- 5. 用cin读取字符(C++)
- 6. 获取JSON特定格式
- 7. 从STD读取密码:: CIN
- 8. 如何使用空格直到换行符来读取cin?
- 9. VGG以特定格式提取特征
- 10. 阅读并以特定的格式
- 11. 如何从文件中读取特定格式的数据?
- 12. VBA:从特定格式的字符串读取日期
- 13. 如何读取特定格式的数据
- 14. 如何读取特定的数据格式
- 15. 读取文件并以特定格式打印。 C++
- 16. 读取具有特定格式的文本文件
- 17. 如何读取特定的输入格式
- 18. 读取来自特定模式
- 19. 特定格式
- 20. 提取特定格式的文本-perl
- 21. 获取输入特定格式在c
- 22. 提取字符串特定的格式
- 23. 如何让cin读取两次?
- 24. C++ cin char逐符号读取
- 25. C++“cin”只读取第一个字
- 26. 如何从文件中读取,然后继续从cin读取?
- 27. 格式特定值
- 28. 在特定格式
- 29. 在特定格式
- 30. 阅读cin至EOF
考虑使用'scanf',它可能是读取格式化数据的更好选择。 – andre 2013-02-15 19:17:48
阅读直到换行,然后在内部解析。 – 2013-02-15 19:19:24
实际上我用它来重载输入分辨率运算符,这就是为什么我需要cin – aceBox 2013-02-15 19:20:13