codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
// Sumar nĂºmeros enteros sin signo almacenados en formato BCD // Agosto de 2009 Con Clase, Salvador Pozo #include <iostream> using namespace std; const unsigned int cadmax = 32; typedef unsigned char numero[cadmax]; bool Sumar(numero, numero, numero); void MostrarBCD(numero); int main() { numero n1={ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x10,0x00 }; numero n2={ 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x09,0x98,0x12 }; numero suma; Sumar(n1, n2, suma); MostrarBCD(n1); cout << " + "; MostrarBCD(n2); cout << " = "; MostrarBCD(suma); cout << endl; return 0; } bool Sumar(numero n1, numero n2, numero r) { int acarreo = 0; int c; for(int i = cadmax-1; i >= 0; i--) { // Sumar digito de menor peso: c = acarreo+(n1[i] & 0x0f) + (n2[i] & 0x0f); if(c > 9) { c-=10; acarreo=1; } else acarreo=0; r[i] = c; // Sumar digito de mayor peso: c = acarreo+((n1[i] >> 4) & 0x0f) + ((n2[i] >> 4) & 0x0f); if(c > 9) { c-=10; acarreo=1; } else acarreo=0; r[i] |= (c << 4); } return !acarreo; } void MostrarBCD(numero n) { char c; bool cero=true; for(unsigned int i = 0; i < cadmax; i++) { c = '0' + ((n[i] >> 4) & 0x0f); if(c != '0') cero=false; if(!cero) cout << c; c = '0' + (n[i] & 0x0f); if(c != '0') cero=false; if(!cero) cout << c; } }
Private
[
?
]
Run code
Submit