本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。在c#教學(xué)的第二個(gè)案例前,先介紹了一個(gè)儲(chǔ)蓄計(jì)算器的實(shí)驗(yàn),該實(shí)驗(yàn)項(xiàng)目是從微軟的教學(xué)光盤中取出的,部分源程序已經(jīng)給出,要求學(xué)生完成事務(wù)處理部分。在做這個(gè)實(shí)驗(yàn)的時(shí)候,要求學(xué)生最好能夠獨(dú)立設(shè)計(jì)此項(xiàng)目。在這里,將該項(xiàng)目的代碼給出如下。有特點(diǎn)的是,控件的名稱使用的是中文。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace saving
{
enum compound
{
每月計(jì)算利息,
每季度計(jì)算利息
}
/// <summary>
/// form1 的摘要說(shuō)明。
/// </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 計(jì)算規(guī)則;
private system.windows.forms.label label5;
private system.windows.forms.label label6;
private system.windows.forms.button 計(jì)算;
private system.windows.forms.numericupdown 存期;
private system.windows.forms.numericupdown 每月存入;
private system.windows.forms.numericupdown 存款總額;
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設(shè)計(jì)器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
計(jì)算規(guī)則.items.add(compound.每月計(jì)算利息);
計(jì)算規(guī)則.items.add(compound.每季度計(jì)算利息);
計(jì)算規(guī)則.selecteditem = 計(jì)算規(guī)則.items[0];
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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.計(jì)算規(guī)則 = 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.計(jì)算 = 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 = "計(jì)算規(guī)則:";
//
// 計(jì)算規(guī)則
//
this.計(jì)算規(guī)則.location = new system.drawing.point(112, 152);
this.計(jì)算規(guī)則.maxdropdownitems = 2;
this.計(jì)算規(guī)則.name = "計(jì)算規(guī)則";
this.計(jì)算規(guī)則.size = new system.drawing.size(168, 20);
this.計(jì)算規(guī)則.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;
//
// 計(jì)算
//
this.計(jì)算.location = new system.drawing.point(248, 320);
this.計(jì)算.name = "計(jì)算";
this.計(jì)算.tabindex = 3;
this.計(jì)算.text = "計(jì)算存款";
this.計(jì)算.click += new system.eventhandler(this.計(jì)算_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(360, 373);
this.controls.add(this.計(jì)算);
this.controls.add(this.存款總額);
this.controls.add(this.每月存入);
this.controls.add(this.計(jì)算規(guī)則);
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 = "儲(chǔ)蓄利率計(jì)算器";
((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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void 計(jì)算_click(object sender, system.eventargs e)
{
double startamount=(double)初始金額.value;
double rate=(double)利率.value;
int years=(int)存期.value;
compound calcfrequency=(compound) 計(jì)算規(guī)則.selecteditem;
double additional=(double)每月存入.value;
decimal totalvalue=0;
rate=rate/100;
int months=12*years;
switch (calcfrequency)
{
case compound.每月計(jì)算利息:
double monthlyrate=rate/12;
for (int i=1; i<=months; i++)
{
startamount+=startamount*monthlyrate;
startamount+=additional;
}
break;
case compound.每季度計(jì)算利息:
double quarterlyrate=rate/4;
for (int i=1; i<=months; i++)
{
if (i%3==0)
{
startamount+=startamount*quarterlyrate;
}
startamount+=additional;
}
break;
default:
messagebox.show("該項(xiàng)工作尚未實(shí)現(xiàn)!");
break;
}
totalvalue=(decimal)startamount;
存款總額.value=totalvalue;
}
}
}