基于上述文法,用wpf实现 A、中缀式错误检查:给出错误信息与错误位置 B、中缀式翻译为逆波兰式:给出翻译后的逆波兰式 C、逆波兰式的求值:执行逆波兰式并给出求值结果,系统要设计GUI,操作友好,给出具体代码

时间: 2024-03-24 11:40:53 浏览: 15
以下是一个用 C# 和 WPF 实现的计算器程序,可以进行中缀式错误检查、中缀式转换为逆波兰式和逆波兰式求值,同时具有友好的 GUI 界面。 MainWindow.xaml: ```xml <Window x:Class="Calculator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Calculator" Height="350" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TextBox x:Name="InputBox" Grid.Row="0" Margin="5"/> <StackPanel Grid.Row="1" Margin="5" Orientation="Horizontal"> <Button x:Name="ButtonCheck" Content="Check"/> <Button x:Name="ButtonTranslate" Content="Translate"/> <Button x:Name="ButtonEvaluate" Content="Evaluate"/> </StackPanel> <TextBlock x:Name="OutputBlock" Grid.Row="2" Margin="5" TextWrapping="Wrap"/> </Grid> </Window> ``` MainWindow.xaml.cs: ```csharp using System; using System.Collections.Generic; using System.Windows; namespace Calculator { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ButtonCheck.Click += ButtonCheck_Click; ButtonTranslate.Click += ButtonTranslate_Click; ButtonEvaluate.Click += ButtonEvaluate_Click; } private void ButtonCheck_Click(object sender, RoutedEventArgs e) { string input = InputBox.Text; int pos = CheckInput(input); if (pos == -1) { OutputBlock.Text = "Input is valid."; } else { OutputBlock.Text = $"Error at position {pos}."; } } private void ButtonTranslate_Click(object sender, RoutedEventArgs e) { string input = InputBox.Text; string output = TranslateInput(input); OutputBlock.Text = output; } private void ButtonEvaluate_Click(object sender, RoutedEventArgs e) { string input = InputBox.Text; string rpn = TranslateInput(input); double result = EvaluateRPN(rpn); OutputBlock.Text = result.ToString(); } private int CheckInput(string input) { int pos = -1; int depth = 0; for (int i = 0; i < input.Length; i++) { char c = input[i]; if (c == '(') { depth++; } else if (c == ')') { depth--; if (depth < 0) { pos = i; break; } } else if (IsOperator(c)) { if (i == 0 || i == input.Length - 1) { pos = i; break; } char prev = input[i - 1]; char next = input[i + 1]; if (IsOperator(prev) || IsOperator(next)) { pos = i; break; } } else if (!IsDigit(c) && c != ' ') { pos = i; break; } } if (depth != 0) { pos = input.Length - 1; } return pos; } private string TranslateInput(string input) { Stack<char> operatorStack = new Stack<char>(); List<string> outputList = new List<string>(); string number = ""; for (int i = 0; i < input.Length; i++) { char c = input[i]; if (IsDigit(c)) { number += c; } else if (c == ' ') { if (number != "") { outputList.Add(number); number = ""; } } else if (IsOperator(c)) { if (number != "") { outputList.Add(number); number = ""; } while (operatorStack.Count > 0 && operatorStack.Peek() != '(' && GetPriority(operatorStack.Peek()) >= GetPriority(c)) { outputList.Add(operatorStack.Pop().ToString()); } operatorStack.Push(c); } else if (c == '(') { if (number != "") { outputList.Add(number); number = ""; } operatorStack.Push(c); } else if (c == ')') { if (number != "") { outputList.Add(number); number = ""; } while (operatorStack.Count > 0 && operatorStack.Peek() != '(') { outputList.Add(operatorStack.Pop().ToString()); } operatorStack.Pop(); } } if (number != "") { outputList.Add(number); } while (operatorStack.Count > 0) { outputList.Add(operatorStack.Pop().ToString()); } string output = string.Join(" ", outputList); return output; } private double EvaluateRPN(string rpn) { Stack<double> stack = new Stack<double>(); string[] tokens = rpn.Split(' '); foreach (string token in tokens) { if (IsDigit(token[0])) { double value = double.Parse(token); stack.Push(value); } else { double b = stack.Pop(); double a = stack.Pop(); double result = 0; if (token == "+") { result = a + b; } else if (token == "-") { result = a - b; } else if (token == "*") { result = a * b; } else if (token == "/") { result = a / b; } stack.Push(result); } } double finalResult = stack.Pop(); return finalResult; } private bool IsDigit(char c) { return c >= '0' && c <= '9'; } private bool IsOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } private int GetPriority(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } else { return 0; } } } } ``` 该程序实现了三个操作:中缀式错误检查、中缀式转换为逆波兰式和逆波兰式求值。在 MainWindow.xaml 中定义了一个文本框用于输入表达式,三个按钮分别对应不同的操作,一个文本块用于显示输出结果。在 MainWindow.xaml.cs 中实现了具体的操作逻辑,包括中缀式错误检查、中缀式转换为逆波兰式和逆波兰式求值的实现。其中,中缀式转换为逆波兰式使用了栈来辅助实现,逆波兰式求值也使用了栈来辅助实现。

相关推荐

最新推荐

recommend-type

WPF实现3D翻牌式倒计时特效

主要为大家详细介绍了WPF实现3D翻牌式倒计时特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

WPF 在image控件用鼠标拖拽出矩形的实现方法

主要介绍了WPF 在image控件用鼠标拖拽出矩形的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

WPF实现窗体中的悬浮按钮

主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

WPF实现ScrollViewer滚动到指定控件处

主要为大家详细介绍了WPF实现ScrollViewer滚动到指定控件处,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

超炫酷的WPF实现Loading控件效果

主要介绍了超炫酷的WPF实现Loading控件效果,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

帮我设计一个基于Android平台的便签APP的代码

很抱歉,我是一个语言模型AI,无法提供完整的Android APP代码。但是,我可以给您一些设计便签APP的建议和思路。 1. 界面设计 首先考虑APP的界面设计,便签APP通常需要一个主界面用于显示所有的便签,以及一个编辑界面用于编辑单个便签。主界面可以采用列表的形式,列出所有的便签,每个便签可以显示标题和摘要内容。在编辑界面中,用户可以输入标题和正文内容,并且可以设置提醒时间、标签、优先级等。 2. 数据存储 便签APP需要一个数据存储的方案,可以考虑使用SQLite数据库来存储便签数据。每个便签可以存储标题、正文内容、提醒时间、标签、优先级等信息。 3. 便签操作 便签APP
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。