简体   繁体   English

货币柜台C程序

[英]Money counter C program

I have generated code that counts the minimum number of 20's, 10's, 5's, 2's and 1's that will add up to a user defined amount of money. 我生成的代码计算了20,10,5,2和1的最小数量,它们将累计到用户定义的金额。 The user is only allowed to enter whole numbers ie no decimal values. 用户只能输入整数,即没有小数值。 I have two questions. 我有两个问题。

  1. If a denomination is not needed, the program outputs a random number instead of 0. How can I fix this? 如果不需要面额,程序会输出一个随机数而不是0.如何解决这个问题呢?
  2. Is it possible to create a function that could replace all of the if statements and possibly the printf statements? 是否可以创建一个可以替换所有if语句和printf语句的printf I'm new to functions so am a little bit lost with them. 我是新手,所以对他们来说有点失落。

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

int main(void)
{
 int pounds;
 int one, two, five, ten, twenty;

    printf("Enter a pounds amount with no decimals, max E999999: \n");
    scanf("%d", &pounds);
    printf("%d\n", pounds);

    if(pounds >= 20)
    {
        twenty = (pounds / 20);
        pounds = (pounds-(twenty * 20));
        printf("%d\n", pounds);
    }
    if(pounds >= 10)
    {
        ten = (pounds / 10);
        pounds = (pounds-(ten * 10));
        printf("%d\n", pounds);
    }
    if(pounds >= 5)
    {
        five = (pounds / 5);
        pounds = (pounds-(five * 5));
        printf("%d\n", pounds);
    }
    if(pounds >= 2)
    {
        two = (pounds / 2);
        pounds = (pounds-(two * 2));
        printf("%d\n", pounds);
    }
    if(pounds >= 1)
    {
        one = (pounds / 1);
        pounds = (pounds-(one * 1));
        printf("%d\n", pounds);
    }


 printf("The smallest amount of denominations you need are: \n");
 printf("20 x %d\n", twenty);
 printf("10 x %d\n", ten);
 printf("5 x %d\n", five);
 printf("2 x %d\n", two);
 printf("1 x %d\n", one);

return 0;
}

This is a great example of why you should initialize your variables when you declare them. 这是一个很好的例子,说明在声明变量时应该初始化变量的原因。

If pounds<20 , then twenty will never get initialized. 如果pounds<20 ,则twenty永远不会初始化。 In C, variables have a (basically) random value assigned to them until you replace it with something else. 在C中,变量具有(基本上)分配给它们的随机值,直到用其他东西替换它。

You just need to do this: 你只需要这样做:

int one = 0, two = 0, five = 0, ten = 0, twenty = 0;

输出0只是将所有变量初始化为0,否则将为它们分配“垃圾”值:

int one = 0, two = 0, five = 0, ten = 0, twenty = 0;

It is always a good practice to initialize all your variables to 0 when you declare them. 在声明它们时,将所有变量初始化为0始终是一个好习惯。 This way you wont get a random value if there are no denominations to be made. 这样,如果没有面额,你就不会得到一个随机值。 You can declare and initiate your variables at the same time by doing this : 您可以通过以下方式同时声明和启动变量:

int a = 0;

or if they are many: 或者如果它们很多:

int a = 0, b = 0, c = 0;

If you don't initialize your variables before you use them the data they have stored in them will be random things that were in your ram before you executed your program. 如果在使用变量之前没有初始化变量,那么它们存储在变量中的数据将是执行程序之前ram中的随机数据。 This is why you get random numbers as answers. 这就是为什么你得到随机数作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM