【问题标题】:How do I count the number of times a variable's value has been used?如何计算变量值被使用的次数?
【发布时间】:2020-11-04 20:58:08
【问题描述】:

对 C 来说非常新。我有一个问题,我需要保持几个变量的使用总数,这些变量的值是货币面额,以便获得它们的最终总数,直到输入值是零。例如,给定 2.45 美元,10 美分使用了多少次,5 美分使用了多少次,直到值为 0?

我想用一个带有计数器的 for 循环,但我只增加一个变量的值,并没有实际计算它被使用的次数,而计数器根本没有被使用。

int count = 0;

if (cents > quarter)
 {
    for (int i = quarter; i < 1; i++)
    {
        new_val = cents/quarter;
        count++;
        printf("count %i\n", count);
    }
  }

所有变量都已声明和/或初始化。

我知道算法完全不正确;我不知道如何纠正它。一些见解将不胜感激。

【问题讨论】:

    标签: c counter


    【解决方案1】:

    只需使用除法得到每个硬币的数量。

    然后将它们相加得到总硬币数。

    const quarter = 25, dime = 10, nickel = 5;
    int quarters, dimes, nickels, pennies, count;
    
    int cents = 245;
    
    quarters = cents / quarter; // how many quarters are needed
    cents = cents % quarter; // the remainder after using all the quarters
    
    dimes = cents / dime; // how many dimes are needed
    cents = cents % dime; // the remainder after using all the dimes
    
    nickels = cents / nickel; // how many nickels are needed
    cents = cents % nickel; // the remainder after using all the nickels
    
    pennies = cents; // pennies are everything left over
    
    count = quarters + dimes + nickels + pennies;
    

    【讨论】:

    • 谢谢两位的建议。我曾想过两种实现算法的方法,但对每一种都还不够。的确,一个好的开始。一个令人困惑的问题是如何计算实际使用的硬币数量。所以,2.45 美元 =245¢ = 9 x25¢ + 2 x 10¢ = 11 个硬币;计数器是真正的问题。
    • count = quarters + dimes + nickels + pennies
    【解决方案2】:

    这是总纲:

    • 您需要一种方法来跟踪硬币、硬币、镍币和便士的数量。最简单的方法是为每个变量创建单独的变量,初始化为0:
      int quarters=0, dimes=0, nickels=0, pennies=0;
      你也可以使用数组:
      int coins[4] = {0};
      并且只需跟踪存储在哪个索引中的面额(即,四分之一存储在索引 0,一角硬币存储在索引 1 等)。
    • 对于每个面额,从输入中减去该金额,直到输入值小于该面额:
      /**
       * Assume input value is in pennies, so $2.45 would be represented as 245
       */
      while ( input > 0 && input >= 25 )
      {
        input -= 25;
        quarters++; // or coins[0]++ if you're using the array method.
      }
      你会为每个面额重复这一点。您可能会更加棘手,并使用另一个数组作为面额值的查找表,这样您就可以在一个循环中完成所有操作,但这应该能让您继续前进。

    编辑

    或者您可以采用 Barmar 的方法并完全避免循环。

    【讨论】:

    • 请参阅 Barmar。显然,我不能在评论中添加两个名字。可以分享投票吗?你们都应得的。
    【解决方案3】:
    struct intVarCounter
    {
        int var;
        int gets;
        int sets;
        int adds;
        // you can have more or remove some,
        // but this is it for this example.
    };
    void init(intVarCounter *ivc, int value)
    {
        *ivc.var=value;
        *ivc.gets=0;
        *ivc.sets=0;
        *ivc.adds=0;
        // remember to remove or add to these
        // if you change the struct's properties
    }
    int get(intVarCounter *ivc)
    {
        *ivc.gets++;
        return *ivc.var;
    }
    int set(intVarCounter *ivc, int value)
    {
        *ivc.set++;
        *ivc.var=value;
        return *ivc.var;
    }
    int add(intVarCounter *ivc, int addend)
    {
        *ivc.adds++;
        *ivc.var+=addend;
        return *ivc.var;
    }
    int main()
    {
        intVarCounter *ivc;
        init(ivc,0);
        set(ivc,10);
        printf("%d",*ivc.sets);
        add(ivc,10);
        printf("%d %d", get(ivc), *ivc.gets);
        return add(ivc,-20);
    }
    

    这创建了一个简单的结构,该结构基本上包装了变量,以便在您获取、设置或添加变量时进行跟踪。如果你想改变它,很简单;只需添加/删除该属性并添加/删除它的关联功能。

    虽然这有问题。您可以直接更改ivc.var,有效绕过计数器。您还必须一直使用init 函数,否则您会得到看起来很奇怪的变量值。您还可以更改gets、sets 等的数量。这可以通过C++ 修复:

    class intVarCounter
    {
        int var; // automatically private to prevent direct change
        int gets;
        int sets;
        int adds;
        // ... you can add/remove these
        public:
            intVarCounter(int value=0)
            {
                int var=value;
                int gets=0;
                int sets=0;
                int adds=0;
            }
            int get()
            {
                gets++;  // you can remove this line to prevent gets tracking
                return var;
            }
            int set(int value=0)
            {
                sets++;  // same here but with sets tracking
                var=value;
                return var;
            }
            int add(int addend=1)
            {
                adds++;
                var+=addend;
                return var;
            }
            int getProp(char *propName) // get the number of gets, sets, etc.
            {
                if(!strcmp(propName,"gets"))
                    return gets;
                else if(!strcmp(propName,"sets"))
                    return sets;
                else if(!strcmp(propName,"adds"))
                    return adds;
                // add/remove from here if you want
                else
                    return 0;
            }
            // you can add more public functions here
    };
    

    这只是intVarCounterclass。如您所见,C++ 解决了我们所有的问题。这个问题的标签是,但这是给那些想用C++代替的人。如果没有外部程序会弄乱您的计数器,C 方式无论如何也不会那么糟糕。

    所有其他答案(在发布时)都根据金额给出了方法,但这适用于您的大多数情况,即使问题中没有钱。试试看,如果有效,请在 cmets 中告诉我!

    【讨论】:

    • 感谢您提供的 C 和 C++ 解决方案。我当然会尝试它们,但不幸的是,我不能将它们包含在我的程序中,因为它们的水平表明某人比我的程序练习所需的知识更丰富,而且肯定超出了我目前的水平。尽管如此,非常感谢。
    • @Den 感谢您的回复。我了解您的情况,希望接受的答案对您有用。 ?
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多