简体   繁体   English

如何在不使用 c++ 中的功能的情况下制作十六进制到十进制转换器?

[英]How to make a hex to decimal converter without using the functions for it in c++?

So I need to make a hex to decimal converter where the user decides how many numbers he want's to put in (eg if he put's in 3 the program let's him write 3 hexadecimal numbers which are converted to decimal) but as I said before I have to do the conversion manually and I can only use libraries iostream, cstring and cmath.所以我需要做一个十六进制到十进制的转换器,用户决定他想输入多少个数字(例如,如果他把 3 放入程序让他写 3 个十六进制数字,这些数字被转换为十进制)但正如我之前所说的我有手动进行转换,我只能使用库 iostream、cstring 和 cmath。 And I can't use string (when the user puts in the hexadecimal numbers they can't be string (don't really know how to explain this) so for example the hex numbers will be stored in char hex and not string hex) If you could help me I would be really grateful and I hope I described the problem good enough!而且我不能使用字符串(当用户输入十六进制数字时,它们不能是字符串(真的不知道如何解释),因此例如十六进制数字将存储在 char hex 而不是 string hex)如果你能帮助我,我将非常感激,我希望我能把问题描述得足够好!

This is my try (I am new to programming so it is pretty bad)这是我的尝试(我是编程新手,所以很糟糕)

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

It doesn't work.它不起作用。

Always try to separate your concerns.始终尝试将您的担忧分开。 I will not deal with input/output, I assume that you can handle that.我不会处理输入/输出,我假设你可以处理。 So, let's implement a function that converts a char array of certain length into decimal:因此,让我们实现一个 function 将一定长度的 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;
}

Note, that the number of digits this code can support is limited.请注意,此代码可以支持的位数是有限的。 For the sake of simplicity I will not cover the support for more advanced cases.为简单起见,我不会介绍对更高级案例的支持。

Since this is apparently homework, I don't want to just post the code for you.由于这显然是家庭作业,我不想只是为您发布代码。 But I worked on this and found several problems.但我对此进行了研究,发现了几个问题。 I suggest taking them in this order:我建议按以下顺序服用:

You have a loop for reading in all the strings.您有一个用于读取所有字符串的循环。 OK, but although the array of strings is dynamically allocated, each string has not been allocated.好的,但是虽然字符串数组是动态分配的,但每个字符串都没有被分配。 That risks error.这冒着错误的风险。 What's the fix?解决方法是什么? One easy one is: get rid of that loop.一个简单的方法是:摆脱那个循环。 Inside the last for-loop, read in the string you want to work on.在最后一个 for 循环中,读入要处理的字符串。 Process it and show its output.处理它并显示它的 output。 Then do the next one.然后做下一个。

If you really want to read them all in and then process them, just make each string not be dynamically allocated.如果你真的想把它们全部读完然后处理它们,只需让每个字符串都不是动态分配的。 This would do it:这会做到:

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

Using that idk array makes things complicated.使用该 idk 数组会使事情变得复杂。 How about this algorithm for printing a single hex string?这个打印单个十六进制字符串的算法怎么样?

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

This eliminates the need for the idk array, so you don't have to worry about reading too far in it这消除了对 idk 数组的需要,因此您不必担心在其中读得太远

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM