C++ c++;将八进制分数转换为十进制分数?
我目前正在开发一个程序,该程序将八进制分数作为输入,并将其转换为十进制分数。到目前为止,我有一部分代码将小数点之前的部分转换为十进制,而不是小数点之后的浮点。我试图使用模数,但没有成功,因为我的变量是浮点 有没有办法将小数点后的剩余数字从八进制转换为十进制?我已经在下面发布了我的代码。感谢您的帮助。谢谢C++ c++;将八进制分数转换为十进制分数?,c++,decimal,octal,C++,Decimal,Octal,我目前正在开发一个程序,该程序将八进制分数作为输入,并将其转换为十进制分数。到目前为止,我有一部分代码将小数点之前的部分转换为十进制,而不是小数点之后的浮点。我试图使用模数,但没有成功,因为我的变量是浮点 有没有办法将小数点后的剩余数字从八进制转换为十进制?我已经在下面发布了我的代码。感谢您的帮助。谢谢 int main() { float num; int rem = 0;; int dec = 0; int i = 0; cout <<
int main()
{
float num;
int rem = 0;;
int dec = 0;
int i = 0;
cout << "Please enter a number with a decimal point: ";
cin >> num;
double ohalf = num - (int)num;
int half = num;
while (half != 0)
{
rem = half % 10;
half /= 10; //Converts first have to decimal
dec += rem *= pow(8, i);
i++;
}
cout << dec;
i = -1;
while (ohalf != 0)
{
rem = ohalf *pow(8, i); //Converts second half to decimal. *This is where I am stuck*
i--;
}
cout << rem;
_getch();
return 0;
}
intmain()
{
浮点数;
int-rem=0;;
int-dec=0;
int i=0;
cout>num;
双ohalf=num-(int)num;
int-half=num;
而(一半!=0)
{
雷姆=10%的一半;
half/=10;//首先必须转换为十进制
dec+=rem*=pow(8,i);
i++;
}
我们可以接受这样一种观点,即只要与基数相乘就可以去掉小数点:
"123.456" in base 16
=> BASE16("1234.56")/16
=> BASE16("12345.6")/(16*16)
=> BASE16("123456")/(16*16*16)
or
"123.456" in base 8
=> BASE8("1234.56")/8
=> BASE8("12345.6")/(8*8)
=> BASE8("123456")/(8*8*8)
所以我们只需要知道小数点后的位数。
然后我们可以删除它并使用std::stoi
转换所需基中的剩余字符串。
作为最后一步,我们需要在小数点后再除以基数^位数.
把所有东西放在一起,你会得到这样的结果:
#include <iostream>
#include <string>
#include <cmath>
using std::cout;
using std::cin;
using std::string;
int main()
{
int base = 8;
string value;
cout << "Please enter a number with a decimal point: ";
cin >> value;
size_t ppos = value.find('.');
if (ppos != string::npos) {
value.replace(ppos,1,"");
} else {
ppos = value.size();
}
size_t mpos = 0;
double dValue = (double)std::stoi(value, &mpos, base);
if (mpos >= ppos)
{
dValue /= std::pow(base, (mpos - ppos));
}
std::cout << dValue << '\n';
return 0;
}
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::string;
int main()
{
int base=8;
字符串值;
cout>值;
size_t ppos=value.find('.');
if(ppos!=string::npos){
值。替换(ppos,1,“”);
}否则{
ppos=value.size();
}
大小_tmpos=0;
双D值=(双)标准::stoi(值和mpos,基);
如果(mpos>=ppos)
{
dValue/=std::pow(基本(mpos-ppos));
}
std::cout如果您确信浮点值的整数部分和小数部分不会溢出long int
的范围,则可以使用std::stoll()
分别解析这两部分,然后将小数部分除以8的适当幂:
#include <cmath>
#include <stdexcept>
#include <string>
double parse_octal_fraction(const std::string s)
{
std::size_t dotpos;
auto whole = std::stoll(s, &dotpos, 8);
if (dotpos+1 >= s.length())
// no fractional part
return whole;
std::size_t fract_digits;
auto frac = std::stoll(s.substr(dotpos+1), &fract_digits, 8);
if (s.find_first_not_of("01234567", dotpos+1) != std::string::npos)
throw std::invalid_argument("parse_octal_fraction");
return whole + frac / std::pow(8, fract_digits);
}
#include <iostream>
int main()
{
for (auto input: { "10", "0", "1.", "0.4", "0.04", "1.04", "1.04 ", "1. 04"})
try {
std::cout << input << " (octal) == " << parse_octal_fraction(input) << " (decimal)" << std::endl;
} catch (const std::invalid_argument e) {
std::cerr << "invalid input: " << e.what() << " " << input << std::endl;
}
}
#包括
#包括
#包括
双解析八进制分数(常量std::字符串s)
{
std::大小\u t dotpos;
自动整定=std::stoll(s和dotpos,8);
如果(dotpos+1>=s.length())
//无小数部分
全部归还;
标准::分位数的大小;
自动分形=标准::斯托尔(s.substr(dotpos+1)和分形数字,8);
如果(s.find_first_not_of(“01234567”,dotpos+1)!=std::string::npos)
throw std::无效的_参数(“解析_八进制_分数”);
返回整数+分形/标准::pow(8个分形数字);
}
#包括
int main()
{
用于(自动输入:{“10”、“0”、“1.”、“0.4”、“0.04”、“1.04”、“1.04”、“1.04”})
试一试{
代码中的std::cout
while (ohalf != 0)
{
rem = ohalf *pow(8, i); //Converts second half to decimal. *This is where I am stuck*
i--;
}
ohalf
永远不会等于零,因此它可能会导致无限循环我认为将输入处理为字符串
会容易得多,在拆分字符串上使用strtol
,适当缩放小数部分,然后将其添加到双精度
中。我认为最好将&maxp
作为std::stoi
的第二个参数使字符串中的任何尾随字符都不计入幂次。P.S.这不是一个“八进制点”而不是一个“小数点”吗-p@TobySpeight相应地调整了我的答案,谢谢你指出这一点。我在文本中留下了小数点以保持一致问题是:-P