Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791511
  • 博文数量: 787
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-22 15:17
文章分类

全部博文(787)

文章存档

2008年(787)

我的朋友

分类:

2008-09-25 16:10:57

 [@more@]

    在C#教学的第二个案例前,先介绍了一个储蓄计算器的实验,该实验项目是从微软的教学光盘中取出的,部分源程序已经给出,要求学生完成事务处理部分。在做这个实验的时候,要求学生最好能够独立设计此项目。在这里,将该项目的代码给出如下。有特点的是,控件的名称使用的是中文。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Saving
{
enum Compound
{
每月计算利息,
每季度计算利息
}
///


/// Form1 的摘要说明。
///

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 存款总额;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
计算规则.Items.Add(Compound.每月计算利息);
计算规则.Items.Add(Compound.每季度计算利息);
计算规则.SelectedItem = 计算规则.Items[0];
}

///


/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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

///


/// 应用程序的主入口点。
///

[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;
}
}
}



--------------------next---------------------

阅读(672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~