学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

【求助】int

【求助】int

# include <stdio.h>
void main()
{
    int num;
    /* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */
    int indiv, ten, hundred, thousand;
    int ten_thousand, hundred_thousand, place;

    printf("请输入一个整数(0~999999):");
    scanf("%d", &num);

    /* 判断变量num的位数 */
    if(num > 99999)
        place = 6;
    else if(num > 9999)
        place = 5;
    else if(num > 999)
        place = 4;
    else if(num > 99)
        place = 3;
    else if(num > 9)
        place = 2;
    else
        place = 1;
    printf("place = %d\n", place);
    
    printf("每位数字为:");

    /* 求出num在各位上的值 */
    hundred_thousand = num/100000;
    ten_thousand = (num - hundred_thousand*100000)/10000;
    thousand = (num - hundred_thousand*100000 - ten_thousand*10000)/1000;
    hundred = (num - hundred_thousand*100000 - ten_thousand*10000
              - thousand*1000)/100;
    ten = (num - hundred_thousand*100000 - ten_thousand*10000
          - thousand*1000 - hundred*100)/10;
    indiv = num - hundred_thousand*100000 - ten_thousand*10000
            - thousand*1000 - hundred*100 - ten*10;

    /* 判断变量num的位数,并根据位数做出相应的输出 */
    switch(place)
    {
    case 1: printf("%d", indiv);
            printf("\n反序数字为:");
            printf("%d\n", indiv);
            break;
    case 2: printf("%d, %d", ten, indiv);
            printf("\n反序数字为:");
            printf("%d%d\n", indiv, ten);
            break;
    case 3: printf("%d, %d, %d", hundred, ten, indiv);
            printf("\n反序数字为:");
            printf("%d%d%d\n", indiv, ten, hundred);
            break;
    case 4: printf("%d, %d, %d, %d", thousand, hundred, ten, indiv);
            printf("\n反序数字为:");
            printf("%d%d%d%d\n", indiv, ten, hundred, thousand);
            break;
    case 5: printf("%d, %d, %d, %d, %d", ten_thousand, thousand,
                   hundred, ten, indiv);
            printf("\n反序数字为:");
            printf("%d%d%d%d%d\n", indiv, ten, hundred,
                    thousand, ten_thousand);
            break;
    case 6: printf("%d, %d, %d, %d, %d, %d", hundred_thousand,
                   ten_thousand, thousand, hundred, ten, indiv);
            printf("\n反序数字为:");
            printf("%d%d%d%d%d%d\n", indiv, ten, hundred, thousand,
                    ten_thousand, hundred_thousand);
            break;
    default: printf("Not find.\n");
             break;
    }
}

TOP

我们都知道 ,int 型变量的范围是 -32767 - 32767 , 在TC 2.0 下 ,当输入    45678 ,TC下运行有误。

        但是在VC++ 6.0  下, 可以输出   45678  及   87654  ,这是为什么?

       难道  int 在 VC++ 6.0 下的范围不是  -32767 - 32767  ?

TOP

恩~编译软件对范围的处理是不一样的~
在VC++ 6.0下默认的int 是无符号型的所以 0-65535
不知道解释的对不对

[ 本帖最后由 windk 于 2008-5-4 22:32 编辑 ]

TOP

-32768~32767吧在TC下

TOP

是这样的,在TC下,int类型是2个字节长度,也就是16bit,如果是有符号的话,最大范围就是-32768~32767了
在VC下,int类型是4个字节长度,也就是32bit,如果是有符号的话,范围是-2147483648~2147483647
从BFS(Breadth First Study)到DFS(Depth First Study)

严重鄙视一切把论坛当成作业生成器和人肉搜索引擎的人

TOP

你可以用sizeof运算符试一下看,在TC下,sizeof(int)=2 而在VC下sizeof(int)=4
从BFS(Breadth First Study)到DFS(Depth First Study)

严重鄙视一切把论坛当成作业生成器和人肉搜索引擎的人

TOP

还是极光有道理。不同的编译器会有不同的范围的。。。。
前不见古人,后不见来者。念天地之悠悠,独怆然而涕下。

TOP

#include<cstdio>
int main()
{
    char c[100];
    int i=0;
    int len;
    while((c[i++]=getchar())!=10);
    len=i;i=0;
    while(putchar(c[len-i++])&&i<=len);
    return 0;
}
学习需要安静。。海盗要重新来过。。

TOP

8楼的程序是干什么用的?

TOP

引用:
恩~编译软件对范围的处理是不一样的~
在VC++ 6.0下默认的int 是无符号型的所以 0-65535
不知道解释的对不对
这解释真强悍……佩服………………
专心编程………
飞燕算法初级群:3996098
我的Blog

TOP

发新话题