谁能帮我详细解答一下C++中atoi的使用条件?

LCR189 2010-07-25 06:41:00
代码如下:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class BoulderRace
{
public:
int winner(vector <string> boulders, int distance);
};

int BoulderRace::winner(vector <string> boulders, int distance)
{
vector <int>count;
int min, locat;
for( int i = 0; i < boulders.size(); i++ )
{
int time = 0, k = 0, m = 0, Dis = 0;
for( int j = 0; j < boulders[i].length(); j++ )
Dis = Dis + atoi( boulders[i][j] ); //此处atoi达不到效果。
k = distance / Dis;
time = k * boulders[i].length();
Dis = Dis * k;
while( Dis < distance )
{
Dis = Dis + boulders[i][m];
time++;
m++;
}
count.push_back( time );
}
min = count[0];
locat = 0;
for( int n = 1; n < count.size(); n++ )
{
if( count[n] < min )
{
min = count[n];
locat = n;
}
}
return locat;
}
...全文
324 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LCR189 2010-07-25
  • 打赏
  • 举报
回复
Dis = Dis + atoi( boulders[i][j] ); //此处atoi达不到效果

此处boulders[i][j]是 (string) boulders[i]中的一个元素。

那么请问atoi可以用于string对象吗?
wade_2003 2010-07-25
  • 打赏
  • 举报
回复

int time = 0, k = 0, m = 0, Dis = 0;
for( int j = 0; j < boulders[i].length(); j++ )
Dis = Dis + atoi( boulders[i][j] ); //此处atoi达不到效果。
k = distance / Dis;


达不到效果是什么意思?

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}


Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854



duke56 2010-07-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lcr189 的回复:]
也就是说atoi的实参只能是指针?
[/Quote]

是指针
int atoi( const char *string );//函数原型
LCR189 2010-07-25
  • 打赏
  • 举报
回复
int i=atoi( const char* );
LCR189 2010-07-25
  • 打赏
  • 举报
回复
也就是说atoi的实参只能是指针?
fjbuilding 2010-07-25
  • 打赏
  • 举报
回复
原因有2:
1.有可能boulders[i][j]返回的是一个浮点数如0.1234,这样由于您使用的是atoi(故名思意array to integer),atoi将只能返回0;此情况下建议使用atof(array to float),它将返回0.1234;

2.有可能boulders[i][j]虽然返回了一个整形比如1234,但是您使用了这一句:k = distance / Dis;
其中k,distance全部是int型的,这就导致如果distance=100,而Dis=1234,k将得到0;这种情况下建议您把所有需要进行精确运算的变量换成float或者double型,当然atoi也要换成atof;

详见msnd;
duke56 2010-07-25
  • 打赏
  • 举报
回复
[code=C/C++]or( int j = 0; j < boulders[i].length(); j++ )
Dis = Dis + atoi( &boulders[i][j] ); //?[code]

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧