fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int atoi(string str);
  4. int main()
  5. {
  6. int t;
  7. cin>>t;
  8. while(t--)
  9. {
  10. string s;
  11. cin>>s;
  12. cout<<atoi(s)<<endl;
  13. }
  14. }
  15.  
  16. /*This is a function problem.You only need to complete the function given below*/
  17. /*You are required to complete this method */
  18. bool isnumeric(char c){
  19. if(c >= '0' && c <= '9')
  20. return true;
  21. return false;
  22. }
  23. int atoi(string str)
  24. {
  25. int i = 0;
  26. int result = 0;
  27. int sign = 1;
  28. while(str[i]) {
  29. if(str[i] == ' '){
  30. i++;
  31. }
  32. if(str[i] == '-') {
  33. sign = -1;
  34. i++;
  35. }
  36. if(isnumeric(str[i])==false){
  37. //cout << -1 << endl;
  38. return -1;
  39. }
  40. result = result * 10 + str[i++] - '0';
  41. }
  42. return sign * result;
  43. }
Success #stdin #stdout 0s 4172KB
stdin
2
1234
12e
stdout
1234
-1