最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

反向波兰语符号C#无法正常工作

SEO心得admin34浏览0评论
本文介绍了反向波兰语符号C#无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个带有结构图的rpn.

I write an rpn, with a struktogram.

最新问题:目前无法正常工作.

Newest Problem: It is'nt work correctly now.

如果输入字符串为"5 +((1 + 2)* 4)-3"

If input string is "5 + ((1 + 2) * 4) - 3"

我的输出是:5 1 2 + 4 * 3-+

My output is: 5 1 2 + 4 * 3 - +

我必须得到以下结果:5 1 2 + 4 * + 3-

I have to got this result: 5 1 2 + 4 * + 3 -

编辑了源文件

* 这是最初的问题,但对我有帮助,现在修复了最初的错误:* ,

在调试时循环或 int i = 12,c值为0 \ 0或其他值 然后将此值作为("括号添加到输出(名称:公式)字符串中.我不知道为什么.最后一个'-'运算符,不要添加到(或不看)在输出字符串的末尾(公式)我误认为是由'('引起的.我用其他字符串输入值尝试了该程序,但始终在字符串中加上'(',但我不知道为什么...我看到它与括号的数量无关.总是只有一个'('添加到我的字符串中... *)是的,英语为LengyelFormula = rpn(匈牙利语)*

At the debug when the loop or int i = 12, the c value is 0\0 or something else and this value is added to output (name: formula) string as a '(' bracket. And I don't know why. And the last '-' operation symbol, don't added to (or not look) at the end of output string (formula) I misgave this problem cause by the '('. I tried the program with other string input value, but always put an '(' to my string, and I don't know why... I saw that It was independt about the numbers of bracket. Always only one '(' add to my string...*) Yes, in english LengyelFormula = rpn (it is hungarian)*

static void Main(string[] args) { String str = "5 + ( ( 1 + 2 ) * 4 ) −3"; String result=LengyelFormaKonvertalas(str); Console.WriteLine(result.ToString()); Console.ReadLine(); } static String LengyelFormaKonvertalas(String input) // this is the rpn method { Stack stack = new Stack(); String str = input.Replace(" ",string.Empty); StringBuilder formula = new StringBuilder(); for (int i = 0; i < str.Length; i++) { char x=str[i]; if (x == '(') stack.Push(x); else if (IsOperandus(x)) // is it operand { formula.Append(x); } else if (IsOperator(x)) // is it operation { if (stack.Count>0 && (char)stack.Peek()!='(' && Prior(x)<=Prior((char)stack.Peek()) ) { char y = (char)stack.Pop(); formula.Append(y); } if (stack.Count > 0 && (char)stack.Peek() != '(' && Prior(x) < Prior((char)stack.Peek())) { char y = (char)stack.Pop(); formula.Append(y); } stack.Push(x); } else { char y=(char)stack.Pop(); if (y!='(') { formula.Append(y); } } } while (stack.Count>0) { char c = (char)stack.Pop(); formula.Append(c); } return formula.ToString(); } static bool IsOperator(char c) { return (c=='-'|| c=='+' || c=='*' || c=='/'); } static bool IsOperandus(char c) { return (c>='0' && c<='9' || c=='.'); } static int Prior(char c) { switch (c) { case '=': return 1; case '+': return 2; case '-': return 2; case '*': return 3; case '/': return 3; case '^': return 4; default: throw new ArgumentException("Rossz paraméter"); } } }

推荐答案

using System; using System.Collections.Generic; using System.Text; class Sample { static void Main(string[] args){ String str = "5 + ( ( 1 + 2 ) * 4 ) -3"; String result=LengyelFormaKonvertalas(str); Console.WriteLine(result); Console.ReadLine(); } static String LengyelFormaKonvertalas(String input){ Stack<char> stack = new Stack<char>(); String str = input.Replace(" ", string.Empty); StringBuilder formula = new StringBuilder(); for (int i = 0; i < str.Length; i++){ char x=str[i]; if (x == '(') stack.Push(x); else if (x == ')'){ while(stack.Count>0 && stack.Peek() != '(') formula.Append(stack.Pop()); stack.Pop(); } else if (IsOperandus(x)){ formula.Append(x); } else if (IsOperator(x)) { while(stack.Count>0 && stack.Peek() != '(' && Prior(x)<=Prior(stack.Peek()) ) formula.Append(stack.Pop()); stack.Push(x); } else { char y= stack.Pop(); if (y!='(') formula.Append(y); } } while (stack.Count>0) { formula.Append(stack.Pop()); } return formula.ToString(); } static bool IsOperator(char c){ return (c=='-'|| c=='+' || c=='*' || c=='/'); } static bool IsOperandus(char c){ return (c>='0' && c<='9' || c=='.'); } static int Prior(char c){ switch (c){ case '=': return 1; case '+': return 2; case '-': return 2; case '*': return 3; case '/': return 3; case '^': return 4; default: throw new ArgumentException("Rossz parameter"); } } }
发布评论

评论列表(0)

  1. 暂无评论