Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<string>
- #include<cmath>
- using namespace std;
- int main() {
- cout << "enter the number: ";
- string s;
- bool x = true;
- while (x)
- {
- getline(cin, s);
- x = false;
- for (int i = 0; i < s.size(); i++)
- {
- if (s[i] != 48 && s[i] != 49)
- {
- cout << "inavalid input, try again:";
- s.clear();
- x = true;
- }
- }
- }
- long long int dec = 0;
- int count = 0;
- for (int i = s.length() - 1; i > 0; i--)
- {
- if (s[0] = 1)
- {
- if (s[i] == 0)
- {
- s[i] = 1;
- }
- else
- {
- s[i] = 0;
- }
- dec += pow(2, count) * (s[i] - '0');
- count++;
- }
- else {
- dec += pow(2, count) * (s[i] - '0');
- count++;
- }
- }
- if (s[0] == 1)
- {
- cout << endl << - dec;
- }
- else
- {
- cout << endl << dec;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement