Advertisement
ConteD

Trasformazione di un numero in decimale.

Oct 7th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. /*
  2.  * Creato da Davide Conte
  3.  * Data di inizio : 06/10/2015
  4.  *
  5. */
  6.  
  7. #include <iostream>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. using namespace std;
  11.  
  12.  
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.             inizio:  
  17.                                 // SETTO LE VARIABILI
  18.                                 int b=0; //base
  19.                                 int v=0; //V = Cn*b^n
  20.                                 int d=0; //numero finale
  21.                                 int n=0; //esponente della potenza
  22.                                 int num; //numero iniziale
  23.                                 int cifre=1; //  variabile per contare le cifre
  24.                                 int calcola=0; //
  25.                                 int cifra; //
  26.                                 bool finito=false; // ^^^^^^^^
  27.                                 int contatore1=0; // contatore per inserire le cifre nell'array
  28.                                 int menu;
  29.                                 int poow;
  30.        
  31.             cout<<"Digita il numero: ";
  32.             cin>>num;
  33.             cout<<"Digita la base: ";
  34.             cin>>b;
  35.             calcola=num;
  36.                 while(!finito) // processo per calcolare il numero delle cifre
  37.                 {
  38.                
  39.                     calcola/=10;
  40.                     if(calcola!=0)
  41.                         cifre++;
  42.                     else
  43.                         finito=true;
  44.                 }
  45.            
  46.                 //cout<<"Nel numero "<<num<<" ci sono "<<cifre<<" cifre!";
  47.             int numeri[cifre];
  48.  
  49.                 while(num>0)
  50.                 {
  51.                     cifra = num%10;
  52.                     num = num/10;
  53.                     //cout<<cifra;
  54.                     contatore1=contatore1++;
  55.                     numeri[contatore1]=cifra;
  56.                 }
  57.                
  58.                 int cifree;
  59.                 cifree = cifre - 1;
  60.                 n=0;
  61.                 while(n<=cifree)
  62.                 {
  63.                     poow = pow (b,n);
  64.                     v = numeri[n]*poow;
  65.                     cout<< endl<< "Potenza : " <<poow    ;
  66.                     cout<<"   V :"<<v << endl;
  67.                     d = d+v;
  68.                     n = n++;
  69.                 }
  70.                
  71.             cout<< endl;
  72.             cout<< "Il numero convertito in base decimale e': " << d;
  73.             cout<< endl << endl;
  74.            
  75.             menu:
  76.             cout<< "Digita :" << endl;
  77.             cout<< "1) Se vuoi uscire" <<endl<< "2) Se vuoi trasformare un altro numero" <<endl;
  78.             cin>>menu;
  79.            
  80.             if(menu==1)
  81.                 abort;
  82.             else
  83.                 goto inizio;
  84.                                                                
  85.                            
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement