mercredi 5 juin 2019

Making a sum with char

#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] = '\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';
}

I was given this school project to make a hexadecimal calculator. The teacher made us the following code. Mandatory

My problem is the void add part. How is it possible to add char? I know there are easier ways to make a hexadecimal calculator, but we have to use that code. So how can I write a sum something like 1cec+bec=28d8 in hexa?

Aucun commentaire:

Enregistrer un commentaire