c ++将八进制分数转换为小数分数?

时间:2016-10-10 14:16:40

标签: c++ decimal octal

我目前正致力于一个程序,该程序旨在将八进制分数作为输入并将其转换为小数。到目前为止,我有一部分代码将小数点前的部分转换为十进制,而不是小数点后的浮点数。我试图使用模数,但因为我的变量是浮点数而失败。

有没有办法将小数点后的剩余数字转换为八进制的十进制数?我在下面发布了我的代码。任何帮助表示赞赏。谢谢!

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;
}

3 个答案:

答案 0 :(得分:1)

认为人们可以通过经常乘以基数来删除小数点:

"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转换所需基数中的剩余字符串。 作为最后一步,我们需要再次通过base^number_of_places_after_decimal

把所有东西放在一起就可以得到这样的东西:

#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;
}

答案 1 :(得分:0)

如果你确信浮点值的整数和小数部分都不会溢出long 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;
        }
}

显示的测试输入给出了这个输出:

  

10(八进制)== 8(十进制)
  0(八进制)== 0(十进制)
  1.(八进制)== 1(十进制)
  0.4(八进制)== 0.5(十进制)
  0.04(八进制)== 0.0625(十进制)
  1.04(八进制)== 1.0625(十进制)
  输入无效:parse_octal_fraction 1.04
  输入无效:parse_octal_fraction 1. 04

答案 2 :(得分:0)

代码

 while (ohalf != 0)
{
    rem = ohalf *pow(8, i); //Converts second half to decimal. *This is where I am stuck*
    i--;
}

ohalf永远不会等于零所以它可能导致无限循环