C指针给出随机值

时间:2015-02-10 11:29:38

标签: c pointers

我正在教自己C乐趣。我在网上找了一些练习课程,找到了一个你输入两个输入的程序:一个是想象产品的数量,第二个是假想客户给你的金额。然后,您返回美元账单的数量,并将您给予客户的更改。

这是我为解决这个问题而编写的完整代码:

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

float getDif(float c, float m)
{
    // First find the difference between
    // c & m (m - c = d)
    float dif = m - c;

    // Return the difference
    return dif;
}

void getChange(float dif, int* cash, float* change)
{
    // If difference is less than 0 we have a problem
    // because the customer gave us less money than
    // the cost of the product
    if(dif < 0)
    {
        fprintf(stderr, "The customer has not given enough money!...\n");
        exit(EXIT_FAILURE);
    }
    // Cash is seen as the difference without any
    // decimals. So if the difference is $13.45
    // the cash would be 13 since cash is, in my
    // mind paper money
    *cash = (int) dif;

    // Change is seen as the difference - cash
    // if the difference is $13.45 the change
    // would be .45
    *change = dif - *cash;
}

void getCash(int cash, int* oneHundred, int* fifty, int* twenty, int* ten,
                int* five, int* one)
{
    if(cash == 0)
    {
        printf("Wow close to exact change! - No need for cash...\n");
        // End the function there is no need to continue
        return;
    }

    // Since cash is > 0 we need to determine the bills to return
    // to the user
    if(cash >= 100)
    {
        while(cash >= 100)
        {
            cash = cash - 100;
            (void) ++*oneHundred;
        }
    }

    if(cash >= 50)
    {
        while(cash >= 50)
        {
            cash = cash - 50;
            (void) ++*fifty;
        }
    }

    if(cash >= 20)
    {
        while(cash >= 20)
        {
            cash = cash - 20;
            (void) ++*twenty;
        }
    }

    if(cash >= 10)
    {
        while(cash >= 10)
        {
            cash = cash - 10;
            (void) ++*ten;
        }
    }

    if(cash >= 5)
    {
        while(cash >= 5)
        {
            cash = cash - 5;
            (void) ++*five;
        }
    }

    if(cash >= 1)
    {
        while(cash >= 1)
        {
            cash = cash - 1;
            (void) ++*one;
        }
    }

    printf("After all loops cash = %d\n", cash);
}

void getCoins(float change, int* quarter, int* dime, int* nickel, int* penny)
{
    // To find the correct change we need to turn the
    // current format of variable change (0.57) into
    // a easier to work with 57.
    int tenChange = change * 100;

    if(tenChange >= 25)
    {
        while(tenChange >= 25)
        {
            tenChange = tenChange - 25;
            (void) ++*quarter;
        }
    }

    if(tenChange >= 10)
    {
        while(tenChange >= 10)
        {
            tenChange = tenChange - 10;
            (void) ++*dime;
        }
    }

    if(tenChange >= 5)
    {
        while(tenChange >= 5)
        {
            tenChange = tenChange - 5;
            (void) ++*nickel;
        }
    }

    if(tenChange >= 1)
    {
        while(tenChange >= 1)
        {
            tenChange = tenChange - 1;
            (void) ++*penny;
        }
    }

    printf("After all loops change = %d\n", tenChange);
}

int main(void)
{
    // Create variables for the various things we create
    float c, m, dif, change;
    int cash, oneHundred, fifty, twenty, ten, five, one, quarter, dime, nickel,
        penny;

    printf("Enter the exact amount of the items (18.37): ");
    // Obtain the cost
    scanf("%f", &c);

    printf("Enter the amount of money given by the customer: ");
    // Obtain the money from customer
    scanf("%f", &m);

    // Obtain the difference of the cost
    // And the money given by calling the
    // getDif() function
    dif = getDif(c,m);

    // Send the difference to the getChange()
    // function, as well as the pointers 
    // cash & change which will be used in the
    // function and returned back here
    getChange(dif, &cash, &change);

    // First send the cash variable to the getCash
    // function along with pointers for each bill
    // The function will calculate the number of bills
    // to give to the customer and return each
    getCash(cash, &oneHundred, &fifty, &twenty, &ten, &five, &one);

    // Print the number of bills to give to the customer
    printf("Give the customer %d Hundred doller bill(s)!\n", oneHundred);
    printf("Give the customer %d Fifty doller bill(s)!\n", fifty);
    printf("Give the customer %d Twenty doller bill(s)!\n", twenty);
    printf("Give the customer %d Ten doller bill(s)!\n", ten);
    printf("Give the customer %d Five doller bill(s)!\n", five);
    printf("Give the customer %d One doller bill(s)!\n", one);

    // Second send the change variable to the getCoins
    // function along with pointers for each type of
    // coin. The function will calculate the number of
    // coins to give to the customer and return each
    getCoins(change, &quarter, &dime, &nickel, &penny);

    // Print the number of coins to give to the customer
    printf("Give the customer %d Quarter(s)\n", quarter);
    printf("Give the customer %d Dime(s)\n", dime);
    printf("Give the customer %d Nickel(s)\n", nickel);
    printf("Give the customer %d Penny(s)\n", penny);

    printf("%d\n", cash);
    printf("%.2f\n", change);
    printf("$%.2f\n", dif);
    return 0;
}

它工作正常,直到我打印出客户的镍和便士的数量。不是给我一个有意义的数字,而是给我一串随机的数字。

例如,这是这些输入的输出:

  

输入确切的项目数量(18.37):123.32

     

输入客户提供的金额:124.00

Wow close to exact change! - No need for cash...
Give the customer 0 Hundred doller bill(s)!
...
Give the customer 0 One doller bill(s)!
After all loops change = 0
Give the customer 2 Quarter(s)
Give the customer 1 Dime(s)
Give the customer 32768 Nickel(s)
Give the customer 1596836443 Penny(s)
0
0.68
$0.68

现在如果程序运行正常,应该说:

  • 2 Quarter(s)
  • 1 Dime(s)
  • 1 Nickel(s)
  • 3 Penny(s)

但是我得到了那些随机(?)值。这可能是我没有清除指针的问题吗?我从未使用过使用它们的语言,而且我还没有学到足够的东西来正确使用它们。

我尝试将所有现金变量(例如oneHundred)设置为NULL,但它给了我这个编译错误:

change.c:204:14: warning: incompatible pointer to integer conversion assigning
      to 'int' from 'void *' [-Wint-conversion]
        oneHundred      = NULL;
                        ^ ~~~~

任何帮助都会很棒!我刚刚开始学习这门语言,我希望这个问题能给我一些关于如何在将来写出更好的C的见解!谢谢!!

1 个答案:

答案 0 :(得分:3)

您不需要将它们设置为NULL因为它们不是指针,而是将它们初始化为0

cash = oneHundred = fifty = twenty = ten = five = one = quarter = dime = nickel = penny = 0;

NULL用于初始化指向特殊值的指针,因此您可以对其进行测试,以检查指针是否指向某处或是否指向。