码迷,mamicode.com
首页 > Windows程序 > 详细

贷款计算器C#实现 课程作业一

时间:2017-12-14 17:56:20      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:nbsp   convert   nta   pos   lin   str   send   event   false   

花了两个小时理解公式,我数学是不是太弱了..

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 贷款计算机
{
    public partial class Form1 : Form
    {
        int pos = 0;
        double year, cost, temp;//分别表示 年限 金额 利率
        public Form1()
        {
            InitializeComponent();
        }

        //pos == 1 表示等额本息
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked )
            {
                checkBox2.Checked = false;
                pos = 1;
            }
            else
                pos = 0;

        }


        //pos == 2 表示等额本金
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox1.Checked = false;
                pos = 1;
            }
            else
                pos = 0;
        }

        //计算按钮
        private void button1_Click(object sender, EventArgs e)
        {
            
            try //选择还款方式
            {
                if ( pos == 0)
                    throw new Exception ();
            }
            catch (Exception)
            {
                MessageBox.Show("先选择还款方式才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            try //输入贷款年限
            {
                year = Convert.ToDouble(textBox1.Text);
                year = year * 12;//转换成贷款月份
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款年限才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款金额
            {
                cost = Convert.ToDouble(textBox2.Text);
                cost = cost * 10000;// 贷款金额,单位元
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款金额才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款利率
            {
                temp = Convert.ToDouble(textBox3.Text);
                temp = temp / 1200;//贷款月利率
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款利率才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";

            if (checkBox1.Checked)
            {
                double temp1 = cost * temp*Math.Pow(1 + temp, year+1) / (Math.Pow(1+temp,year) -1);//*temp * cost
                double temp2 = temp1 * year -cost;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
            else if(checkBox2.Checked)
            {
                double temp1 = ((cost / year + cost * temp) + cost / year * (1 + temp)) / 2 ; //总利息
                double temp2 = temp1 * year - cost ;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            try //选择还款方式
            {
                if (pos == 0)
                    throw new Exception();
            }
            catch (Exception)
            {
                MessageBox.Show("先选择还款方式才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            try //输入贷款年限
            {
                year = Convert.ToDouble(textBox1.Text);
                year = year * 12;//转换成贷款月份
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款年限才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款金额
            {
                cost = Convert.ToDouble(textBox2.Text);
                cost = cost * 10000;// 贷款金额,单位元
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款金额才能进行此运算!", "错误", MessageBoxButtons.OK);
            }

            try  //输入贷款利率
            {
                temp = Convert.ToDouble(textBox3.Text);
                temp = temp / 1200;//贷款月利率
            }
            catch (Exception)
            {
                MessageBox.Show("先输入贷款利率才能进行此运算!", "错误", MessageBoxButtons.OK);
            }
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";

            if (checkBox1.Checked)
            {
                double temp1 = cost * temp * Math.Pow(1 + temp, year + 1) / (Math.Pow(1 + temp, year) - 1);//*temp * cost
                double temp2 = temp1 * year - cost;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
            else if (checkBox2.Checked)
            {
                double temp1 = ((cost / year + cost * temp) + cost / year * (1 + temp)) / 2; //总利息
                double temp2 = temp1 * year - cost ;
                double temp3 = temp1 * year;
                textBox4.Text = temp1.ToString();
                textBox5.Text = temp2.ToString();
                textBox6.Text = temp3.ToString();
            }
        }
    }
}

 

贷款计算器C#实现 课程作业一

标签:nbsp   convert   nta   pos   lin   str   send   event   false   

原文地址:http://www.cnblogs.com/Draymonder/p/8038704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!