2010-10-10 6 views
-2

je suis en train de résoudre cette tâche: Sorting Numerical Data Alphabeticallytri de données numériques par ordre alphabétique

ici est mon code

#include <iostream> 
#include <math.h> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <ostream> 
using namespace std; 
float count(int n) { 
    float total=0; 
    while (n!=0) { 
     n/=10; 
     total++; 
    } 
    return total; 
} 

struct Int { 
    int x; 
}; 

int Sort(const struct Int&a,const struct Int&b){ 
    float s=0; 
    float t=0; 
    if ((count(a.x))==1 && (count(b.x))==1){ 
     if (a.x<b.x){ 
      return a.x; 
     } 
     return b.x; 
    } 

    if ((count(a.x))==1){ 
     s=(float)b.x/(std::powf(10,(count(b.x)-1))); 
     if (a.x<s){ 
      return a.x; 
     } 
     return b.x; 
    } 

    if (b.x==1){ 
     s=(float)a.x/(std::powf(10,count(a.x)-1)); 
     if (s<b.x){ 
      return a.x; 
     } 
     return b.x; 
    } 
    int n=count(a.x)<=count(b.x)?count(a.x):count(b.x); 
    for (int i=1;i<n;i++){ 
     s=(float)a.x/(std::powf(10,count(a.x)-i)); 
     t=(float)b.x/(std::powf(10,count(b.x)-i)); 

     if (s<t) 
      return b.x; 
    } 
    return b.x; 
} 

int main(){ 

    vector<Int>v(3); 
    for (int i=0;i<3;i++){ 
     cin>>v[i].x; 
    } 
    std::sort(v.begin(),v.end(),Sort); 
    vector<Int>::iterator it; 
    for (int i=0;i<3;i++){ 

     cout<<v[i].x<<endl; 
    } 
    return 0; 
] 

il compile bien, mais quand je saisir des chiffres, il m'a montré des exceptions, il a fini ubnormaly s'il vous plaît me dire pourquoi ?

+0

Quelle aide vous cherchez? Ne comprenez-vous pas les messages d'erreur, ou croyez-vous qu'ils sont erronés, ou quoi? –

+1

Il n'y a pas d'erreurs, seulement des avertissements. Expliquez votre problème plus précisément, s'il vous plaît. – You

+0

je mettrai à jour mon code –

Répondre

1

Voici une version commentée qui peut vous aider à travailler ce qui ne va pas

#include <iostream> 
#include <math.h> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <ostream> 
using namespace std; 

//number of digits in n 
//why does this return a float? It's always going to be an integer 

float count(int n) { 
    float total=0; 
    while (n!=0) { 
     n/=10; 
     total++; 
    } 
    return total; 
} 

struct Int { 
    int x; 
}; 

    // Should be boolean? 
int Sort(const struct Int&a,const struct Int&b){ 
    float s=0; 
    float t=0; 
    // if they're both one digit long, we can just compare 
    if ((count(a.x))==1 && (count(b.x))==1){ 
     if (a.x<b.x){ 
      return a.x; 
     } 
     return b.x; 
    } 

    // if the first one is one digit long, but the second is two digits, 
    // then calculate the significand of b, and compare that to a 
    // so if a is 3 and b is 34, we compare 3 and 3.4 
    if ((count(a.x))==1){ 
     s=(float)b.x/(std::powf(10,(count(b.x)-1))); 
     if (a.x<s){ 
      return a.x; 
     } 
     return b.x; 
    } 

    ///missing a count() here, but the idea is to do the same as above with a and b swapped 
    if (b.x==1){ 
     s=(float)a.x/(std::powf(10,count(a.x)-1)); 
     if (s<b.x){ 
      return a.x; 
     } 
     return b.x; 
    } 
    // n is min (count(a.x), count(b.x)) 
    int n=count(a.x)<=count(b.x)?count(a.x):count(b.x); 
    // I tihnk this is trying to compare increasing numbers if digits of the significand // comparison that alwasys produces the same result each time around the loop 
    // but instead it does the same comparison each time 
    // because s and t both get 10 times smaller each time around the loop... 
    for (int i=1;i<n;i++){ 
     // oops. Cast has higher precedence than/so this probably doesn't do what you think 
     s=(float)a.x/(std::powf(10,count(a.x)-i)); 
     t=(float)b.x/(std::powf(10,count(b.x)-i)); 

     if (s<t) 
      // this was probably intended to be a.x 
      return b.x; 
    } 

    return b.x; 
} 

int main(){ 

    vector<Int>v(3); 
    for (int i=0;i<3;i++){ 
     cin>>v[i].x; 
    } 
    std::sort(v.begin(),v.end(),Sort); 
    vector<Int>::iterator it; 
    for (int i=0;i<3;i++){ 

     cout<<v[i].x<<endl; 
    } 
    return 0; 
} 
1

J'utiliserais divmod pour extraire les chiffres en caractères, puis trier sur les chaînes résultantes.

Questions connexes