基于c#的简易逆波兰式算法的计算器,用集合来处理运算符和运算数,包含平方、开方、三角函数、log、加减乘除,写出窗体程序代码并有详细中文注释,以及有详细的各控件属性。
时间: 2024-03-09 14:44:39 浏览: 103
C#代码案例-逆波兰式计算器的开发源代码(超详细)
以下是基于C#的简易逆波兰式算法的计算器的窗体程序代码:
```csharp
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Calculator
{
public partial class Form1 : Form
{
//设置运算符和运算数的集合
private readonly HashSet<char> operators = new HashSet<char>() { '+', '-', '*', '/', '^', '(', ')' };
private readonly HashSet<string> functions = new HashSet<string>() { "sin", "cos", "tan", "log", "sqrt" };
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
//获取输入的表达式
string input = txtInput.Text;
//将表达式转换为后缀表达式
List<string> postfixExpression = ConvertToPostfixExpression(input);
//计算后缀表达式的值
double result = CalculatePostfixExpression(postfixExpression);
//将计算结果显示在文本框中
txtResult.Text = result.ToString();
}
catch (Exception ex)
{
//如果出现异常,将异常信息显示在文本框中
txtResult.Text = ex.Message;
}
}
private List<string> ConvertToPostfixExpression(string input)
{
List<string> postfixExpression = new List<string>();
Stack<string> stack = new Stack<string>();
string temp = "";
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
if (Char.IsDigit(c) || c == '.')
{
temp += c;
}
else if (operators.Contains(c))
{
if (temp != "")
{
postfixExpression.Add(temp);
temp = "";
}
if (c == '(')
{
stack.Push(c.ToString());
}
else if (c == ')')
{
while (stack.Peek() != "(")
{
postfixExpression.Add(stack.Pop());
}
stack.Pop();
}
else
{
while (stack.Count > 0 && GetPriority(stack.Peek()[0]) >= GetPriority(c))
{
postfixExpression.Add(stack.Pop());
}
stack.Push(c.ToString());
}
}
else if (Char.IsLetter(c))
{
temp += c;
if (functions.Contains(temp))
{
stack.Push(temp);
temp = "";
}
}
}
if (temp != "")
{
postfixExpression.Add(temp);
}
while (stack.Count > 0)
{
postfixExpression.Add(stack.Pop());
}
return postfixExpression;
}
private double CalculatePostfixExpression(List<string> postfixExpression)
{
Stack<double> stack = new Stack<double>();
foreach (string s in postfixExpression)
{
if (double.TryParse(s, out double num))
{
stack.Push(num);
}
else if (functions.Contains(s))
{
double arg = stack.Pop();
switch (s)
{
case "sin":
stack.Push(Math.Sin(arg));
break;
case "cos":
stack.Push(Math.Cos(arg));
break;
case "tan":
stack.Push(Math.Tan(arg));
break;
case "log":
stack.Push(Math.Log10(arg));
break;
case "sqrt":
stack.Push(Math.Sqrt(arg));
break;
}
}
else
{
double num2 = stack.Pop();
double num1 = stack.Pop();
switch (s)
{
case "+":
stack.Push(num1 + num2);
break;
case "-":
stack.Push(num1 - num2);
break;
case "*":
stack.Push(num1 * num2);
break;
case "/":
if (num2 == 0)
{
throw new Exception("除数不能为零!");
}
stack.Push(num1 / num2);
break;
case "^":
stack.Push(Math.Pow(num1, num2));
break;
}
}
}
if (stack.Count != 1)
{
throw new Exception("表达式有误!");
}
return stack.Pop();
}
private int GetPriority(char c)
{
switch (c)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//清空文本框
txtInput.Clear();
txtResult.Clear();
}
}
}
```
上述代码使用了集合来处理运算符和运算数,包含了平方、开方、三角函数、log、加减乘除等运算符。同时,还实现了将中缀表达式转换为后缀表达式的功能,以及对后缀表达式进行计算的功能。窗体程序中包含了两个文本框和两个按钮,分别用于输入表达式、显示计算结果和清空文本框。具体的控件属性如下:
控件名称:txtInput
控件类型:TextBox
控件属性:
- Name:txtInput
- Text:(空)
控件名称:txtResult
控件类型:TextBox
控件属性:
- Name:txtResult
- ReadOnly:True
- Text:(空)
控件名称:btnCalculate
控件类型:Button
控件属性:
- Name:btnCalculate
- Text:计算
控件名称:btnClear
控件类型:Button
控件属性:
- Name:btnClear
- Text:清空
阅读全文