作者dendrobium (石斛兰)
看板C_and_CPP
标题Re: [问题] cin 的分隔符号
时间Wed Jul 25 09:52:20 2007
※ 引述《chrisdar (克里斯)》之铭言:
: 程式:
: int a=0,b=0;
: cin >> a >> b;
: cout << a << "," << b << endl;
: 输入:
: 20 20 --> 测试OK
: (20,20) --> 值都没写进去
: 想问的是:能够把'(' ',' ')'加入cin中当分隔符吗? 原先只有 '\n' '\t' ' '三种分隔符
#include<iostream>
using namespace std;
int main()
{
int a,b;
char ch;
cin.get(ch);
int tmp;
while(ch!=')')
{
if( ch == '(' )
tmp=0;
else if( ch == ',' )
{
a=tmp;
tmp=0;
}
else//(input[i]是数字)
tmp=tmp*10+( (int)ch - ((int)'0') );
cin.get(ch);
}
b=tmp;
cout<<"a="<<a<<"\nb="<<b<<endl;
system("PAUSE");
return 0;
}
或
#include<iostream>
using namespace std;
int main()
{
int a,b;
char input[20];
cin.getline(input,20);
int i=0;
int tmp;
while( input[i] != '\0' )
{
if( input[i] == '(' )
tmp=0;
else if( input[i] == ',' )
{
a=tmp;
tmp=0;
}
else if( input[i] == ')' )
b=tmp;
else//(input[i]是数字)
tmp=tmp*10+( (int)input[i] - ((int)'0') );
i++;
}
cout<<"a="<<a<<"\nb="<<b<<endl;
system("PAUSE");
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.123.8.25
※ 编辑: dendrobium 来自: 122.123.8.25 (07/25 12:43)
1F:→ dendrobium:被m? 07/25 13:40
2F:推 chrisdar:後来我放弃使用非原先定义的分隔符号当分隔符号改用正规 07/25 14:22
3F:→ chrisdar: ' ' '\n' '\t' 当分隔符号. 07/25 14:22