分享
 
 
 

C#教学经验谈(3):储蓄计算器的源程序

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有