Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int stringtoint(string liczba)
- {
- int wynik = 0, potega = 1;
- for(int i = (liczba.size() - 1); i >= 0; --i)
- {
- wynik += (static_cast<int>(liczba[i]) - 48) * potega;
- potega *= 10;
- }
- return wynik;
- }
- double stringtodouble(string liczba)
- {
- bool ujemna = false;
- if(liczba[0] == '-')
- ujemna = true;
- unsigned separator;
- for(unsigned i = 0; i < liczba.size(); ++i)
- if(liczba[i] == '.')
- {
- separator = i;
- break;
- }
- double potega;
- if(separator == 0)
- potega = 0.1;
- else
- {
- potega = 1;
- for(unsigned i = 1; i < separator; ++i)
- potega *= 10;
- if(ujemna)
- potega /= 10;
- }
- double wynik = 0;
- for(unsigned i = 0; i < liczba.size(); ++i)
- {
- if(ujemna && i == 0)
- continue;
- if(i == separator)
- continue;
- wynik += (static_cast<int>(liczba[i]) - 48) * potega;
- potega /= 10;
- }
- if(ujemna)
- return -wynik;
- return wynik;
- }
- int main()
- {
- string liczba = "8.2438";
- double dliczba = stringtodouble(liczba);
- cout << dliczba << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement