用变量查找剩余金额

时间:2013-11-27 20:06:19

标签: c function numbers

我参与的数量有限,并将它们分配给一个数组......完成了。

例如......我只有100张抽奖券。所以,我只能为很多人购买门票分配100张门票。有些人可能会购买19,有些人可能会购买1.但是当我达到100时,我不知道如何停下来检查,并且只给剩下的剩余门票。

这是一些代码。

我的函数调用:

int raffle_person, raffle_ticket, person;
                fscanf(ifp, "%d", &raffle_ticket);
                fscanf(ifp, "%d", &raffle_person);


            profit+=  Raffle(raffle_person,raffle_ticket, number_raffle_items,raffle_price, num_raffle_ticket);
            num_raffle_ticket = num_raffle_ticket - raffle_ticket;

            }

基本上它会从文件中扫描信息,然后调用该函数(忽略利润),然后获取票证的数量并从购买的数量中减去它。

这是功能:

float Raffle (int person, int amt_raffle_tickets, int
num_raffle_prize,float price_of_raffle_ticket, int num_raffle_ticket
)
{
    int i; float profit = 0;

    if (num_raffle_ticket <= amt_raffle_tickets){

        if (num_raffle_ticket - amt_raffle_tickets >0){
            int k;
            k = amt_raffle_tickets + num_raffle_ticket;

            printf("RAFFLE TICKETS %d - %d given to PERSON %d\n", raffle_index, k + amt_raffle_tickets -1 , person);
        }

        printf("NO rffle tikes given to %d\n" , person); return 0; } else if
            (num_raffle_ticket < amt_raffle_tickets){

            } printf("RAFFLE TICKETS %d - %d given to PERSON %d\n", raffle_index,
                    raffle_index + amt_raffle_tickets -1 , person);

        for (i=0; i<amt_raffle_tickets; i++){
            myRaffle.raffle_ticket[raffle_index] = person;    profit+= price_of_raffle_ticket;
            raffle_index++;

        }

        return profit;
}

首先,if语句检查该人购买的金额是否已经更大......然后停止该功能...(工作)

但我遇到了问题,因为我不知道如何将剩余的门票数量分配给一个人。就像我已经分配了96张门票一样,我如何找到与100相同的差异,并且仅将四个分配给此人?

1 个答案:

答案 0 :(得分:0)

这是你最初编写的函数,用更正统的方式编写 式:

float Raffle(int person, int amt_raffle_tickets, int num_raffle_prize,
             float price_of_raffle_ticket, int num_raffle_ticket)

amt_raffle_tickets参数是请求的票数; num_raffle_ticket参数是可用票数。

{
    int i;
    float profit = 0;

    if (num_raffle_ticket <= amt_raffle_tickets)
    {
        if (num_raffle_ticket - amt_raffle_tickets > 0)
        {
            int k;
            k = amt_raffle_tickets + num_raffle_ticket;
            printf("RAFFLE TICKETS %d - %d given to PERSON %d\n",
                   raffle_index, k + amt_raffle_tickets - 1, person);
        }
        printf("NO raffle tickets given to %d\n", person);
        return 0;

printf()return看起来像是错位的,也许意味着 进入else if子句中的空语句块。 但是,由于条件,永远不会输入else if块 由上面的if条件涵盖,这就留下了什么问题 printf()return应该这样做。

    }
    else if (num_raffle_ticket < amt_raffle_tickets)
    { 
    }
    printf("RAFFLE TICKETS %d - %d given to PERSON %d\n", raffle_index,
           raffle_index + amt_raffle_tickets -1, person);

    for (i = 0; i < amt_raffle_tickets; i++)
    {
        myRaffle.raffle_ticket[raffle_index] = person;
        profit+= price_of_raffle_ticket;
        raffle_index++;
    }

    return profit;
}

不幸的是,变量raffle_index未声明,因此我们可以 假设它是一个全局变量,并且给定它的打印方式必须是 int raffle_index。同样,您的myRaffle结构不是 记录要么,这也很难知道发生了什么。 但是,我相信结构应该具有价值 raffle_index。从不使用参数num_raffle_prize

我已经解决了未指定的位并提出了工作代码。 它从标准输入读取而不是从文件中读取,因为我很懒。

固定代码的示例运行:

$ ./raffle
13 3
RAFFLE TICKETS 0 - 12 given to PERSON 3
15 2
RAFFLE TICKETS 13 - 27 given to PERSON 2
17 231
RAFFLE TICKETS 28 - 44 given to PERSON 231
Ticket 0 for person 3
Ticket 1 for person 3
Ticket 2 for person 3
Ticket 3 for person 3
Ticket 4 for person 3
Ticket 5 for person 3
Ticket 6 for person 3
Ticket 7 for person 3
Ticket 8 for person 3
Ticket 9 for person 3
Ticket 10 for person 3
Ticket 11 for person 3
Ticket 12 for person 3
Ticket 13 for person 2
Ticket 14 for person 2
Ticket 15 for person 2
Ticket 16 for person 2
Ticket 17 for person 2
Ticket 18 for person 2
Ticket 19 for person 2
Ticket 20 for person 2
Ticket 21 for person 2
Ticket 22 for person 2
Ticket 23 for person 2
Ticket 24 for person 2
Ticket 25 for person 2
Ticket 26 for person 2
Ticket 27 for person 2
Ticket 28 for person 231
Ticket 29 for person 231
Ticket 30 for person 231
Ticket 31 for person 231
Ticket 32 for person 231
Ticket 33 for person 231
Ticket 34 for person 231
Ticket 35 for person 231
Ticket 36 for person 231
Ticket 37 for person 231
Ticket 38 for person 231
Ticket 39 for person 231
Ticket 40 for person 231
Ticket 41 for person 231
Ticket 42 for person 231
Ticket 43 for person 231
Ticket 44 for person 231
Profit = 90.00
$

工作代码

主要变量重命名继续进行。该计划是关于抽奖,所以 实际上没有必要在任何地方重复raffle。我没有 完全删除它,但我确实重命名了很多变量。

#include <assert.h>
#include <stdio.h>

enum { MAX_TICKETS = 100 };
static int raffle_index = 0;
static struct
{
    int raffle_ticket[MAX_TICKETS];
} myRaffle;
static float Raffle(int person, int req_tickets,
                    float ticket_price, int avail_tickets);

int main(void)
{
    FILE *ifp = stdin;
    int raffle_person;
    int raffle_ticket;
    float raffle_price = 2.00;
    int avail_tickets = MAX_TICKETS;
    float profit = 0.0;

    while (avail_tickets > 0 &&
           fscanf(ifp, "%d", &raffle_ticket) == 1 && 
           fscanf(ifp, "%d", &raffle_person) == 1)
    {
        profit +=  Raffle(raffle_person, raffle_ticket, 
                          raffle_price, avail_tickets);
        avail_tickets = avail_tickets - raffle_ticket;
    }

    for (int i = 0; i < raffle_index; i++)
        printf("Ticket %d for person %d\n", i, myRaffle.raffle_ticket[i]);

    printf("Profit = %.2f\n", profit);
    return 0;
}

float Raffle(int person, int req_tickets,
             float ticket_price, int avail_tickets)
{
    int i;
    float profit = 0;

    assert(avail_tickets > 0);
    if (avail_tickets < req_tickets)
        req_tickets = avail_tickets;
    printf("RAFFLE TICKETS %d - %d given to PERSON %d\n",
           raffle_index, raffle_index + req_tickets - 1, person);

    for (i = 0; i < req_tickets; i++)
    {
        myRaffle.raffle_ticket[raffle_index++] = person;
        profit+= ticket_price;
    }

    return profit;
}

另一天,我们将讨论反复添加浮动的智慧 点号到另一个浮点数;这不是它的方式 应该完成,但对于手头的代码就足够了。