计算库存信息的总数和平均值

时间:2016-07-20 06:29:17

标签: c formula

所以我的程序启动并运行,但我没有得到正确的值,我应该是。 Here is the report

我试图获得49%而不是.49%而且平均值也搞砸了?这只是公式上的一个简单的混乱还是我完全搞砸了?真的很困惑,实际上很高兴我达到了这一点,但感觉真的很困难。任何帮助都会很棒。

#include <stdio.h>
//prototypes
void getInputs(char*ticker,float*buyPrice,float*sellPrice);         //3.1
float getPrice(int whole,int num,int dem);                          //3.1.1
float calcGainLoss(float buyPrice,float sellPrice);                 //3.2
void showTransactionReport(char ticker[],float gainLoss);           //3.3
void showSummaryReport(float totGainLoss,int numOfTransactions);    //3.4

int main(void)
{
    char ticker[15];
    float gainLoss;
    float buyPrice;
    float sellPrice;
    float totGainLoss=0;
    int numOfTransactions=0;
    char answer;
    do{
        getInputs(ticker,&buyPrice,&sellPrice); //call 3.1/.1
        gainLoss=calcGainLoss(buyPrice,sellPrice); //call 3.2
        showTransactionReport(ticker,gainLoss); //call 3.3
        totGainLoss= totGainLoss + gainLoss;
        numOfTransactions++;
        printf("Would you like to do another transaction? (Y/y or N/n) ==> ");
        fflush(stdin);
        scanf(" %c", &answer);
    }while (answer == 'Y' || answer == 'y');

    showSummaryReport(totGainLoss,numOfTransactions);

    fflush(stdin);
    getchar();
    return 0;
}
//function 3.1
void getInputs(char*ticker,float*buyPrice,float*sellPrice)
{
    int whole=0;
    int num=0;
    int dem=0;
    printf("Enter the stock ticker ==> ");
    scanf("%s",ticker);
    printf("Input buy price of %s ==> ",ticker);
    scanf("%d %d/%d",&whole,&num,&dem);
    *buyPrice=getPrice(whole,num,dem);
    printf("Input sell price==> ");
    scanf("%d %d/%d",&whole,&num,&dem);
    *sellPrice=getPrice(whole,num,dem);
}
float getPrice(int whole,int num,int dem)
{
    return whole + float(num) / float(dem);
}
float calcGainLoss(float buyPrice,float sellPrice)
{
    return 1 - (buyPrice / sellPrice);
}
void showTransactionReport(char ticker[0],float gainLoss)
{
    printf("Stock Description               Gain/Loss%\n");
    printf("=================               ==========\n");
    printf("%-17s               %10.2f %% \n",ticker,gainLoss);
}
void showSummaryReport(float totGainLoss,int numOfTransactions)
{
    printf("Total Gain/Loss:                %10.2f %% \n",totGainLoss);
    printf("Average Gain/Loss:              %10.2f %% \n",numOfTransactions / totGainLoss);
}

1 个答案:

答案 0 :(得分:-1)

收益/损失百分比应按如下方式计算:((sellprice/buyprice) - 1)*100 - &lt; 确保buyprice不为0!&gt; - 以及总收益/损失是:((totalSellPrices/totalBuyPrices) - 1)*100

为此,只需稍微修改一下主循环:

float totalBuyPrice = 0.0f;
float totalSellPrice = 0.0f;
do{
    getInputs(ticker,&buyPrice,&sellPrice); //call 3.1/.1
    gainLoss=calcGainLoss(buyPrice,sellPrice); //call 3.2
    showTransactionReport(ticker,gainLoss); //call 3.3
    //totGainLoss= totGainLoss + gainLoss;
    totalBuyPrice += buyPrice;
    totalSellPrice += sellPrice;
    numOfTransactions++;
    printf("Would you like to do another transaction? (Y/y or N/n) ==> ");
    fflush(stdin);
    scanf(" %c", &answer);
}while (answer == 'Y' || answer == 'y');

然后在showSummaryReport中做适当的更改:

void showSummaryReport(float totalBuyPrice, float totalSellPrice, int numOfTransactions)
{
    float totGainLoss = 0.0f;
    if (totalBuyPrice)
    {
        totGainLoss = (totalSellPrice/totalBuyPrice - 1)*100;
    }
    printf("Total Gain/Loss:                %10.2f %% \n",totGainLoss);
    printf("Average Gain/Loss:              %10.2f %% \n",totGainLoss / numOfTransactions);
}

当然不要忘记更新函数和变量声明。