我正在尝试解决此任务: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 :(得分:1)
我使用divmod将数字提取为字符,然后对结果字符串进行排序。
答案 1 :(得分: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;
}