设为首页收藏本站|繁體中文 快速切换版块

 找回密码
 立即加入
搜索
查看: 965|回复: 0

[练习]写一个atoi()函数

[复制链接]
  • TA的每日心情
    慵懒
    2016-4-21 12:07
  • 签到天数: 3 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    累计签到:3 天
    连续签到:1 天
    发表于 2010-5-6 07:59:55 | 显示全部楼层 |阅读模式

    马上加入,结交更多好友,共享更多资料,让你轻松玩转电力研学社区!

    您需要 登录 才可以下载或查看,没有账号?立即加入

    ×
    本函数my_atoi与C标准库函数atoi相同,测试代码中与atoi比较输出结果。3 [: n7 n( i& Z( s& t6 ?
    #include <iostream>
    2 d; K  p6 U6 v* r) Y+ Y) E' X#include <cstdlib>
    ( K" c& U. _3 E( D+ t+ |#include <string>: t) g9 M1 e9 a9 }7 _  [  G1 R
    #include <stdlib.h>% t# I+ D* E2 b% x
    #include <ctype.h>0 D5 A, t. y1 Z# q3 K5 N+ W
    #include <string>% ]  n5 |; Y( |* Y" H; c
    #include <list>
    2 m4 J$ G  D& v2 wusing namespace std;( v' @" }  l! _7 c4 c
    //  atoi
    7 F) {: X4 C8 Z* qint my_atoi ( const char * str )
    5 Q1 s/ ~: N3 Y/ d0 K6 S8 N, S; V{. B" L+ @: X( v' o
        int flag = 1; //for minus or positive* m2 Y  D  c4 q- Z1 Y
        int result = 0;
    9 Z$ Y% G  K+ Q% X    if(str)
    ( b1 a2 \% K3 |1 y% _0 w    {
    * W/ q8 U! o/ G; }        while( *str )
    5 D6 @" N) X; z( w# c: _; f! ]        {
    0 J$ S! _0 M3 b$ Y6 X2 @% R2 z            if(( '-'==*str ) || ( '+'==*str))
    5 f  y/ b7 D3 z2 Q' i' w" c+ s- E7 P            {$ }6 R( ?9 k& j. l
                   flag = ( '-'==*str ) ? -1 : 1 ;
    * r2 y0 I( V& c" X               while( isspace(*(++str)) );. u& V1 X% E$ Q$ _
                }7 v$ u5 W; a  r' R2 I
                if( isdigit(*str) )* Y- ]. D. S% z) l: p
                {$ n: P- w+ ^- G! w8 Q% C; ?
                   break;
    ! r& v- g9 {  m6 v& w# L            }  j  `1 L5 ^$ o  r5 @, z/ h, D! i$ X
                str++;
    : Z1 i' _* e" b# {& K9 e4 |        }
    - T8 [: ?* x1 ?- L; I        if(*str)
    ; O! O3 K, U: t# n/ g        {
    . G% S  N3 j5 E, e            do{
    1 q  I2 H/ l* k( y7 e  i$ i! s& d4 w               result =result * 10 -'0' + *str;
    * Q; n% v" {1 I            }while( isdigit(*(++str)) );9 c. L8 f. P2 O# N4 J
                result *= flag;' b- e' I* w* W* h
            }/ I' E9 Q/ p- ~, g  A
        }7 C  a7 J# G( ^/ ~' D
        return result;
    ; o! n) F# I) g) m, Q5 I2 f( j}5 h$ L) A9 f5 H- ?: H
    int main(int argc, char *argv[])0 M( Z9 p! \# \
    {
    ( L2 M4 C, K/ l- }. x  list<string> li;
    8 d( G& ]6 w: C6 t/ `8 Z  A% d  @  li.push_back("0");# k- o8 j3 t1 Q( b' j9 x) ]6 {
      li.push_back("123");
    / ?* W1 e% F$ u) V/ F  li.push_back("-12");
    ' x) g2 m/ C0 w3 m4 v! e" F  li.push_back("1 ");& I- }0 N6 p% _4 e, A& s
      li.push_back("1235k344");; `; p" o4 z7 T. F0 |- y: Z. U
      li.push_back("  -1234");7 n+ S6 a+ C* {( n& G
      li.push_back("  - 123");- }, |, `6 r8 O# w6 m+ o5 i7 p! ?0 h
      li.push_back(" -    1234"); // \t# |9 f' M0 E$ K2 B( K' A; M2 F& j; v
      li.push_back("1000000000000000000000000000");
    9 t9 N# M  T4 t  li.push_back("1a");! v% z: f( a6 \, r) _. d: J
      li.push_back("-");
    + h2 K: y" V( ?: b  for(list<string>::iterator it = li.begin(); it != li.end(); it++)
    % m/ x. K7 [1 H7 O1 h5 n% _% h  {
    1 w4 C8 i' e9 I3 D/ b      cout <<"input = "<<*it
    ' E+ U/ |+ Q7 K  ?! B9 i4 ]           << ", atio() = " <<atoi(it->c_str())
    3 e9 P, j& t. @' g! ^+ V$ |% a# B           <<", my_atio() = " << my_atoi(it->c_str())<<endl;/ _8 n+ o  N! x8 C  Z6 @* o  z
      }& \3 C" l( }) N: Y. }
      return 0;
    - _. R1 A6 Q, R7 p}
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    楼主热帖
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】
    您需要登录后才可以回帖 登录 | 立即加入

    本版积分规则

    招聘斑竹

    小黑屋|手机版|APP下载(beta)|Archiver|电力研学网 ( 赣ICP备12000811号-1|赣公网安备36040302000210号 )|网站地图

    GMT+8, 2026-10-31 08:38

    Powered by Discuz! X3.5 Licensed

    © 2001-2026 Discuz! Team.

    快速回复 返回顶部 返回列表