簡體   English   中英

我正在做 cs50 pset 1 cash,每當我運行此代碼時,終端都會變得空白

[英]I am doing the cs50 pset 1 cash and whenever i am running this code, the terminal is getting blank

我正在做 cs50 pset 1 cash 並且每當我運行此代碼時,每次按下輸入時終端都會變空白。 我寫了一個數字,它不接受它。 我相信我的 get_cents() 函數有問題,但我不明白它是什么。

#include <cs50.h>
#include <stdio.h>

int get_cents(void);
int calculate_quarters(int cents);
int calculate_dimes(int cents);
int calculate_nickels(int cents);
int calculate_pennies(int cents);


int main(void)
{
// Ask how many cents the customer is owed
int cents = get_cents();

// Calculate the number of quarters to give the customer
int quarters = calculate_quarters(cents);
cents = cents - quarters * 25;

// Calculate the number of dimes to give the customer
int dimes = calculate_dimes(cents);
cents = cents - dimes * 10;

// Calculate the number of nickels to give the customer
int nickels = calculate_nickels(cents);
cents = cents - nickels * 5;

// Calculate the number of pennies to give the customer
int pennies = calculate_pennies(cents);
cents = cents - pennies * 1;

// Sum coins
int coins = quarters + dimes + nickels + pennies;

// Print total number of coins to give the customer
printf("%i\n", coins);
}

int get_cents(void)
{
int n;
do
{
    n = get_int("Money Owed: ");
}
while (n <= 0);
return n;
}


int calculate_quarters(int cents)
{
int i = 0;
if (cents >= 25)
{
    do
    {
        i += 1;
    }
    while (cents >= 25);
    return i;
}
return 0;
}

int calculate_dimes(int cents)
{
int i = 0;
if (cents >= 10)
{
    do
    {
        i += 1;
    }
    while (cents >= 10);
    return i;
}
return 0;
}

int calculate_nickels(int cents)
{
int i = 0;
if (cents >= 5)
{
    do
    {
        i += 1;
    }
    while (cents >= 5);
    return i;
}
return 0;
}

int calculate_pennies(int cents)
{
if (cents >= 1)
{
    return cents;
}
return 0;
}

當我將 return i 行添加到所有函數時發生這種情況,我不知道該怎么做。

您所有的calculate_*函數都不會按應有的方式減少cents 它們是無限循環。

但是,所有這些功能都只是重復減法的除法。 怎么樣(例如):

int
calculate_quarters(int cents)
{

    return cents / 25;
}

您是否需要以這種方式使用函數? 使用面額表可能會更好(例如) int denom[] = { 25, 10, 5, 1 }; 並循環。


這是一個重構版本。 注釋如下:

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

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

const int denoms[] = { 25, 10, 5, 1 };
const char *names[] = { "quarters", "dimes", "nickels", "pennies" };

int
get_cents(void)
{
    int n;

    do {
        n = get_int("Money Owed: ");
    } while (n <= 0);

    return n;
}

int
main(int argc,char **argv)
{

    --argc;
    ++argv;

    int cents = get_cents();
    printf("%d cents\n",cents);

    int coins = 0;
    for (int idx = 0;  idx < COUNTOF(denoms);  ++idx) {
        int denom = denoms[idx];

        // is amount remaining larger than the denomination of the current coin?
        if (cents >= denom) {
            // get number of the current denomination to dispense
            int count = cents / denom;

            // remember total number of coins
            coins += count;

            // show how many of this coin to dispense
            printf("%d %s\n",count,names[idx]);

            // reduce total number of cents
            cents %= denom;
        }
    }

    // show the number of coins
    printf("%d coins\n",coins);

    return 0;
}

這是一些程序輸出:

Money Owed: 137
137 cents
5 quarters
1 dimes
2 pennies
8 coins

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM