2016-07-20 92 views
-1

所以我有我的程序启动并运行,但我没有得到我应该的正确值。 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); 
} 
+2

您应该提供:输入,预期产出,实际产出 – 4386427

+0

我的买入价格是20 1/2,我的卖价是40 1/2,所以我预计APPLE股票的产量会增加49%,而且它实际上是0.49%。我预计总收益损失也是49%,而结果是.49%。平均也应该是49%,因为只有一个输入,但最后是2.03% –

+1

对我来说这看起来很奇怪'返回1 - (buyPrice/sellPrice);'所以如果你卖超过购买的1000,000倍, GainLoss将接近1.这是你想要的吗? – 4386427

回答

-1

的收益/损失的百分比应计算如下:((sellprice/buyprice) - 1)*100 - < 确保buyprice不为0!> - 总收益/损失为:((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); 
} 

当然不要忘了更新的功能和变量声明。