- 论坛徽章:
- 0
|
网上查了,没有太好用的。银行给出的在线计算都要输入利息,这个要求属于扯淡。
这个自动计算要输入的是本金,存入日期,存入期限(活期,死期0.5年,1,2,3,5年)
和取款日期。
如果不输入取款日期,假设今天取款。
如果输入D为取款日期,则假设以后到期了取款。
我实验了,基本正确。
只能计算活期,或整存整取,不能计算其它灵活的存取类型。- //bank interest auto calculation.
- //Coding by SEEKER. 2011.12.15
- #include <stdio.h>
- #include <float.h>
- #define _XOPEN_SOURCE /* glibc2 needs this */
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- char *intrests = "\
- 06/10/1999 0.99 2.16 2.25 2.43 2.7 2.88 \n\
- 02/21/2002 0.72 1.89 1.98 2.25 2.52 2.79 \n\
- 10/29/2004 0.72 2.07 2.25 2.7 3.24 3.6 \n\
- 08/19/2006 0.72 2.25 2.52 3.06 3.69 4.14 \n\
- 03/18/2007 0.72 2.43 2.79 3.33 3.96 4.41 \n\
- 05/19/2007 0.72 2.61 3.06 3.69 4.41 4.95 \n\
- 07/21/2007 0.81 2.88 3.33 3.96 4.68 5.22 \n\
- 08/22/2007 0.81 3.15 3.6 4.23 4.95 5.49 \n\
- 09/15/2007 0.81 3.42 3.87 4.5 5.22 5.76 \n\
- 12/21/2007 0.72 3.78 4.14 4.68 5.4 5.85 \n\
- 10/09/2008 0.72 3.51 3.87 4.41 5.13 5.58 \n\
- 10/30/2008 0.72 3.24 3.6 4.14 4.77 5.13 \n\
- 11/27/2008 0.36 2.25 2.52 3.06 3.6 3.87 \n\
- 12/23/2008 0.36 1.98 2.25 2.79 3.33 3.6 \n\
- 10/20/2010 0.36 2.2 2.5 3.25 3.85 4.2 \n\
- 12/26/2010 0.36 2.5 2.75 3.55 4.15 4.55 \n\
- 02/09/2011 0.4 2.8 3 3.9 4.5 5 \n\
- 04/06/2011 0.5 3.05 3.25 4.15 4.75 5.25 \n\
- 07/07/2011 0.5 3.3 3.5 4.4 5 5.5 \n\
- ";
- struct intr_list {
- time_t from, to;
- float intr_temp;
- float intr_half;
- float intr_y1;
- float intr_y2;
- float intr_y3;
- float intr_y5;
- } intr_list[20];
- int intr_max;
- char *yasctime(struct tm *t)
- {
- static char buf[32];
- sprintf(buf, "%d-%d-%d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
- return buf;
- /*
- char *tp;
- char *cp = asctime(t);
- tp = strrchr(cp, '\n');
- if(tp) *tp = 0;
- return cp;
- */
- }
- time_t addtime(time_t t, float length)
- {
- struct tm tm;
-
- tm = *localtime(&t);
- if(length >= 1.0) {
- tm.tm_year += length;
- } else if(length == 0.5) {
- tm.tm_year += tm.tm_mon/6; //0-5 keep old, 6-11 inc
- tm.tm_mon += 6;
- tm.tm_mon %= 12;
- }
- return mktime(&tm);
- }
- float INTR_TAX = 0; //0.05 previously
- float deposit_rmb;
- time_t deposit_time;
- float deposit_period;
- time_t withdraw_time; //if empty, today
- float get_interest(time_t tm, float p)
- {
- int i;
- if(tm < intr_list[0].from) {
- printf("error 0. no data\n");
- return 0;
- }
- //if(tm > intr_list[intr_max-1].from) {
- // printf("error 1. no data\n");
- // return 0;
- //}
- if(tm > intr_list[intr_max-1].from) {
- i = intr_max - 1;
- goto get_it;
- }
-
- for(i = 0; i < intr_max; i++) {
- if(tm >= intr_list[i].from && tm < intr_list[i+1].from) {
- get_it:
- if(p == 0.0) return intr_list[i].intr_temp;
- if(p == 0.5) return intr_list[i].intr_half;
- if(p == 1.0) return intr_list[i].intr_y1;
- if(p == 2.0) return intr_list[i].intr_y2;
- if(p == 3.0) return intr_list[i].intr_y3;
- if(p == 5.0) return intr_list[i].intr_y5;
-
- printf("error 2.\n");
- }
- }
- printf("error 3.\n");
- }
- calc_it(time_t dt, float length, time_t wt)
- {
- int last = 0;
- float intr, intr_rmb;
- time_t due_time;
- struct tm tm, *tp;
- float total;
- total = deposit_rmb;
- loop:
- if(last) return;
- //printf(" withdraw %s\n", yasctime(localtime(&wt)));
- // if((unsigned long)dt >= (unsigned long)withdraw_time) {
- //if(difftime(wt, dt) < 0.0) return;
- if(difftime(wt, dt) < 3600*24*4) return;
-
- printf("> start %s %.1f year period:\n", yasctime(localtime(&dt)), length);
- if(length >= 0.5) { //huo qi interest assumed
- if(difftime(wt, dt) < 3600*24*365*length) {
- length = -difftime(wt, dt)/3600/24/365;
- last = 1;
- printf("last flexible peroid. length year = %.2f\n", -length);
- }
- }
- intr = get_interest(dt, length > 0.0? length: 0.0);
- printf("base rmb: %.2f\n", total);
- printf("yearly interest %.2f\n", intr);
- //tm hold this segment's deadline
- tm = *localtime(&dt);
- if(length >= 1.0) {
- tm.tm_year += length;
- } else if(length == 0.5) {
- /*
- if(tm.tm_mon <= 5) //1-6 yue
- tm.tm_mon += 6;
- else {
- tm.tm_mon += 6;
- tm.tm_mon %= 12;
- tm.tm_year++;
- }
- */
- tm.tm_year += tm.tm_mon/6; //0-5 keep old, 6-11 inc
- tm.tm_mon += 6;
- tm.tm_mon %= 12;
- } else if(length < 0.0) { //last huo qi
- length = -length;
- tm = *localtime(&wt);
- } else if(length == 0.0) { //is huo qi
- length = difftime(wt, dt)/3600/24/365;
- tm = *localtime(&wt);
- last = 1;
- printf("flexible peroid. length year = %.2f\n", length);
- } else
- printf("error of length.\n");
-
- due_time = mktime(&tm);
- tp = localtime(&due_time);
- printf("due time %s\n", yasctime(tp));
- intr_rmb = (total*intr/100.0)*length;
- printf("initial interest rmb %.2f\n", intr_rmb);
- if(INTR_TAX > 0.0) {
- printf("after tax interest %.2f\n", intr_rmb*(1 - INTR_TAX));
- intr_rmb = intr_rmb*(1-INTR_TAX);
- }
- total = total + intr_rmb;
- printf("interest gain %.2f\n", intr_rmb);
- printf("base + interest: %.2f\n", total);
- if(last) return;
- printf("-------------------------\n");
- dt = due_time;
- goto loop;
- }
- main(int argc, char **argv)
- {
- char temp[32];
- int i;
- char *cp;
- struct tm tm, *tp, *tp1;
- struct intr_list l;
- if(argv[1] == 0) {
- printf("Usage: \n\
- calc RMBs DEPOSIT_TIME DEPOSIT_LENGTH [WITHDRAW_TIME]\n\
- for example:\n\
- calc 10000 12/30/2009 3 [12/30/2011] or\n\
- calc 10000 2009-12-30 3 2011-12-30\n\
- \n\
- if no WITHDRAW_TIME given, TODAY is assumed.\n\
- if WITHDRAW_TIME is 'd', assuming withdraw will be done in future due time\n");
- return;
- }
- deposit_rmb = atol(argv[1]);
- argv++;
- printf("%s", intrests);
- for(i = 0, cp = intrests; *cp; i++) {
- cp = strchr(cp, '/');
- if(!cp) break;
- cp -= 2;
- strncpy(temp, cp, 10);
- temp[10] = 0;
- memset(&tm, 0, sizeof(struct tm));
- strptime(temp, "%m/%d/%Y", &tm);
- //tm.tm_year -= 1980;
- cp += 10;
- sscanf(cp, "%f %f %f %f %f %f", &l.intr_temp, &l.intr_half, &l.intr_y1, &l.intr_y2, &l.intr_y3, &l.intr_y5);
- //printf("%s %d ---- %f\n", asctime(&tm), tm.tm_year, l.intr_y5);
- l.from = mktime(&tm);
- intr_list[i] = l;
- }
- intr_max = i;
- //get deposit time
- strcpy(temp, argv[1]);
- if(strchr(temp, '-')) {
- strptime(temp, "%Y-%m-%d", &tm);
- } else if(strchr(temp, '/')) {
- strptime(temp, "%m/%d/%Y", &tm);
- } else {
- printf("date format is 2011-11-29 or 11/29/2011.\n");
- return;
- }
- deposit_time = mktime(&tm);
- sscanf(argv[2], "%f", &deposit_period);
-
- //get withdraw time
- if(argv[3]) {
- strcpy(temp, argv[3]);
- strptime(temp, "%m/%d/%Y", &tm);
- if(strchr(temp, '-'))
- strptime(temp, "%Y-%m-%d", &tm);
- withdraw_time = mktime(&tm);
- if(!isdigit(argv[3][0])) { //automatic withdraw at next due time
- int i;
- for(i = 0; i < 16; i++) {
- withdraw_time = addtime(deposit_time, i*deposit_period);
- if(withdraw_time >= time(0)) break;
- }
- printf("will withdraw in %s\n", yasctime(localtime(&withdraw_time)) );
- }
- } else
- withdraw_time = time(0);
- printf("deposit in (yyyy-mm-dd) %s\t", yasctime(localtime(&deposit_time)));
- printf("deposit length (in year) %.1f\t", deposit_period);
- printf("withdraw in %s\n", yasctime(localtime(&withdraw_time)));
- printf("interest: %.2f\n", get_interest(deposit_time, deposit_period));
- calc_it(deposit_time, deposit_period, withdraw_time);
-
- }
复制代码 |
|