目录
1.用上位机实现简单计算器的简单探索
在编程的广袤领域中,构建一个简单的计算器不仅是一项充满趣味的实践,更是深入理解编程语言和算法逻辑的有效途径。今天,让我们一同踏上使用 C# 实现简单计算器的精彩旅程。
一、功能蓝图与设计考量
一个基础但实用的计算器应涵盖加、减、乘、除这四则运算,同时配备清零、退格等辅助功能,以提升用户体验。在设计之初,我们需精心规划这些功能的实现方式和交互逻辑。
1.1. 在处理用户输入的超长算式
-
使用合适的数据结构存储算式
- 可以使用字符串来接收用户输入的超长算式。
-
解析算式
- 将算式分解为数字、运算符和括号等元素。可以通过遍历字符串,并根据特定的规则(如空格、运算符等)进行分割。
-
处理运算符优先级
- 定义运算符的优先级,例如乘法和除法的优先级高于加法和减法。
-
使用栈来计算
- 可以使用两个栈,一个用于存储数字,另一个用于存储运算符。
- 按照运算符的优先级,从栈中取出数字和运算符进行计算,并将结果压回数字栈。
-
处理括号
- 当遇到左括号时,将其之后的计算结果暂存,直到遇到右括号时,将暂存的结果取出进行计算。
二、用户界面的精心雕琢
利用 C# 的 Windows Forms 或 WPF 框架,我们能够塑造出直观且易用的计算器界面。数字按钮、运算符按钮以及用于显示计算结果的文本框等元素,都需要合理布局。例如,通过设置按钮的大小、颜色和字体样式,使界面不仅美观,还能符合人体工程学,方便用户操作。
三、核心计算逻辑的构建
这无疑是计算器的灵魂所在。当用户点击数字和运算符按钮时,程序需准确捕获输入,并按照预定的运算规则执行计算。在 C# 中,我们可以借助事件处理机制来响应按钮的点击事件。每当用户输入一个数字,将其添加到一个字符串中。当遇到运算符时,先将当前的数字字符串转换为数值,存储到一个变量中,然后根据运算符决定后续的操作。
对于加法和减法,直接将后续输入的数字与之前存储的数字进行相应运算;对于乘法和除法,需要特别注意运算顺序和精度问题。
此外,还需考虑除数为零的情况,通过异常处理机制抛出相应的错误提示,确保程序的健壮性。
四、代码实现与关键技术剖析
以下是一个更为详细的 C# 计算器代码示例:
using System;
class Calculator
{
static double PerformOperation(double num1, double num2, char op)
{
switch (op)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2!= 0)
{
return num1 / num2;
}
else
{
Console.WriteLine("除数不能为零!");
return double.NaN;
}
default:
Console.WriteLine("无效的运算符!");
return double.NaN;
}
}
static void Main()
{
double num1 = 0, num2 = 0, result = 0;
char op = '\0';
string input = "";
Console.Write("请输入操作数或运算符: ");
while (true)
{
char key = Console.ReadKey(true).KeyChar;
if (char.IsDigit(key))
{
input += key;
}
else if (key == '.')
{
if (!input.Contains('.'))
{
input += key;
}
}
else if (char.IsWhiteSpace(key))
{
continue;
}
else if (char.IsOperator(key))
{
if (input!= "")
{
num1 = Convert.ToDouble(input);
input = "";
}
op = key;
}
else if (key == '=')
{
if (input!= "")
{
num2 = Convert.ToDouble(input);
input = "";
}
result = PerformOperation(num1, num2, op);
Console.WriteLine("结果: " + result);
num1 = result;
op = '\0';
}
else if (key == 'C')
{
num1 = 0;
num2 = 0;
result = 0;
op = '\0';
input = "";
Console.WriteLine("已清零");
}
else if (key == 'B')
{
if (input.Length > 0)
{
input = input.Substring(0, input.Length - 1);
}
}
else
{
Console.WriteLine("无效输入,请重新输入");
}
}
}
}
在上述代码中,PerformOperation
方法用于执行具体的运算操作。而在 Main
方法中,通过不断读取用户输入,根据输入的字符类型进行相应的处理,实现了完整的计算流程。
4.1.计算器设计界面
本文主要用button作为按钮,richtextbox作为输出框来实现简单计算器,button数字的顺序为从左向右逐渐增大
计算器设计代码如下:
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 WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num3;
a = this.richTextBox1.Lines[0];
num3 = Convert.ToDouble(a);
richTextBox1.Text += "\ncos(" + a + ")\n";
richTextBox1.Text += "=" + Math.Cos(Math.PI * num3 / 180);
}
private void button2_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num2;
a = this.richTextBox1.Lines[0];
num2 = Convert.ToDouble(a);
richTextBox1.Text += "\nsin(" + a + ")\n";
richTextBox1.Text += "=" + Math.Sin(Math.PI * num2 / 180);
}
private void button3_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num4;
a = this.richTextBox1.Lines[0];
num4 = Convert.ToDouble(a);
double d = Math.Pow(num4, 2.0);
richTextBox1.Text += "\n^2(" + a + ")\n";
richTextBox1.Text += "=" +d;
}
private void button4_Click(object sender, EventArgs e)
{
string a = richTextBox1.Text;
double num1;
a= this.richTextBox1.Lines[0];
num1 = Convert.ToDouble(a);
richTextBox1.Text += "\nsqrt("+a+")\n";
richTextBox1.Text += "=" + Math.Sqrt(num1);
}
private void button5_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";//清空过程显示框
richTextBox1.Text += "0";
}
private void button6_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";//清空过程显示框
}
private void button7_Click(object sender, EventArgs e)
{
string a =richTextBox1.Text;
string temp = a.Remove(a.LastIndexOf(""), 1);
richTextBox1.Text = temp;
}
private void button8_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n/\n";
}
private void button9_Click(object sender, EventArgs e)
{
richTextBox1.Text += "9";
}
private void button10_Click(object sender, EventArgs e)
{
richTextBox1.Text += "8";
}
private void button11_Click(object sender, EventArgs e)
{
richTextBox1.Text += "7";
}
private void button12_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n*\n";
}
private void button13_Click(object sender, EventArgs e)
{
richTextBox1.Text += "6";
}
private void button14_Click(object sender, EventArgs e)
{
richTextBox1.Text += "5";
}
private void button15_Click(object sender, EventArgs e)
{
richTextBox1.Text += "4";
}
private void button16_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n-\n";
}
private void button17_Click(object sender, EventArgs e)
{
richTextBox1.Text += "3";
}
private void button18_Click(object sender, EventArgs e)
{
richTextBox1.Text += "2";
}
private void button19_Click(object sender, EventArgs e)
{
richTextBox1.Text += "1";
}
private void button20_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n+\n";
}
private void button21_Click(object sender, EventArgs e)
{
richTextBox1.Text += "\n^n\n";
}
private void button22_Click(object sender, EventArgs e)
{
richTextBox1.Text += "0";
}
private void button23_Click(object sender, EventArgs e)
{
richTextBox1.Text += ".";
}
private void button24_Click(object sender, EventArgs e)
{
double a = Convert.ToDouble(this.richTextBox1.Lines[0]);
double b = Convert.ToDouble(this.richTextBox1.Lines[2]);
string c = this.richTextBox1.Lines[1];
if (c == "+")
{
double d = a + b;
richTextBox1.Text += "=\n" + d;
}
if (c == "-")
{
double d = a - b;
richTextBox1.Text += "=\n" + d;
}
if (c == "*")
{
double d = a * b;
richTextBox1.Text += "=\n" + d;
}
if (c == "/")
{
double d = a / b;
richTextBox1.Text += "=\n" + d;
}
if (c == "^n")
{
double d = Math.Pow(a, b);
richTextBox1.Text += "=\n" + d;
}
}
}
}
五、性能优化与拓展可能性
尽管这只是一个简单的计算器,但仍有优化的空间。例如,对于频繁的数值转换操作,可以考虑使用缓存机制提高效率。在功能拓展方面,可以加入三角函数、指数函数等高级运算,或者支持多位数的高精度计算。
六、总结与展望
通过这个简单计算器的实现,我们不仅掌握了 C# 中基本的编程技巧,如事件处理、数据类型转换和异常处理,还深入理解了算法和逻辑的重要性。这为我们未来开发更复杂、更强大的应用程序奠定了坚实的基础。期待您能在此基础上,发挥创意,打造出属于自己的独特计算器!