变量得到错误的值?

时间:2015-10-20 13:22:15

标签: c

大家好我需要制作一个输入数字n的程序。  没有具体说明n有多大,所以没有限制。

输出应该是:example n = 123456

  1. 123456 - 12345 + 1234 - 123 + 12 - 1 = 112233

  2. 1 + 12 + 123 + 1234 + 12345 + 123456 = 137171

  3. 我完成了第一部分,第二部分也是如此,但是当我打印第二个等式时,它打印的值不是我计算的值。

    这里是代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
        int a=0;
        printf("Enter an integer >=0: ");
        scanf("%ld",&num);
        c=num;
        sum=num;
        printf("%ld ",num);
        while(num>0)
        {
            if(a==0)
            {
                num/=10;
                sum-=num;
                printf(" - %ld",num);
                a=1;
            }
            else if (a==1)
            {
                num/=10;
                sum+=num;
                printf(" + %ld",num);
                a=0;
            }
        }
        printf("= %ld\n",sum);
        d=c;
        printf("d: %ld\n ",d);
        while(d>10)
        {
            b++;
            d/=10;
            printf("%ld\n",d);
        }
        printf("b:%lu\n",b);
        printf("c: %lu\n",c);
    
        for(b;b>0;b--)
        {
            base=10^b;
            num2=c/base;
    
            if (b==1)
            {
                printf("%ld",num2);
            }
            else
            {
                printf("%ld + ",num2);
            }
            sum2+=num2;
        }
        printf("= %ld",sum2);
        return 0;
    } 
    

    我知道我有额外的价值观,而且我打印的其他内容并不是我所说的,我只是检查哪些值不正确。我认为由于我打印它的方式或因为变量类型而导致我num2=c/base;的值不正确,我尝试使用数字5005005,num2第一次应该是5,我得到417k ish。任何帮助表示赞赏。

    编辑:我更改了电源错误,但现在程序崩溃了。

    新代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
        unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base;
        int a=0;
        printf("Enter an integer >=0: ");
        scanf("%ld",&num);
        c=num;
        sum=num;
        printf("%ld ",num);
        while(num>0){
    
                if(a==0){
                num/=10;
                sum-=num;
                printf(" - %ld",num);
                a=1;
                } else if (a==1){
                num/=10;
                sum+=num;
                printf(" + %ld",num);
                a=0;
                }
    
        }
        printf("= %ld\n",sum);
        d=c;
        printf("d: %ld\n ",d);
        while(d>10){
            b++;
            d/=10;
            printf("%ld\n",d);
        }
        printf("b:%lu\n",b);
        printf("c: %lu\n",c);
    
    
    
        for(b;b>=0;b--){
            base= powl(10,b);
            num2=c/base;
    
            if (b==0){
                printf("%ld",num2);
            }else{
            printf("%ld + ",num2);
            }
            sum2+=num2;
            printf("%ld",sum2);
        }
        printf("= %ld",sum2);
        return 0;
    }
    

    edit2:修复它,我仍然不知道为什么它在使用&gt; = 0时崩溃

    这里有修复:

    for(b;b>0;b--){
                base= powl(10,b);
                num2=c/base;
    
                if (b==1){
                    printf("%ld + ",num2);
                    sum2+=num2;
                    num2=c;
                    printf("%ld",num2);
                }else{
                printf("%ld + ",num2);
                }
                sum2+=num2;
                printf("%ld",sum2);
            }
    

1 个答案:

答案 0 :(得分:0)

  • 您无法通过^运算符计算功率:它是xor运算符。
  • 当输入的前两位数为d>10时,
  • 10会导致错误的结果。
  • Inlenmented b因为缺少最后一个号码。

固定代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0;
    int a=0;
    printf("Enter an integer >=0: ");
    scanf("%ld",&num);
    c=num;
    sum=num;
    printf("%ld ",num);
    while(num>0){

        if(a==0){
            num/=10;
            sum-=num;
            printf(" - %ld",num);
            a=1;
        } else if (a==1){
            num/=10;
            sum+=num;
            printf(" + %ld",num);
            a=0;
        }

    }
    printf("= %ld\n",sum);
    d=c;
    printf("d: %ld\n ",d);
    while(d>=10){
        b++;
        d/=10;
        printf("%ld\n",d);
    }
    printf("b:%lu\n",b);
    printf("c: %lu\n",c);


    for(num2=c,b++;b>0;b--){

        if (b==1){
            printf("%ld",num2);
        }else{
            printf("%ld + ",num2);
        }
        sum2+=num2;
        num2/=10;

    }
    printf("= %ld",sum2);
    return 0;
}

<强>更新
要获得1 + 12 + 123 + 1234 + 12345 + 123456, 您可以使用base来计算数字。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned long int num,sum,num2=0,sum2=0,b=0,c=0,d=0,base=1;
    int a=0;
    printf("Enter an integer >=0: ");
    scanf("%ld",&num);
    c=num;
    sum=num;
    printf("%ld ",num);
    while(num>0){

        if(a==0){
            num/=10;
            sum-=num;
            printf(" - %ld",num);
            a=1;
        } else if (a==1){
            num/=10;
            sum+=num;
            printf(" + %ld",num);
            a=0;
        }

    }
    printf("= %ld\n",sum);
    d=c;
    printf("d: %ld\n ",d);
    while(d>=10){
        b++;
        d/=10;
        base*=10;
        printf("%ld\n",d);
    }
    printf("b:%lu\n",b);
    printf("c: %lu\n",c);


    for(b++;b>0;b--){

        num2=c/base;
        if (b==1){
            printf("%ld",num2);
        }else{
            printf("%ld + ",num2);
        }
        sum2+=num2;
        base/=10;

    }
    printf("= %ld",sum2);
    return 0;
}