c++忽略特定的字符
C++ ignore specific character while cin
我的输入如下:
4 5i 6 7i
不查找并替换为字符串,
我想将这些单独的值存储在double
中但是忽略了'i'
a = 4, b = 5, c = 6, d = 7下面是我正在处理的丑陋代码
我迷路了,请帮帮我
谢谢!
char c, real[1024], img[1024];
int i = 0;
bool flag = false,
flag2 = false;
while( c = input.get() )
{
if( input.peek() == ' '){
i = 0;
flag2 = true;
}
if( !flag2 ){
real[ i++ ] = c;
}else{
img[ i++ ] = c;
}
if( flag )
{
break;
}
while( input.peek() == 'i' )
{
if( input.peek() == 'i' )
{
flag = true;
i = 0;
flag2 = true;
}if( input.peek() == ' ' )
{
i = 0;
flag2 = true;
}
input.ignore(1, 'i');
}
}
if( flag ){
obj.doubleValueA = 0.0 ;
obj.doubleValueB = atof( real );
return input;
}else{
obj.doubleValueA = atof( real );
obj.doubleValueB = atof( img );
}
return input; // enables cin >> a >> b >> c
#include<iostream>
#include<vector>
#include<sstream>
#include<ctype>
#include<string>
using namespace std;
int parseNumber( string number )
{
unsigned i = 0;
// Find the position where the character starts
while ( i < number.size() && isdigit( number[i] )
{
i++;
}
number = number.substr( 0, i );
// Retrieve the number from the string using stringstream
stringstream ss( number );
int result;
ss >> result;
// Return the result
return result;
}
int main()
{
string input;
getline( cin, input );
stringstream ss( input );
string number;
vector<int> complexNumbers;
while ( ss >> number )
{
complexNumbers.push_back( parseNumber( number ) );
}
for ( unsigned i = 0; i < complexNumbers.size(); i++ )
{
cout << complexNumbers[i] << " ";
}
}
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存