Lineare Algebra (42003-42004)

ZahlenSysteme

 

ZahlenSysteme.cpp
#include <iostream.h>

//Basen
#define DUAL 2
#define DUO 2
#define OKTAL 8
#define OKT 8
#define DEZIMAL 10
#define DEZ 10
#define HEXADEZIMAL 16
#define HEX 16


char *DosZSys(char *string, char aus_b, char in_b);

char *DosZSys(char *string, char aus_b, char in_b)
{
int i, h;
unsigned int idec=0;
char z;
static char *hilf;

for (i=0; string[i]!='\0'; i++)
{
 z = string[i]-'0';
 if(z>=0 && z<aus_b)
  idec = idec * aus_b + z;
 else
 {
  z = ((z + '0')|('a'-'A'))-('a'-10);
  if(aus_b==16 && z >=0 && z <=aus_b)
   idec = idec * aus_b + z;
  else 
   break;
 }
}

i=0;
for(h=idec; h>0; i++)
 h=h/in_b;

hilf = new char[i];
hilf[i]='\0';
i--;

while(idec>0)
{
 z = idec % in_b + '0';
 if(z >'9') 
  z = z-'0'-10+'A';
 hilf[i] = z;
 i--;
 idec = idec / in_b;
}
return hilf;
}


int
main()
{
//Beispiele
cout<<
"D 4567 to Hex: " << DosZSys("4567",DEZ ,HEX)<< endl;
cout<<
"D 4567 to Dual: " <<DosZSys("4567",DEZ ,DUAL)<<endl;
cout<<
"D 4567 to Oktal: "<<DosZSys("4567",DEZ ,OKT)<< endl;
cout<<
"D 3FF to Dez: " <<  DosZSys("3FF" ,HEX ,DEZ)<< endl;

return 0;
}