-1
所以我有我的程序启动并运行,但我没有得到我应该的正确值。 计算股票信息的总和平均值
我想得到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);
}
您应该提供:输入,预期产出,实际产出 – 4386427
我的买入价格是20 1/2,我的卖价是40 1/2,所以我预计APPLE股票的产量会增加49%,而且它实际上是0.49%。我预计总收益损失也是49%,而结果是.49%。平均也应该是49%,因为只有一个输入,但最后是2.03% –
对我来说这看起来很奇怪'返回1 - (buyPrice/sellPrice);'所以如果你卖超过购买的1000,000倍, GainLoss将接近1.这是你想要的吗? – 4386427