作者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