如何使用堆栈检测 } 括号错误?
我的代码可以检测到 { 括号错误,如果文件中只有 } 括号,那么它将抛出空堆栈异常。我想知道如何检测两个括号错误,如果 { this 或 } 这个括号丢失,那么它将显示错误。 类 StackCS {
#region Vaiables
Stack stack;
char[] charArray;
string[] stringArray;
int linenumber;
string chError = string.Empty;
public string ChError { get { return chError +" "+ linenumber; } set { chError = value; } }
public StackCS()
{
charArray = new char[1000];
stack = new Stack();
}
#endregion
#region File
private void ReadCharFromFile(string add)
{
using (StreamReader rdr = new StreamReader(add))
{
for (int i = 0; i < charArray.Length; i++)
{
charArray[i] = (char)rdr.Read();
}
}
}
public void ReadFile(string add)
{
ReadCharFromFile(add);
stringArray = File.ReadAllLines(add);
}
#endregion
#region Stack
public void FillingStack()
{
for (int i = 0; i < charArray.Length; i++)
{
if (charArray[i] == '{')
{
stack.Push('{');
}
else if (charArray[i] == '}')
stack.Pop();
}
}
public void CheckingStack()
{
for (int i = 0; i < stringArray.Length; i++)
{
if (stack.Count != 0)
{
chError = "Error At:";
linenumber = i;
}
else
{
chError = "No Error";
}
}
}
public override string ToString()
{
string temp = string.Empty;
for (int i = 0; i < stringArray.Length; i++)
{
temp +=i+"\t\t"+stringArray[i] + "\n";
}
return temp;
}
#endregion
}
static void Main(string[] args)
{
StackCS obj = new StackCS();
try
{
// set the file location, according to ur PC setting
obj.ReadFile(@"C:\Users\5609\Desktop\Class1.cs");
obj.FillingStack();
obj.CheckingStack();
Console.WriteLine("Line Number" + " " + "File Content"+"\n");
Console.WriteLine(obj);
Console.WriteLine(obj.ChError);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
obj.CheckingStack();
Console.WriteLine("Line Number" + " " + "File Content" + "\n");
Console.WriteLine(obj);
Console.WriteLine(obj.ChError);
}
}
My code can detect { bracket error, if in file there is only } bracket then it will throw empty stack exception. I want to know how can I detect both bracket errors, if { this or } this bracket missing then it will show error.
class StackCS
{
#region Vaiables
Stack stack;
char[] charArray;
string[] stringArray;
int linenumber;
string chError = string.Empty;
public string ChError { get { return chError +" "+ linenumber; } set { chError = value; } }
public StackCS()
{
charArray = new char[1000];
stack = new Stack();
}
#endregion
#region File
private void ReadCharFromFile(string add)
{
using (StreamReader rdr = new StreamReader(add))
{
for (int i = 0; i < charArray.Length; i++)
{
charArray[i] = (char)rdr.Read();
}
}
}
public void ReadFile(string add)
{
ReadCharFromFile(add);
stringArray = File.ReadAllLines(add);
}
#endregion
#region Stack
public void FillingStack()
{
for (int i = 0; i < charArray.Length; i++)
{
if (charArray[i] == '{')
{
stack.Push('{');
}
else if (charArray[i] == '}')
stack.Pop();
}
}
public void CheckingStack()
{
for (int i = 0; i < stringArray.Length; i++)
{
if (stack.Count != 0)
{
chError = "Error At:";
linenumber = i;
}
else
{
chError = "No Error";
}
}
}
public override string ToString()
{
string temp = string.Empty;
for (int i = 0; i < stringArray.Length; i++)
{
temp +=i+"\t\t"+stringArray[i] + "\n";
}
return temp;
}
#endregion
}
static void Main(string[] args)
{
StackCS obj = new StackCS();
try
{
// set the file location, according to ur PC setting
obj.ReadFile(@"C:\Users\5609\Desktop\Class1.cs");
obj.FillingStack();
obj.CheckingStack();
Console.WriteLine("Line Number" + " " + "File Content"+"\n");
Console.WriteLine(obj);
Console.WriteLine(obj.ChError);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
obj.CheckingStack();
Console.WriteLine("Line Number" + " " + "File Content" + "\n");
Console.WriteLine(obj);
Console.WriteLine(obj.ChError);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有最后的右大括号,那么堆栈上会留下一些内容,如果堆栈中有任何元素,最后只需返回即可。如果不这样做,则表达式无效。
另一种情况是您有一个没有正式表达式的右大括号,在这种情况下您会遇到 } 但堆栈为空(错误)。
因此,一旦检查了堆栈,您就可以确保 { } 中包含的所有表达式都是正确的,而不是堆栈上没有未检查的元素,因此 stack.Empty() 为 true 则表达式有效。
If you don't have a final closing brace then something will be left on the stack, at the end simply return if you have any elements in the stack. If you dont the expression is invalid.
The other case is that you have a closing brace without a formal expression in which case you encounter } but the stack is empty (error).
So once you've checked the stack you have made sure that all expressions contained in { } are proper than that no elements were left unchecked on the stack so stack.Empty() is true then the expression is valid.