#include <stdio.h> int myatoi(char *); int main() { char str2[][20]={ " 2024", " -21.50", "+16m", " 654,321", "+-40", "x86", "-x86", "2024/1/31", ""}; int ival, i; for ( i=0; str2[i][0]!='\0'; i++ ){ ival = myatoi( str2[i] ); printf("String:%s\n", str2[i]); printf("Value:%d\n", ival); } return 0; } /* 縺薙�蠕後↓髢「謨ー myatoi 繧剃ス懈�縺吶k */ /********************************************************************** myatoi 蠑墓焚譁�ュ怜�縺九i謨エ謨ー繧定ェュ縺ソ蜿悶j縲(nt蝙九↓縺励※霑斐☆髢「謨ー縲�1譯√b隱ュ繧√↑縺��エ蜷医�0縺瑚ソ斐k縲� 蜈磯�ュ縺ョ繧ケ繝壹�繧ケ縺ッ隱ュ縺ソ鬟帙�縺吶� 谺。縺ォ-隨ヲ蜿キ縺梧擂繧後�縲∬イ�縺ョ謨エ謨ー繧定ソ斐☆縲�+隨ヲ蜿キ縺ェ繧峨◎縺ョ縺セ縺セ隱ュ縺ソ蜿悶▲縺滓紛謨ー繧定ソ斐☆縲� 縺ゅ→縺ッ騾」邯壹☆繧区焚蟄励r隱ュ縺ソ縲∵焚蟄嶺サ・螟悶↓蜃コ莨壹∴縺ー邨ゆコ�� **********************************************************************/ int myatoi(char *str) { int i; int sgn=1, val=0; /* val縺ッ0縺ォ蛻晄悄蛹悶@縺ヲ縺翫¥ */ /* 蜈磯�ュ縺ョ繧ケ繝壹�繧ケ縺ッ繧ケ繧ュ繝�� */ for ( i=0; str[i]!='\0'; i++ ){ if ( str[i]!=' ' ) break; } /* +-隨ヲ蜿キ縺ョ繝√ぉ繝�け */ if ( str[i]=='-' ) { sgn = -1; /* 蠕後〒-1蛟阪〒縺阪k繧医≧縺ォ螟画焚縺ォ菫晄戟 */ i++; } else if ( str[i]=='+' ) { i++; /* +縺ョ蝣エ蜷医�菴輔b縺帙★�第枚蟄励せ繧ュ繝�� */ } /* 騾」邯壹☆繧区焚蟄励�隱ュ縺ソ蜿悶j */ while (1) { if ( str[i]>='0' && str[i]<='9' ) { val = val*10 + (int)(str[i] - '0'); /* 蟾ヲ縺ォ荳譯√★繧峨@縲∬ェュ縺ソ霎シ繧薙□謨ー蟄励r雜ウ縺� */ i++; } else break; /* 謨ー蟄嶺サ・螟厄シ医レ繝ォ譁�ュ励b�峨↑繧芽ェュ縺ソ霎シ縺ソ邨ゆコ� */ } return sgn*val; /* -隨ヲ蜿キ縺後≠縺」縺溷�エ蜷医�-1蛟阪@縺ヲ謌サ繧雁、縺ィ縺吶k */ }