-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOctToDec.cpp
More file actions
35 lines (34 loc) · 1.08 KB
/
OctToDec.cpp
File metadata and controls
35 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <regex>
#include <math.h>
#include <tchar.h>
int _tmain(int argc, _TCHAR* argv[])
{
const unsigned int nMaxOctDigits = static_cast<unsigned int>(ceil(log2(UINT32_MAX)/3));
const std::regex oRegEx("^[0-7]+$");
long long nOctPower = 1L;
long long nDecNum = 0L;
std::string strLine;
system("cls");
std::cout << "Input an octal number\r\n";
std::getline(std::cin, strLine);
unsigned int nStrLen = static_cast<unsigned int>(strLine.length());
bool bIsRightString = (nStrLen <= nMaxOctDigits) && (std::regex_match(strLine, oRegEx));
if (!bIsRightString)
{
std::cout << "Wrong octal number format!!!\r\n";
std::cin.get();
return 0;
}
for (unsigned int i = 0; i < nStrLen; i++)
{
unsigned int nOctDigit = strLine[nStrLen - 1 - i] - '0';
nDecNum += (nOctDigit * nOctPower);
nOctPower *= 8L;
}
std::cout << "The decimal equivalent of the octal number "
<< strLine << " is: " << nDecNum << "\r\n";
std::cin.get();
return 0;
}