上一篇:QQ验证码识别源代码(C#/NET1.1) >>
C#教学经验谈(3):储蓄计算器的源程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Saving
{
enum Compound
{
每月计算利息,
每季度计算利息
}
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.NumericUpDown 初始金额;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.NumericUpDown 利率;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ComboBox 计算规则;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button 计算;
private System.Windows.Forms.NumericUpDown 存期;
private System.Windows.Forms.NumericUpDown 每月存入;
private System.Windows.Forms.NumericUpDown 存款总额;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
计算规则.Items.Add(Compound.每月计算利息);
计算规则.Items.Add(Compound.每季度计算利息);
计算规则.SelectedItem = 计算规则.Items[0];
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.初始金额 = new System.Windows.Forms.NumericUpDown();
this.label2 = new System.Windows.Forms.Label();
this.利率 = new System.Windows.Forms.NumericUpDown();
this.label3 = new System.Windows.Forms.Label();
this.存期 = new System.Windows.Forms.NumericUpDown();
this.label4 = new System.Windows.Forms.Label();
this.计算规则 = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.每月存入 = new System.Windows.Forms.NumericUpDown();
this.label6 = new System.Windows.Forms.Label();
this.存款总额 = new System.Windows.Forms.NumericUpDown();
this.计算 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.初始金额)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.利率)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.存期)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.每月存入)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.存款总额)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(32, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 16);
this.label1.TabIndex = 0;
this.label1.Text = "初始金额:";
//
// 初始金额
//
this.初始金额.DecimalPlaces = 2;
this.初始金额.Increment = new System.Decimal(new int[] {
100,
0,
0,
0});
this.初始金额.Location = new System.Drawing.Point(112, 32);
this.初始金额.Maximum = new System.Decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.初始金额.Name = "初始金额";
this.初始金额.Size = new System.Drawing.Size(168, 21);
this.初始金额.TabIndex = 1;
this.初始金额.ThousandsSeparator = true;
this.初始金额.Value = new System.Decimal(new int[] {
1000,
0,
0,
0});
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(32, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 0;
this.label2.Text = "利率(%):";
//
// 利率
//
this.利率.DecimalPlaces = 2;
this.利率.Increment = new System.Decimal(new int[] {
1,
0,
0,
65536});
this.利率.Location = new System.Drawing.Point(112, 72);
this.利率.Name = "利率";
this.利率.Size = new System.Drawing.Size(168, 21);
this.利率.TabIndex = 1;
this.利率.ThousandsSeparator = true;
this.利率.Value = new System.Decimal(new int[] {
20,
0,
0,
65536});
//
// label3
//
this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(32, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 16);
this.label3.TabIndex = 0;
this.label3.Text = "存期(年):";
//
// 存期
//
this.存期.Location = new System.Drawing.Point(112, 112);
this.存期.Name = "存期";
this.存期.Size = new System.Drawing.Size(168, 21);
this.存期.TabIndex = 1;
this.存期.ThousandsSeparator = true;
this.存期.Value = new System.Decimal(new int[] {
5,
0,
0,
0});
//
// label4
//
this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(32, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(88, 16);
this.label4.TabIndex = 0;
this.label4.Text = "计算规则:";
//
// 计算规则
//
this.计算规则.Location = new System.Drawing.Point(112, 152);
this.计算规则.MaxDropDownItems = 2;
this.计算规则.Name = "计算规则";
this.计算规则.Size = new System.Drawing.Size(168, 20);
this.计算规则.TabIndex = 2;
//
// label5
//
this.label5.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label5.Location = new System.Drawing.Point(32, 192);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(88, 16);
this.label5.TabIndex = 0;
this.label5.Text = "每月存入:";
//
// 每月存入
//
this.每月存入.DecimalPlaces = 2;
this.每月存入.Location = new System.Drawing.Point(112, 192);
this.每月存入.Maximum = new System.Decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.每月存入.Name = "每月存入";
this.每月存入.Size = new System.Drawing.Size(168, 21);
this.每月存入.TabIndex = 1;
this.每月存入.ThousandsSeparator = true;
//
// label6
//
this.label6.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label6.Location = new System.Drawing.Point(32, 264);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(88, 16);
this.label6.TabIndex = 0;
this.label6.Text = "存款总额:";
//
// 存款总额
//
this.存款总额.DecimalPlaces = 2;
this.存款总额.Increment = new System.Decimal(new int[] {
100,
0,
0,
0});
this.存款总额.Location = new System.Drawing.Point(112, 264);
this.存款总额.Maximum = new System.Decimal(new int[] {
-1156317184,
46566128,
0,
0});
this.存款总额.Name = "存款总额";
this.存款总额.ReadOnly = true;
this.存款总额.Size = new System.Drawing.Size(168, 21);
this.存款总额.TabIndex = 1;
this.存款总额.ThousandsSeparator = true;
//
// 计算
//
this.计算.Location = new System.Drawing.Point(248, 320);
this.计算.Name = "计算";
this.计算.TabIndex = 3;
this.计算.Text = "计算存款";
this.计算.Click += new System.EventHandler(this.计算_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 373);
this.Controls.Add(this.计算);
this.Controls.Add(this.存款总额);
this.Controls.Add(this.每月存入);
this.Controls.Add(this.计算规则);
this.Controls.Add(this.存期);
this.Controls.Add(this.利率);
this.Controls.Add(this.初始金额);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.label5);
this.Controls.Add(this.label6);
this.Name = "Form1";
this.Text = "储蓄利率计算器";
((System.ComponentModel.ISupportInitialize)(this.初始金额)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.利率)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.存期)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.每月存入)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.存款总额)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void 计算_Click(object sender, System.EventArgs e)
{
double startAmount=(double)初始金额.Value;
double rate=(double)利率.Value;
int years=(int)存期.Value;
Compound calcFrequency=(Compound) 计算规则.SelectedItem;
double additional=(double)每月存入.Value;
decimal totalValue=0;
rate=rate/100;
int months=12*years;
switch (calcFrequency)
{
case Compound.每月计算利息:
double monthlyRate=rate/12;
for (int i=1; i<=months; i++)
{
startAmount+=startAmount*monthlyRate;
startAmount+=additional;
}
break;
case Compound.每季度计算利息:
double quarterlyRate=rate/4;
for (int i=1; i<=months; i++)
{
if (i%3==0)
{
startAmount+=startAmount*quarterlyRate;
}
startAmount+=additional;
}
break;
default:
MessageBox.Show("该项工作尚未实现!");
break;
}
totalValue=(decimal)startAmount;
存款总额.Value=totalValue;
}
}
}
下一篇:C#实现Window管道技术 >>
相关文章:
- · c#.net常用的小函数和方法集
- · 用Visual C#打造个性化的IE浏览器
- · 如何用UltraEdit编译C#源程序
- · .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)
- · Visual C#网络编程之TCP
- · C# Programming Guidelines
- · C#设计模式之原型(ProtoType)
- · C#设计模式之抽象工厂(AbstractFactory)
- · C# Coding Standard
- · c#.net常用的小函数和方法集
- · c#.net函数列表.txt
- · c#.net常用的小函数参考
- · C#中使用XML——编写XML
- · C#版ftp方法实现类库代码
- · Visual C# .NET 入门
- · 在C#后代码里使用IE WEB Control TreeView
- · C#里操作时间的例子!
- · 在C#中利用SharpZipLib进行文件的压缩和解压缩
- · C#中使用XML——基于DOM的案例分析
- · 远程重启计算机(C#)
- · C#中使用XML——实现DOM
- · C#中使用XML——编写XML
- · 用C#实现生成PDF文档
- · 使用C#编写的一个定时关机程序
- · 三层架构之数据库访问层完全篇(C#)
- · c#中使用 Win32 和其他库
- · C#执行存储过程的简化
- · 用C#写一个Web自定义控件
- · 在C#中用最简洁有效的代码执行存储过程并返回数据
- · C#里的InputBox
- · 关于C#数组初始化的效率测试
- · C#第2个程序,读取偶网站的数据!
- · 深入理解C#编程中的组件-事件-委托
- · 通过Visual C#.NET建一个DTS任务
- · 用C#的Raw Socket实现网络封包监视
- · 利用C#实现标准的 Dispose模式
- · 从 C++ 向 C# 迁移
- · 使用 C# 2.0 命令行编译器
