Advertisement
MeehoweCK

Untitled

Jul 30th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int stringtoint(string liczba)
  7. {
  8.     int wynik = 0, potega = 1;
  9.  
  10.     for(int i = (liczba.size() - 1); i >= 0; --i)
  11.     {
  12.         wynik += (static_cast<int>(liczba[i]) - 48) * potega;
  13.         potega *= 10;
  14.     }
  15.  
  16.     return wynik;
  17. }
  18.  
  19. double stringtodouble(string liczba)
  20. {
  21.     unsigned separator;
  22.     for(unsigned i = 0; i < liczba.size(); ++i)
  23.         if(liczba[i] == '.')
  24.         {
  25.             separator = i;
  26.             break;
  27.         }
  28.     double potega;
  29.     if(separator == 0)
  30.         potega = 0.1;
  31.     else
  32.     {
  33.         potega = 1;
  34.         for(unsigned i = 1; i < separator; ++i)
  35.             potega *= 10;
  36.     }
  37.  
  38.     double wynik = 0;
  39.     for(unsigned i = 0; i < liczba.size(); ++i)
  40.     {
  41.         if(i == separator)
  42.             continue;
  43.         wynik += (static_cast<int>(liczba[i]) - 48) * potega;
  44.         potega /= 10;
  45.     }
  46.     return wynik;
  47. }
  48.  
  49. int main()
  50. {
  51.     string liczba = ".2438";
  52.     double dliczba = stringtodouble(liczba);
  53.     cout << dliczba << endl;
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement