C#控制台编写简易计算机程序
一、功能需求分析:
- 实现简单的0—9这10个数之间的加减乘除;
- 实现正负号功能;
- 实现清除功能;
- 实现小数点运算;
- 实现删除功能。
二、详细实现过程:
1. A代表第一个输入的数字,b代表第二个输入的数字,布尔型c为判断条件,d代表运算符号。
2. 第一个要解决的问题:怎样让按钮上的数字在文本框上显示?
通过if--else语句把按钮上的数字赋给文本框语句如下:textBox1.Text += "某个数字";
注意事项:除数不能为0,设计判断代码提示出错:if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
3.第二个要解决的问题:怎样实现加减乘除运算?
在=号按钮中写代码:switch (d)
{
case "+":
a = b + double.Parse(textBox1.Text); break;
case "-":
a = b - double.Parse(textBox1.Text); break;
case "*":
a = b * double.Parse(textBox1.Text); break;
case "/":
a = b / double.Parse(textBox1.Text); break;
}
textBox1.Text = a + "";
4.接下来就是各个功能按键的实现过程:
(1)清除按钮:textBox1 .Text ="";
(2)正负键按钮:if (textBox1.Text.IndexOf("-") == -1 && textBox1.Text.Length > 0)
{
String s = textBox1.Text;
textBox1.Text = "-" + s;
}
else
{
if (textBox1.Text.Length > 0)
{
String s = textBox1.Text.Substring(1);
textBox1.Text="+s" ;
}
}
(3)退格按钮:if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
三、功能按钮的设计
“+”按键:字体设计:宋体, 12pt, style=Bold, Italic
颜色设计:Highlight(蓝色)
“-“按键:字体设计:宋体, 12pt, style=Bold, Italic
颜色设计:Fuchsia(红色)
“*”按键:字体设计:宋体, 12pt, style=Bold, Italic
颜色设计:Red
“/”按键:字体设计:宋体, 12pt, style=Bold, Italic
颜色设计:255, 128, 0(橘黄色)
“=”按键:字体设计:宋体, 14.25pt, style=Bold
颜色设计:Lime(翠绿)
“C“按键:字体设计:宋体, 15pt;颜色设计:Lime(翠绿)
” ±“按钮:字体设计:宋体, 12pt;颜色设计:Highlight
“←”按键:字体设计:宋体, 12pt;颜色设计:Gold(金黄色)。
四、简易计算器展示:
源程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Calculator : Form
{
private double a = 0;
private double b = 0;
private bool c = false;
private string d;
public Calculator()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "1";
}
private void button2_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "2";
}
private void button3_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "3";
}
private void button4_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "4";
}
private void button5_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "5";
}
private void button6_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "6";
}
private void button7_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "7";
}
private void button8_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "8";
}
private void button9_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "9";
}
//10按钮“0”键
private void button10_Click(object sender, EventArgs e)
{
if (c == true)
{
textBox1.Text = "";
c = false;
}
textBox1.Text += "0";
if (d == "/")
{
textBox1.Clear();
MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//11按钮小数点
private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.Text = "0.";
}
else if (textBox1.Text.IndexOf(".") >= 0)
{
MessageBox.Show("已经添加小数点1!", "提示");
}
else
{
textBox1.Text += ".";
}
}
private void button12_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "+";
}
private void button13_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "-";
}
private void button14_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "*";
}
private void button15_Click(object sender, EventArgs e)
{
c = true;
b = double.Parse(textBox1.Text);
d = "/";
}
//16按钮等号键
private void button16_Click(object sender, EventArgs e)
{
switch (d)
{
case "+":
a = b + double.Parse(textBox1.Text); break;
case "-":
a = b - double.Parse(textBox1.Text); break;
case "*":
a = b * double.Parse(textBox1.Text); break;
case "/":
a = b / double.Parse(textBox1.Text); break;
}
textBox1.Text = a + "";
c = true;
}
//17按钮清零
private void button17_Click(object sender, EventArgs e)
{
textBox1 .Text ="";
}
//18按钮正负键
private void button18_Click(object sender, EventArgs e)
{
if (textBox1.Text.IndexOf("-") == -1 && textBox1.Text.Length > 0)
{
String s = textBox1.Text;
textBox1.Text = "-" + s;
}
else
{
if (textBox1.Text.Length > 0)
{
String s = textBox1.Text.Substring(1);
textBox1.Text="+s" ;
}
}
}
//19按钮退格键
private void button19_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
}
}