| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 876 人关注过本帖
标题:无聊的时候写的,感觉还是有点用的
取消只看楼主 加入收藏
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
结帖率:100%
收藏
 问题点数:0 回复次数:1 
无聊的时候写的,感觉还是有点用的
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
    double
        depAcct = 0,
        lnAcct = 400000,   /*贷款总额*/
        ln_rate = 0.06426; /*贷款年利息*/
    int i, j,
        period = 240,      /*期数*/
        rfn_sty = 1;       /*还贷方式0等额本金,1等额本息*/
    double
        *lnAcctbal,        /*贷款余额*/
        *depAcctbal,       /*总还款*/
        *payrateAcct,      /*每期应还利息*/
        *payAcct,          /*每期应还款*/
        *paybaseAcct;      /*每期应还本金*/
    double tlnAcct, tdepAcct;
    FILE *fp;

    lnAcctbal = (double *)malloc((period + 1)*sizeof(double));
    depAcctbal = (double *)malloc((period + 1)*sizeof(double));
    payrateAcct = (double *)malloc((period + 1)*sizeof(double));
    payAcct = (double *)malloc((period + 1)*sizeof(double));
    paybaseAcct = (double *)malloc((period + 1)*sizeof(double));

    tdepAcct = depAcct;
    tlnAcct = lnAcct;

    /*等额本金还贷*/
    for(i = 0; i < period; i ++)
    {
        paybaseAcct[i] = 1/(double)period*lnAcct;
        payrateAcct[i] = tlnAcct*ln_rate/12;
        payAcct[i] = paybaseAcct[i] + payrateAcct[i];
        lnAcctbal[i] = (1 - ((double)(i + 1))/(double)period)*lnAcct;
        depAcctbal[i] = tdepAcct + payAcct[i];
        tlnAcct = lnAcctbal[i];
        tdepAcct = tdepAcct + payAcct[i];
    }

    if((fp = fopen("等额本金.txt", "w")) == NULL)
    {
        printf("打开文件失败\n");
        free(lnAcctbal);
        free(depAcctbal);
        free(payrateAcct);
        free(payAcct);
        free(paybaseAcct);
        return -1;
    }

    fprintf(fp, "等额本息还贷:总金额%.2lf,年利%.6lf,期数%d(%d年)\n",
        lnAcct, ln_rate, period, period/12);
    fprintf(fp,"%-8s%-12s%-12s%-12s%-12s%-12s\n",
        "期数", "贷款余额", "还本金", "还利息", "本期还款","总还款");
    for(j = 0; j < i; j++)
    {
        fprintf(fp,"%-8d%-12.02lf%-12.2lf%-12.02lf%-12.2lf%-12.02lf\n",
            j+1, lnAcctbal[j], paybaseAcct[j], payrateAcct[j], payAcct[j],depAcctbal[j]);
    }
    fclose(fp);

    tdepAcct = depAcct;
    tlnAcct = lnAcct;

    /*等额本息还贷*/
    for(i = 0; i < period; i ++)
    {
        paybaseAcct[i] = (pow((1 + ln_rate/12), i+1) - pow((1 + ln_rate/12), i))/(pow((1 + ln_rate/12), period) - 1)*lnAcct;
        payrateAcct[i] = tlnAcct*ln_rate/12;
        payAcct[i] = paybaseAcct[i] + payrateAcct[i];
        lnAcctbal[i] = tlnAcct - paybaseAcct[i];
        depAcctbal[i] = tdepAcct + payAcct[i];
        tdepAcct = depAcctbal[i];
        tlnAcct = tlnAcct - paybaseAcct[i];
    }

    if((fp = fopen("等额本息.txt", "w")) == NULL)
    {
        printf("打开文件失败\n");
        free(lnAcctbal);
        free(depAcctbal);
        free(payrateAcct);
        free(payAcct);
        free(paybaseAcct);
        return -1;
    }

    fprintf(fp, "等额本息还贷:总金额%.2lf,年利%.6lf,期数%d(%d年)\n",
        lnAcct, ln_rate, period, period/12);
    fprintf(fp,"%-8s%-12s%-12s%-12s%-12s%-12s\n",
        "期数", "贷款余额", "还本金", "还利息", "本期还款","总还款");
    for(j = 0; j < i; j++)
    {
        fprintf(fp,"%-8d%-12.02lf%-12.2lf%-12.02lf%-12.2lf%-12.02lf\n",
            j+1, lnAcctbal[j], paybaseAcct[j], payrateAcct[j], payAcct[j],depAcctbal[j]);
    }
    fclose(fp);
    free(lnAcctbal);
    free(depAcctbal);
    free(payrateAcct);
    free(payAcct);
    free(paybaseAcct);
    printf("Create over!\n");

    return 0;
}
搜索更多相关主题的帖子: 贷款 利息 double int include 
2008-07-04 17:45
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
收藏
得分:0 

只有想不到,没有做不到
2008-07-04 18:08
快速回复:无聊的时候写的,感觉还是有点用的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018245 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved