【C#】简单计算器源代码

form1.cs


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

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void clear_Click(object sender, EventArgs e)
{
box1.Text = "";
box2.Text = "";
jg.Text = "";
box1.Focus();

}

private void equal_Click(object sender, EventArgs e)
{
string b1 = box1.Text;
string b2 = box2.Text;
string fh = choice.Text;
double sum;
if (string.IsNullOrEmpty(b1) || string.IsNullOrEmpty(b2))
{
MessageBox.Show("请输入要计算的数据");
return;
}

double _a = 0;
bool _aParse = double.TryParse(b1, out _a);
double _b = 0;
bool _bParse = double.TryParse(b2, out _b);
if (!_aParse || !_bParse)
{
MessageBox.Show("请输入数字");
return;
}
if (string.IsNullOrEmpty(fh))
{
MessageBox.Show("请选择计算符号");
return;
}

switch (choice.Text.ToString())
{
case "+": sum = _a + _b;
jg.Text = Convert.ToString(sum = _a + _b);
MessageBox.Show(Convert.ToString(sum = _a + _b));
break;
case "-": sum = _a - _b;
jg.Text = Convert.ToString(sum = _a - _b);
MessageBox.Show(Convert.ToString(sum = _a - _b));
break;
case "*": sum = _a * _b;
jg.Text = Convert.ToString(sum = _a * _b);
MessageBox.Show(Convert.ToString(sum = _a * _b));
break;
case "/": sum = _a / _b;
jg.Text = Convert.ToString(sum = _a / _b);
MessageBox.Show(Convert.ToString(sum = _a / _b));
break;
}
clear.Focus();
}

private void Form1_Load(object sender, EventArgs e)
{
choice.Items.Add("+");
choice.Items.Add("-");
choice.Items.Add("*");
choice.Items.Add("/");
}
}

}

form.Designer.cs


namespace WindowsApplication2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

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

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.box1 = new System.Windows.Forms.TextBox();
this.jg = new System.Windows.Forms.Label();
this.equal = new System.Windows.Forms.Button();
this.box2 = new System.Windows.Forms.TextBox();
this.clear = new System.Windows.Forms.Button();
this.choice = new System.Windows.Forms.ComboBox();
this.tishi = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// box1
//
this.box1.Location = new System.Drawing.Point(36, 115);
this.box1.Name = "box1";
this.box1.Size = new System.Drawing.Size(100, 21);
this.box1.TabIndex = 0;
//
// jg
//
this.jg.AutoSize = true;
this.jg.Location = new System.Drawing.Point(522, 117);
this.jg.Name = "jg";
this.jg.Size = new System.Drawing.Size(35, 12);
this.jg.TabIndex = 3;
this.jg.Text = "_____";
//
// equal
//
this.equal.Location = new System.Drawing.Point(416, 106);
this.equal.Name = "equal";
this.equal.Size = new System.Drawing.Size(75, 23);
this.equal.TabIndex = 3;
this.equal.Text = "等于";
this.equal.UseVisualStyleBackColor = true;
this.equal.Click += new System.EventHandler(this.equal_Click);
//
// box2
//
this.box2.Location = new System.Drawing.Point(290, 114);
this.box2.Name = "box2";
this.box2.Size = new System.Drawing.Size(100, 21);
this.box2.TabIndex = 2;
//
// clear
//
this.clear.Location = new System.Drawing.Point(416, 174);
this.clear.Name = "clear";
this.clear.Size = new System.Drawing.Size(75, 23);
this.clear.TabIndex = 4;
this.clear.Text = "清除";
this.clear.UseVisualStyleBackColor = true;
this.clear.Click += new System.EventHandler(this.clear_Click);
//
// choice
//
this.choice.FormattingEnabled = true;
this.choice.Location = new System.Drawing.Point(157, 117);
this.choice.Name = "choice";
this.choice.Size = new System.Drawing.Size(94, 20);
this.choice.TabIndex = 1;
this.choice.Text = "+";
//
// tishi
//
this.tishi.AutoSize = true;
this.tishi.Location = new System.Drawing.Point(155, 89);
this.tishi.Name = "tishi";
this.tishi.Size = new System.Drawing.Size(131, 12);
this.tishi.TabIndex = 6;
this.tishi.Text = "亲,请选择计算符号哦~";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(605, 275);
this.Controls.Add(this.tishi);
this.Controls.Add(this.choice);
this.Controls.Add(this.clear);
this.Controls.Add(this.box2);
this.Controls.Add(this.equal);
this.Controls.Add(this.jg);
this.Controls.Add(this.box1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox box1;
private System.Windows.Forms.Label jg;
private System.Windows.Forms.Button equal;
private System.Windows.Forms.TextBox box2;
private System.Windows.Forms.Button clear;
private System.Windows.Forms.ComboBox choice;
private System.Windows.Forms.Label tishi;

}
}

【C#】简单计算器源代码

时间: 2024-10-30 19:15:42

【C#】简单计算器源代码的相关文章

j2ee-JSP之简单计算器

来源韩顺平.j2ee视频实战教程jsp第1讲(下集) -------------------------------------------------------------------------------------------------------- 简单计算器,可以控制输入的数(仅第一个数)不能为空且不能为字符串 myCal.jsp代码 1 <!--这是计算器的界面 --> 2 <!-- 可以控制输入的数不能为空且不能为字符串 --> 3 <%@ page co

JAVA编写的简单计算器

package com.hellojava.practice.test; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; impo

HDU1237 简单计算器 【栈】+【逆波兰式】

版本:1.0 日期:2014.5.17 2014.6.1 版权:© 2014 kince 转载注明出处 在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { public Button(Context context) { this(context, null); } public Button(Context context, AttributeSet att

[Java.web]简单计算器

项目的  WebRoot 目录下的 calculator.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <title>计算结果</title> </head> <body> <jsp:us

NOIP19:简单计算器

/* 1.4编程基础之逻辑表达式与条件分支 19:简单计算器 总时间限制: 1000ms 内存限制: 65536kB 描述 一个最简单的计算器,支持+, -, *, / 四种运算.仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围. 输入 输入只有一行,共有三个参数,其中第1.2个参数为整数,第3个参数为操作符(+,-,*,/). 输出 输出只有一行,一个整数,为运算结果.然而: 1. 如果出现除数为0的情况,则输出:Divided by zero! 2. 如果出现无效的操作符

hdoj 1237 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14512    Accepted Submission(s): 4920 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整

Shell 实现简单计算器功能

Shell 实现简单计算器功能,脚本如下: [[email protected] scripts]# cat jisuan.sh #!/bin/bash print_usage(){     printf $"USAGE:$0 NUM1 {+|-|*|/} NUM2\n"     exit 1 } #判断传入的参数是不是3个 if [ $# -ne 3 ]   then     print_usage fi firstnum=$1 secondnum=$3 op=$2 #对传入的参数进

Javascript 实现简单计算器实例代码

Javascript 实现简单计算器实例代码 这篇文章主要介绍了Javascript 实现简单计算器实例代码的相关资料,需要的朋友可以参考下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

菜鸟学Android编程——简单计算器《一》

菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少,加减乘除就可以. 第一步:设计布局文件 界面如下图: 由于刚开始学Android,对布局文件也不是很了解,边查边找,最后凑合着写好了布局文件. 注意事项:此布局文件用到了GridLayout布局,是在Android4.0以上才出现的(不知道谷歌有没有开发相应的包来适配4.0以下版本). 有关Gri