一个模拟纳指stock的小例子:
#include <stdio.h>
#include <stdlib.h>#define MAX_YEAR 100
#define ONE_YEAR_DAYS 240#define BEGIN_YUAN 10000
#define ONE_ADD_YUAN 4000
#define ONE_ADD_DAY 20
#define GOLD_FLOW_TIMES 3
#define GOLD_ADD_TIMES 1#define GOLD_POS 0.618// 2000 年开始
int arrBgn[100] = {0};int main(int argc, char* argv[])
{arrBgn[0] = 4000;arrBgn[1] = 3000;arrBgn[2] = 2000;arrBgn[3] = 1000;arrBgn[4] = 2000;arrBgn[5] = 2100;arrBgn[6] = 2300;arrBgn[7] = 2500;arrBgn[8] = 2400;arrBgn[9] = 1400;arrBgn[10] = 2200;arrBgn[11] = 2600;arrBgn[12] = 2600;arrBgn[13] = 3100;arrBgn[14] = 4100;arrBgn[15] = 4500;arrBgn[16] = 4300;arrBgn[17] = 5300;arrBgn[18] = 7400;arrBgn[19] = 6600;arrBgn[20] = 9100;arrBgn[21] = 9100;int iOwnYuan = BEGIN_YUAN;float iTotalYuan = BEGIN_YUAN;int iYearNum = MAX_YEAR;int iOneAddYuan = ONE_ADD_YUAN;int iOndAddDayNum = ONE_ADD_DAY;if (2 == argc){iYearNum = atoi(argv[1]);}else if (3 == argc){iYearNum = atoi(argv[1]);iOneAddYuan = atoi(argv[2]);}else if (4 == argc){iYearNum = atoi(argv[1]);iOneAddYuan = atoi(argv[2]);iOndAddDayNum = atoi(argv[3]);}printf("-----------------------------stock------------------------------------\n");printf("stock.exe 1 1000 5 -----------\n");printf("iBeginYuan:%d \n", BEGIN_YUAN);printf("iYearNum:%d\n", iYearNum);printf("iOneAddYuan:%d ", iOneAddYuan);printf("iOndAddDayNum:%d\n", iOndAddDayNum);printf("------------------------------------------------------------------------\n");// 算法1//for (int i = 0; i < iYearNum; i++)//{// // if (0 != arrBgn[i+1])// {// float iAdd = arrBgn[i+1] - arrBgn[i];// float fFlow = (iAdd *100 / ONE_YEAR_DAYS)/arrBgn[i];//// for (int j = 0; j < ONE_YEAR_DAYS; j++)// {// if (0 == j % iOndAddDayNum)// {// for (int k = 0; k < iOndAddDayNum; k++)// {// iTotalYuan = iTotalYuan + iTotalYuan *fFlow / 100;// }//// iTotalYuan += iOneAddYuan;// iOwnYuan += iOneAddYuan;// }// }//// iTotalYuan = iTotalYuan * 99 /100;//// printf("year:%d; iTotalYuan:%d, iOwnYuan:%d, rate:%d!\n\n", i+1, iTotalYuan, iOwnYuan, (iTotalYuan-iOwnYuan)*100/iOwnYuan);// //// }//}// 算法2for (int i = 0; i < iYearNum; i++){if (0 != arrBgn[i + 1]){float iAdd = arrBgn[i + 1] - arrBgn[i];float fFlow = (iAdd * 100 / ONE_YEAR_DAYS) / arrBgn[i];int iGoldDay = ONE_YEAR_DAYS * GOLD_POS;float iGoldAdd = iAdd / GOLD_POS;float fGoldFlow = (iGoldAdd * 1000 / iGoldDay) / arrBgn[i];int nGoldtime = 0;float iGoldAdd2 = arrBgn[i + 1] - (iGoldAdd + arrBgn[i]);float fGoldFlow2 = (iGoldAdd2 * 1000 / (ONE_YEAR_DAYS- iGoldDay)) / arrBgn[i];int nGoldtime2 = 0;int iPreOwnYuan = iOwnYuan;for (int j = 0; j < iGoldDay; j++){if (0 == j % iOndAddDayNum){for (int k = 0; k < iOndAddDayNum; k++){iTotalYuan = iTotalYuan + iTotalYuan *fGoldFlow / 1000;}if (fGoldFlow < 0){iTotalYuan += iOneAddYuan * GOLD_FLOW_TIMES;iOwnYuan += iOneAddYuan * GOLD_FLOW_TIMES;}else{iTotalYuan += iOneAddYuan * GOLD_ADD_TIMES;iOwnYuan += iOneAddYuan * GOLD_ADD_TIMES;} nGoldtime++;}}printf("yearPos:%d: iTotalYuan:%0.f, iOwnYuan:%d, rate:%0.1f,flow1:%.1f, flow2:%.1f, add:%d!\n", i + 1, iTotalYuan, iOwnYuan, (iTotalYuan - iOwnYuan) * 100 / iOwnYuan, fGoldFlow, fGoldFlow2, iOwnYuan - iPreOwnYuan);iPreOwnYuan = iOwnYuan;for (int j = iGoldDay; j < ONE_YEAR_DAYS; j++){if (0 == j % iOndAddDayNum){for (int k = 0; k < iOndAddDayNum; k++){iTotalYuan = iTotalYuan + iTotalYuan *fGoldFlow2 / 1000;}if (fGoldFlow < 0){iTotalYuan += iOneAddYuan * GOLD_FLOW_TIMES;iOwnYuan += iOneAddYuan * GOLD_FLOW_TIMES;}else{iTotalYuan += iOneAddYuan * GOLD_ADD_TIMES;iOwnYuan += iOneAddYuan * GOLD_ADD_TIMES;}}nGoldtime2++;}iTotalYuan = iTotalYuan * 99 / 100;printf("year:%d: iTotalYuan:%0.0f, iOwnYuan:%d, rate:%0.1f, add:%d, bgn:%d, end:%d,\n\n", i + 1, iTotalYuan, iOwnYuan, (iTotalYuan - iOwnYuan) * 100 / iOwnYuan, iOwnYuan - iPreOwnYuan, arrBgn[i], arrBgn[i + 1]);}}// 算法3//for (int i = 0; i < iYearNum; i++)//{//// if (0 != arrBgn[i + 1])// {// float iAdd = arrBgn[i + 1] - arrBgn[i];// float fFlow = (iAdd * 100 / ONE_YEAR_DAYS) / arrBgn[i];//// int iGoldDay = ONE_YEAR_DAYS * GOLD_POS;// float iGoldAdd = iAdd / GOLD_POS;// float fGoldFlow = (iGoldAdd * 1000 / iGoldDay) / arrBgn[i];// int nGoldtime = 0;//// float iGoldAdd2 = arrBgn[i + 1] - (iGoldAdd + arrBgn[i]);// float fGoldFlow2 = (iGoldAdd2 * 1000 / (ONE_YEAR_DAYS - iGoldDay)) / arrBgn[i];// int nGoldtime2 = 0;////// int nFlowAddYuan = iOneAddYuan;// bool bFlowAddYuan = false;////// for (int j = 0; j < iGoldDay; j++)// {// if (0 == j % iOndAddDayNum)// {// for (int k = 0; k < iOndAddDayNum; k++)// {// iTotalYuan = iTotalYuan + iTotalYuan *fGoldFlow / 1000;// }//// if (fGoldFlow < 0)// {// iTotalYuan += nFlowAddYuan;// iOwnYuan += nFlowAddYuan;// bFlowAddYuan = true;// }// else// {// iTotalYuan += iOneAddYuan * GOLD_ADD_TIMES;// iOwnYuan += iOneAddYuan * GOLD_ADD_TIMES;// bFlowAddYuan = false;// }// nGoldtime++;//// if (bFlowAddYuan)// {// if (nFlowAddYuan <= 2000)// {// nFlowAddYuan = 2000;// }// else if (nFlowAddYuan <= 3000)// {// nFlowAddYuan = 3000;// }// else if (nFlowAddYuan <= 5000)// {// nFlowAddYuan = 5000;// }// else// {// nFlowAddYuan = iOneAddYuan;// } // }// else// {// nFlowAddYuan = iOneAddYuan;// }//// }// }//// printf("yearPos:%d; iTotalYuan:%0.f, iOwnYuan:%d, rate:%0.1f,flow1:%.1f, flow2:%.1f!\n", i + 1, iTotalYuan, iOwnYuan, (iTotalYuan - iOwnYuan) * 100 / iOwnYuan, fGoldFlow, fGoldFlow2);//// for (int j = iGoldDay; j < ONE_YEAR_DAYS; j++)// {// if (0 == j % iOndAddDayNum)// {// for (int k = 0; k < iOndAddDayNum; k++)// {// iTotalYuan = iTotalYuan + iTotalYuan *fGoldFlow2 / 1000;// }//// if (fGoldFlow < 0)// {// iTotalYuan += nFlowAddYuan;// iOwnYuan += nFlowAddYuan;// bFlowAddYuan = true;// }// else// {// iTotalYuan += iOneAddYuan * GOLD_ADD_TIMES;// iOwnYuan += iOneAddYuan * GOLD_ADD_TIMES;// bFlowAddYuan = false;// }//// }// nGoldtime2++;//// if (bFlowAddYuan)// {// if (nFlowAddYuan <= 2000)// {// nFlowAddYuan = 2000;// }// else if (nFlowAddYuan <= 3000)// {// nFlowAddYuan = 3000;// }// else if (nFlowAddYuan <= 5000)// {// nFlowAddYuan = 5000;// }// else// {// nFlowAddYuan = iOneAddYuan;// }// }// else// {// nFlowAddYuan = iOneAddYuan;// }// }//// iTotalYuan = iTotalYuan * 99 / 100;//// printf("year:%d; iTotalYuan:%0.0f, iOwnYuan:%d, rate:%0.1f!\n\n", i + 1, iTotalYuan, iOwnYuan, (iTotalYuan - iOwnYuan) * 100 / iOwnYuan);////// }//}getchar();
return 0;
}
运行结果如下:
-----------------------------stock------------------------------------
stock.exe 1 1000 5 -----------
iBeginYuan:10000
iYearNum:100
iOneAddYuan:4000 iOndAddDayNum:20
------------------------------------------------------------------------
yearPos:1: iTotalYuan:86340, iOwnYuan:106000, rate:-18.5,flow1:-2.7, flow2:1.7, add:96000!
year:1: iTotalYuan:147768, iOwnYuan:154000, rate:-4.0, add:48000, bgn:4000, end:3000,yearPos:2: iTotalYuan:157785, iOwnYuan:250000, rate:-36.9,flow1:-3.6, flow2:2.2, add:96000!
year:2: iTotalYuan:237703, iOwnYuan:298000, rate:-20.2, add:48000, bgn:3000, end:2000,yearPos:3: iTotalYuan:166375, iOwnYuan:394000, rate:-57.8,flow1:-5.5, flow2:3.4, add:96000!
year:3: iTotalYuan:268097, iOwnYuan:442000, rate:-39.3, add:48000, bgn:2000, end:1000,yearPos:4: iTotalYuan:1604507, iOwnYuan:474000, rate:238.5,flow1:10.9, flow2:-6.7, add:32000!
year:4: iTotalYuan:939397, iOwnYuan:490000, rate:91.7, add:16000, bgn:1000, end:2000,yearPos:5: iTotalYuan:1058497, iOwnYuan:522000, rate:102.8,flow1:0.5, flow2:-0.3, add:32000!
year:5: iTotalYuan:1035802, iOwnYuan:538000, rate:92.5, add:16000, bgn:2000, end:2100,yearPos:6: iTotalYuan:1257928, iOwnYuan:570000, rate:120.7,flow1:1.0, flow2:-0.6, add:32000!
year:6: iTotalYuan:1198725, iOwnYuan:586000, rate:104.6, add:16000, bgn:2100, end:2300,yearPos:7: iTotalYuan:1429801, iOwnYuan:618000, rate:131.4,flow1:1.0, flow2:-0.6, add:32000!
year:7: iTotalYuan:1366413, iOwnYuan:634000, rate:115.5, add:16000, bgn:2300, end:2500,yearPos:8: iTotalYuan:1367174, iOwnYuan:730000, rate:87.3,flow1:-0.4, flow2:0.3, add:96000!
year:8: iTotalYuan:1430819, iOwnYuan:778000, rate:83.9, add:48000, bgn:2500, end:2400,yearPos:9: iTotalYuan:760419, iOwnYuan:874000, rate:-13.0,flow1:-4.6, flow2:2.8, add:96000!
year:9: iTotalYuan:993269, iOwnYuan:922000, rate:7.7, add:48000, bgn:2400, end:1400,yearPos:10: iTotalYuan:2742086, iOwnYuan:954000, rate:187.4,flow1:6.2, flow2:-3.8, add:32000!
year:10: iTotalYuan:2009748, iOwnYuan:970000, rate:107.2, add:16000, bgn:1400, end:2200,yearPos:11: iTotalYuan:2798365, iOwnYuan:1002000, rate:179.3,flow1:2.0, flow2:-1.2, add:32000!
year:11: iTotalYuan:2527575, iOwnYuan:1018000, rate:148.3, add:16000, bgn:2200, end:2600,yearPos:12: iTotalYuan:2559575, iOwnYuan:1050000, rate:143.8,flow1:0.0, flow2:0.0, add:32000!
year:12: iTotalYuan:2549820, iOwnYuan:1066000, rate:139.2, add:16000, bgn:2600, end:2600,yearPos:13: iTotalYuan:3605498, iOwnYuan:1098000, rate:228.4,flow1:2.1, flow2:-1.3, add:32000!
year:13: iTotalYuan:3233944, iOwnYuan:1114000, rate:190.3, add:16000, bgn:2600, end:3100,yearPos:14: iTotalYuan:5721794, iOwnYuan:1146000, rate:399.3,flow1:3.5, flow2:-2.2, add:32000!
year:14: iTotalYuan:4776810, iOwnYuan:1162000, rate:311.1, add:16000, bgn:3100, end:4100,yearPos:15: iTotalYuan:5699752, iOwnYuan:1194000, rate:377.4,flow1:1.1, flow2:-0.7, add:32000!
year:15: iTotalYuan:5369920, iOwnYuan:1210000, rate:343.8, add:16000, bgn:4100, end:4500,yearPos:16: iTotalYuan:5060957, iOwnYuan:1306000, rate:287.5,flow1:-0.5, flow2:0.3, add:96000!
year:16: iTotalYuan:5179411, iOwnYuan:1354000, rate:282.5, add:48000, bgn:4500, end:4300,yearPos:17: iTotalYuan:7814111, iOwnYuan:1386000, rate:463.8,flow1:2.5, flow2:-1.6, add:32000!
year:17: iTotalYuan:6841427, iOwnYuan:1402000, rate:388.0, add:16000, bgn:4300, end:5300,yearPos:18: iTotalYuan:13706282, iOwnYuan:1434000, rate:855.8,flow1:4.3, flow2:-2.7, add:32000!
year:18: iTotalYuan:10977905, iOwnYuan:1450000, rate:657.1, add:16000, bgn:5300, end:7400,yearPos:19: iTotalYuan:9173795, iOwnYuan:1546000, rate:493.4,flow1:-1.2, flow2:0.7, add:96000!
year:19: iTotalYuan:9673800, iOwnYuan:1594000, rate:506.9, add:48000, bgn:7400, end:6600,yearPos:20: iTotalYuan:18783782, iOwnYuan:1626000, rate:1055.2,flow1:4.1, flow2:-2.5, add:32000!
year:20: iTotalYuan:15181152, iOwnYuan:1642000, rate:824.6, add:16000, bgn:6600, end:9100,yearPos:21: iTotalYuan:15213152, iOwnYuan:1674000, rate:808.8,flow1:0.0, flow2:0.0, add:32000!
year:21: iTotalYuan:15076860, iOwnYuan:1690000, rate:792.1, add:16000, bgn:9100, end:9100,