Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Antonio VIllanueva Binario -> decimal
- #include <iostream>
- #include <cmath> //pow
- #include <algorithm> // std::reverse
- using namespace std;
- class binarioDecimal {
- public:
- binarioDecimal (const string & str):str(str), decimal (0){};
- void conversionDecimal(){//bin->dec
- std::reverse(str.begin(), str.end());
- for (int n= ( str.length() ) ; n>=0 ; n--){
- if (str[n]=='1'){
- cout <<pow(2, n)<<" ==== "<< str[n]<< " , n = "<<n <<endl;
- decimal += pow(2, n);
- }
- }
- }
- int getDecimal (){return decimal;}
- private:
- string str;
- int decimal;
- };
- int main(int argc, char *argv[])
- {
- string s="0000011111";
- binarioDecimal d(s);
- d.conversionDecimal();
- cout << s<<" = "<<d.getDecimal()<< endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement