简体   繁体   English

十六进制计算器问题。 有人可以再次检查我的代码吗?

[英]Hexa Calculator Problem. Can someone check my code again?

So I'm back with my school project. 所以我回来参加我的学校项目。 The teacher gave us more code to work with. 老师给了我们更多的代码。 A hexadecimal calculator. 十六进制计算器。 At first glance it looks finished but... the math is wrong. 乍看之下,它看起来已经完成了,但是...数学是错误的。 Can someone look over this code and tell me what's wrong or what can I do to make it right? 有人可以看一下这段代码,然后告诉我什么地方出错了,或者我可以做些什么来使它正确? I need to keep most of this code as is. 我需要将大部分代码保持原样。 So I can't change the principle. 所以我不能改变原则。 I must stick with this. 我必须坚持这一点。 When I run this code I do get a result, but it's not the result I need. 当我运行此代码时,我确实得到了结果,但这不是我需要的结果。 I need to do something like "1cec+bec = 28d8". 我需要执行类似“ 1cec + bec = 28d8”的操作。 Again, I don't get errors, but I don't get the right result. 同样,我没有得到错误,但是没有得到正确的结果。

#include <iostream>
#include <cstring>

using namespace std;

int test(char[], int);
void decal(char[], int n);
int add2(char a, char b, int r);
void add(char a[], char b[], char c[]);

int main()
{
    char a[10], b[10], c[10];
    int valid;

    do {
        cout << "Insert first number (maximum 5 char hexa):" << endl;
        cin >> a;
        valid = test(a, strlen(a));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);
    cout << "First number: " << a << endl;
    decal(a, strlen(a));
    cout << "First number after completing: " << a << endl;

    do {
        cout << "Insert 2nd number (maximum 5 char hexa):" << endl;
        cin >> b;
        valid = test(b, strlen(b));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);

    decal(b, strlen(b));
    cout << "2nd number: " << b << endl;

    add(a, b, c);  
    cout << "Rezultat: " << c << endl;
    return 0;
}

void add(char a[], char b[], char c[])
{
    int i, r=0, sab;
    c[6] = '\0';
    for(i = 4; i>= 0; i--)
    {
        sab = add2(a[i], b[i], r);  
        if(sab < 10)
        {
            r = 0;
            c[i+1] = sab + 48; // '0' ... '9'
        }

        else
            if(sab < 16)
            {
                r = 0;
                c[i] = 97 + (sab - 10); // 'a' ... 'f'
            }

            else
            {
                r = 1;
                sab = sab - 16;
                if(sab < 10)
                {
                    r = 0;
                    c[i+1] = sab + 48; // '0' ... '9'
                }
                else
                {
                    r = 0;
                    c[i] = 97 + (sab - 10); // 'a' ... 'f'

                }
            }
    }
    c[0] = 48 + r;
}

int add2(char a, char b, int r)
{
    int ai, bi, ci, ai1, bi1;
    ai = tolower(a);
    if(ai <= 57)
        ai1 = ai - 48;
    else
        ai1 = ai - 97 + 10;
    bi = tolower(b);
    if(bi <= 57)
        bi1 = bi - 48;
    else
        bi1 = bi - 97 + 10;
    ci = ai1 + bi1;
    return ci;
}

int test(char x[], int n)
{
    if(n > 5)
        return 0;  

    for(int i = 0; i < n; i++)
    {
        if(x[i] <48 || (x[i] > 57 && x[i] < 65) || (x[i] > 70 && x[i] < 97) || x[i] > 102)
            return 0;  
    }
    return 1;
}

void decal(char x[], int n)
{

    int i, nz;
    x[5] = '\0';
    nz = 5 - strlen(x); 
    if(nz > 0) {
        for(i = 0; i < n; i++)
            x[5 - i-1] = x[n-i-1];
    }

    for(i = 0; i < nz; i++)
            x[i] = '0';
}

add2 takes r (which appears to be meant as a carry bit) as a parameter, but doesn't actually use it. add2r (似乎是一个进位位)作为参数,但实际上并未使用它。

In any case, the way you compute r doesn't make sense. 无论如何,计算r的方法是没有意义的。 There's exactly one place where it's set to 1 , but then it's unconditionally reset back to 0 a couple lines later. 恰好在一个位置将其设置为1 ,但是随后几行将其无条件地重置为0

The bottom line is, you are dropping all carries. 最重要的是,您正在放弃所有进位。

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

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