【问题标题】:Varied interest rate calculator可变利率计算器
【发布时间】:2014-02-03 23:26:00
【问题描述】:

嘿,我是编码新手,想知道你们是否可以帮助我计算不同的利率,然后将它们添加到下一个利率。所以基本上我试图获得利率 A 并将其添加到 100 的起始值。然后我想获得 100 的利率 B 并将该值添加到利息 A。到目前为止,这是我的代码,但我得到了 10 行对于每个利率。抱歉,如果这听起来令人困惑,但希望代码能让它更清晰,或者如果阅读本文的人愿意,我可以尝试更好地解释。谢谢!!

int intv;

cout << " Accumulate interest on a savings account. ";
cout << " Starting value is $100 and interest rate is 1.25% ";

cout << endl;

intv = 100;
index = 1;

while ( index <= 10 )

{
    cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.27% for a total of " << .0127 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.28% for a total of " << .0128 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.30% for a total of " << .0130 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.31% for a total of " << .0131 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.32% for a total of " << .0132 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.35% for a total of " << .0135 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.36% for a total of " << .0136 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.38% for a total of " << .0138 * intv + intv << "." << endl;
    cout << " Year " << index << " adds 1.40% for a total of " << .0140 * intv + intv << "." << endl;

    index = index + 1;

}

我只需要提示,而不是为我这样做。我想自己解决这个问题,但我不知道我必须做什么。

程序的理想结果是给我这个:

第 1 年增加 1.25,总共 101.25 第 2 年增加 1.27,总共 102.52 第 3 年增加 1.28,总计 103.80 第 4 年增加 1.30,总共 105.09 第 5 年增加 1.31,总计 106.41 第 6 年增加 1.33,总共 107.74 第 7 年增加 1.35,总共 109.09 第 8 年增加 1.36,总共 110.45 第 9 年增加 1.38,总计 111.83 第 10 年增加 1.40,总计 113.23

贷记的总利息为 13.23

【问题讨论】:

  • 为您的问题添加更多标签。我不确定这是什么语言,所以这将是一个有用的标签! ;-)
  • 对不起。它在 C++ 中,我正在使用 Visual Studio 2012
  • 没有问题!标签只是帮助您获得更多回复,因为人们喜欢查看他们擅长的问题。
  • 如果你给我一个你想要的输出示例,我敢打赌我几乎可以为你编写代码。 (虽然我不懂 C++!)
  • 好吧,我们可以帮助您解决您遇到的任何问题,但我们不会为您解决您的作业。试一试,当你遇到一些错误时编辑你的帖子

标签: c++ calculator


【解决方案1】:

听起来你可以使用for 循环:

double rate = 0.125;

for (unsigned int index = 0; index < max_rates; ++index)
{
    cout << " Year " << index << " adds "
         << (rate * 100.0)
         << "% for a total of "
         << rate * intv + intv << "." << endl;
    rate += 0.002;
}

【讨论】:

  • 好吧,它似乎只适用于一种利率。有没有办法让它计算不同的费率,这样我就不必拥有此代码的多个副本?
  • 您可以将多个感兴趣的值存储在一个数组中,并使用另一个 for 循环对所有兴趣重复此操作
【解决方案2】:

你需要使用一个函数来替换

cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl; 

该函数可以将索引转换为附加值

double foo(int index);

输入值为'index',输出值为1.25%、1.38%等相加值

然后删除所有 cout 行。只需添加这一行:

cout << " Year " << index << " adds " << foo(index) * 100.0 << "% for a total of " << foo(index) * intv + intv << "." << endl; 

我想这就是你想要的。

【讨论】:

  • 好吧,它工作了,但我想我现在在某个地方写错了,因为它说“第 1 年增加了 .000125%,总共 101.25。谢谢大家!!
  • 抱歉,出现错误。将 'foo(index) / 100.0' 更改为 ' foo(index) * 100.0'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
相关资源
最近更新 更多