从字符串中提取数字并排序输出
2011-03-31 23:23
429 查看
从字符串中提取数字并排序输出
看到别人的讨论贴,也做了下。题目很简单,可以直接用标准库函数,也可以自已造轮子。标准库中能用到的只有:atoi / strtol系列,显然得用 strtol(有些库atoi就是用strtol实现的)
在用strtol前,先要对不安全的strtol封装下。
int str2i(const char str[], const char* &next, int base = 10)
{
return strtol(str, const_cast<char**>(&next), base);
}
可采用两种算法:
1 先忽略空白字符,然后直接调用str2i (代码 solve1)
2 先找到合法数字的第一个字符,再调用str2i(代码 solve2)
当然也可以自行实现strtol(代码: solve3和solve4)。
View Code
// http://cnblogs.com/flyinghearts #include <iostream>
#include <algorithm>
#include <iterator>
#include <deque>
#include <limits>
#include <cstdlib>
using std::cout;
int str2i(const char str[], const char* &next, int base = 10)
{
return strtol(str, const_cast<char**>(&next), base);
}
void solve1(const char* str)
{
if (str == NULL) return;
std::deque<int> dq;
const char* p = str;
const char* next = NULL;
while (*p) {
if (isspace(*p)) {
++p;
continue;
}
errno = 0;
int value = str2i(p, next);
if (errno != 0) {
cout << "data error at pos: " << p - str << "\n";
p = next;
continue;
}
if (next > p) {
p = next;
dq.push_back(value);
continue;
}
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(cout," "));
cout << "\n\n";
}
void solve2(const char* str)
{
if (str == NULL) return;
std::deque<int> dq;
const char* p = str;
const char* next = NULL;
while (*p) {
if (!isdigit(*p) && (*p != '-' || !isdigit(*(p + 1)))) {
++p;
continue;
}
errno = 0;
int value = str2i(p, next);
if (errno != 0) {
cout << "data error at pos: " << p - str << "\n";
p = next;
continue;
}
if (next > p) {
p = next;
dq.push_back(value);
}
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
void solve3(const char* str)
{
//const int max_int = -1u / 2u;
const int max_int = std::numeric_limits<int>::max();
const int max_value = max_int / 10;
const int value_left = max_int % 10;
const char* p = str;
std::deque<int> dq;
while (*p) {
while (*p == ' ' || *p == '\t') ++p;
bool negative = false;
if (*p == '-') { negative = true; ++p; }
else if (*p == '+') { ++p; }
int value = 0;
bool range_error = false;
bool value_read = false;
const char *old = p;
for ( ; *p; ++p) {
int tmp = *p - '0';
if (tmp < 0 || tmp > 9) break;
if (range_error) continue;
if (value > max_value || (value == max_value && tmp > value_left + negative)) {
range_error = true;
continue;
}
value = value * 10 + tmp;
value_read = true;
}
if (range_error) {
cout << "data error at pos: " << old - str << "\n";
continue;
}
if (value_read) {
if (negative) value = - value;
dq.push_back(value);
}
if (*p == 0) break;
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
void solve4(const char* str)
{
//const int max_int = -1u / 2u;
const int max_int = std::numeric_limits<int>::max();
const int max_value = max_int / 10;
const int value_left = max_int % 10;
const char* p = str;
std::deque<int> dq;
bool negative = false;
while (*p) {
if (!isdigit(*p)) {
if (*p++ != '-' ) continue;
if (!isdigit(*p)) continue;
negative = true;
}
const char *old = p;
int value = *p++ - '0';
bool range_error = false;
for ( ; *p; ++p) {
if (!isdigit(*p)) break;
if (range_error) continue;
int tmp = *p - '0';
if (value > max_value || (value == max_value && tmp > value_left + negative)) {
range_error = true;
continue;
}
value = value * 10 + tmp;
}
if (range_error) {
cout << "data error at pos: " << old - str << "\n";
continue;
}
if (negative) {
value = - value;
negative = false;
}
dq.push_back(value);
if (*p == 0) break;
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
int main()
{
char ss[] = "2147483646 2147483647 2147483648 -2147483647 -2147483648 -2147483649";
char str[] = "-a33k&-99+r5s-w1f10g-2d4vc+511m99999999999999gc3-";
solve1(str);
solve2(str);
solve3(str);
solve4(str);
solve1(ss);
solve2(ss);
solve3(ss);
solve4(ss);
}
看到别人的讨论贴,也做了下。题目很简单,可以直接用标准库函数,也可以自已造轮子。标准库中能用到的只有:atoi / strtol系列,显然得用 strtol(有些库atoi就是用strtol实现的)
在用strtol前,先要对不安全的strtol封装下。
int str2i(const char str[], const char* &next, int base = 10)
{
return strtol(str, const_cast<char**>(&next), base);
}
可采用两种算法:
1 先忽略空白字符,然后直接调用str2i (代码 solve1)
2 先找到合法数字的第一个字符,再调用str2i(代码 solve2)
当然也可以自行实现strtol(代码: solve3和solve4)。
View Code
// http://cnblogs.com/flyinghearts #include <iostream>
#include <algorithm>
#include <iterator>
#include <deque>
#include <limits>
#include <cstdlib>
using std::cout;
int str2i(const char str[], const char* &next, int base = 10)
{
return strtol(str, const_cast<char**>(&next), base);
}
void solve1(const char* str)
{
if (str == NULL) return;
std::deque<int> dq;
const char* p = str;
const char* next = NULL;
while (*p) {
if (isspace(*p)) {
++p;
continue;
}
errno = 0;
int value = str2i(p, next);
if (errno != 0) {
cout << "data error at pos: " << p - str << "\n";
p = next;
continue;
}
if (next > p) {
p = next;
dq.push_back(value);
continue;
}
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(cout," "));
cout << "\n\n";
}
void solve2(const char* str)
{
if (str == NULL) return;
std::deque<int> dq;
const char* p = str;
const char* next = NULL;
while (*p) {
if (!isdigit(*p) && (*p != '-' || !isdigit(*(p + 1)))) {
++p;
continue;
}
errno = 0;
int value = str2i(p, next);
if (errno != 0) {
cout << "data error at pos: " << p - str << "\n";
p = next;
continue;
}
if (next > p) {
p = next;
dq.push_back(value);
}
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
void solve3(const char* str)
{
//const int max_int = -1u / 2u;
const int max_int = std::numeric_limits<int>::max();
const int max_value = max_int / 10;
const int value_left = max_int % 10;
const char* p = str;
std::deque<int> dq;
while (*p) {
while (*p == ' ' || *p == '\t') ++p;
bool negative = false;
if (*p == '-') { negative = true; ++p; }
else if (*p == '+') { ++p; }
int value = 0;
bool range_error = false;
bool value_read = false;
const char *old = p;
for ( ; *p; ++p) {
int tmp = *p - '0';
if (tmp < 0 || tmp > 9) break;
if (range_error) continue;
if (value > max_value || (value == max_value && tmp > value_left + negative)) {
range_error = true;
continue;
}
value = value * 10 + tmp;
value_read = true;
}
if (range_error) {
cout << "data error at pos: " << old - str << "\n";
continue;
}
if (value_read) {
if (negative) value = - value;
dq.push_back(value);
}
if (*p == 0) break;
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
void solve4(const char* str)
{
//const int max_int = -1u / 2u;
const int max_int = std::numeric_limits<int>::max();
const int max_value = max_int / 10;
const int value_left = max_int % 10;
const char* p = str;
std::deque<int> dq;
bool negative = false;
while (*p) {
if (!isdigit(*p)) {
if (*p++ != '-' ) continue;
if (!isdigit(*p)) continue;
negative = true;
}
const char *old = p;
int value = *p++ - '0';
bool range_error = false;
for ( ; *p; ++p) {
if (!isdigit(*p)) break;
if (range_error) continue;
int tmp = *p - '0';
if (value > max_value || (value == max_value && tmp > value_left + negative)) {
range_error = true;
continue;
}
value = value * 10 + tmp;
}
if (range_error) {
cout << "data error at pos: " << old - str << "\n";
continue;
}
if (negative) {
value = - value;
negative = false;
}
dq.push_back(value);
if (*p == 0) break;
++p;
}
std::sort(dq.begin(), dq.end());
std::copy(dq.begin(), dq.end(), std::ostream_iterator<int>(std::cout," "));
cout << "\n\n";
}
int main()
{
char ss[] = "2147483646 2147483647 2147483648 -2147483647 -2147483648 -2147483649";
char str[] = "-a33k&-99+r5s-w1f10g-2d4vc+511m99999999999999gc3-";
solve1(str);
solve2(str);
solve3(str);
solve4(str);
solve1(ss);
solve2(ss);
solve3(ss);
solve4(ss);
}
相关文章推荐
- [原]Java面试题-将字符串中数字提取出来排序后输出
- python 如何从字符串中提取出不同数字并输出
- 将n个字符串中的数字提取并排序
- 有一串字符串a12hh123mm21m78 ,请将其中的数字提取出来存入数组,使用选择排序算法将其进行排序:
- HDOJ1106 排序(字符串数字提取)
- 数字字符串排序后重新输出
- 牛客网三道编程题(四)华为机试在线训练(三)将数字逆序输出、将n个字符串按照字典顺序排序
- 解析字符串中的数字,并将其排序后输出字符串
- 输出字符串中的数字 并进行排序
- php数字字符串排序输出不能使用任何php内置函数
- 大的字符串的中数据转化为数字,字符数值的提取转化,atoi();
- 字符串提取数字/汉字/英文字母
- Mysql中实现提取字符串中的数字的自定义函数分享
- 输入6个字符串,并对它们按从大到小的顺序排序后输出。
- 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
- 气泡法排序,输入5个int数字,由小到大输出
- Python中字符串中的数字提取方法
- 《任意输入一个合理大小的字符串,实现逆序输出并统计一个字符串中字母、数字、空格及其他字符的个数》
- 在主函数中输入10个等长的字符串。用另一个函数对它们排序,然后主函数输出10个已经排好的字符串
- Excel中 提取字符串中的字符或数字