【问题标题】:Functions different datatypes calculate rent函数不同的数据类型计算租金
【发布时间】:2017-09-05 20:06:21
【问题描述】:

试图制作一个函数来计算总和与租金的起始金额。 我的 printf("%.1f", calcFutureValue[i]);出于某种原因不想工作,如果它与不同的数据类型有关,我进行了查找,因为 numberOfYears 是一个 int 但不确定。 我错过了什么?

#include <stdio.h>
#include <math.h>

double calcFutureValue(double startingAmount, double interest, int numberOfYears);


int main()
{
    double startingAmount = 10000;
    double interest = 1.045;
    int numberOfYears = 3;

    calcFutureValue(startingAmount, interest, 3);


    for (int i = 0; i < numberOfYears; i++)
    {
        printf("%.1f", calcFutureValue[i]);
    }
    getchar();

    return 0;
}


double calcFutureValue(double startingAmount, double interest, int numberOfYears)
{
    for (int i = 0; i < numberOfYears; i++)
    {
        startingAmount * interest * pow(numberOfYears, 2);
    }
    getchar();
}

【问题讨论】:

  • 您学习 C 的资源对您不利。您似乎对程序流程的工作方式感到困惑。
  • printf("%.1f", calcFutureValue(startingAmount, interest, i + 1)); 代替。您应该了解函数的工作原理和其他基本知识。
  • 没有足够的参数......当然,这不能编译?
  • calcFutureValue() 是一个函数。尝试像使用 calcFutureValue[i] 的数组一样使用它的代码是什么? “我的 printf("%.1f", calcFutureValue[i]); 出于某种原因不想工作”,因为代码不是合法的 C 代码。发布您的编译器错误,而不是使用诸如“出于某种原因不想工作”之类的不确定描述

标签: c function types double


【解决方案1】:

试试这个代码:

 #include <stdio.h>
 #include <math.h>

 double calcFutureValue( int numberOfYears);

 const double startingAmount = 10000;
 const double interest = 1.045;

 int main()
 {
     int numberOfYears = 3;
     int i;

    for (i = 0; i < numberOfYears; i++)
    {
        printf("%.2f\n", calcFutureValue(i));
    }

    return 0;
 }


 double calcFutureValue(int numberOfYears)
 {
    return startingAmount * pow(interest, numberOfYears);
 }

说明: 复利公式有误。 由于这是处理货币,因此小数宽度为 2 是合适的。 由于startingAmount 和interest 是常量,它们可以是常量全局变量。

【讨论】:

  • 谢谢 Nguai al,我会尝试更多地尝试以找到一些计算方法。谢谢
猜你喜欢
  • 2013-04-27
  • 1970-01-01
  • 2022-06-24
  • 2011-06-27
  • 2018-02-21
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多