用炭赚一笔钱

Making a sum with char

本文关键字:一笔      更新时间:2023-10-16
#include <iostream>
#include <cstring>
using namespace std;
int test(char[], int);
void decal(char[], int n);
int main()
{
    char a[10], b[10], c[10];
    int valid;
    do {
        cout << "Insert first number (maximum 5 chars hexa):" << endl;
        cin >> a;
        valid = test(a, strlen(a));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);
    cout << "First number: " << a << endl;
    decalez(a, strlen(a));
    cout << "First number after insert: " << a << endl;
    do {
        cout << "Insert 2nd number (maximum 5 chars hexa):" << endl;
        cin >> b;
        valid = test(b, strlen(b));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);
    decalez(b, strlen(b));
    cout << "2nd number after insert: " << b << endl;
    add(a, b);  //  Calculating c
    cout << "Result: " << c << endl;
    return 0;
}
int test(char x[], int n)
{
    if(n > 5)
        return 0;  //  Too many numbers
    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] = '';
    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';
}

我得到了这个学校项目,制作一个十六进制计算器。老师给我们做了以下代码。命令的

我的问题是空白添加部分。怎么可能添加字符?我知道有更简单的方法来制作十六进制计算器,但我们必须使用该代码。那么我怎样才能用六进制写出像 1cec+bec=28d8 这样的总和呢?

假设这些函数存在

// Convert hexadecimal string 'number' into an integer.
int fromHex(const char* number);
// Convert 'number' to hexadecimal in 'result', assuming that the result will fit.
void toHex(int number, char* result);

你可以这样写add

void add(const char* hex_a, const char* hex_b, char* hex_c)
{
    int a = fromHex(hex_a);
    int b = fromHex(hex_b);
    toHex(a + b, hex_c);
}

实现作为练习的转换函数。

下面是如何添加十六进制数的示例。这个简单的代码以 5 位溢出环绕。

#include <iostream>
#include <exception>
#include <cstring>
using std::cout;
using std::endl;
struct BadDigit : public std::exception { };
int hexToDec( const char c ) {
    if ( c >= '0' && c <= '9' ) {
        return c -'0';
    }
    else if( c>= 'A' && c<='F') {
        return c - 'A' + 10;
    }
    else if ( c >= 'a' && c<='f' ) {
        return c - 'a' + 10;
    }
    else {
        throw BadDigit();
    }
}
char decToHex( int d ) {
    if ( d >= 0 && d <= 9 ) {
        return d + '0';
    }
    else if ( d >= 10 && d <= 15 ) {
        return d - 10 + 'A';
    }
    else {
        throw BadDigit();
    }
}
void add( const char *a, const char *b, char *s )
{
    int rem = 0;
    for (int i = 4; i >= 0; i-- ) {
        int ai = hexToDec(a[i]);
        int bi = hexToDec(b[i]);
        int ri = ai + bi + rem;
        s[i] = decToHex( ri % 16 );
        rem = ri / 16;
    }
}
int main()
{
    const char *hex1 = "effff";
    const char *hex2 = "00001";
    char result[7];
    try {
        add(hex1, hex2, result);
    }
    catch (std::exception& e) {
        cout << "Exception: " << e.what() << endl;
        return 1;
    }
    cout << hex1 << " + " << hex2 << " = " << result << endl;
    return 0;
}