计算投资收益2.0版

in steemstem •  21 days ago

接着上次的计算投资收益的C++程序,做点小更改。更改什么呢?因为很多时候我们只想知道收益的百分比,就足够了。比如说,每天1%的收益,10天后能赚百分之多少?20天后能赚百分之多少?所以,上次的程序改一下,用来实现这个目标。

int main()

{
float daily, time; 
int roi;

cout<<"What's your daily return(%)?"<<endl;
cin>>daily;

cout<<"How many days?"<<endl;
cin>>time;

roi=100*(pow(1+daily/100,time)-1);

cout<<"You will make "<<roi<<"% after "<< time <<" days!"<<endl;
}

要注意几点。
  1. 每天的收益和天数必须要定义成float,虽然他们可以都是整数,反正天数肯定是整数,但一定不能定义为int。因为在我们调用的计算幂的pow函数中,底数和幂都不能是int。至少也得是double,所以干脆都定义为float好了。关于这个问题,在C++网站上有明确的说明。

  2. 因为我们只计算收益百分比,所以计算公式跟上次有点不同。首先不用考虑初始投资了,然后还得减掉1。最后,因为要换算成百分数,所以还得乘以100。

  3. 收益百分比(roi)可以定义为int,因为如果不需要特别精确的话,取整数就可以了。比方说,知道赚20%就可以了,一般来说,不需要精确到小数点后一位或者两位。

好了,运行一下,结果是这样的。

Picture114.jpg

Picture116.jpg

日赚1%的话,30天一共赚34%,70天翻倍。挺好挺好,都算对了。这就算是这个小程序的升级2.0版吧。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  




This post has been voted on by the SteemSTEM curation team and voting trail in collaboration with @curie.

If you appreciate the work we are doing then consider voting both projects for witness by selecting stem.witness and curie!

For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

Congratulations @softmetal! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published a post every day of the week

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:

Christmas Challenge - The party continues

Support SteemitBoard's project! Vote for its witness and get one more award!

請問c++網站的圖片可以免費供商業使用嗎?

·

既然有疑问,那就把图片删掉好了。

吃了吗?快来使用超级好用的steemit客户端---Partiko,这个可是我们华人团队开发的哦。假如我的留言打扰到你,请回复“取消”。

吃了吗?这是哪里?你是谁?我为什么会来这边?你不要给我点赞不要点赞,哈哈哈哈哈哈。假如我的留言打扰到你,请回复“取消”。

Hi @softmetal!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 2.746 which ranks you at #13107 across all Steem accounts.
Your rank has improved 102 places in the last three days (old rank 13209).

In our last Algorithmic Curation Round, consisting of 235 contributions, your post is ranked at #161.

Evaluation of your UA score:
  • Only a few people are following you, try to convince more people with good work.
  • The readers appreciate your great work!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server