C#教學經驗談(3):儲蓄計算機的來源程式

來源:互聯網
上載者:User
程式 在C#教學的第二個案例前,先介紹了一個儲蓄計算機的實驗,該實驗項目是從微軟的教學光碟片中取出的,部分來源程式已經給出,要求學生完成交易處理部分。在做這個實驗的時候,要求學生最好能夠獨立設計此項目。在這裡,將該項目的代碼給出如下。有特點的是,控制項的名稱使用的是中文。



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





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.