细算起来,学习C#快有一周的时间了吧,简单做了个计算器的小例子。不足之处,还希望大家多多指导。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsCalculator
{
public partial class Form1 : Form
{
//构造器
public Form1()
{
InitializeComponent();
this.initializeValues();
}
//定义缓存变量
public int resultValue;
public int currentValue;
private Operation cachedOperation;
// 做初始化工作
public void initializeValues()
{
this.resultValue = 0;
this.currentValue = 0;
this.cachedOperation = Operation.None;
}
// 显示当前正在输入的值
public void DisplayCurrentValue()
{
this.textBox1.Text = this.currentValue.ToString();
}
// 显示已有结果
public void DisplayResultValue()
{
this.textBox1.Text = this.resultValue.ToString();
}
// 数字键点击事件处理器
private void NumberButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (!btn.Text.ToString().Equals("."))
{
this.currentValue = this.currentValue * 10 + int.Parse(btn.Text);
}
else
{
this.currentValue = this.currentValue + int.Parse(btn.Text.ToString());
}
this.DisplayCurrentValue();
}
//操作符
private void OperatorButton_Click(object sender, EventArgs e)
{
this.CalculateCore();
if (sender == buttonAdd)
{
this.cachedOperation = Operation.Add;
}
if (sender == buttonSub)
{
this.cachedOperation = Operation.Sub;
}
if (sender == buttonMul)
{
this.cachedOperation = Operation.Mul;
}
if (sender == buttonDiv)
{
this.cachedOperation = Operation.Div;
}
}
//result
private void buttonResult_Click(object sender, EventArgs e)
{
CalculateCore();
}
// 运算核心
private void CalculateCore()
{
switch (this.cachedOperation)
{
case Operation.Add:
this.resultValue += this.currentValue;
break;
case Operation.Sub:
this.resultValue -= this.currentValue;
break;
case Operation.Mul:
this.resultValue *= this.currentValue;
break;
case Operation.Div:
if (this.currentValue != 0)
{
this.resultValue /= this.currentValue;
}
else
{
MessageBox.Show("Zero Error!");
}
break;
case Operation.None:
default:
this.resultValue = this.currentValue;
break;
}
this.currentValue = 0;
this.DisplayResultValue();
}
// 运算类型
enum Operation
{
Add,
Sub,
Mul,
Div,
None
}
private void buttonClear_Click(object sender, EventArgs e)
{
this.initializeValues();
this.DisplayResultValue();
}
}
}
程序说明:
1.将0-9数字键名称改为:NumberButton;
2.将+、-、*、/运算符键名称改为:OperatorButton;
分享到:
相关推荐
源码07 多功能计算器源码07 多功能计算器源码07 多功能计算器源码07 多功能计算器源码07 多功能计算器
多功能计算器 具有log 平方 多进制 等各种运算
C#多功能数学计算器源码,里面的功能很全的,方便使用
1.支持多进制四则混合运算,三角函数,统计函数 2.支持二进制,八进制,十进制,十六进制
C#多功能数学计算器附完整源码 C#多功能数学计算器附完整源码
android科学计算器源码、源码打包文件、界面更漂亮!android科学计算器源码、源码打包文件、界面更漂亮!android科学计算器源码、源码打包文件、界面更漂亮!
课程设计多功能小计算器源码及实验报告,有详细说明
本实例是Android科学计算器的详细案例介绍,包括所有界面的布局,里面的逻辑功能的代码实现。
C#328-C#多功能计算器,源代码
C#源码 多功能计算器 C#源码 多功能计算器 C#源码 多功能计算器
基于MFC进行多功能计算器的设计,使程序分为计算器及转换器两个部分。 1.其中计算器分为三种模式: (1)标准计算器(包括对浮点数及整数进行加减乘除运算、求平方、开根号、取模等功能) (2)科学计算器(包括对...
这是一个多功能计算器,有很多功能,比如:数据阶乘/加。园的计算,常用加减乘除等等。
c# 多功能计算器 实例 源码 功能很强大
android:简单计算器+源码+注释 android:简单计算器+源码+注释 android:简单计算器+源码+注释
说明: 此计算机可以计算同时超过多个操作项的什. 例如: y = 3 + 64 * (2 + 3^5) + sinPI 的值, 用括号区分优先级,如果有大量很长的算式需要计算,可以试一下这个计算器. 开发语言: C#语言,用Stack原理...
本计算器实现了的功能有: 1、二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算 2、科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行 3、以角度、弧度两...
c#编写的计算器源码 多功能+注释详解 实现加减乘除混合运算等
界面漂亮一点,还是有bug欢迎大家改进!
基于51单片机的多功能计算器设计_基于51单片机的多功能计算器设计_源码