C程序将输入金额分解为银行票据

C程序将输入金额分解为银行票据,c,C,我正在做一个练习,将一笔钱分解成纸币,但我得到了一个浮点例外 #include <stdio.h> int main() { int amount, n100, n50, n20, n10, n5, n2, n1; printf("Input the amount: \n"); scanf("%i", &amount); n100 = amount / 100; n50 = (amount

我正在做一个练习,将一笔钱分解成纸币,但我得到了一个浮点例外

#include <stdio.h>

int main()
{
    int amount, n100, n50, n20, n10, n5, n2, n1;

    printf("Input the amount: \n");
    scanf("%i", &amount);

    n100 = amount / 100;
    n50 = (amount % 100) / 50;
    n20 = (int)(n50 % (amount % 100)) / 20;
    n10 = (int)(n20 % (n50 % (amount % 100))) / 10;
    n5 = (int)(n10 % (n20 % (n50 % (amount % 100)))) / 5;
    n2 = (int)(n5 % (n10 % (n20 % (n50 % (amount % 100))))) / 2;
    n1 = (int)(n2 % (n5 % (n10 % (n20 % (n50 % (amount % 100)))))) / 1;
    
    printf("%i Note(s) of 100.00\n", n100);
    printf("%i Note(s) of 50.00\n", n50);
    printf("%i Note(s) of 20.00\n", n20);
    printf("%i Note(s) of 10.00\n", n10);
    printf("%i Note(s) of 5.00\n", n5);
    printf("%i Note(s) of 2.00\n", n2);
    printf("%i Note(s) of 1.00\n", n1);

    return 0;   
}
#包括
int main()
{
整数金额,n100,n50,n20,n10,n5,n2,n1;
printf(“输入金额:\n”);
scanf(“%i”和金额);
n100=金额/100;
n50=(金额%100)/50;
n20=(整数)(n50%(金额%100))/20;
n10=(int)(n20%(n50%(金额%100))/10;
n5=(内部)(n10%(n20%(n50%(金额%100))/5;
n2=(int)(n5%(n10%(n20%(n50%(金额%100);))/2;
n1=(int)(n2%(n5%(n10%(n20)(n50%(金额%100‘‘‘)’))/1;
printf(“%i注释100.00\n”,n100);
printf(“%i注释50.00\n”,n50);
printf(“%i注释20.00\n”,n20);
printf(“%i注释10.00\n”,n10);
printf(“%i注释5.00\n”,n5);
printf(“%i注释2.00\n”,n2);
printf(“%i注释1.00\n”,n1);
返回0;
}

如大多数金额的注释中所述,您将在某个点遇到
x%0
,这是未定义的行为,并将引发错误

改进代码的基本方法如下:

#包括
int main()
{
整数金额,n100,n50,n20,n10,n5,n2,n1;
int rest;
printf(“输入金额:\n”);
scanf(“%i”和金额);
n100=金额/100;
剩余=金额%100;
n50=休息/50;
rest=rest%50;
n20=静止/20;
rest=rest%20;
n10=静止/10;
rest=rest%10;
n5=休息/5;
rest=rest%5;
n2=静止/2;
rest=rest%2;
n1=休息;
printf(“%i注释100.00\n”,n100);
printf(“%i注释50.00\n”,n50);
printf(“%i注释20.00\n”,n20);
printf(“%i注释10.00\n”,n10);
printf(“%i注释5.00\n”,n5);
printf(“%i注释2.00\n”,n2);
printf(“%i注释1.00\n”,n1);
返回0;
}

通过这种方式,您可以减少大量的施法,更清楚地了解发生了什么,并且不可能遇到零分割。

正如在对大多数金额的评论中所述,您将在某个点遇到
x%0
,这是未定义的行为,并且会引发错误

改进代码的基本方法如下:

#包括
int main()
{
整数金额,n100,n50,n20,n10,n5,n2,n1;
int rest;
printf(“输入金额:\n”);
scanf(“%i”和金额);
n100=金额/100;
剩余=金额%100;
n50=休息/50;
rest=rest%50;
n20=静止/20;
rest=rest%20;
n10=静止/10;
rest=rest%10;
n5=休息/5;
rest=rest%5;
n2=静止/2;
rest=rest%2;
n1=休息;
printf(“%i注释100.00\n”,n100);
printf(“%i注释50.00\n”,n50);
printf(“%i注释20.00\n”,n20);
printf(“%i注释10.00\n”,n10);
printf(“%i注释5.00\n”,n5);
printf(“%i注释2.00\n”,n2);
printf(“%i注释1.00\n”,n1);
返回0;
}

通过这种方式,您可以减少铸造次数,更清楚地了解正在发生的情况,并且不可能遇到零分割。

在干燥原则下,如何使用面额和计数的数组

每当我看到(例如)
v1、v2、v3、…、vN时,它就会“呼喊”一个数组

对于这个用例,这可能不是很明显。但是,更容易看出N是否是一个大数字(例如1000)。也就是说,我们没有1000个单独的变量,而是有一个包含1000个元素的[array]变量

#include <stdio.h>

#define COUNTOF(_arr)       (sizeof(_arr) / sizeof(_arr[0]))

int denoms[] = { 100, 50, 20, 10, 5, 2, 1 };
#define COUNT   COUNTOF(denoms)

int counts[COUNT];

int
main(void)
{
    int amount;

    printf("Input the amount: ");
    fflush(stdout);
    scanf("%d",&amount);

    int value = amount;
    for (int idx = 0;  idx < COUNT;  ++idx) {
        int denom = denoms[idx];
        counts[idx] = value / denom;
        value %= denom;
    }

    for (int idx = 0;  idx < COUNT;  ++idx) {
        int count = counts[idx];
        if (count)
            printf("%d Notes of %d.00\n",count,denoms[idx]);
    }

    return 0;
}
#包括
#定义COUNTOF(_arr)(sizeof(_arr)/sizeof(_arr[0]))
int denoms[]={100,50,20,10,5,2,1};
#定义计数(denoms)
整数计数[计数];
int
主(空)
{
整数金额;
printf(“输入金额:”);
fflush(stdout);
scanf(“%d”和金额);
整数值=金额;
对于(int idx=0;idx
在干式原则下,使用数组表示面额和计数如何

每当我看到(例如)
v1、v2、v3、…、vN时,它就会“呼喊”一个数组

对于这个用例,这可能不是很明显。但是,更容易看出N是否是一个大数字(例如1000)。也就是说,我们没有1000个单独的变量,而是有一个包含1000个元素的[array]变量

#include <stdio.h>

#define COUNTOF(_arr)       (sizeof(_arr) / sizeof(_arr[0]))

int denoms[] = { 100, 50, 20, 10, 5, 2, 1 };
#define COUNT   COUNTOF(denoms)

int counts[COUNT];

int
main(void)
{
    int amount;

    printf("Input the amount: ");
    fflush(stdout);
    scanf("%d",&amount);

    int value = amount;
    for (int idx = 0;  idx < COUNT;  ++idx) {
        int denom = denoms[idx];
        counts[idx] = value / denom;
        value %= denom;
    }

    for (int idx = 0;  idx < COUNT;  ++idx) {
        int count = counts[idx];
        if (count)
            printf("%d Notes of %d.00\n",count,denoms[idx]);
    }

    return 0;
}
#包括
#定义COUNTOF(_arr)(sizeof(_arr)/sizeof(_arr[0]))
int denoms[]={100,50,20,10,5,2,1};
#定义计数(denoms)
整数计数[计数];
int
主(空)
{
整数金额;
printf(“输入金额:”);
fflush(stdout);
scanf(“%d”和金额);
整数值=金额;
对于(int idx=0;idx
x%0
未定义,您可能会在该代码的某个地方遇到零。您正在除以0(最有可能)。你可以用
amount
减去
n100*100
,然后
n50*50
等等,而不是用模做复杂的数学运算。你注意到你计算
amount%100
的次数不少于六次吗?你做了很多从
int
int
的转换工作,无害但没有用处。
n50
为0或1。因此,
(n50%(金额%100))
为0或1(定义时),并且
(n50%(am