无聊的时候写的,感觉还是有点用的
#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;
}