假设我有
int a; cin >> a;
int b; cin >> b;
int c; cin >> c;
我想知道如何从两个输入中得到相同的结果:
1.2.3和1 2 3
如果我输入1.2.3
,则a为1,b为2,c为3答案 0 :(得分:6)
您可以使用std::scanf
:
if (std::scanf("%d%*1[ .]%d%*1[ .]%d", &a, &b, &c) == 3) {
... // Input was successful
}
棘手的部分是mat说明符:%*1[ .]
它扫描并忽略单个空格或单个点。
答案 1 :(得分:1)
这篇文章被标记为C ++。
这是一些需要考虑的C ++:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cassert>
// forward
// stream input, extract and discard junk char
void trial1(std::string s);
// stream input, ignore() junk char,
// with error check for each integer input
void trial2(std::string s);
// sscanf of string
void trial3(std::string s);
int main(int , char** )
{
std::cout << "\n"; // stream input, extract and discard junk char
trial1("1.2.3");
trial1("1,2,3");
trial1("1 2 3"); // fail
std::cout << "\n"; // stream input, ignore junk char, error checks
trial2("1.2.3");
trial2("1,2,3");
trial2("1 2 3");
trial2("11.12.13");
trial2("111,222,333");
trial2("1234 1234 1234");
trial2("1234 -12345678 1234");
trial2("1x2y3");
trial2("1xx2yy3"); // fail - ignore skips 1 char, not 2
std::cout << "\n"; // sscanf of std::string
trial3("1.2.3");
trial3("1 2 3");
trial3("1,2,3");
trial3("111,222,333");
trial3("1234 -12345678 1234");
trial3("1234,1234,1234");
trial3("1xx2yy3"); // fail
std::cout << "\n"; // requirements?
trial1("foo bar");
trial2("foo bar");
trial3("foo bar");
}
void trial1(std::stringstream& sin)
{
char junk;
int a = -1; sin >> a >> junk;
int b = -1; sin >> b >> junk;
int c = -1; sin >> c >> junk;
std::cout << " --> " << a << " " << b << " " << c;
}
void trial1(std::string s)
{
std::cout << '\n' << __FUNCTION__ << " '" << s << "'";
std::stringstream sin (s);
trial1(sin);
}
void trial2(std::stringstream& sin)
{
int a = -1;
int b = -1;
int c = -1;
do{
sin >> a;
if(!sin.good() && !sin.eof()) // check for error
std::cerr << " error on a " << std::flush;
sin.ignore(); // ignore 1 char
sin >> b;
if(!sin.good() && !sin.eof()) // check for error
std::cerr << " error on b " << std::flush;
sin.ignore();
sin >> c;
if(!sin.good() && !sin.eof()) // check for error
std::cerr << " error on c " << std::flush;
}while(0);
std::cout << " --> " << a << " " << b << " " << c;
}
void trial2(std::string s)
{
std::cout << '\n' << __FUNCTION__ << " '" << s << "'";
std::stringstream sin (s);
trial2(sin);
}
void trial3(std::string s)
{
std::cout << '\n' << __FUNCTION__ << " '" << s << "'";
int a = -1;
int b = -1;
int c = -1; // note added ','----v---------v
if (std::sscanf(s.c_str(), "%d%*1[ .,]%d%*1[ .,]%d", &a, &b, &c) == 3)
std::cout << " --> " << a << " " << b << " " << c;
else
std::cout << " --> " << a << " " << b << " " << c << " FAILED." << std::flush;
}
带输出:
trial1 '1.2.3' --> 1 2 3
trial1 '1,2,3' --> 1 2 3
trial1 '1 2 3' --> 1 3 -1
trial2 '1.2.3' --> 1 2 3
trial2 '1,2,3' --> 1 2 3
trial2 '1 2 3' --> 1 2 3
trial2 '11.12.13' --> 11 12 13
trial2 '111,222,333' --> 111 222 333
trial2 '1234 1234 1234' --> 1234 1234 1234
trial2 '1234 -12345678 1234' --> 1234 -12345678 1234
trial2 '1x2y3' --> 1 2 3
trial2 '1xx2yy3' error on b error on c --> 1 0 -1
trial3 '1.2.3' --> 1 2 3
trial3 '1 2 3' --> 1 2 3
trial3 '1,2,3' --> 1 2 3
trial3 '111,222,333' --> 111 222 333
trial3 '1234 -12345678 1234' --> 1234 -12345678 1234
trial3 '1234,1234,1234' --> 1234 1234 1234
trial3 '1xx2yy3' --> 1 -1 -1 FAILED.
trial1 'foo bar' --> 0 -1 -1
trial2 'foo bar' error on a error on b error on c --> 0 -1 -1
trial3 'foo bar' --> -1 -1 -1 FAILED.
答案 2 :(得分:0)
您可以使用getline()获取整个输入,使用strtok()来标记字符串。您可能需要验证输入等。以下是一个示例。
#include <iostream>
#include <string.h>
using namespace std;
void DoSomething(char* inputChar){
printf("%s\n", inputChar);
}
int main() {
char myInput[256];
char* pch;
char* delimiters = " .";
//Loop Through Input
while (cin.getline(myInput,256)) {
cout << "-" << myInput << endl;
pch = strtok(myInput, delimiters);
while (pch != NULL){
//Individual input chars here
DoSomething(pch);
//Set NULL to find next delimited set of char
pch = strtok(NULL, delimiters);
}
}
return 0;
}
您可以修改分隔符列表以及是否使用输入存储或执行其他操作。如果你只需要使用一次,显然getline()可以从外部while()中取出。 atoi()也可以尝试将char *转换为int。
我使用了输入
1 2 3
1.2.3
1
答案 3 :(得分:0)
您可以将文字作为字符串阅读,替换&#39;。&#39;使用空格,然后使用std::istringstream
转换为数字:
std::string text_read;
std::getline(cin, text_read);
std::string::size_type position = text_read.find('.');
while (position != std::string::npos)
{
text_read[position] = ' ';
position = text_read.find('.');
}
int a;
int b;
int c;
std::istringstream text_stream(text_read);
text_stream >> a;
text_stream >> b;
text_stream >> c;
这是一种蛮力技术。您可以使用std::transform
进行优化。