将一串数字转换为任何形式的整数
Converting a string of numbers to any form of an int
作为更大程序的一部分,我必须将一串数字转换为整数(最终是浮点数)。不幸的是,我不允许使用铸造或 atoi。
我想一个简单的操作是这样的:
void power10combiner(string deciValue){
int result;
int MaxIndex=strlen(deciValue);
for(int i=0; MaxIndex>i;i++)
{
result+=(deciValue[i] * 10**(MaxIndex-i));
}
}
会工作。如何将字符转换为整数?我想我可以使用 ASCII 转换,但无论如何我都无法将字符添加到整数中(假设转换方法是有一个巨大的 if 语句,该语句返回每个 ASCII 数字后面的不同数值)。
有很多
方法可以做到这一点,并且可以对函数进行一些优化和更正。
1)你没有从函数返回任何值,所以返回类型现在是int。
2) 您可以通过传递 const 引用来优化此功能。
现在来看例子。
使用 std::stringstream 进行转换。
int power10combiner(const string& deciValue)
{
int result;
std::stringstream ss;
ss << deciValue.c_str();
ss >> result;
return result;
}
不使用 std::stringstream 进行转换。
int power10combiner(const string& deciValue)
{
int result = 0;
for (int pos = 0; deciValue[pos] != ' '; pos++)
result = result*10 + (deciValue[pos] - '0');
return result;
}
由 suggestion 编辑,并添加了一些解释。
int base = 1;
int len = strlen(deciValue);
int result = 0;
for (int i = (len-1); i >= 0; i--) { // Loop right to left. Is this off by one? Too tired to check.
result += (int(deciValue[i] - '0') * base); // '0' means "where 0 is" in the character set. We are doing the conversion int() because it will try to multiply it as a character value otherwise; we must cast it to int.
base *= 10; // This raises the base...it's exponential but simple and uses no outside means
}
这假设字符串只是数字。如果您需要更多说明,请发表评论。
您可以通过简单地实现任何数字基数的位值系统,将字符串迭代解析为整数。假设您的字符串以 null 结尾且数字无符号:
unsigned int parse(const char * s, unsigned int base)
{
unsigned int result = 0;
for ( ; *s; ++s)
{
result *= base;
result += *s - '0'; // see note
}
return result;
}
如前所述,这仅适用于使用数字 0
、...、9
的最多 10 个基数,这些数字保证在执行字符集中按顺序排列。如果您需要更大的数字基数或更自由的符号集,则需要将指示行中的*s - '0'
替换为确定输入字符数字值的合适查找机制。
我会使用
std::stringstream,但还没有人发布使用 strtol 的解决方案,所以这里有一个。请注意,它不执行处理超出范围的错误。在 unix/linux 上,您可以使用errno
变量来检测此类错误(通过将其与 ERANGE
进行比较)。
顺便说一句,有用于浮点数的strtod/strtof/strtelld函数。
#include <iostream>
#include <cstdlib>
#include <string>
int power10combiner(const std::string& deciValue){
const char* str = deciValue.c_str();
char* end; // the pointer to the first incorrect character if there is such
// strtol/strtoll accept the desired base as their third argument
long int res = strtol(str, &end, 10);
if (deciValue.empty() || *end != ' ') {
// handle error somehow, for example by throwing an exception
}
return res;
}
int main()
{
std::string s = "100";
std::cout << power10combiner(s) << std::endl;
}
相关文章:
- C++模板函数,用于比较任何无符号整数和有符号整数
- 如何检测除整数以外的任何内容是否传递给我的类构造函数?
- 如何在不给它任何类型(如整数)的情况下定义某物?
- 整数文本太大,无法用任何整数类型表示--C++
- boost::任何带有结构体和无符号整数
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- 你能在任何独立于架构的低级语言中获得更小的整数吗?
- 任何可以在单个 CPU 指令中在 0 和 1 之间翻转位/整数/布尔值的可能代码
- 为什么当用户输入除整数之外的任何内容时,我的程序会无限循环输出?C++
- 在比较两个字符串之前(整数)之前,将字符串转换为整数有任何好处
- 在C 中弹出任何元素后,会自动调整数组大小
- wchar_t保证与任何整数类型不同
- 分形编程-任何优化此代码以实现实时渲染的方法
- 如何检查输入是否是没有任何其他字符的有效整数
- 除 "6" 作为最右边数字的整数输入外,任何整数输入都可以使用,但任何数字(如 ...6.导致分段错误,为什么?
- 对于每个标量“T”,是否有任何保证存在相同大小的整数
- 如何仅将 16 位分配给二进制文件中的任何整数,而不是C++中的正常 32 位
- C++ 中任何基数中的整数文本
- 依靠任何数字类型(无符号、整数等)的隐式提升来加倍是否安全