octal to decimal
Sun Nov 15 2020 02:54:42 GMT+0000 (UTC)
Posted by
@mahmoud hussein
#c++
#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>
using namespace std;
int octal2Dec(string& octal);
bool validity(int number);
int stringToNum(string number);
int main()
{
string number;
getline (cin , number);
cout << octal2Dec(number);
return 0;
}
int octal2Dec(string& octal)
{
int digits, decimal = 0, count = 0,oct = stringToNum(octal);
while (oct != 0)
{
digits = oct % 10;
if(validity(digits) == true)
decimal += digits* pow(8,count);
else
{
return NAN;
}
oct /= 10;
count++;
}
return decimal;
}
bool validity(int number)
{
return number % 8 == 0 || number % 9 == 0? false : true;
}
int stringToNum(string number)
{
int convertedNumber = 0;
for (int i = 0; i < number.length(); i++)
{
convertedNumber = (convertedNumber * 10) + static_cast<int>(number[i] - '0');
}
return convertedNumber;
}
content_copy Copy
http://cpp.sh/
Comments