Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- //ile cyfr ma liczba
- int len(int a){
- int rzad = 1;
- while(true){
- if(a < pow(10,rzad)) return rzad;
- else rzad += 1;
- }
- }
- //wybieranie cyfry z konkretnej pozycji
- int getpos(int a, int pos){
- int x;
- x = a%(int(pow(10,pos)));
- x = x-(a%(int(pow(10,pos-1))));
- x = x/int(pow(10,pos-1));
- return x;
- }
- //dopisywanie liczby b do liczby a
- void join (int &a, int b){
- a = a * pow(10,len(b));
- a += b;
- }
- int convert(int num,int from,int to){
- if(from == to) return num; // nic nie zmienia
- if(to == 10){ //zamienia na b10
- int liczba10 = 0;
- for(int i = 1; i<= len(num); i = i+1){
- liczba10 = liczba10+getpos(num,i)*int(pow(from,i-1));
- }
- return liczba10;
- }
- if(from == 10){
- int wynik = 0;
- if(num > 1){
- join(wynik,convert(floor(num/to),10,to));
- }
- join(wynik,num%to);
- return wynik;
- }else{ //najpierw zamienia na b10 a potem na wybrany system
- num = convert(num,from,10);
- return convert(num,10,to);
- }
- }
- int main(){
- int x,y,z;
- cout<<"podaj liczbe: ";
- cin>>x;
- cout<<"podaj system: ";
- cin>>y;
- cout<<"konwertuj do: ";
- cin>>z;
- cout<<convert(x,y,z)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement