tags:

views:

69

answers:

2

i am trying to solve this task: http://stackoverflow.com/questions/719614/sorting-numerical-data-alphabetically

here is my 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;
]

it compiles fine but when i enter numbers it showed me exceptions it finished ubnormaly please tell me why?

+1  A: 

I'd use divmod to extract the digits into characters, then sort on the resultant strings.

Ignacio Vazquez-Abrams
+1  A: 

Here's a commented version which may help you work out what's wrong

#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;
}
Paul