标签:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 检查括号是否匹配
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string expression;
Stack<char> leftStack = new Stack<char>();
expression = txtExpression.Text.Trim();
//bool isRight = true;
char c;
for (int i = 0; i < expression.Length; i++)
{
c = expression[i];
//如果是左括号
if (IsLeft(c))
{
leftStack.Push(c);
}
else if (IsRight(c))//如果是右括号
{
//如果栈为空,表明有多余的右括号
if (leftStack.Count == 0)
{
txtResult.Text = "表达式错误:有多余的右括号" + c.ToString();
//isRight = false;
//break;
return;
}
else if (IsPiPei(leftStack.Peek(), c))
//判断取出的右括号是否与栈顶的左括号匹配
{
leftStack.Pop();
}
else
{
txtResult.Text = "表达式错误:右括号"
+ c.ToString() + "与左括号"
+ leftStack.Peek().ToString()
+ "不匹配";
//isRight = false;
//break;
return;
}
}
}
if (leftStack.Count == 0) //&& isRight==true)
{
txtResult.Text = "表达式正确";
}
else
{
txtResult.Text = "表达式错误:有多余的左括号";
}
}
//判断传入的字符是否是左括号
bool IsLeft(char c)
{
if (c == '(' || c == '{' || c == '[')
{
return true;
}
else
{
return false;
}
}
//判断传入的字符是否是右括号
bool IsRight(char c)
{
if (c == ')' || c == '}' || c == ']')
{
return true;
}
else
{
return false;
}
}
//判断传入的左右括号是否匹配
bool IsPiPei(char left, char right)
{
//首先需要验证left为左括号,right为右括号
if (IsLeft(left) && IsRight(right))
{
if (left == '(' && right == ')' ||
left == '{' && right == '}' ||
left == '[' && right == ']'
)
{
return true;
}
else
{
return false;
}
}
else
{
throw new Exception("left应该为左括号,right应该为右括号");
}
}
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 多进制转换
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnTransform_Click(object sender, EventArgs e)
{
string digitChar = "0123456789ABCDEF";
int number;//存放10进制的数
int d;//存放进制数
Stack<char> stack = new Stack<char>();
string result = null; //存放转换后的结果
number = Int32.Parse(txtNumber.Text);
d = Int32.Parse(txtD.Text);
//第一步:将转换结果的每一位数放入栈中
do
{
stack.Push(digitChar[number % d]);
number = number / d;
} while (number != 0);
//第二步:将栈中存放的每一位数出栈,并添加到结果字符串末尾
while (stack.Count != 0)
{
result = result + stack.Pop();
}
txtResult.Text = result;
}
}
}using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 移除火车车厢
{
public partial class Form1 : Form
{
Stack<char> resultStack;
public Form1()
{
InitializeComponent();
}
private void btnRemove_Click(object sender, EventArgs e)
{
char removeChar;
Stack<char> middleStack = new Stack<char>();
if (resultStack == null)
{
resultStack = new Stack<char>();
resultStack.Push('A');
resultStack.Push('B');
resultStack.Push('C');
resultStack.Push('D');
resultStack.Push('E');
}
removeChar = txtRemove.Text[0];
while (resultStack.Count != 0)
{
//如果要移除的编号等于栈顶元素的编号
if (removeChar == resultStack.Peek())
{
resultStack.Pop();
break;
}
else//如果要移除的编号不等于栈顶元素的编号
{
//将结果栈的栈顶元素出栈,并且将此元素放入中间栈中
middleStack.Push(resultStack.Pop());
}
}
while (middleStack.Count != 0)
{
resultStack.Push(middleStack.Pop());
}
txtTrain.Text = "";
foreach (char c in resultStack)
{
txtTrain.Text = txtTrain.Text + c.ToString();
}
//将结果字符串倒转
}
}
}标签:
原文地址:http://blog.csdn.net/zhangchen124/article/details/51637559