程序无法从数字中删除零。 [英] program not working for removing zeroes from a number.

查看:84
本文介绍了程序无法从数字中删除零。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<math.h>
int main(void){
    long n, number = 0;
    int x=0, counter = 0;
    printf("Enter a Positive Number.\n=>");
    scanf("%ld", &n);
    while(n != 0){
        x = n%10;
        if(x != 0){
            x = x * pow(10, counter);
            number = number + x;
            counter++;
        }
        n= n/10;

    }
    printf("The Number after removing all '0' from given number is : %ld", number);
}





假设我输入了一个输入6204040.然后我期待6244作为输出,但我得到了6243作为输出。

代码中有什么问题???



suppose I entered a enter 6204040. Then i'm expecting 6244 as a output, but i'm getting 6243 as output.
What is wrong in the code???

推荐答案

嗯 - 找出的最好方法是使用调试器。

这是一个工具,它允许你运行你的程序,看看它运行时到底发生了什么。

在线上放一个断点

Well - the best way to find out is to use a debugger.
This is a tool which allows you to run your program and look at exactly what is happening while it runs.
Put a breakpoint on the line
while(n != 0){

然后运行你的程序。当它到达线时,它将停止以允许您决定接下来会发生什么,并查看变量中的内容。

一次一行地执行您的程序,找出您期望的内容每次发生,并将其与发生的事情进行比较。如果它们不一样,那么为什么不呢?

如果是的话,继续前行,继续前进,直到出现问题为止。



当你发现差异时,问题应该是显而易见的!

这是一项技能 - 可以预见的称为调试 - 就像你通过使用所开发和改进的所有技能一样。当你使用像这样的小程序时,你会有很多容易学习的东西,而不是后来当你有​​50万行代码并且不知道问题出在哪里时! :笑:



试一试 - 这并不难,真的 - 你会学到一项非常有用的技能!

And run your program. When it reaches the line it will stop to allow you to decide what happens next, and to look at what is in the variables.
Step through your program a line at a time, working out what you expect to happen each time, and comparing it to what does happen. If they aren't the same, then why not?
If they are, move on to the next line and keep going until something looks wrong.

When you find a difference, it should be reasonably obvious what the problem is!
This is a skill - predictably called debugging - which like all skills you only develop and improve by using. And it's a lot easy to learn when you are working with a tiny program like this one than later when you have half a million lines of code and don't know where the problem is! :laugh:

Give it a try - it's not difficult, really - and you will learn a very useful skill!


这篇关于程序无法从数字中删除零。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆