我要按第一列的数字从小到大重新排列,第一例相同,按第二例从小到大排,并且去掉逗号有大神给编一下这个程序
c++怎么对数据筛选排序问题
答案:2 悬赏:70
解决时间 2021-04-28 13:24
- 提问者网友:✐ۖ﹏ℳ๓北风
- 2021-04-27 21:02
最佳答案
- 二级知识专家网友:萌萌哒小可爱
- 2021-04-27 21:38
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
#include
struct data_t {
unsigned first, second;
double d1, d2;
};
inline bool operator<(data_t const &a, data_t const &b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
inline std::ostream& operator<<(std::ostream& ostr, data_t const &a) {
return ostr << a.first << "\t" << a.second << "\t"
<< std::setprecision(18) << std::fixed << a.d1 << "\t"
<< std::setprecision(18) << std::fixed << a.d2;
}
int main(int const arc, char const *arg[]) {
if (arc < 3) {
std::cerr << "请输入文件名:输入、输出\n";
return __LINE__;
}
std::vector data;
{// 读入
std::ifstream istr(arg[1]);
if (!istr) {
std::cerr << "无法打开输入文件\n";
return __LINE__;
}
std::string line;
data_t tmp;
std::streamsize num = 0;
while (getline(istr, line)) {
++num;
if (std::sscanf(line.c_str(), "%u%*[^0-9]%u%*[^0-9]%lf%*[^0-9]%lf", &tmp.first, &tmp.second, &tmp.d1, &tmp.d2) != 4) {
std::cerr << "第" << num << "数据有误\n";
return __LINE__;
}
data.push_back(tmp);
}
std::sort(data.begin(), data.end());
}
{// 输出
std::ofstream ostr(arg[2]);
if (!ostr) {
std::cerr << "无法打开输出文件\n";
return __LINE__;
}
for (size_t i = 0; i != data.size(); ++i) {
ostr << data[i] << "\n";
}
}
return 0;
}输入in.txt
3 , 3 , 0 , -17.0648460388184
3, 6, 0, 16.2186546325684
2, 5, 0, 15.2066106769265
2, 2, 0, -16
输出
2 2 0.000000000000000000 16.000000000000000000
2 5 0.000000000000000000 15.206610676926500147
3 3 0.000000000000000000 17.064846038818398455
3 6 0.000000000000000000 16.218654632568398455
#include
#include
#include
#include
#include
#include
#include
struct data_t {
unsigned first, second;
double d1, d2;
};
inline bool operator<(data_t const &a, data_t const &b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
inline std::ostream& operator<<(std::ostream& ostr, data_t const &a) {
return ostr << a.first << "\t" << a.second << "\t"
<< std::setprecision(18) << std::fixed << a.d1 << "\t"
<< std::setprecision(18) << std::fixed << a.d2;
}
int main(int const arc, char const *arg[]) {
if (arc < 3) {
std::cerr << "请输入文件名:输入、输出\n";
return __LINE__;
}
std::vector
{// 读入
std::ifstream istr(arg[1]);
if (!istr) {
std::cerr << "无法打开输入文件\n";
return __LINE__;
}
std::string line;
data_t tmp;
std::streamsize num = 0;
while (getline(istr, line)) {
++num;
if (std::sscanf(line.c_str(), "%u%*[^0-9]%u%*[^0-9]%lf%*[^0-9]%lf", &tmp.first, &tmp.second, &tmp.d1, &tmp.d2) != 4) {
std::cerr << "第" << num << "数据有误\n";
return __LINE__;
}
data.push_back(tmp);
}
std::sort(data.begin(), data.end());
}
{// 输出
std::ofstream ostr(arg[2]);
if (!ostr) {
std::cerr << "无法打开输出文件\n";
return __LINE__;
}
for (size_t i = 0; i != data.size(); ++i) {
ostr << data[i] << "\n";
}
}
return 0;
}输入in.txt
3 , 3 , 0 , -17.0648460388184
3, 6, 0, 16.2186546325684
2, 5, 0, 15.2066106769265
2, 2, 0, -16
输出
2 2 0.000000000000000000 16.000000000000000000
2 5 0.000000000000000000 15.206610676926500147
3 3 0.000000000000000000 17.064846038818398455
3 6 0.000000000000000000 16.218654632568398455
全部回答
- 1楼网友:废途浑身病态
- 2021-04-27 21:48
支持一下感觉挺不错的
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息