Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //ile cyfr ma liczba
  8. int len(int a){
  9. int rzad = 1;
  10. while(true){
  11. if(a < pow(10,rzad)) return rzad;
  12. else rzad += 1;
  13. }
  14. }
  15.  
  16. //wybieranie cyfry z konkretnej pozycji
  17. int getpos(int a, int pos){
  18. int x;
  19. x = a%(int(pow(10,pos)));
  20. x = x-(a%(int(pow(10,pos-1))));
  21. x = x/int(pow(10,pos-1));
  22.  
  23. return x;
  24. }
  25.  
  26. //dopisywanie liczby b do liczby a
  27. void join (int &a, int b){
  28. a = a * pow(10,len(b));
  29. a += b;
  30. }
  31.  
  32. int convert(int num,int from,int to){
  33. if(from == to) return num; // nic nie zmienia
  34. if(to == 10){ //zamienia na b10
  35. int liczba10 = 0;
  36. for(int i = 1; i<= len(num); i = i+1){
  37. liczba10 = liczba10+getpos(num,i)*int(pow(from,i-1));
  38. }
  39. return liczba10;
  40. }
  41. if(from == 10){
  42. int wynik = 0;
  43. if(num > 1){
  44. join(wynik,convert(floor(num/to),10,to));
  45. }
  46. join(wynik,num%to);
  47. return wynik;
  48. }else{ //najpierw zamienia na b10 a potem na wybrany system
  49. num = convert(num,from,10);
  50. return convert(num,10,to);
  51. }
  52. }
  53.  
  54.  
  55. int main(){
  56. int x,y,z;
  57. cout<<"podaj liczbe: ";
  58. cin>>x;
  59. cout<<"podaj system: ";
  60. cin>>y;
  61. cout<<"konwertuj do: ";
  62. cin>>z;
  63. cout<<convert(x,y,z)<<endl;
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement