模特网站模板,百度搜索风云榜,做网站备案要处省的电话号码,wordpress两栏主题集合类型#xff08;Collection)下篇_xml collection-CSDN博客
以上是堆栈的简单介绍#xff0c;下方是堆栈的使用
题目#xff1a;给定一个逆波兰表达式#xff08;后缀表达式#xff09;的字符串数组tokens#xff0c;其中每个元素是一个操作数#xff08;数字…集合类型Collection)下篇_xml collection-CSDN博客
以上是堆栈的简单介绍下方是堆栈的使用
题目给定一个逆波兰表达式后缀表达式的字符串数组tokens其中每个元素是一个操作数数字或者操作符、-、*、/。使用Stackdouble来计算表达式的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 逆波兰表达式
{internal class Program{static void Main(string[] args){string[] tokens { 3, 7, , 6, * };Console.WriteLine(EvaluatePostfix(tokens));}static double EvaluatePostfix(string[]tokens){Stackdouble stack new Stackdouble();foreach (string token in tokens){if(double.TryParse(token,out double num)){stack.Push(num);}else{double operand2 stack.Pop();double operand1 stack.Pop();switch (token){case :stack.Push(operand1 operand2);break;case -:stack.Push(operand1 - operand2);break;case *:stack.Push(operand1 * operand2);break;case /:stack.Push(operand1 / operand2);break;}}}return stack.Pop();}}
}