ConteD

Trasformazione in decimale.

Nov 24th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. /*
  2. Linguaggio  : C++
  3. Descrizione : programma per convertire
  4.                           un numero di qualsiasi base
  5.                           in decimale.
  6.                          
  7. Creato da   : Davide Conte
  8. */
  9.  
  10.  
  11. #include <iostream>
  12. #include <stdlib.h>
  13. #include <math.h>
  14. using namespace std;
  15.  
  16. int b,v,d,n,num,cifra;
  17. int calcola=0;
  18. int cifre=1;
  19. bool finito=false;
  20. int contatore1=0;
  21.  
  22.  
  23. int main()
  24. {
  25. cout<<"Programma per trasformare un numero di qualsiasi base in decimale."<<endl;
  26. cout<<endl<<endl;
  27. cout<<"Inserisci il numero : ";
  28. cin>>num;
  29. cout<<endl;
  30. cout<<"Inserisci la base :";
  31. cin>>b;
  32. calcola=num;
  33.  
  34.  
  35. while(!finito)
  36. {
  37.         calcola/=10;
  38.                 if(calcola!=0)
  39.         cifre++;
  40.                 else
  41.         finito=true;
  42.        
  43. }
  44.  
  45.  
  46.  
  47. int numeri[cifre];
  48.  
  49.  
  50. while(num>0)
  51. {
  52. cifra = num%10;
  53. num = num/10;
  54. numeri[contatore1]=cifra;
  55. contatore1++;
  56. }
  57.  
  58. n=0;d=0;
  59. while(n<=cifre-1)
  60. {
  61.         v=numeri[n]*pow(b,n);
  62.         d= d+v;
  63.         n++;
  64. }
  65.  
  66. cout<<endl;
  67. cout<<"Il numero convertito in base decimale e': " << d;
  68. cout<<endl<<endl;
  69.  
  70. return 0;
  71.        
  72.        
  73.        
  74. }
Advertisement
Add Comment
Please, Sign In to add comment