fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. long long stringToLongLong(string myString, unsigned short base = 10)
  8. {
  9. std::string::iterator beginningOfNumber = myString.begin(),
  10. pointerToWhereYouLeftItLastTime = myString.begin();
  11.  
  12. long long returnValue = 0;
  13.  
  14. if (base >= 2 || base <= 10)
  15. {
  16. while (pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime < 48 || *pointerToWhereYouLeftItLastTime > 48 + base - 1))
  17. {
  18. pointerToWhereYouLeftItLastTime++;
  19. }
  20.  
  21. if (pointerToWhereYouLeftItLastTime != myString.end())
  22. {
  23. beginningOfNumber = pointerToWhereYouLeftItLastTime;
  24. }
  25. while (pointerToWhereYouLeftItLastTime != myString.end() && *pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 48 + base - 1)
  26. {
  27. pointerToWhereYouLeftItLastTime++;
  28. }
  29. }
  30. else if (base <= 16)
  31. {
  32. while (pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime < 48 || *pointerToWhereYouLeftItLastTime > 57) && (*pointerToWhereYouLeftItLastTime < 65 || *pointerToWhereYouLeftItLastTime > 65 + base - 11) && (*pointerToWhereYouLeftItLastTime < 97 || *pointerToWhereYouLeftItLastTime > 97 + base - 11))
  33. {
  34. pointerToWhereYouLeftItLastTime++;
  35. }
  36.  
  37. if (pointerToWhereYouLeftItLastTime != myString.end())
  38. {
  39. beginningOfNumber = pointerToWhereYouLeftItLastTime;
  40. }
  41. while (pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 57 || *pointerToWhereYouLeftItLastTime >= 65 && *pointerToWhereYouLeftItLastTime <= 65 + base - 11 || *pointerToWhereYouLeftItLastTime >= 97 && *pointerToWhereYouLeftItLastTime <= 97 + base - 11))
  42. {
  43. pointerToWhereYouLeftItLastTime++;
  44. }
  45. }
  46.  
  47. if (beginningOfNumber < myString.end())
  48. {
  49. for (unsigned long long lenght = pointerToWhereYouLeftItLastTime - beginningOfNumber; lenght > 0; lenght--)
  50. {
  51. returnValue += (*beginningOfNumber - '0')*std::pow(base, (lenght - 1));
  52.  
  53. beginningOfNumber++;
  54. }
  55. }
  56.  
  57. return returnValue;
  58. }
  59.  
  60. int main()
  61. {
  62. string s = "1000";
  63. long long l = stringToLongLong(s);
  64. std::cout << l << std::endl;
  65. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1000