首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化转换算法

优化转换算法
EN

Stack Overflow用户
提问于 2011-07-08 22:35:30
回答 7查看 421关注 0票数 2

我最近一直在读一本书中的一个练习。任务是创建一个程序,打印1-256之间的二进制、八进制和十六进制等值的所有数字。我们只应该使用到目前为止在书中学到的方法,这意味着只使用for,while和do..while循环,if和else if语句,将整数转换为ASCII等价物和一些更基本的东西(例如cmath和iomanip)。

因此,经过一些工作,这是我的结果。然而,它是凌乱的、不优雅的和令人困惑的。有没有人有任何建议来提高代码效率(或优雅...:P)和性能?

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int decimalValue, binaryValue, octalValue, hexadecimalValue, numberOfDigits;
cout << "Decimal\t\tBinary\t\tOctal\t\tHexadecimal\n\n";
for (int i = 1; i <= 256; i++)
{
    binaryValue = 0;
    octalValue = 0;
    hexadecimalValue = 0;
    if (i != 0) 
    {
    int x, j, e, c, r = i, tempBinary, powOfTwo, tempOctal, tempDecimal;
    for (j = 0; j <=8; j++) //Starts to convert to binary equivalent
    {
        x = pow(2.0, j);
        if (x == i)
        {
              powOfTwo = 1;
              binaryValue = pow(10.0, j);
              break;
        }
        else if (x > i)
        {
              powOfTwo = 0;
              x /= 2;
              break;
        }
    }
    if (powOfTwo == 0)
    {
    for (int k = j-1; k >= 0; k--)
    {
        if ((r-x)>=0)
        {
           r -= x;
           tempBinary = pow(10.0, k);
           x /= 2;
        }
        else if ((r-x)<0)
        {
           tempBinary = 0;
           x /= 2;
        }
        binaryValue += tempBinary;
    }
    } //Finished converting
    int counter = ceil(log10(binaryValue+1)); //Starts on octal equivalent
    int iter;
    if (counter%3 == 0)
    {
       iter = counter/3;
    }
    else if (counter%3 != 0)
    {
       iter = (counter/3)+1; 
    }
    c = binaryValue;
    for (int h = 0; h < iter; h++)
    {
        tempOctal = c%1000;
        int count = ceil(log10(tempOctal+1));
        tempDecimal = 0;
        for (int counterr = 0; counterr < count; counterr++)
        {
            if (tempOctal%10 != 0)
            {
                 e = pow(2.0, counterr);
                 tempDecimal += e;
            }
            tempOctal /= 10;
        }
        octalValue += (tempDecimal * pow(10.0, h));
        c /= 1000;
    }//Finished Octal conversion
    cout << i << "\t\t" << binaryValue << setw(21-counter) << octalValue << "\t\t";
    int c1, tempHex, tempDecimal1, e1, powOf;
    char letter;
    if (counter%4 == 0)//Hexadecimal equivalent
    {
       iter = counter/4;
    }
    else if (counter%4 != 0)
    {
       iter = (counter/4)+1;
    }
    c1 = binaryValue;
    for (int h = 0, g = iter-1; h < iter; h++, g--)
    {
        powOf = g*4;
        if (h == 0)
        {
              tempHex = c1 / pow(10.0, powOf);
        }
        else if (h > 0)
        {
             tempHex = c1 / pow(10.0, powOf);
             tempHex %= 10000;
        }
        int count = ceil(log10(tempHex+1));
        tempDecimal1 = 0;
        for (int counterr = 0; counterr < count; counterr++)
        {
            if (tempHex%10 != 0)
            {
                 e1 = pow(2.0, counterr);
                 tempDecimal1 += e1;
            }
            tempHex /= 10;
        }
        if (tempDecimal1 <= 9)
        {
        cout << tempDecimal1;
        }
        else if (tempDecimal1 > 9)
        {
        cout << char(tempDecimal1+55); //ASCII's numerical value for A is 65. Since 10-15 are supposed to be letters you just add 55
        }
    }
    cout << endl;
    }
}
system("pause");
return 0;
}

如有任何改进建议,将不胜感激。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-07-08 22:39:24

分解每种输出类型的函数,然后循环整数列表,并通过调用每种不同格式的函数依次输出每种输出。

代码语言:javascript
复制
for (int i = 1; i <= 256; ++i)
{
  printBin(i);
  printHex(i);
  printOct(i);
}

根本的问题是,这么长时间的函数需要重构才能更加模块化。假设您正在编写代码,以供其他人使用。他们怎么能叫你的main呢?他们如何理解代码的每一部分在做什么?如果你把每一段有特定工作的代码都作为一个函数来调用,那么就更容易理解它的意图,并在以后重用它。

票数 5
EN

Stack Overflow用户

发布于 2011-07-08 22:45:49

您已经介绍了“iomanip”,这意味着您已经介绍了“iostream”。

如果是这样的话,看看下面的内容:

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
using namespace std;

int x = 250;
cout << dec << x << " " 
     << oct << x << " "
     << hex << x << "\n"
     << x << "\n";       // This will still be in HEX
票数 6
EN

Stack Overflow用户

发布于 2011-07-09 01:00:30

你有没有考虑过写一个通用的函数来处理任何基础?

将非负数转换为泛型基数很简单...你只需要计算number % base,得到最低有效位,然后用number除以base,重复得到其他数字……

代码语言:javascript
复制
std::string converted_number;
do {
    int digit = number % base;
    converted_number = digits[digit] + converted_number;
    number = number / base;
} while (number != 0);

一旦你有了一个通用的转换函数,那么解决你的问题就很容易了。只需使用base=2、8和16调用它,即可获得所需的字符串形式的结果。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6626030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档