程序已写好,但找了很久都不知到问题在哪里
pydqh 2003-10-13 04:56:18 这是一个输入单词输出数字的程序,百位以下smallword()可以
但以上就不行。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int smallword(char s[]);
const char *num[35] = {
"zero","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","forteen","fifteen",
"sixteen","seventeen","eighteen","nineteen","twenty","thirty",
"fourty","fifty","sixty","seventy","eighty","ninety","hundred",
"thousand","million"
};
main(){
char word[500]="",word2[500]="";
printf("please input a word:");
while(1){
fgets(word,500,stdin);
char *token;
char line1[500],line2[500],line3[500],line[500];
char *string[500];
string[0]="";
long int total1=0,total2=0,total3=0,total;
int c,j,n,k,ch;
word[strlen(word)-1]='\0';
printf("word1 is:%s",word):
for(k=0;k<strlen(word);k++)
{
ch = tolower(word[k]);
word[k] = ch;
}
printf("word is:%s",word);
strcpy(word2,word);
token = strtok(word," ");
c=-1;
while(token)
{
c++;
string[c]=token;
token=strtok(NULL," ");
}
if(( strstr(word2,"million")!='\0')&&(strstr(word2,"thousand")!='\0'))
{
for(j=0;j<c+1;j++)
{ if (strcasecmp(string[j],"million")==0)
{
n=j;
line1[strlen(line1)-1]='\0';
total1=total1+smallword(line1)*1000000;
break;
}
strcat(line1,string[j]);
strcat(line1," ");
}
for(j=n+1;j<c+1;j++)
{
if (strcasecmp(string[j],"thousand")==0)
{
n=j;
line2[strlen(line2)-1]='\0';
total2=total2+smallword(line2)*1000;
break;
}
strcat(line2,string[j]);
strcat(line2," ");
}
for(j=n+1;j<c+1;j++)
{
strcat(line3,string[j]);
strcat(line3," ");
}
line3[strlen(line3)-1]='\0';
total3=total3+smallword(line3);
total=total1+total2+total3;
}
if((strstr(word2,"million")!='\0')&&(strstr(word2,"thousand")=='\0'))
{
for(j=0;j<c+1;j++)
{
if (strcasecmp(string[j],"million")==0)
{
n=j;
line1[strlen(line1)-1]='\0';
total1=total1+smallword(line1)*1000000;
break;
}
strcat(line1,string[j]);
strcat(line1," ");
}
for(j=n+1;j<c+1;j++)
{
strcat(line2,string[j]);
strcat(line2," ");
}
line2[strlen(line2)-1]='\0';
total2=total2+smallword(line2);
total=total1+total2;
}
if((strstr(word2,"million")=='\0')&&(strstr(word2,"thousand")!='\0'))
{
for(j=0;j<c+1;j++)
{
if (strcasecmp(string[j],"thousand")==0)
{
n=j;
line1[strlen(line1)-1]='\0';
total1=total1+smallword(line1)*1000;
break;
}
strcat(line1,string[j]);
strcat(line1," ");
}
for(j=n+1;j<c+1;j++)
{
strcat(line2,string[j]);
strcat(line2," ");
}
line2[strlen(line2)-1]='\0';
total2=total2+smallword(line2);
total=total1+total2;
}
if((strstr(word2,"million")=='\0')&&(strstr(word2,"thousand")=='\0'))
{
for(j=0;j<c+1;j++)
{
strcat(line,string[j]);
strcat(line," ");
}
line[strlen(line)-1]='\0';
total=smallword(line);
}
printf("%ld\n",total);
}
}
int smallword(char s[])
{
char *p1,*p2,*p;
int digit,digit1,digit2,sum1=0,sum2=0,sum=0,i;
if(strstr(s,"hundred")!='\0')
{
p1= strtok(s," ");
while(p1)
{
if (strcasecmp(p1,"hundred")==0)
{sum1=digit1*100;
break;
}
for( i=0;i<35;i++)
{ if (strcasecmp(p1,num[i])==0)
{ if(i>20&&i<28)
digit1 = 10*i-180;
if(i>0&&i<=20)
digit1 = i;
break;
}
}
p1=strtok(NULL," ");
}
p2 =strtok(NULL," ");
while(p2)
{
for(i=0;i<35;i++)
{ if (strcasecmp(p2,num[i])==0)
{ if(i>20&&i<28)
digit2 = 10*i-180;
if(i>=0&&i<=20)
digit2 = i;
break;
}
}
sum2=sum2+digit2;
p2=strtok(NULL," ");
}
sum=sum1+sum2;
}
else
{ p =strtok(s," ");
while(p)
{
for(i=0;i<35;i++)
{
if (strcasecmp(p,num[i])==0)
{ if((i>20)&&(i<28)){
digit = 10*i-180;
}
if((i>=0)&&(i<=20)){
digit = i;
}
break;
}
}
sum=sum+digit;
p=strtok(NULL," ");
}
}
return(sum);
}