c语言中的例题不懂?

Python023

c语言中的例题不懂?,第1张

年金公式(这道题是按月 一样的)D= P*(1-1/(1+R)^m)/R

如果你不知道怎么来的,百度一下‘贷款利息公式’就行了

变形得到(1+R)^M=P/(P-D*R)

两边同时取对数,得到m*log10(1+R)=log10(P/(P-D*R))=log10(P)-log10(P-D*R)

再变形得到m=m=(log10(p)-log10(p-d*r))/log10(1+r)

/*

* main.c

*

* Created on: 2011-6-8

* Author: icelights

*/

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

#define APR10.0747/*<1年(含1年)年利率*/

#define APR20.0756/*1-3年(含3年)年利率*/

#define APR30.0774/*3-5年(含5年)年利率*/

#define APR40.0783/*5年以上年利率*/

#define A_TO_M 1/12 /*月利率 = 年利率 / 12*/

#define RTP 12/*Reimbursement total periods还款总期数 =年限*12*/

#define LENGTH 80

struct LoanInfo

{

/*姓名*/

char name[LENGTH]

/*贷款总额*/

doubleLoanAmount

/*贷款年限*/

doubleLoanYear

/*月付*/

doubleMonthlyPayment

/*总利息*/

doubleTotalInterest

/*还款总额*/

doubleReimbursementAmount

/*年利率*/

doubleapr

struct LoanInfo * next

}

void CalcShow(struct LoanInfo * cur, struct LoanInfo * hd,

struct LoanInfo * prv)

int main(void)

{

int temp

struct LoanInfo * head = NULL

struct LoanInfo * prev, * current

current = (struct LoanInfo *)malloc(sizeof(struct LoanInfo))

if (NULL == head)

{

head = current

}

else

{

prev->next = current

}/*End of if (NULL == head)*/

puts("请输入姓名")

gets(current->name)

fflush(stdin)

puts("请输入贷款数额(单位:万元)")

scanf("%lf", ¤t->LoanAmount)

fflush(stdin)

puts("请输入贷款年限")

scanf("%lf", ¤t->LoanYear)

fflush(stdin)

printf("姓名:%s,贷款年限:%lf, 贷款数额%lf",

current->name, current->LoanYear, current->LoanAmount)

prev = current

puts("请确认Y/N")

temp = getchar()

switch(toupper(temp))

{

case 'Y' : CalcShow(current, head, prev)

break

case 'N' : free(current)

main()

break

default: puts("输入错误")

free(current)

break

}

return 0

}

void CalcShow(struct LoanInfo * cur, struct LoanInfo * hd,

struct LoanInfo * prv)

{

char lcv_temp

if (cur->LoanYear <= 1)

cur->apr = APR1

else if (cur->LoanYear <= 3)

cur->apr = APR2

else if (cur->LoanYear <= 5)

cur->apr = APR3

else

cur->apr = APR4

/*End of if (year <= 1)*/

cur->LoanAmount = 10000 * cur->LoanAmount

cur->ReimbursementAmount = cur->LoanAmount * pow((1 + cur->apr), cur->LoanYear)

cur->MonthlyPayment = cur->ReimbursementAmount / (cur->LoanYear * RTP)

cur->TotalInterest = cur->ReimbursementAmount - cur->LoanAmount

printf("姓名:%s 贷款年限:%.0lf\n"

"贷款数额:%.2lf 每月还款额:%.2lf\n"

"利息合计:%.2lf 还款总额:%.2lf\n",

cur->name, cur->LoanYear, cur->LoanAmount,

cur->MonthlyPayment, cur->TotalInterest, cur->ReimbursementAmount)

puts("是否继续计算Y/N")

lcv_temp = getchar()

switch(toupper(lcv_temp))

{

case 'Y' : free(cur)

main()

break

case 'N' : free(cur)

exit(0)

default: puts("输入错误")

free(cur)

main()

break

}

system("pause")

}

#include <stdio.h>

float cal_power(float x, int n)

{

        float p=1.0

        while(n>0) {

                p=p*x

                n--

        }

        return p

}

float  cal_money(int loan, float rate, int month)

{

        double tmp

        tmp=cal_power(1+rate,month)

        return loan*rate*tmp/(tmp-1)

}

int main(void)

{

        int loan,year,month

        float money,rate

        printf("Enter loan: ")

        scanf("%d",&loan)

        printf("Enter rate: ")

        scanf("%f",&rate)

        for(year=5year<=30year++) {

                month=12*year

                money=cal_money(loan,rate,month)

                printf("money(%d,%d)=%.0f\n",loan,year,money)

        }

        return 0

}