在C中解引用指针有困难

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

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

  1. #include <stdio.h>
  2. #define ADULT_TICKET_PRICE 69.00 //define constants
  3. #define CHILD_TICKET_PRICE 35.00
  4. int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets); //declare function PurchaseTickets()
  5. int main(void)
  6. {
  7. double funds;
  8. int childTickets;
  9. int adultTickets;
  10. printf("How much money do you have to purchase tickets?\n");
  11. if(scanf("%lf", &funds) != 1) //check for errors in user input
  12. {
  13. printf("Invalid input, exiting program.\n");
  14. return 1; //will exit program if invalid characters are entered
  15. }
  16. printf("How many child tickets would you like to purchase?\n");
  17. if(scanf("%d", &childTickets) != 1) //check for errors in user input
  18. {
  19. printf("Invalid input, exiting program.\n");
  20. return 1; //will exit program if invalid characters are entered
  21. }
  22. printf("How many adult tickets would you like to purchase?\n");
  23. if(scanf("%d", &adultTickets) != 1) //check for errors in user input
  24. {
  25. printf("Invalid input, exiting program.\n");
  26. return 1; //will exit program if invalid characters are entered
  27. }
  28. double *pfunds = &funds;
  29. PurchaseTickets(pfunds, adultTickets, childTickets);
  30. return 0;
  31. }
  32. /*****************************************************************************************
  33. *
  34. * Function: PurchaseTickets()
  35. *
  36. * Parameters:
  37. *
  38. * pRemainingCash - points to a variable containing the amt of cash from user
  39. * adultTickets - specifies the number of adult tickets the user wants to buy
  40. * childTickets - specifies the number of child tickets the user wants to buy
  41. *
  42. * Description:
  43. *
  44. * The function will determind if the user has enough money to purchase the
  45. * specified number of tickets. If they do, the function deducts the proper funds
  46. * from their remaining cash and returns the total number of tickets purchased.
  47. * if they do not the function returns 0.
  48. *
  49. *****************************************************************************************/
  50. int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
  51. {
  52. double adultCost = (adultTickets * ADULT_TICKET_PRICE);
  53. double childCost = (childTickets * CHILD_TICKET_PRICE);
  54. double totalCost = adultCost + childCost;
  55. int totalTickets = adultTickets + childTickets;
  56. double remainingCash = *pRemainingCash;
  57. pRemainingCash = &remainingCash;
  58. pRemainingCash = remainingCash - totalCost;
  59. if (*pRemainingCash >= totalCost)
  60. {
  61. printf("You have purchased %d tickets, and you have %.2f remaining.",totalTickets, pRemainingCash);
  62. }
  63. else
  64. {
  65. printf("You do not have enough money to purchase the tickets.");
  66. return 0;
  67. }
  68. return 1;
  69. }
ecbunoof

ecbunoof1#

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

  1. // heading stuff omitted for brevity
  2. int main(void)
  3. {
  4. double funds;
  5. int childTickets;
  6. int adultTickets;
  7. /* Get funds */ // omitted for brevity
  8. /* Get # of Child tickets */
  9. /* Get # of Adult tickets */
  10. int totTckts = PurchaseTickets( &funds, adultTickets, childTickets );
  11. // Notice that the function performs its task.
  12. // Reporting those results belongs in the calling function
  13. if( totTckts )
  14. printf( "%d tickets, and you have %.2f remaining.", totTckts, funds );
  15. else
  16. printf( "Insufficient funds to purchase those tickets." );
  17. return 0;
  18. }
  19. int PurchaseTickets( double *pFunds, int nAdult, int nChild )
  20. {
  21. double Cost
  22. = (nAdult * ADULT_TICKET_PRICE)
  23. + (nChild * CHILD_TICKET_PRICE);
  24. if( *pFunds < Cost ) // Insufficient available funds?
  25. return 0; // no show
  26. *pFunds -= Cost; // subtract cost from "double funds;" variable in main()
  27. return nAdult + nChild; // return # of purchased tickets
  28. }
展开查看全部
qij5mzcb

qij5mzcb2#

PurchaseTickets函数中有几个问题。

  1. double remainingCash = *pRemainingCash;
  2. pRemainingCash = &remainingCash;

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

  1. pRemainingCash = remainingCash - totalCost;

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

  1. int PurchaseTickets(double *pRemainingCash, int adultTickets, int childTickets)
  2. {
  3. double adultCost = (adultTickets * ADULT_TICKET_PRICE);
  4. double childCost = (childTickets * CHILD_TICKET_PRICE);
  5. double totalCost = adultCost + childCost;
  6. int totalTickets = adultTickets + childTickets;
  7. if (*pRemainingCash >= totalCost)
  8. {
  9. // Subtract cost here
  10. *pRemainingCash -= totalCost;
  11. // Note *pRemainingCash in the next line.
  12. // You need to dereference to obtain the value pointed at.
  13. printf("You have purchased %d tickets, and you have %.2f remaining.",
  14. totalTickets, *pRemainingCash);
  15. // Return success
  16. return totalTickets;
  17. }
  18. // This code won't be reached in the success case because of the return.
  19. // Removing the else avoids the possibility of the compiler warning that
  20. // all paths do not return a value.
  21. printf("You do not have enough money to purchase the tickets.");
  22. return 0;
  23. }

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

展开查看全部
6yoyoihd

6yoyoihd3#

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

  1. //double remainingCash = *pRemainingCash; // Not needed
  2. //pRemainingCash = &remainingCash;
  3. //pRemainingCash = remainingCash - totalCost;
  4. if (*pRemainingCash >= totalCost)
  5. {
  6. printf("You have purchased %d tickets, and you have %.2f remaining.",totalTickets, *pRemainingCash - totalCost); // Note remainder calculation
  7. }

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

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

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

  1. #include <stdio.h>
  2. #define ADULT_TICKET_PRICE 69.00 //define constants
  3. #define CHILD_TICKET_PRICE 35.00
  4. void PurchaseTickets(double pRemainingCash, int adultTickets, int childTickets); //declare function PurchaseTickets()
  5. int main(void)
  6. {
  7. double funds;
  8. int childTickets;
  9. int adultTickets;
  10. printf("How much money do you have to purchase tickets? ");
  11. if(scanf("%lf", &funds) != 1) //check for errors in user input
  12. {
  13. printf("Invalid input, exiting program.\n");
  14. return 1; //will exit program if invalid characters are entered
  15. }
  16. printf("How many child tickets would you like to purchase? ");
  17. if(scanf("%d", &childTickets) != 1) //check for errors in user input
  18. {
  19. printf("Invalid input, exiting program.\n");
  20. return 1; //will exit program if invalid characters are entered
  21. }
  22. printf("How many adult tickets would you like to purchase? ");
  23. if(scanf("%d", &adultTickets) != 1) //check for errors in user input
  24. {
  25. printf("Invalid input, exiting program.\n");
  26. return 1; //will exit program if invalid characters are entered
  27. }
  28. PurchaseTickets(funds, adultTickets, childTickets);
  29. return 0;
  30. }
  31. /*****************************************************************************************
  32. *
  33. * Function: PurchaseTickets()
  34. *
  35. * Parameters:
  36. *
  37. * pRemainingCash - points to a variable containing the amt of cash from user
  38. * adultTickets - specifies the number of adult tickets the user wants to buy
  39. * childTickets - specifies the number of child tickets the user wants to buy
  40. *
  41. * Description:
  42. *
  43. * The function will determind if the user has enough money to purchase the
  44. * specified number of tickets. If they do, the function deducts the proper funds
  45. * from their remaining cash and returns the total number of tickets purchased.
  46. * if they do not the function returns 0.
  47. *
  48. *****************************************************************************************/
  49. void PurchaseTickets(double pRemainingCash, int adultTickets, int childTickets)
  50. {
  51. double adultCost = (adultTickets * ADULT_TICKET_PRICE);
  52. double childCost = (childTickets * CHILD_TICKET_PRICE);
  53. double totalCost = adultCost + childCost;
  54. int totalTickets = adultTickets + childTickets;
  55. pRemainingCash -= totalCost;
  56. if (pRemainingCash >= 0.00)
  57. {
  58. printf("You have purchased %d tickets, and you have %.2f remaining.\n",totalTickets, pRemainingCash);
  59. }
  60. else
  61. {
  62. printf("You do not have enough money to purchase the tickets.\n");
  63. }
  64. return;
  65. }

请注意以下改进:

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

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

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

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

展开查看全部

相关问题