-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstoi.cpp
49 lines (46 loc) · 933 Bytes
/
stoi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <iostream>
using namespace std;
bool stoi(const string& s, int& n)
{
if(s.length() == 0)
return false;
bool minus = false;
for(int i=0; i<s.length(); i++){
if(i == 0){
if(s[i] == '+')
continue;
else if(s[i] == '-'){
minus = true;
continue;
}
else if(!isdigit(s[i]))
return false;
else{
n = s[i]-'0';
}
}
else{
if(!isdigit(s[i]))
return false;
n = n*10 + (s[i]-'0');
}
}
if(minus)
n = -n;
return true;
}
int main()
{
while(1){
string str;
int n;
cin >> str;
bool res = stoi(str, n);
if(res)
cout<<n<<endl;
else
cout<<"illegal integer"<<endl;
}
return 0;
}