Read complex numbers (a+bi) from text file in C++

前端 未结 4 1546
难免孤独
难免孤独 2021-01-15 09:03

i want to read an array of complex numbers (in a+bi form). There are several suggestions I found on the internet, however those methods only result the re

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-15 09:26

    Your requirement (the format of input you seek) does not correspond to the method you are using to read it. The default streaming of a complex is the real and the imaginary part separated by spaces and in brackets - the sign between is not mandatory, and the trailing i is not required.

    The obvious way to parse for input you seek is to read the real part, read a character, check if the character is a signed (+ or -) and ignore spaces, then read the imaginary part. That is trivial with istreams.

提交回复
热议问题