gpt4 book ai didi

计算股票信息总数和平均值

转载 作者:行者123 更新时间:2023-11-30 19:51:42 24 4
gpt4 key购买 nike

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

我试图获得 49% 而不是 0.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);
}

最佳答案

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

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

关于计算股票信息总数和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com