程序已写好,但找了很久都不知到问题在哪里

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);
}

...全文
78 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
pydqh 2003-10-19
  • 打赏
  • 举报
回复
这是一个输入一个单词,输出相应数字的程序。例如,输入one hundred,输出100。

这个程序用UNIX下的C语言写。我的程序分成两个部份。函数smallword()用来处理百位以下的数,这个函数可以运行。用这个程序处理百位以上的数就有问题。这个程序可以通过编译,有时候可以运行,有时候不可以。会不会是因为我用了数组指针的关系我想了很久都找不到原因,请指教。程序内容在下面。


我的算法是:把整个单词用strtok()把它分开成一个一个独立的单词,然后存在指针数组string里面,例如:输如one million one thusand one hundred,运行strtok()后,string[1]=one,string[2]=million,string[3]=one,string[4]=thousand,string[5]=one. 然后按四种情况处理:1.有“million” 和“thousand”;2. 有“million”, 没有“thouand”;3. 没有“thoudsand” ,有“thousand”;4. 没有“million”, 没有“thousand”.
最后调用写好的处理三位数的函数smallword()去处理每一部分:million前的部分,million和thousand之间的部份,百位以下的部分。
pydqh 2003-10-17
  • 打赏
  • 举报
回复
This program is written by myself.This is a program running in the Unix environment.
147272947 2003-10-15
  • 打赏
  • 举报
回复
#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 (strcmp(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 (strcmp(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 (strcmp(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 (strcmp(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);

}

return 0;
}



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 (strcmp(p1,"hundred")==0)
{sum1=digit1*100;
break;
}
for( i=0;i<35;i++)
{ if (strcmp(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 (strcmp(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 (strcmp(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);
}
147272947 2003-10-15
  • 打赏
  • 举报
回复
调试结果同上。
bbgbianbaogui 2003-10-15
  • 打赏
  • 举报
回复
还有 将main前加void
将strcasecmp全部改成strcmp
试一试能够编绎了。
bbgbianbaogui 2003-10-15
  • 打赏
  • 举报
回复
1 printf("word1 is:%s",word);
2 printf("word1 is:%s",word);将此行下一行删除,在回车
3 if (strcasecmp(string[j],"thousand")==0) strcasecmp没有定义
tutu604 2003-10-15
  • 打赏
  • 举报
回复
不懂
sevencat 2003-10-15
  • 打赏
  • 举报
回复
不过,这是你自己写的吗?还是有些是从其他地方贴过来的?
sevencat 2003-10-15
  • 打赏
  • 举报
回复
大家不要笑,
strcasecmp、strcmp
这类的东西在好多代码里面有,这是有原因的,因为有些系统没有实现strcmp这个函数。大家用linux下的autoconf就知道了。
RichJ 2003-10-14
  • 打赏
  • 举报
回复
look,兄弟,去查C语言教程吧!

Compare strings.

int strcmp( const char *string1, const char *string2 );

Return Value

The return value for each of these functions indicates the lexicographic relation of string1 to string2.

Parameters

string1, string2

Null-terminated strings to compare

Remarks

The strcmp function compares string1 and string2 lexicographically and returns a value indicating their relationship. wcscmp and _mbscmp are wide-character and multibyte-character versions of strcmp. The arguments and return value of wcscmp are wide-character strings; those of _mbscmp are multibyte-character strings. _mbscmp recognizes multibyte-character sequences according to the current multibyte code page and returns _NLSCMPERROR on an error. (For more information, see Code Pages.) These three functions behave identically otherwise.




qufan 2003-10-14
  • 打赏
  • 举报
回复
我也拷下去看看,说实话太长了,我按了13个pagedown键来看到结尾那
yndfcd 2003-10-14
  • 打赏
  • 举报
回复
多写几次就不会再错了。
pjh 2003-10-14
  • 打赏
  • 举报
回复
太长了,也太多 了,还没有看就头痈了,我先拷下去
lxy2651 2003-10-14
  • 打赏
  • 举报
回复
to lemon520(喷血) :佩服。
langzi8818 2003-10-13
  • 打赏
  • 举报
回复
dao
lemon520 2003-10-13
  • 打赏
  • 举报
回复
#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"
};

void 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,ch;
unsigned k; //改成这样就没有警告了

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 (strcmp(string[j],"million")==0) //是strcmp吧?
{
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 (strcmp(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 (strcmp(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 (strcmp(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 (strcmp(p1,"hundred")==0) //我不知道strcasecmp()函数是什么.晕!请指教
{sum1=digit1*100;
break;
}
for( i=0;i<35;i++)
{ if (strcmp(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 (strcmp(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 (strcmp(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);
}
lemon520 2003-10-13
  • 打赏
  • 举报
回复
我不想看............:(
yaway 2003-10-13
  • 打赏
  • 举报
回复
编译器会告诉你错误的地方,仔细找呀!!!!!!!!!
liao2001 2003-10-13
  • 打赏
  • 举报
回复
你把大家当读码器呀?
我发现好多人怎么老喜欢摆出一大堆的代码而不给出自己的算法,浪费别人的时间,谁有那么多时间帮你看?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧