当前位置: 移动技术网 > IT编程>开发语言>c# > 房贷计算器代码2.0

房贷计算器代码2.0

2019年10月26日  | 移动技术网IT编程  | 我要评论
  1 using system;
  2 using system.collections.generic;
  3 using system.linq;
  4 using system.text;
  5 using system.threading.tasks;
  6 using system.drawing;
  7 using system.reflection;
  8 using system.net.networkinformation;
  9 
 10 namespace play
 11 {
 12     class program
 13     {
 14         //短期利率与长期利率
 15         double lowrate = 0;
 16         double highrate = 0;
 17 
 18         //keyvaluepair<datetime, >;
 19         datetime dt1 = new datetime(2015, 3, 1);
 20         datetime dt2 = new datetime(2015, 5, 11);
 21         datetime dt3 = new datetime(2015, 6, 28);
 22         datetime dt4 = new datetime(2015, 8, 26);
 23         //贷款金额
 24         static double totalmoney, balance;
 25 
 26         static string choice;
 27         static string newchoice;
 28         //期数
 29         int n;
 30         //月供
 31         double paymonth;
 32         double rate;
 33         double interest;
 34         /// <summary>
 35         /// 用来构建欢迎界面
 36         /// </summary>
 37         void paint()
 38         {
 39             console.writeline("***************************");
 40             console.writeline("***************************");
 41             console.writeline("***************************");
 42             console.writeline("欢迎使用房贷计算器");
 43             console.writeline("***************************");
 44             console.writeline("***************************");
 45             console.writeline("***************************");
 46 
 47             console.writeline("请按任意键继续");
 48             console.readkey();
 49             console.clear();
 50         }
 51         /// <summary>
 52         /// 按日期选择利率
 53         /// </summary>
 54         /// <param name="dt">输入的日期</param>
 55         void ratechoose(datetime dt)
 56         {
 57             if (dt >= dt4)
 58             {
 59                 lowrate = 0.0275;
 60                 highrate = 0.0325;
 61             }
 62             else if (dt >= dt3)
 63             {
 64                 lowrate = 0.03;
 65                 highrate = 0.035;
 66             }
 67             else if (dt >= dt2)
 68             {
 69                 lowrate = 0.0325;
 70                 highrate = 0.0375;
 71             }
 72             else if (dt >= dt1)
 73             {
 74                 lowrate = 0.035;
 75                 highrate = 0.04;
 76             }
 77             else
 78             {
 79                 console.writeline("你输入的日期不在范围内");
 80                 console.readkey();
 81                 environment.exit(0);
 82             }
 83         }
 84         /// <summary>
 85         /// 计算月供
 86         /// </summary>
 87         void payment(datetime dt)
 88         {
 89             //设置缓冲区高度,即设置显示的最大行数
 90             console.bufferheight = 1000;
 91             ratechoose(dt);
 92             switch (choice)
 93             {
 94                 case "1":
 95                     {
 96                         //等额本息,月供计算方法
 97                         for (n = 1; n <= 360; n++)
 98                         {
 99 
100 
101                             if (n <= 60)
102                             {
103                                 rate = lowrate;
104                             }
105                             else
106                             {
107                                 rate = highrate;
108                             }
109                             //计算月供
110                             double p;
111                             p = rate / 12;
112                             paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
113                             //利息
114                             interest = n * paymonth;
115                             if (n % 2 == 0)
116                             {
117                                 console.foregroundcolor = consolecolor.blue;
118                             }
119                             else
120                             {
121                                 console.foregroundcolor = consolecolor.white;
122                             }
123                             console.writeline("{0}期按揭贷款的月供是{1:0.00}元,总利息是{2:0.00}元", n, paymonth, interest);
124 
125                         }
126                     }
127                     break;
128                 case "2":
129                     {
130                         //等额本金,月供计算方法
131                         for (n = 1; n <= 360; n++)
132                         {
133                             if (n <= 60)
134                             {
135                                 rate = lowrate;
136                             }
137                             else
138                             {
139                                 rate = highrate;
140                             }
141                             //计算月供
142                             double p;
143                             p = rate / 12;
144                             paymonth = (totalmoney / n) + totalmoney * p;
145                             interest = ((totalmoney / n + totalmoney * p) + totalmoney / n * (1 + p)) / 2 * n - totalmoney;
146                             console.writeline("{0}期按揭贷款的首月还款额是{1:0.00}元,总利息是{2:0.00}元", n, paymonth, interest);
147                         }
148                     }
149                     break;
150             }
151         }
152 
153         /// <summary>
154         /// 月贷计算方法
155         /// </summary>
156         /// <param name="n">还款期数</param>
157         void compute(int n)
158         {
159             console.writeline("期次\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
160             switch (choice)
161             {
162 
163                 case "1":
164                     //等额本息法计算房贷
165                     {
166                         if (n <= 60)
167                         {
168                             rate = lowrate;
169                         }
170                         else
171                         {
172                             rate = highrate;
173                         }
174 
175                         double p;
176                         p = rate / 12;
177                         paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
178                         for (int i = 1; i <= n; i++)
179                         {
180                             //string s = i.tostring();
181                             interest = totalmoney * p;
182                             double capital = paymonth - interest;
183                             totalmoney -= capital;
184                             console.writeline("{0}\t\t{1:0.00}\t\t{2:0.00}\t{3:0.00}\t{4:0.00}", i, interest, capital, paymonth, totalmoney);
185                         }
186                     }
187                     break;
188                 case "2":
189                     //等额本金法计算房贷
190                     {
191                         if (n <= 60)
192                         {
193                             rate = lowrate;
194                         }
195                         else
196                         {
197                             rate = highrate;
198                         }
199                         double p;
200                         p = rate / 12;
201                         double capital = totalmoney / n;
202                         for (int i = 1; i <= n; i++)
203                         {
204 
205                             interest = totalmoney * p;
206                             paymonth = capital + interest;
207                             totalmoney -= capital;
208 
209                             console.writeline("{0}\t\t{1:0.00}\t\t\t{2:0.00}\t\t{3:0.00}\t{4:0.00}", i, interest, capital, paymonth, totalmoney);
210                         }
211                     }
212                     break;
213 
214             }
215         }
216         /// <summary>
217         /// 计算还款状况
218         /// </summary>
219         /// <param name="n0">还款期数</param>
220         /// <param name="dt">首次还款时间</param>
221         void repayment(int n0, datetime dt)
222         {
223             balance = totalmoney;
224             datetime dt0 = dt;
225             double rate0;
226             double capital;
227             double p;
228             console.writeline("还款时间\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
229             switch (choice)
230             {
231                 case "1":
232                     {
233                         
234                         ratechoose(dt0);
235                         if (n0 <= 60)
236                         {
237                             rate0 = lowrate;
238                         }
239                         else
240                         {
241                             rate0 = highrate;
242                         }
243                         n = n0;
244                         for (int i = 1; i <=n0; i++)
245                         {
246                             ratechoose(dt);
247                             rate = (n0 <= 60 ? lowrate : highrate);
248                             
249                             if (dt.year != dt0.year && rate != rate0)//这里应该有问题?
250                             {
251                                 double p0 = rate0 / 12;
252                                 double paymonth0= (p0 * totalmoney * math.pow((1 + p0), n)) / ((math.pow(1 + p0, n) - 1));
253                                 double interest0 = balance * p0;
254                                 double capital0 = paymonth0 - interest0;
255                                 
256                                 rate0 = rate;
257                                
258                                 interest = balance * (rate0/12);
259                                 totalmoney = balance;
260                                 balance -= capital0;
261                                
262                                 paymonth = capital0 + interest;
263                                 n =n0 - i+1;
264                                 console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital0, paymonth, balance);
265                                 dt = dt.addmonths(1);
266                                 continue;
267                             }
268 
269 
270                            
271                             p = rate0 / 12;
272                             paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
273 
274                             interest = balance * p;
275                              capital = paymonth - interest;
276                             balance -= capital;
277                             console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, paymonth, balance);
278                             dt = dt.addmonths(1);
279 
280                         }
281 
282                     }
283                     break;
284                 case "2":
285                     {
286                         ratechoose(dt0);
287                         rate0 = (n0 <= 60 ? lowrate : highrate);
288                         n = n0;
289                         for (int i = 0; i < n0; i++)
290                         {
291                             ratechoose(dt);
292                             rate = (n0 <= 60 ? lowrate : highrate);
293                             if (dt.year!=dt0.year&&rate!=rate0)
294                             {
295                                 rate0 = rate;
296                             }
297                             p = rate0 / 12;
298                             capital = totalmoney / n0;
299                             interest = balance * p;
300                             paymonth = capital + interest;
301                             balance -= capital;
302                             console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, paymonth, balance);
303                             dt = dt.addmonths(1);
304                         }
305                     }
306                     break;
307             }
308         }
309         /// <summary>
310         ///实现提前还款功能
311         /// </summary>
312         /// <param name="n">还款总期数</param>
313         /// <param name="dt1">首次还款时间</param>
314         /// <param name="dt2">提前还款时间</param>
315         void prepayment(int n0,datetime dt1,datetime dt2,double money)
316         {
317             balance = totalmoney;
318             datetime dt0 = dt1;
319             double rate0;
320             double capital;
321             double p;
322             bool flag = false;
323             console.writeline("还款时间\t\t偿还利息\t\t偿还本金\t\t还款额\t\t剩余本金");
324             switch (choice)
325             {
326                 case "1":
327                     {
328                         ratechoose(dt0);
329                         rate0 = (n0 <= 60 ? lowrate : highrate);
330                         n = n0;
331                         for(int i=1;i<=n0;i++)
332                         {
333                             ratechoose(dt1);
334                             rate = (n0 <= 60 ? lowrate : highrate);
335                            
336 
337                             if (dt1.year != dt0.year && rate != rate0)//这里应该有问题?
338                             {
339                                 double p0 = rate0 / 12;
340                                 double paymonth0 = (p0 * totalmoney * math.pow((1 + p0), n)) / ((math.pow(1 + p0, n) - 1));
341                                 double interest0 = balance * p0;
342                                 double capital0 = paymonth0 - interest0;
343 
344                                 rate0 = rate;
345 
346                                 interest = balance * (rate0 / 12);
347                                 totalmoney = balance;
348                                 balance -= capital0;
349 
350                                 paymonth = capital0 + interest;
351                                 n = n0 - i + 1;
352                                 console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt1, interest, capital0, paymonth, balance);
353                                 dt1 = dt1.addmonths(1);
354                                 continue;
355                             }
356 
357 
358 
359                             p = rate0 / 12;
360                             if (dt2.year == dt1.year && dt2.month == dt1.month)
361                             {
362                                 balance -= money;
363                                 totalmoney = balance;
364                                 switch (newchoice)
365                                 {
366                                     case "1":
367                                         {
368 
369                                             n = n0 - i + 1;
370                                             flag = true;
371                                         }
372                                         break;
373                                     case "2":
374                                         {
375                                             double temp = math.log((paymonth / (paymonth - totalmoney * p)), (1 + p));
376                                             n= (int)math.ceiling(temp);
377                                             n0 = n + i - 1;
378                                         }
379                                         break;
380                                 }
381 
382                             }
383                             paymonth = (p * totalmoney * math.pow((1 + p), n)) / ((math.pow(1 + p, n) - 1));
384                             
385 
386                             interest = balance * p;
387                             capital = paymonth - interest;
388                             balance -= capital;
389                             if (flag)
390                             {
391                                 paymonth += money;
392                                 capital += money;
393                                 flag = false;
394                             }
395                             console.writeline("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt1, interest, capital, paymonth, balance);
396                             dt1 = dt1.addmonths(1);
397                         }
398                     }
399                     break;
400                 case "2":
401                     {
402 
403                     }
404                     break;
405             }
406         }
407         static void main(string[] args)
408         {
409 
410             {
411                 program p = new program();
412                 p.paint();
413                 console.writeline("请选择您的操作!1.房贷计算 2.月供速算 3.还款状况 4.提前还款");
414                 console.writeline("请选择您的还款方式?1.等额本息 2.等额本金");
415                 choice = console.readline();
416                 console.writeline("请输入你的贷款金额");
417                 totalmoney = convert.todouble(console.readline());
418                 console.writeline("请输入您贷款的期数");
419                 int number = convert.toint32(console.readline());
420                 console.writeline("请输入你的首次贷款时间");
421                 datetime dt = convert.todatetime(console.readline());
422                 console.writeline("请输入你的提前还款时间");
423                 datetime dtr = convert.todatetime(console.readline());
424                 console.writeline("请输入你的还款金额");
425                 double partmoney = convert.todouble(console.readline());
426                 console.writeline("请选择你要的后续操作!1.减少月供 2.缩短年限");
427                  newchoice = console.readline();
428                 //console.writeline("你输入的日期是{0},短期利率是{1},长期利率是{2}",dt,p.lowrate,p.highrate);
429                 //dt= dt.addmonths(1);
430                 // console.writeline(dt);
431                 // p.repayment(number, dt);
432                 // p.compute(number);
433                 // p.payment(dtr);
434                 p.prepayment(number, dt, dtr, partmoney);
435                 console.readkey();
436             }
437         }
438     }
439 }

    经过两三天的修改,代码进一步完善。但是鉴于自己水平有限,代码错误之处在所难免。不足之处,希望大家提出宝贵意见,使代码进一步完善。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网