вот дела когда-то...велосипед правда
| Код | #include <stdlib.h> #include<iostream.h> #include<string> #include <conio.h> //---------------------------------------------------------------------------
int Stepen(int chislo=0,int stepen=0) { int step=1, tmp=chislo; while (step<stepen) { chislo=chislo*tmp; step++; } return chislo; }
int func(const char *chislo=0,int size=0) { int *mas=new int[size]; unsigned int result=0; int step=1,i=size-1; for (register int i=0;i<size;i++) mas[i]=chislo[i]-'0'; if (mas[i]==1){ result=1; i-=1; size--; }else{ result=0; i-=1; size--; } while (size) {
result=result+mas[i]*Stepen(2,step); i--; size--; step++; } delete[] mas; return result; }
int main(int argc, char* argv[]) { unsigned int number = 0; char str[25]; string bin_chislo;
cout<<"Input number: "; cin>>bin_chislo; for (register string::size_type i=0;i<bin_chislo.size();i++) { if ((bin_chislo[i]!='0') && (bin_chislo[i]!='1')){ cerr<<"Error : " <<"System of the numeration does not correspond!"; getch(); return -1; } } number=func(bin_chislo.c_str(),bin_chislo.size()); itoa(number, str, 16);
cout<< "Dec = "<<number<<endl; cout<< "Hex = "<<str<<endl;
getch(); return 0;
}
|
упс пардон...тут наоборот...ну лан мож кому пригодиться |