2010-10-10 38 views
-2

我試圖解決這個任務:Sorting Numerical Data Alphabetically排序數值數據按字母順序

這裏是我的代碼

#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; 
] 

它編譯罰款,但是當我輸入數字就表明了我的異常完成它ubnormaly請告訴我爲什麼?

+0

你在找什麼幫助?你不明白錯誤信息,或者相信他們是錯誤的,或者是什麼? – 2010-10-10 19:00:32

+1

沒有錯誤,只有警告。請更準確地解釋你的問題。 – You 2010-10-10 19:09:31

+0

我將更新我的代碼 – 2010-10-10 19:12:06

回答

1

這裏有一個註釋版本可以幫助你找出什麼是錯的

#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

我會使用divmod將數字提取爲字符,然後對結果字符串進行排序。