2016-07-20 120 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); 
} 

當然不要忘了更新的功能和變量聲明。