我的任务是起草一份方案,向用户询问他们需要多少钱,然后向用户询问他们想要购买多少票。 我必须具备主要()职能和采购标的功能。 我对点人来说是新鲜事,在如何利用这些变量分配当地变量和利用这些当地变量进行计算方面,我感到非常困惑。 我能否了解一下我究竟是哪里错了,以及如何把这变成一个可贵的方案?
EDIT:必须有两个功能:主票和购票。 如果用户没有足够的钱购买票,则需要使用变数剩余现金的点子,方案必须退还零。
#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?
");
if(scanf("%lf", &funds) != 1) //check for errors in user input
{
printf("Invalid input, exiting program.
");
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.
");
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.
");
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;
}