Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    211

    [c++]Questa propio non la capisco

    Ho una difficoltà e non ne riesco a venire fuori. Mi chiedo: com'è possibile che lo stesso codice fatto girare su visual studio 2005 e dev-c++ produca 2 risultati diversi? Provare per credere(in c++ 2005 ho aggiunto l'istruzione #include"stdafx.h"). E non è tutto: provate a togliere le // dalla istruzione che ho appositamente colorato e vedrete che su dev-c++ il sisultato dell'operazione elevazione è corretto. Vi riassumo brevemente cosa fa il programma:legge da file 2 numeri A(numero con virgola) e B. Eleva A a B. Il risultato è un numero che non perde di precisione. Provate ad esempio con A=5.1234 B=2 , in dev-c++(4.9.8.0) se decommentate la riga che ho colorato in arancio il risultato è corretto. Se la commentate il risultato non è più corretto. Come è possibile?
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <string>
    #include <ctype.h>
    #include <vector>

    #define DEBUG
    //#define TEST

    using namespace std;

    //123: Cifre[0]=3; Cifre[1]=2; Cifre[2]=1;
    class Numero
    {
    public:
    int* Cifre;
    int Len;
    int PosVirgola;
    Numero()
    {
    Numero(0);
    }

    Numero (int len)
    {
    PosVirgola=0;
    Len=len;
    Cifre=new int[len];
    //Azzera
    for (int i=0;i<len;i++)
    Cifre[i]=0;
    }

    //Si puo' non utilizzare len
    Numero (int len,int n)
    {
    PosVirgola=0;
    if (len==-1)
    Len=Numero::LenNumero(n);
    else
    Len=len;
    Cifre=new int[Len];

    int i=0;
    while (n>0)
    {
    Cifre[i++]=(n%10);
    n/=10;
    }
    }

    int& operator[](int a)
    {
    return Cifre[a];
    }

    static int LenNumero(int a);
    };

    ostream& operator<<(ostream& s, Numero a)
    {
    int Zero=0;
    while (Zero<a.PosVirgola && a[Zero]==0) Zero++;

    bool PrintVirgola=true;
    if (Zero==a.PosVirgola)PrintVirgola=false;

    int j=a.Len-1;
    if (a.Len==a.PosVirgola) s<<"0.";
    while (j>=Zero)
    {
    s<<a[j];
    if ((j==a.PosVirgola) && PrintVirgola)
    s<<".";
    j--;
    }

    return s;

    }

    istream& operator>>(istream& s, Numero& a)
    {
    //Legge tutti gli eventuali spazi iniziali
    char c;

    while ((s.get(c)) && (isspace(c)));

    int* Arr=new int[1000]; int i=0; int PosVirgolaEnd=-1;
    do
    {
    if (c=='.')
    PosVirgolaEnd=i;
    else Arr[i++]=c-'0';
    }
    while((s.get(c)) && (!isspace(c)));

    //Sistema la virgola
    a=Numero(i);
    if (PosVirgolaEnd==-1)
    a.PosVirgola=0;
    else
    a.PosVirgola=i-PosVirgolaEnd;

    //Sistema le cifre
    for (int j=0;j<i;j++)
    {
    //Scambia
    a.Cifre[i-j-1]=Arr[j];
    }

    return s;


    }

    int Numero::LenNumero(int a)
    {
    int Cont=0;
    while (a>0)
    {
    a/=10;
    Cont++;
    }
    return Cont;
    }

    Numero operator + (Numero a,Numero b)
    {
    //Inizializza
    Numero Ret(max(a.Len,b.Len)+1);

    //Somma cifra per cifra
    int Resto=0;
    int j=0;
    for(j=0;j<min(a.Len,b.Len);j++)
    {
    int Somma=a.Cifre[j]+b.Cifre[j];
    Ret.Cifre[j]=(Somma+Resto)%10;
    if (Somma+Resto>=10)
    Resto=1;
    else
    Resto=0;
    }
    //bool Enter=false;
    while (j<max(a.Len,b.Len))
    {

    int Somma=(a.Len>b.Len)?(a.Cifre[j])b.Cifre[j]);
    Ret.Cifre[j]=(Somma+Resto)%10;
    if (Somma+Resto>=10)
    Resto=1;
    else
    Resto=0;
    j++;
    }
    if (Resto==0) Ret.Len--;
    else
    Ret[Ret.Len-1]=1;

    return Ret;

    }

    Numero operator * (Numero a,Numero b)
    {
    #ifdef DEBUG
    //cout<<a<<" "<<b<<"\n";
    #endif
    Numero ProdN(1,0); int ProdNDec=0;
    for (int i=0;i<b.Len;i++)
    {
    Numero Somma(-1,0);
    int Dec=0;
    for(int j=0;j<a.Len;j++)
    {

    Numero TT(-1,b[i]*a[j]);int TTCont=TT.Len-1;
    Numero Temp(TT.Len+Dec);
    for (int k=Temp.Len-1;k>=0;k--)
    {
    if (TTCont>=0)
    Temp[k]=TT[TTCont--];
    else
    Temp[k]=0;
    }
    Somma= Somma+Temp;
    Dec++;
    }
    Numero SommaTemp(Somma.Len+ProdNDec);
    int SommaCont=Somma.Len-1;
    for (int k=SommaTemp.Len-1;k>=0;k--)
    {
    if(SommaCont>=0)
    SommaTemp[k]=Somma[SommaCont--];
    else
    SommaTemp[k]=0;
    }
    #ifdef DEBUG
    //cout<<"SommaTemp:"<<SommaTemp<<"\n";
    #endif
    ProdN=ProdN+SommaTemp;
    ProdNDec++;
    }

    ProdN.PosVirgola=a.PosVirgola+b.PosVirgola;
    return ProdN;

    }

    Numero Potenza(Numero r,int ex)
    {

    Numero Ret(1);
    Ret[0]=1;
    Ret.PosVirgola=0;
    for(int i=0;i<ex;i++)
    {

    Ret=Ret*r;
    #ifdef DEBUG
    //cout<<r<<" "<<ex<<" Esponente("<<i<<"):"<<Ret<<"\n";
    #endif

    }

    return Ret;
    }

    void Test()
    {
    /*Numero a(3);
    Numero b(2);
    a.PosVirgola=2;
    b.PosVirgola=1;
    a[0]=9;
    a.Cifre[1]=0;
    a.Cifre[2]=1;
    b.Cifre[0]=1;
    b.Cifre[1]=1;
    Numero Res=a*b;*/
    Numero a; Numero n;
    cin>>a>>n;
    cout<<a+n;
    }

    int main(void)
    {
    #ifdef TEST
    Test();
    #else
    int cont=0;
    int n;
    Numero s;
    vector<Numero> Res;
    while(cin>>s>>n)
    {
    Res.push_back(Potenza(s,n));
    cout<<Res[Res.size()-1]<<"\n";
    }
    for(int i=0;i<Res.size();i++)
    cout<<Res[i];

    #endif
    return 0;
    }

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Cosi' non si capisce quasi nulla del codice ... mettilo tra i tag appositi in modo di mantenere l'indentazione ...

  3. #3
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463

    Moderazione

    Suggerisco anche l'uso di titoli più significativi, che indichino il problema contenuto nella discussione piuttosto che le elucubrazioni mentali di chi la scrive, altrimenti non ci si capirebbe più nulla sfogliando i thread di "Programmazione".
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  4. #4
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    211
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <string>
    #include <ctype.h>
    #include <vector>

    #define DEBUG
    //#define TEST

    using namespace std;

    //123: Cifre[0]=3; Cifre[1]=2; Cifre[2]=1;
    class Numero
    {
    public:
    int* Cifre;
    int Len;
    int PosVirgola;
    Numero()
    {
    Numero(0);
    }

    Numero (int len)
    {
    PosVirgola=0;
    Len=len;
    Cifre=new int[len];
    //Azzera
    for (int i=0;i<len;i++)
    Cifre[i]=0;
    }

    //Si puo' non utilizzare len
    Numero (int len,int n)
    {
    PosVirgola=0;
    if (len==-1)
    Len=Numero::LenNumero(n);
    else
    Len=len;
    Cifre=new int[Len];

    int i=0;
    while (n>0)
    {
    Cifre[i++]=(n%10);
    n/=10;
    }
    }

    int& operator[](int a)
    {
    return Cifre[a];
    }

    static int LenNumero(int a);
    };

    ostream& operator<<(ostream& s, Numero a)
    {
    int Zero=0;
    while (Zero<a.PosVirgola && a[Zero]==0) Zero++;

    bool PrintVirgola=true;
    if (Zero==a.PosVirgola)PrintVirgola=false;

    int j=a.Len-1;
    if (a.Len==a.PosVirgola) s<<"0.";
    while (j>=Zero)
    {
    s<<a[j];
    if ((j==a.PosVirgola) && PrintVirgola)
    s<<".";
    j--;
    }

    return s;

    }

    istream& operator>>(istream& s, Numero& a)
    {
    //Legge tutti gli eventuali spazi iniziali
    char c;

    while ((s.get(c)) && (isspace(c)));

    int* Arr=new int[1000]; int i=0; int PosVirgolaEnd=-1;
    do
    {
    if (c=='.')
    PosVirgolaEnd=i;
    else Arr[i++]=c-'0';
    }
    while((s.get(c)) && (!isspace(c)));

    //Sistema la virgola
    a=Numero(i);
    if (PosVirgolaEnd==-1)
    a.PosVirgola=0;
    else
    a.PosVirgola=i-PosVirgolaEnd;

    //Sistema le cifre
    for (int j=0;j<i;j++)
    {
    //Scambia
    a.Cifre[i-j-1]=Arr[j];
    }

    return s;


    }

    int Numero::LenNumero(int a)
    {
    int Cont=0;
    while (a>0)
    {
    a/=10;
    Cont++;
    }
    return Cont;
    }

    Numero operator + (Numero a,Numero b)
    {
    //Inizializza
    Numero Ret(max(a.Len,b.Len)+1);

    //Somma cifra per cifra
    int Resto=0;
    int j=0;
    for(j=0;j<min(a.Len,b.Len);j++)
    {
    int Somma=a.Cifre[j]+b.Cifre[j];
    Ret.Cifre[j]=(Somma+Resto)%10;
    if (Somma+Resto>=10)
    Resto=1;
    else
    Resto=0;
    }
    //bool Enter=false;
    while (j<max(a.Len,b.Len))
    {

    int Somma=(a.Len>b.Len)?(a.Cifre[j])b.Cifre[j]);
    Ret.Cifre[j]=(Somma+Resto)%10;
    if (Somma+Resto>=10)
    Resto=1;
    else
    Resto=0;
    j++;
    }
    if (Resto==0) Ret.Len--;
    else
    Ret[Ret.Len-1]=1;

    return Ret;

    }

    Numero operator * (Numero a,Numero b)
    {
    #ifdef DEBUG
    cout<<a<<b;
    #endif
    Numero ProdN(1,0); int ProdNDec=0;
    for (int i=0;i<b.Len;i++)
    {
    Numero Somma(-1,0);
    int Dec=0;
    for(int j=0;j<a.Len;j++)
    {

    Numero TT(-1,b[i]*a[j]);int TTCont=TT.Len-1;
    Numero Temp(TT.Len+Dec);
    for (int k=Temp.Len-1;k>=0;k--)
    {
    if (TTCont>=0)
    Temp[k]=TT[TTCont--];
    else
    Temp[k]=0;
    }
    Somma= Somma+Temp;
    Dec++;
    }
    Numero SommaTemp(Somma.Len+ProdNDec);
    int SommaCont=Somma.Len-1;
    for (int k=SommaTemp.Len-1;k>=0;k--)
    {
    if(SommaCont>=0)
    SommaTemp[k]=Somma[SommaCont--];
    else
    SommaTemp[k]=0;
    }
    #ifdef DEBUG
    //cout<<"SommaTemp:"<<SommaTemp<<"\n";
    #endif
    ProdN=ProdN+SommaTemp;
    ProdNDec++;
    }

    ProdN.PosVirgola=a.PosVirgola+b.PosVirgola;
    return ProdN;

    }

    Numero Potenza(Numero r,int ex)
    {

    Numero Ret(1);
    Ret[0]=1;
    Ret.PosVirgola=0;
    for(int i=0;i<ex;i++)
    {

    Ret=Ret*r;
    #ifdef DEBUG
    //cout<<r<<" "<<ex<<" Esponente("<<i<<"):"<<Ret<<"\n";
    #endif

    }

    return Ret;
    }

    void Test()
    {
    /*Numero a(3);
    Numero b(2);
    a.PosVirgola=2;
    b.PosVirgola=1;
    a[0]=9;
    a.Cifre[1]=0;
    a.Cifre[2]=1;
    b.Cifre[0]=1;
    b.Cifre[1]=1;
    Numero Res=a*b;*/
    Numero a; Numero n;
    cin>>a>>n;
    cout<<a+n;
    }

    int main(void)
    {
    #ifdef TEST
    Test();
    #else
    int cont=0;
    int n;
    Numero s;
    vector<Numero> Res;
    while(cin>>s>>n)
    {
    Res.push_back(Potenza(s,n));
    cout<<Res[Res.size()-1]<<"\n";
    }
    for(int i=0;i<Res.size();i++)
    cout<<Res[i];

    #endif
    return 0;
    }

  5. #5
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    211
    codice:
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <string>
    #include <ctype.h>
    #include <vector>
    
    #define DEBUG
    //#define TEST
    
    using namespace std;
    
    //123: Cifre[0]=3; Cifre[1]=2; Cifre[2]=1;
    class Numero
    {
    public:
        int* Cifre;
        int Len;
        int PosVirgola;
        Numero()
        {
            Numero(0);
        }
        
        Numero (int len)
        {
            PosVirgola=0;
            Len=len;
            Cifre=new int[len];
            //Azzera
            for (int i=0;i<len;i++)
                    Cifre[i]=0;
        }
        
        //Si puo' non utilizzare len 
        Numero (int len,int n)
        {
            PosVirgola=0;
            if (len==-1)
                    Len=Numero::LenNumero(n);   
            else
                   Len=len;
            Cifre=new int[Len];
             
            int i=0;
            while (n>0)
            {
                    Cifre[i++]=(n%10);
                    n/=10;
            }
        }
        
        int& operator[](int a)
        {
            return Cifre[a];
        }
        
        static int LenNumero(int a);
    };
    
    ostream& operator<<(ostream& s, Numero a)
    {
        int Zero=0;
        while (Zero<a.PosVirgola && a[Zero]==0) Zero++;
        
        bool PrintVirgola=true;
        if (Zero==a.PosVirgola)PrintVirgola=false;
    
        int j=a.Len-1;
        if (a.Len==a.PosVirgola) s<<"0.";
        while (j>=Zero)
        {
            s<<a[j];
            if ((j==a.PosVirgola) && PrintVirgola)
                    s<<".";
            j--;
        }
        
        return s;
    
    }
    
    istream& operator>>(istream& s, Numero& a)
    {
        //Legge tutti gli eventuali spazi iniziali
        char c;
        
        while ((s.get(c)) && (isspace(c)));
        
        int* Arr=new int[1000]; int i=0; int PosVirgolaEnd=-1;
        do
        {
            if (c=='.')      
                   PosVirgolaEnd=i;     
            else Arr[i++]=c-'0';
        }
        while((s.get(c)) && (!isspace(c)));
        
        //Sistema la virgola
        a=Numero(i);
        if (PosVirgolaEnd==-1)
            a.PosVirgola=0;
        else
            a.PosVirgola=i-PosVirgolaEnd;
         
        //Sistema le cifre
        for (int j=0;j<i;j++)
        {
            //Scambia
            a.Cifre[i-j-1]=Arr[j];
        }
       
        return s;
    
    
    }
    
    int Numero::LenNumero(int a)
    {
        int Cont=0;
        while (a>0)
        {
            a/=10;
            Cont++;
        }
        return Cont;
    }
    
    Numero operator + (Numero a,Numero b)
    {
        //Inizializza
        Numero Ret(max(a.Len,b.Len)+1);
        
        //Somma cifra per cifra
        int Resto=0;
        int j=0;
        for(j=0;j<min(a.Len,b.Len);j++)
        {
            int Somma=a.Cifre[j]+b.Cifre[j];
            Ret.Cifre[j]=(Somma+Resto)%10;
            if (Somma+Resto>=10)
                    Resto=1;
                else
                    Resto=0;
        }
        //bool Enter=false;
        while (j<max(a.Len,b.Len))
        {
      
            int Somma=(a.Len>b.Len)?(a.Cifre[j]):(b.Cifre[j]);
            Ret.Cifre[j]=(Somma+Resto)%10;
            if (Somma+Resto>=10)
                    Resto=1;
                else
                    Resto=0;
            j++;
        }
        if (Resto==0) Ret.Len--;
        else
        Ret[Ret.Len-1]=1;
            
        return Ret;
    
    }
    
    Numero operator * (Numero a,Numero b)
    {
        #ifdef DEBUG
        cout<<a<<b; 
        #endif
        Numero ProdN(1,0); int ProdNDec=0;
        for (int i=0;i<b.Len;i++)
        {
            Numero Somma(-1,0);
            int Dec=0;
            for(int j=0;j<a.Len;j++)
            {
                    
                    Numero TT(-1,b[i]*a[j]);int TTCont=TT.Len-1;
                    Numero Temp(TT.Len+Dec);
                    for (int k=Temp.Len-1;k>=0;k--)
                    {
                         if (TTCont>=0) 
                              Temp[k]=TT[TTCont--];
                         else
                              Temp[k]=0;     
                    }
                    Somma= Somma+Temp;
                    Dec++;       
            }
            Numero SommaTemp(Somma.Len+ProdNDec);
            int SommaCont=Somma.Len-1;
            for (int k=SommaTemp.Len-1;k>=0;k--)
            {
                    if(SommaCont>=0)
                            SommaTemp[k]=Somma[SommaCont--];
                    else
                            SommaTemp[k]=0;
            }
            #ifdef DEBUG
            //cout<<"SommaTemp:"<<SommaTemp<<"\n";
            #endif
            ProdN=ProdN+SommaTemp;
            ProdNDec++;
        }
    
        ProdN.PosVirgola=a.PosVirgola+b.PosVirgola;
        return ProdN;  
    
    }
    
    Numero Potenza(Numero r,int ex)
    {
        
        Numero Ret(1);
        Ret[0]=1;
        Ret.PosVirgola=0;
        for(int i=0;i<ex;i++)
        {
     
            Ret=Ret*r;   
            #ifdef DEBUG
                    //cout<<r<<" "<<ex<<" Esponente("<<i<<"):"<<Ret<<"\n";
            #endif
            
            }
            
        return Ret;
    }
    
    void Test()
    {
      /*Numero a(3);
      Numero b(2);
      a.PosVirgola=2;
      b.PosVirgola=1;
      a[0]=9;
      a.Cifre[1]=0;
      a.Cifre[2]=1;
      b.Cifre[0]=1;
      b.Cifre[1]=1;
      Numero Res=a*b;*/
      Numero a; Numero n;
      cin>>a>>n;
      cout<<a+n;
    }
    
    int main(void)
    {
    #ifdef TEST
      Test();
    #else
      int cont=0;
      int n;
      Numero s;
      vector<Numero> Res;
      while(cin>>s>>n)
      {
            Res.push_back(Potenza(s,n));
            cout<<Res[Res.size()-1]<<"\n";
      }
      for(int i=0;i<Res.size();i++)
        cout<<Res[i];
    
    #endif
      return 0;
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.