Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (WinAPI, Konsole) » führende null(en) sollen nicht angezeigt werden

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
13.11.2005, 11:04 Uhr
~Suderwurzen
Gast


ich hab ein programm erstellt, das mittels feldern 2 achtstellige zahlen zu einer beliebigen basis addiert. allerdings darf bei der ausgabe die führende null nicht angezeigt werden.
hab schon ewig lang herumprobiert, bin aber auf keinen grünen zweig gekommen.

bitte um hilfe!

es handel sich um die funktion PrintBase, bin mir aber auch nicht sicher.

C++:

#include <iostream>
using namespace std;

bool AddBase (int a [8], int b [8], int c [8], int base);
bool AddBin (int a [8], int b [8], int c [8]);
bool AddHex (int a [8], int b [8], int c [8]);
void PrintBase (int a [8], char suffix);
void PrintDigit (int digit);
void PrintHex (int a [8]);
void PrintBin (int a [8]);


int main() {
    int a [8] = {0, 1, 1, 1, 1, 1, 1, 1};
    int b [8] = {0, 1, 1, 1, 1, 1, 1, 1};
    int c [8];
    int base = 2;

    if (base == 2) {
        PrintBin (a);
        PrintBin (b);
        AddBin (a, b, c);
        PrintBin (c);
    } else if (base == 16) {
        PrintHex (a);
        PrintHex (b);
        AddHex (a, b, c);
        PrintHex(c);
    } else {
        PrintBase (a, 'x');
        PrintBase (b, 'x');
        AddBase (a, b, c, base);
        PrintBase (c, 'x');

    }
}

bool AddBase (int a [8], int b [8], int c [8], int base) {

    for (int i = 7; i >= 0; i--) {
        if (a[i] + b[i] >= base) {
            a[i - 1] = a[i - 1] + 1; //nächstes element "1 weiter"
        }
        c[i]= (a[i] + b[i]) % base;
    }
    return c;
}
bool AddBin (int a [8], int b [8], int c [8]) {
    AddBase (a, b, c, 2);
    return c;
}

bool AddHex (int a [8], int b [8], int c [8]) {
    AddBase (a, b, c, 16);
    return c;

}
void PrintBase (int a [8], char suffix) {
    
    for (int i = 0; i <= 7; i++) {        
        PrintDigit (a[i]);
    }
    cout << suffix << endl;
    
}

void PrintDigit (int digit) {
    if (digit < 10) {
        cout << digit;
    } else {
        char c = 'A' + digit - 10; cout << c;
    }
}


void PrintHex (int a [8]) {
    PrintBase (a, 'h');
}

void PrintBin (int a [8]) {
    PrintBase (a, 'b');
}


 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
13.11.2005, 13:22 Uhr
J-jayz-Z
Perl Crack ala Carte
(Operator)


So meinst du:

C++:
#include <iostream>
using namespace std;

bool AddBase (int a [8], int b [8], int c [8], int base);
bool AddBin (int a [8], int b [8], int c [8]);
bool AddHex (int a [8], int b [8], int c [8]);
void PrintBase (int a [8], char suffix);
void PrintDigit (int digit);
void PrintHex (int a [8]);
void PrintBin (int a [8]);


int main() {
    int a [8] = {0, 1, 1, 1, 1, 1, 1, 1};
    int b [8] = {0, 1, 1, 1, 1, 1, 1, 1};
    int c [8];
    int base = 2;

    if (base == 2) {
        PrintBin (a);
        PrintBin (b);
        AddBin (a, b, c);
        PrintBin (c);
    } else if (base == 16) {
        PrintHex (a);
        PrintHex (b);
        AddHex (a, b, c);
        PrintHex(c);
    } else {
        PrintBase (a, 'x');
        PrintBase (b, 'x');
        AddBase (a, b, c, base);
        PrintBase (c, 'x');

    }
}

bool AddBase (int a [8], int b [8], int c [8], int base) {

    for (int i = 7; i >= 0; i--) {
        if (a[i] + b[i] >= base) {
            a[i - 1] = a[i - 1] + 1; //nächstes element "1 weiter"
        }
        c[i]= (a[i] + b[i]) % base;
    }
    return c;
}
bool AddBin (int a [8], int b [8], int c [8]) {
    AddBase (a, b, c, 2);
    return c;
}

bool AddHex (int a [8], int b [8], int c [8]) {
    AddBase (a, b, c, 16);
    return c;

}
void PrintBase (int a [8], char suffix) {
    
    for (int i = 0; i <= 7; i++) {        
        if(i == 0) {
            if(not a[i] == 0)
                PrintDigit (a[i]);
        } else if(not (a[i] == 0 && a[i-1] == 0))
                PrintDigit (a[i]);
    }
    cout << suffix << endl;
    
}

void PrintDigit (int digit) {
    if (digit < 10) {
        cout << digit;
    } else {
        char c = 'A' + digit - 10; cout << c;
    }
}


void PrintHex (int a [8]) {
    PrintBase (a, 'h');
}

void PrintBin (int a [8]) {
    PrintBase (a, 'b');
}

?
--
perl -Mstrict -Mwarnings -e 'package blub; sub new { bless {} } sub bar {my $self=shift; $self->{bla}="66756e2d736f66742e6465"; return $self->{bla};} my $foo=blub->new();print "Hallo ";print pack("H*",$foo->bar()); print "\n"'
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (WinAPI, Konsole) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: