/// <summary> /// 计算结果 /// </summary> /// <returns></returns> public float GetResult() { Console.ForegroundColor = ConsoleColor.Yellow; float result = 0; string a = ""; float top, topnext; LinkStack <float> numbers = new LinkStack <float>(); foreach (char c in suffix) { if (char.IsDigit(c) || c == '.') { a += c; } else if (c == ' ' && a != "") { numbers.Push(float.Parse(a)); Console.WriteLine("传入了数" + a); a = ""; } if (c == '+') { top = numbers.Pop(); topnext = numbers.Pop(); float temp = topnext + top; Console.WriteLine("加法运算结果 " + temp); numbers.Push(temp); } else if (c == '-') { top = numbers.Pop(); topnext = numbers.Pop(); float temp = topnext - top; Console.WriteLine("减法运算结果 " + temp); numbers.Push(temp); } else if (c == '*') { top = numbers.Pop(); topnext = numbers.Pop(); float temp = topnext * top; Console.WriteLine("乘法运算结果 " + temp); numbers.Push(temp); } else if (c == '/') { top = numbers.Pop(); topnext = numbers.Pop(); float temp = topnext / top; Console.WriteLine("除法运算结果 " + temp); numbers.Push(temp); } } result = numbers.Pop(); numbers.Clear(); Console.ForegroundColor = ConsoleColor.White; return((float)Math.Round(result, 4)); }
static void Main(string[] args) { //1.使用BCL中的Stack<T> IStackDS <Char> STACK = new LinkStack <char>(); STACK.Push('a'); STACK.Push('b'); STACK.Push('c'); Console.WriteLine(STACK.Count); char temp = STACK.pop(); //取得栈顶数据,并把该数据删除 Console.WriteLine(temp); Console.WriteLine(STACK.Count); char temp2 = STACK.peek(); Console.WriteLine(temp2); Console.ReadKey(); }
static void Main(string[] args) { //Stack<char> stack = new Stack<char>(); //IStackDS<char> stack = new SeqStack<char>(30); IStackDS <char> stack = new LinkStack <char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); Console.WriteLine("push a b c 之后的数据个数为:" + stack.Count); char temp = stack.Pop(); Console.WriteLine("pop之后得到的数据是:" + temp); Console.WriteLine("pop之后,栈中数据的个数是:" + stack.Count); char temp2 = stack.Peek(); Console.WriteLine("peek之后得到的数据是:" + temp2); Console.WriteLine("peek之后,栈中数据的个数是:" + stack.Count); stack.Clear(); Console.WriteLine("clear之后栈中数据的个数:" + stack.Count); Console.ReadKey(); }