c语言字符串数组的字符无效,C语言数组,字符串的问题

题目描述:

bVN3xG?w=1001&h=556

运行结果:

bVN3xQ?w=317&h=153

为什么下面的代码提交上去是Wrong Answer?望指教!

#include

#include

#include

typedef struct data

{

char first[4];

char second[4];

}data;

char atoDigit( char[] );

int ctoD( char X);

int strtoNum( char[] );

int main() {

char str[10];

int number;

while( gets(str) )

{

number = strtoNum( str );

if( number == 0 )

break;

printf("%d\n",number);

}

return 0;

}

char atoDigit( char *str)

{

char digital;

if( strcmp( str, "zero") == 0 )

digital = '0';

if( strcmp( str, "one") == 0 )

digital = '1';

if( strcmp( str, "two") == 0 )

digital = '2';

if( strcmp( str, "three") == 0 )

digital = '3';

if( strcmp( str, "four") == 0 )

digital = '4';

if( strcmp( str, "five") == 0 )

digital = '5';

if( strcmp( str, "six") == 0 )

digital = '6';

if( strcmp( str, "seven") == 0 )

digital = '7';

if( strcmp( str, "eight") == 0 )

digital = '8';

if( strcmp( str, "nine") == 0 )

digital = '9';

return digital;

}

int strtoNum( char *str )

{

data ret;

int i, firstNum, secondNum;

char tmpStr[6], *ptrToDigita;

i = 0;

ptrToDigita = ret.first;

while( *str != '\0')

{

if( *str == '+' )

{

memset( tmpStr,0,sizeof(tmpStr) );

i = 0;

*ptrToDigita = '\0';

ptrToDigita = ret.second;

str += 2;

}

if( *str == '=')

{

*ptrToDigita = '\0';

break;

}

if( *str != ' ')

{

tmpStr[i] = *str;

i++;

}

else

{

if( strlen( tmpStr ) > 0 )

{

tmpStr[i] = '\0';

*ptrToDigita = atoDigit( tmpStr );

ptrToDigita++;

memset( tmpStr,0,sizeof(tmpStr) );

i = 0;

}

}

str++;

}

firstNum = secondNum = 0;

for(i = 0; i < strlen(ret.first); i++)

{

firstNum += ctoD(ret.first[i]) * pow(10,strlen(ret.first)-1-i);

}

for(i = 0; i < strlen(ret.second); i++)

{

secondNum += ctoD(ret.second[i]) * pow(10,strlen(ret.second)-1-i);

}

return firstNum + secondNum;

}

int ctoD( char X )

{

if( X >= '0' && X <= '9')

return X - 48;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值