在C中解引用指针有困难

8ehkhllq  于 2023-10-16  发布在  其他
关注(0)|答案(3)|浏览(92)

我的任务是写一个程序,询问用户他们必须花多少钱,然后询问用户他们想买多少张票。我必须有一个main()函数和一个PurchaseTickets()函数。我是新的指针,并获得相当困惑如何使用它们与分配局部变量和使用这些局部变量的计算。我能不能深入了解一下我错在哪里,以及如何把它变成一个可用的程序?
编辑:必须有两个功能:主要和购买门票。使用一个指针来表示变量remaining cash是必要的,如果用户没有足够的钱来买票,程序必须返回0。

#include <stdio.h>

#define ADULT_TICKET_PRICE 69.00                                                    //define constants
#define CHILD_TICKET_PRICE 35.00

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets);    //declare function PurchaseTickets()

int main(void)
{
    double funds;
    int childTickets;
    int adultTickets;

    printf("How much money do you have to purchase tickets?\n");
    if(scanf("%lf", &funds) != 1)                        //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many child tickets would you like to purchase?\n");
    if(scanf("%d", &childTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many adult tickets would you like to purchase?\n");
    if(scanf("%d", &adultTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    double *pfunds = &funds;

    PurchaseTickets(pfunds, adultTickets, childTickets);

    return 0;
}

/*****************************************************************************************
 *
 *  Function:  PurchaseTickets()
 *
 *  Parameters:
 *
 *      pRemainingCash - points to a variable containing the amt of cash from user
 *      adultTickets - specifies the number of adult tickets the user wants to buy
 *      childTickets - specifies the number of child tickets the user wants to buy
 *
 *  Description: 
 *
 *      The function will determind if the user has enough money to purchase the
 *      specified number of tickets. If they do, the function deducts the proper funds
 *      from their remaining cash and returns the total number of tickets purchased.
 *      if they do not the function returns 0.
 *
*****************************************************************************************/

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
{
    double adultCost = (adultTickets * ADULT_TICKET_PRICE);
    double childCost = (childTickets * CHILD_TICKET_PRICE);
    double totalCost = adultCost + childCost;

    int totalTickets = adultTickets + childTickets;

    double remainingCash = *pRemainingCash;
    pRemainingCash = &remainingCash;

    pRemainingCash = remainingCash - totalCost;

    if (*pRemainingCash >= totalCost)
    {
        printf("You have purchased %d tickets, and you have %.2f remaining.",totalTickets, pRemainingCash);
    }

    else
    {
        printf("You do not have enough money to purchase the tickets.");
        return 0;
    }

    return 1;
}
ecbunoof

ecbunoof1#

这是一个混乱。没有重复@NoDakker的评论,下面是代码的缩写版本,显示了所需的操作。请注意,为了增加清晰度,一些变量已被重命名(并删除了注解)。

// heading stuff omitted for brevity
int main(void)
{
    double funds;
    int childTickets;
    int adultTickets;

    /* Get funds */ // omitted for brevity

    /* Get # of Child tickets */

    /* Get # of Adult tickets */

    int totTckts = PurchaseTickets( &funds, adultTickets, childTickets );

    // Notice that the function performs its task.
    // Reporting those results belongs in the calling function
    if( totTckts )
        printf( "%d tickets, and you have %.2f remaining.", totTckts, funds );
    else
        printf( "Insufficient funds to purchase those tickets." );

    return 0;
}

int PurchaseTickets( double *pFunds, int nAdult, int nChild )
{
    double Cost
        = (nAdult * ADULT_TICKET_PRICE)
        + (nChild * CHILD_TICKET_PRICE);

    if( *pFunds < Cost ) // Insufficient available funds?
        return 0; // no show

    *pFunds -= Cost; // subtract cost from "double funds;" variable in main()

    return nAdult + nChild; // return # of purchased tickets
}
qij5mzcb

qij5mzcb2#

PurchaseTickets函数中有几个问题。

double remainingCash = *pRemainingCash;
pRemainingCash = &remainingCash;

这段代码复制pRemainingCash指向的值,然后将其指向本地副本。这段代码是正确的,但它似乎不是正确的做法。

pRemainingCash = remainingCash - totalCost;

这是你遇到一个错误,由于语法。它试图将浮点值分配给指针,而不是指针指向的内容。需要*pRemainingCash = remainingCash - totalCost;
这将解决错误,但不是问题,因为它修改的是本地副本,而不是传入的值。
另一个问题是,你应该检查他们是否能负担得起的门票之前减去成本。我希望,如果成本超过可用资金,你会离开可用资金不变。
以下是一个清理版本:

int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
{
    double adultCost = (adultTickets * ADULT_TICKET_PRICE);
    double childCost = (childTickets * CHILD_TICKET_PRICE);

    double totalCost = adultCost + childCost;
    int totalTickets = adultTickets + childTickets;

    if (*pRemainingCash >= totalCost)
    {
        // Subtract cost here
        *pRemainingCash -= totalCost;
        // Note *pRemainingCash in the next line. 
        // You need to dereference to obtain the value pointed at.
        printf("You have purchased %d tickets, and you have %.2f remaining.",
            totalTickets, *pRemainingCash);

        // Return success
        return totalTickets;
    }
    // This code won't be reached in the success case because of the return.
    // Removing the else avoids the possibility of the compiler warning that
    // all paths do not return a value.
    printf("You do not have enough money to purchase the tickets.");
    return 0;
}

main中的另一个小建议。
而不是double *pfunds = &funds;只是调用像PurchaseTickets(&funds, adultTickets, childTickets);这样的函数
你所拥有的工作,但我认为最好不要创建一个额外的变量。如果在此之后还有更多的代码,则可能会混淆使用哪一个。

6yoyoihd

6yoyoihd3#

我检查了你的代码,从我所看到的理想的解决方案来看,这个程序的上下文真的不需要指针。首先,由于“PurchaseTickets”函数实际上并不返回任何所需的信息,因此将其设置为空返回条目更有意义。其次,由于函数所需的输入变量可以全部作为值传递,因此真的不需要发送指针引用。
为了最小化对代码的影响,可以在函数中将以下语句与“printf”语句的重构一起沿着注解掉。

//double remainingCash = *pRemainingCash;           // Not needed
    //pRemainingCash = &remainingCash;

    //pRemainingCash = remainingCash - totalCost;

    if (*pRemainingCash >= totalCost)
    {
        printf("You have purchased %d tickets, and you have %.2f remaining.",totalTickets, *pRemainingCash - totalCost);    // Note remainder calculation
    }

这在终端产生了以下测试输出。

craig@Vera:~/C_Programs/Console/TicketPurchase/bin/Release$ ./TicketPurchase 
How much money do you have to purchase tickets?
800
How many child tickets would you like to purchase?
4
How many adult tickets would you like to purchase?
4
You have purchased 8 tickets, and you have 384.00 remaining.

注意代码的观察和简化的事情,下面是你的程序的重构版本。

#include <stdio.h>

#define ADULT_TICKET_PRICE 69.00                                                    //define constants
#define CHILD_TICKET_PRICE 35.00

void PurchaseTickets(double pRemainingCash, int adultTickets, int childTickets);    //declare function PurchaseTickets()

int main(void)
{
    double funds;
    int childTickets;
    int adultTickets;

    printf("How much money do you have to purchase tickets? ");
    if(scanf("%lf", &funds) != 1)                       //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many child tickets would you like to purchase? ");
    if(scanf("%d", &childTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    printf("How many adult tickets would you like to purchase? ");
    if(scanf("%d", &adultTickets) != 1)                 //check for errors in user input
    {
        printf("Invalid input, exiting program.\n");
        return 1;                                       //will exit program if invalid characters are entered
    }

    PurchaseTickets(funds, adultTickets, childTickets);

    return 0;
}

/*****************************************************************************************
 *
 *  Function:  PurchaseTickets()
 *
 *  Parameters:
 *
 *      pRemainingCash - points to a variable containing the amt of cash from user
 *      adultTickets - specifies the number of adult tickets the user wants to buy
 *      childTickets - specifies the number of child tickets the user wants to buy
 *
 *  Description:
 *
 *      The function will determind if the user has enough money to purchase the
 *      specified number of tickets. If they do, the function deducts the proper funds
 *      from their remaining cash and returns the total number of tickets purchased.
 *      if they do not the function returns 0.
 *
*****************************************************************************************/

void PurchaseTickets(double pRemainingCash, int adultTickets, int childTickets)
{
    double adultCost = (adultTickets * ADULT_TICKET_PRICE);
    double childCost = (childTickets * CHILD_TICKET_PRICE);
    double totalCost = adultCost + childCost;

    int totalTickets = adultTickets + childTickets;

    pRemainingCash -= totalCost;

    if (pRemainingCash >= 0.00)
    {
        printf("You have purchased %d tickets, and you have %.2f remaining.\n",totalTickets, pRemainingCash);
    }

    else
    {
        printf("You do not have enough money to purchase the tickets.\n");
    }

    return;
}

请注意以下改进:

  • “PurchaseTickets”函数的返回值已更改为“void”,因为整数返回值似乎没有用处。
  • 所有变量都作为值传递,而不是使用任何变量指针。
  • 删除了不必要的变量,以简化计算和比较。

在这些改进之后,接下来是对程序和购票功能的一系列额外测试。

craig@Vera:~/C_Programs/Console/TicketPurchase/bin/Release$ ./TicketPurchase 
How much money do you have to purchase tickets? 300.00
How many child tickets would you like to purchase? 4
How many adult tickets would you like to purchase? 4
You do not have enough money to purchase the tickets.
craig@Vera:~/C_Programs/Console/TicketPurchase/bin/Release$ ./TicketPurchase 
How much money do you have to purchase tickets? 600
How many child tickets would you like to purchase? 4
How many adult tickets would you like to purchase? 4
You have purchased 8 tickets, and you have 184.00 remaining.

概括一下,在函数中传递变量地址代替变量值是有正当理由的,但是应该始终了解函数的上下文以确定是否有必要。寻找关于函数定义的额外教程,以了解指针何时是有益的,这可能是明智的。

相关问题