如何计算变量值被使用的次数?
How do I count the number of times a variable's value has been used?
对 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);
}
}
所有变量都已声明and/or已初始化。
我知道算法m完全不正确;我不知道如何纠正它。一些见解将不胜感激。
只需使用除法即可得到每个硬币的数量。
然后将它们相加得到硬币总数。
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;
这里是大纲:
- 您需要一种方法来跟踪 25 美分、10 美分、5 美分和便士的数量。最简单的方法是为每个创建单独的变量,初始化为
0
:int quarters=0, dimes=0, nickels=0, pennies=0;
您还可以使用数组:int coins[4] = {0};
并且只需跟踪哪个面额存储在哪个索引处(即,25 美分硬币存储在索引 0 处,1 美分硬币存储在索引 1 处,等等)。
- 对于每个面额,从输入中减去该金额,直到输入值小于该面额:
<pre>/**
* Assume input value is in pennies, so .45 would be represented as 245
*/
while ( input > 0 && input >= 25 )
{
input -= 25;
quarters++; // or coins[0]++ if you're using the array method.
}
你会为每个面额重复这一点。您可能会更加棘手,并使用另一个数组作为面额值的查找 table,这样您就可以在一个循环中完成所有操作,但这应该可以让您继续。
编辑
或者您可以使用 Barmar 的方法并完全避免循环。
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);
}
这创建了一个简单的结构,基本上包装了变量,以便在您获取、设置或添加变量时进行跟踪。如果你想改变它,很简单;只是 add/remove 属性 和 add/remove 它的关联函数。
虽然这有问题。您可以直接更改 ivc.var
,有效地绕过计数器。您还必须始终使用 init
函数,否则您会得到看起来很奇怪的变量值。您还可以更改 get
、set
等的数量。这可以通过 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
};
这只是 intVarCounter
的 class
。如您所见,C++
解决了我们所有的问题。这道题的tag是c,但这是给想用C++
代替的。 C
如果没有外部程序会弄乱您的计数器,那么方法还算不错。
所有其他答案(在发布时)都根据金额给出了方法,但这适用于您的大多数情况,即使问题中没有包含金钱。尝试一下,如果有效,请在评论中告诉我!
对 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);
}
}
所有变量都已声明and/or已初始化。
我知道算法m完全不正确;我不知道如何纠正它。一些见解将不胜感激。
只需使用除法即可得到每个硬币的数量。
然后将它们相加得到硬币总数。
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;
这里是大纲:
- 您需要一种方法来跟踪 25 美分、10 美分、5 美分和便士的数量。最简单的方法是为每个创建单独的变量,初始化为
0
:
您还可以使用数组:int quarters=0, dimes=0, nickels=0, pennies=0;
并且只需跟踪哪个面额存储在哪个索引处(即,25 美分硬币存储在索引 0 处,1 美分硬币存储在索引 1 处,等等)。int coins[4] = {0};
- 对于每个面额,从输入中减去该金额,直到输入值小于该面额:
<pre>/** * Assume input value is in pennies, so .45 would be represented as 245 */ while ( input > 0 && input >= 25 ) { input -= 25; quarters++; // or coins[0]++ if you're using the array method. }
你会为每个面额重复这一点。您可能会更加棘手,并使用另一个数组作为面额值的查找 table,这样您就可以在一个循环中完成所有操作,但这应该可以让您继续。
编辑
或者您可以使用 Barmar 的方法并完全避免循环。
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);
}
这创建了一个简单的结构,基本上包装了变量,以便在您获取、设置或添加变量时进行跟踪。如果你想改变它,很简单;只是 add/remove 属性 和 add/remove 它的关联函数。
虽然这有问题。您可以直接更改 ivc.var
,有效地绕过计数器。您还必须始终使用 init
函数,否则您会得到看起来很奇怪的变量值。您还可以更改 get
、set
等的数量。这可以通过 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
};
这只是 intVarCounter
的 class
。如您所见,C++
解决了我们所有的问题。这道题的tag是c,但这是给想用C++
代替的。 C
如果没有外部程序会弄乱您的计数器,那么方法还算不错。
所有其他答案(在发布时)都根据金额给出了方法,但这适用于您的大多数情况,即使问题中没有包含金钱。尝试一下,如果有效,请在评论中告诉我!