【问题标题】:How to make a hex to decimal converter without using the functions for it in c++?如何在不使用 C++ 中的函数的情况下制作十六进制到十进制转换器?
【发布时间】:2021-02-03 17:59:54
【问题描述】:

所以我需要制作一个十六进制到十进制的转换器,用户可以在其中决定要输入多少个数字(例如,如果他输入 3,程序让他写 3 个十六进制数字,然后转换为十进制),但正如我所说在我必须手动进行转换之前,我只能使用库 iostream、cstring 和 cmath。而且我不能使用字符串(当用户输入十六进制数字时,它们不能是字符串(真的不知道如何解释)所以例如十六进制数字将存储在 char hex 而不是 string hex)如果你能帮助我,我将非常感激,我希望我能把问题描述得足够好!

这是我的尝试(我是编程新手,所以很糟糕)

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
    int broj;
    do
    {
        cout << "Unesite broj brojeva: ";
        cin >> broj;
    }
    while (broj < 1);
    char *hex = new char [broj];
    for (int i = 0; i < broj; i++)
    {
        cout << "Unesite " << i+1 << ". broj: ";
        cin >> hex[i];
    }
    for (int i = 0; i < broj; i++)
    {
        char idk[10000];
        idk[10000] = hex[i];
        int duljina = strlen(idk);
        int pom = 0;
        int halp = duljina;
        for (int j = 0; i < duljina; i++)
        {
            if (idk[j] == 'A' || idk[j] == 'a') idk[j] = 10;
            if (idk[j] == 'B' || idk[j] == 'b') idk[j] = 11;
            if (idk[j] == 'C' || idk[j] == 'c') idk[j] = 12;
            if (idk[j] == 'D' || idk[j] == 'd') idk[j] = 13;
            if (idk[j] == 'E' || idk[j] == 'e') idk[j] = 14;
            if (idk[j] == 'F' || idk[j] == 'f') idk[j] = 15;
            pom += idk[j]*(pow(16, halp));
            halp--;
        }
        cout << pom << endl;
    }

    return 0;
}

它不起作用。

【问题讨论】:

  • idk[10000] = hex[i]; 这是一个错误的超出范围的访问。
  • int duljina = strlen(idk); 这是未初始化变量的错误用法。
  • idk[10000] = hex[i]; 将大小为 10000 的数组的第 10001 个元素设置为 hex[i] 的单个元素的值。这是修改超出数组末尾的元素的未定义行为。
  • 如果你不能使用c++标准库中的std::string类,你必须自己实现它的功能,特别是原始char数组的内存管理,使用new [] 分配,并使用delete[] 正确释放。它可以帮助您研究std::string 的实现,并且可能会简化它以满足您的需求。

标签: c++ iostream


【解决方案1】:

始终尝试将您的顾虑分开。我不会处理输入/输出,我假设你可以处理。那么,让我们实现一个函数,将一定长度的 char 数组转换为十进制:

int getDigit(char input) {
    switch (input) {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case 'a':
        case 'A': return 10;
        case 'b':
        case 'B': return 11;
        case 'c':
        case 'C': return 12;
        case 'd':
        case 'D': return 13;
        case 'e':
        case 'E': return 14;
        case 'f':
        case 'F': return 15;
    }
    return 0;
}

long hex2Decimal(char input[], int length) {
    long output = 0;
    long digit = 1;
    int index = 0;
    for (index = length - 1; index >= 0; index--) {
        output += getDigit(input[index]) * digit;
        digit *= 16;
    }
    return output;
}

请注意,此代码可以支持的位数是有限的。为简单起见,我不会介绍对更高级案例的支持。

【讨论】:

    【解决方案2】:

    由于这显然是家庭作业,我不想只是为您发布代码。但我对此进行了研究,发现了几个问题。我建议按以下顺序服用:

    你有一个读取所有字符串的循环。好的,但是虽然字符串数组是动态分配的,但每个字符串没有被分配。这冒着错误的风险。解决方法是什么?一个简单的方法是:摆脱那个循环。在最后一个 for 循环中,读入要处理的字符串。处理它并显示它的输出。然后做下一个。

    如果你真的想将它们全部读入然后处理它们,只需让每个字符串都不是动态分配的。这样就可以了:

    using str = char [256];
    str* hexes = new str[broj];
    

    使用 idk 数组会使事情变得复杂。这个打印单个十六进制字符串的算法怎么样?

    for each character in the hex string counting BACKWARDS from the last character
           pom *= 16; //multiply the number you've got so far by 16 -- like moving left a digit
           figure out what the new digit means
           add that digit to pom
    

    这消除了对 idk 数组的需要,因此您不必担心在其中读得太远

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 2013-11-30
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2011-12-09
      相关资源
      最近更新 更多