| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 655 人关注过本帖
标题:一个不超过10位的数字,要显示每个数字对应英文,程序不知道哪里出错,10位 ...
只看楼主 加入收藏
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:5 
一个不超过10位的数字,要显示每个数字对应英文,程序不知道哪里出错,10位数字老错
#include<stdio.h>
#include<math.h>

int main(void)
{
     int a,x,number,y,z,j;
     float b;
     double m,n;
     long int i;
     printf("Please enter a number but no larger than 10 digits:");
     scanf_s("%d",&a);
     number=0;
     n=10;

     for (i=1;i<=a;i=i*10)//count number of digitals
     {
         number=number+1;
     }
     z=number;

     for (j=z;j>0;j--)
     {   
         m=j-1;
         x=pow(n,m);
         b=a/x;
         y=(int)b;
         switch (y)
     {
        case 0: // if y=0
            printf("zero"); // print zero
            break; // terminate
        case 1: // if y=1
            printf("one"); // print one
            break; // terminate
        case 2: // if y=2
            printf("two"); // print two
            break; // terminate
        case 3: // if y=3
            printf("three"); // print three
            break; // terminate
        case 4: // if y=4
            printf("four"); // print four
            break; // terminate
        case 5: // if y=5
            printf("five"); // print five
            break; // terminate
        case 6: // if y=6
            printf("six"); // print six
            break; // terminate
        case 7: // if y=7
            printf("seven"); // print seven
            break; // terminate
        case 8: // if y=8
            printf("eight"); // print eight
            break; // terminate
        case 9: // if y=9
            printf("nine"); // print nine
            break; // terminate
         }
            a=a-y*x;
            if (j>1)
                printf("-");
            else
                printf(" ");
            
     }
     printf("\n");
     return 0;
}




十位数字一下都可以就是不知道为什么每次到十位数字就出错。
找不到问题。。。。
搜索更多相关主题的帖子: include double number count 英文 
2014-02-19 08:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:18 
“不超过10位的数字”
------ 如果你上过小学,应该能推导出这个数最大为 9999999999
如果你上过初中,应该能计算出9999999999至少需要34bits才能表示,公式为:ln(9999999999)/ln(2)=33.22=34
如果你学过一点点C或C++,应该知道C/C++没有强制规定int的表达范围,能表达至少34bits整型的类型有:uint64_t uint_least64_t uint_fast64_t 等等


2014-02-19 08:52
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
收藏
得分:0 
回复 2楼 rjsp
前两个没问题,第三句话我不是很懂。我刚接触c不到一个月。是说int不能表示这个数字吗? 所以用long int?
2014-02-19 08:58
yuchao130
Rank: 2
等 级:论坛游民
帖 子:20
专家分:32
注 册:2014-2-19
收藏
得分:0 
回复 2楼 rjsp
已解决,谢谢
2014-02-19 09:20
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:2 
溢出

DO IT YOURSELF !
2014-02-19 09:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下是引用yuchao130在2014-2-19 08:58:37的发言:

前两个没问题,第三句话我不是很懂。我刚接触c不到一个月。是说int不能表示这个数字吗? 所以用long int?
C/C++也没有强制规定long int必须大于32bits呀

程序代码:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main()
{
    uint64_t n;
    if( 1!=scanf("%"SCNu64,&n) || n>9999999999 )
        return 1;

    int ds[10], ds_len=0;
    for( uint64_t t=n; t!=0; t/=10 )
        ds[ds_len++] = t%10;
    if( ds_len == 0 )
        ds[ds_len++] = 0;

    static const char* en_zh[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    for( int i=ds_len; i!=0; --i )
        printf( "%s%c", en_zh[ds[i-1]], "\n-"[i!=1] );

    return 0;
}

2014-02-19 10:02
快速回复:一个不超过10位的数字,要显示每个数字对应英文,程序不知道哪里出错, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015236 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved