对为什么代码运行不正确感到困惑

时间:2016-06-22 12:03:00

标签: c floating-point rounding infinite-loop

**我做了一些改动,我想知道你是否可以帮我找出它没有运行的原因。当我运行它时,文本被打印但输入永远不会停止运行。 ** 该程序旨在复制(?)贪心算法。它首先询问用户欠多少钱,然后提供可以进行更改的最小硬币数。

我非常感谢您对我的代码提供的任何帮助。

#include <stdio.h>
#include <math.h>

int main(void)

{
    //declared variables
    float change;
    int num = 0;

    // prompts for amount owed

    printf("Please provide the amount owed: ");
    change = GetFloat();
    while (change != 0)

        change = change * 100;
    int owed = round(change);

    {
        // if statements for calculating number of coins required
        if (owed >= 25)
        {
            owed = owed - 25;
            num ++;
        }
        if (owed >= 10 && owed < 25)
        {
            owed = owed - 10;
            num ++;
        }
        if (owed >= 5 && owed < 10)
        {
            owed = owed - 5;
            num ++;
        }
        if (owed >= 1 && owed < 5)
        {
            owed = owed - 1;
            num ++;
        }
    }
    printf("%d\n", num);
}

当我改变while语句并注释掉'change = change * 100;'时 它开始起作用了。但是,这样做不会执行round函数。

  change = GetFloat() * 100;
    while (change != 0)

    // change = change * 1000;
    // int owed = round(change);

1 个答案:

答案 0 :(得分:1)

据我所知,部分

     android.view.InflateException: Binary XML file line #40: Binary XML file line #40: Error inflating class <unknown>
            at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at com.spintum.preexam.EntryPage1.onCreateView(EntryPage1.java:19)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:603)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:645)
            at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at com.spintum.preexam.EntryPage1.onCreateView(EntryPage1.java:19)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
                at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:603)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at android.view.LayoutInflater.createView(LayoutInflater.java:619)
            at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at com.spintum.preexam.EntryPage1.onCreateView(EntryPage1.java:19)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:603)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 33249648 byte allocation with 4194304 free bytes and 20MB until OOM
            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
            at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
            at android.content.res.Resources.loadDrawable(Resources.java:2540)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:870)


 at android.widget.ImageView.<init>(ImageView.java:152)
        at android.widget.ImageView.<init>(ImageView.java:140)
        at android.widget.ImageView.<init>(ImageView.java:136)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at android.view.LayoutInflater.createView(LayoutInflater.java:619)
            at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at com.spintum.preexam.EntryPage1.onCreateView(EntryPage1.java:19)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
            at android.view.Choreographer.doCallbacks(Choreographer.java:670)
            at android.view.Choreographer.doFrame(Choreographer.java:603)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)nter code here

while (change != 0) change = change * 100; 非零时启动的无限循环。根据平台,乘法最终会达到change或正无穷大,从而导致循环永远持续。