
C语言库函数:atoi
实现了此功能。
int
atoi(const
char
*nptr)
函数说明:
参数nptr字符串,如果第一个非空格字符备笑存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符
\0)
字符时停止转换,返回整型数。
代码如下:
#include
<stdio.h>虚迟
#include
<string.h>
#include
<stdbool.h>
/**
*
功能:string转int
*
注意:
*
1、字符串是否为空;
*
2、忽略\t
或空格;
*
3、溢出判断;
*
4、差滚李输入字符串只有“+”或“-”号时;
*
5、扫描参数src字符串,跳过前面的空格字符,直到遇上数
字或正负符号才开始做转换,而再遇到非数字或字符串结束时
('\0')才结束转换,并将结果返回。
*/
int
convertionStringToInt(char
*src)
{
bool
flag=false
long
num=0
/*使用long类型,数据溢出情形*/
int
minus=0
if((NULL
==
src)||('\0'==
*src))
//判断如惨字符串指针是否为空
{
return
0
}
if(('\t'
==
*src)||('
'
==
*src))
//忽略tab字符和空格字符
{
src
++
}
if('-'
==
*src)
//判断是否是‘+’
或
‘-’符号
{
flag=true
src++
}
else
if('+'
==
*src)
{
src++
}
minus=flag?-1:1
while('\0'
!=
*src)
{
if(('0'
<=
*src)&&('9'
>=
*src))
{
num=
num*10+minus*((*src)-'0')
if((!flag
&&
num
>
0x7FFFFFFF)
||
(flag
&&
num
<
(signed
int)0x80000000))
//判断转换后的整型数是否溢出,注意正负数均要判断
{
num
=
0
break
}
src++
}
else
{
break
//遇到非数字字符时结束转换
}
}
return
num
}
int
main()
{
char
data[8][15]={"4673","15234234234","-3489","+9776","ab67","123bd","+","-"}
int
i=0
int
val=0
for(i=0i<8i++)
{
printf("Before
data[%ld]=%s
",i,data[i])
val=convertionStringToInt(data[i])
printf("After
Convert=%d\n",val)
}
return
0
}
微信小返启程序数组转字符串
var arr = [1,2,3,4,5,6]var str = arr.join(',')
console.log(str) // 1,2,3,4,5,6
您好。漏并如希望蔽纳可以帮助到你!
合并版本的代码:#include <stdlib.h>
#include <stdio.h>
#include <math.h>
static char cha = 'o'
static char chb = 'x'
static char chc = 'U'
static char chd = 'D'
static char chx = 'o'
static char chy = 'x'
int isSSZ(int x, int y) // y在x的顺时针方向,距离桐敏限制1到4
{
switch(x)
{
case '0': case '1': case '2':
case '3': case '4': case '5': return (y>=x&&y<=x+4)?1:0
case '6': return (y=='7'||y=='8'||y=='9'||y=='0')?1:0
case '7': return (y=='8'||y=='9'||y=='0'||y=='1')?1:0
case '8': return (y=='9'||y=='0'||y=='1'||y=='2')?1:0
case '9': return (y=='0'||y=='1'||y=='2'||y=='3')?1:0
default : return 0
}
}
void main(int argc, char* argv[])
{
if(argc<3)
{
printf("Error.\nno input file or output file.\n")
system("pause")
exit(-2)
}
FILE *fpi,*fpo
char num[50]
char bin[][5] = { "000", "000", "000", "000", "000" }
char loop[][5] = { "000", "000", "000", "000", "000" }
char tmp[4] = { '\郑轮敬0', '\0', '\0', '\0' }
fpi=fopen(argv[1],"rt")
fpo=fopen(argv[2],"wt")
int count = 0, ixx = 0
if(!fpi||!fpo)
{
printf("Error open input file or output file.")
system("pause")
exit(-1)
}
while(!feof(fpi))
{
if( fgets( num, 50, fpi ) != NULL)
{
count++
if(count%30==1) fprintf(fpo,"num,bin01,bin02,bin03,bin04,bin05,loop1,loop2,loop3,loop4,loop5\n")
num[3]=loop[0][3]=loop[1][3]=loop[2][3]=loop[3][3]=loop[4][3]='\0'
num[3]=bin[0][3]=bin[1][3]=bin[2][3]=bin[3][3]=bin[4][3]='\0'
bin[0][0]=(num[0]=='0')?chx:((num[0]=='7')?chy:((num[0]=='1'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'))
bin[0][1]=(num[1]=='0')?chx:((num[1]=='7')?chy:((num[1]=='1'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'))
bin[0][2]=(num[2]=='0')?chx:((num[2]=='7')?chy:((num[2]=='1'喊慎||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'))
bin[1][0]=(num[0]=='1')?chx:((num[0]=='8')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='9')?'1':'2'))
bin[1][1]=(num[1]=='1')?chx:((num[1]=='8')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='9')?'1':'2'))
bin[1][2]=(num[2]=='1')?chx:((num[2]=='8')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='9')?'1':'2'))
bin[2][0]=(num[0]=='2')?chx:((num[0]=='9')?chy:((num[0]=='0'||num[0]=='3'||num[0]=='5'||num[0]=='8')?'1':'2'))
bin[2][1]=(num[1]=='2')?chx:((num[1]=='9')?chy:((num[1]=='0'||num[1]=='3'||num[1]=='5'||num[1]=='8')?'1':'2'))
bin[2][2]=(num[2]=='2')?chx:((num[2]=='9')?chy:((num[2]=='0'||num[2]=='3'||num[2]=='5'||num[2]=='8')?'1':'2'))
bin[3][0]=(num[0]=='3')?chx:((num[0]=='4')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='5'||num[0]=='8')?'1':'2'))
bin[3][1]=(num[1]=='3')?chx:((num[1]=='4')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='5'||num[1]=='8')?'1':'2'))
bin[3][2]=(num[2]=='3')?chx:((num[2]=='4')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='5'||num[2]=='8')?'1':'2'))
bin[4][0]=(num[0]=='5')?chx:((num[0]=='6')?chy:((num[0]=='1'||num[0]=='2'||num[0]=='3'||num[0]=='8')?'1':'2'))
bin[4][1]=(num[1]=='5')?chx:((num[1]=='6')?chy:((num[1]=='1'||num[1]=='2'||num[1]=='3'||num[1]=='8')?'1':'2'))
bin[4][2]=(num[2]=='5')?chx:((num[2]=='6')?chy:((num[2]=='1'||num[2]=='2'||num[2]=='3'||num[2]=='8')?'1':'2'))
fprintf( fpo, "%s", num )
fprintf( fpo, "," )
fprintf( fpo, "%s,%s,%s,%s,%s", bin[0], bin[1], bin[2], bin[3], bin[4] )
if(count==1)
{
fprintf( fpo, "\n" )
tmp[0]=num[0]tmp[1]=num[1]tmp[2]=num[2]
continue
}
ixx=(num[0]-tmp[0])%10ixx=(ixx>=0)?ixx:(10+ixx)
loop[0][0]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[0],tmp[0])?chc:chd))
loop[1][0]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[0],tmp[0])?chc:chd))
loop[2][0]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[0],tmp[0])?chc:chd))
loop[3][0]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[0],tmp[0])?chc:chd))
loop[4][0]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[0],tmp[0])?chc:chd))
ixx=(num[1]-tmp[1])%10ixx=(ixx>=0)?ixx:(10+ixx)
loop[0][1]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[1],tmp[1])?chc:chd))
loop[1][1]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[1],tmp[1])?chc:chd))
loop[2][1]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[1],tmp[1])?chc:chd))
loop[3][1]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[1],tmp[1])?chc:chd))
loop[4][1]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[1],tmp[1])?chc:chd))
ixx=(num[2]-tmp[2])%10ixx=(ixx>=0)?ixx:(10+ixx)
loop[0][2]=(ixx==0)?cha:((ixx==5)?chb:(isSSZ(num[2],tmp[2])?chc:chd))
loop[1][2]=(ixx==1)?cha:((ixx==6)?chb:(isSSZ(num[2],tmp[2])?chc:chd))
loop[2][2]=(ixx==2)?cha:((ixx==7)?chb:(isSSZ(num[2],tmp[2])?chc:chd))
loop[3][2]=(ixx==3)?cha:((ixx==8)?chb:(isSSZ(num[2],tmp[2])?chc:chd))
loop[4][2]=(ixx==4)?cha:((ixx==9)?chb:(isSSZ(num[2],tmp[2])?chc:chd))
fprintf( fpo, "," )
fprintf( fpo, "%s,%s,%s,%s,%s", loop[0], loop[1], loop[2], loop[3], loop[4] )
fprintf( fpo, "\n" )
tmp[0]=num[0]tmp[1]=num[1]tmp[2]=num[2]
} else
printf( "done. %d line(s) processed.\n", count)
}
fclose(fpi)
fclose(fpo)
system("pause")
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)