进制转换
class conversion1{
public static void conver(int i,int k) {
Stack1 s1=new Stack1(100);
while(i>0) {
s1.push(new Integer(i%k));
i=i/k;
}
while(!s1.isempty()) {
if((int)s1.get()>=10) {
System.out.print((char)((int)s1.pop()+'a'-10));
}
else {
System.out.print(s1.pop());
}
}
}
public static void main(String[] args) {
conver(30,2);
}
}
符号匹配
方法一:
class match1{
public static void ismatch(String s) {
Stack1 s1=new Stack1(s.length());
for(int i=0;i<s.length();i++) {
char a = s.charAt(i);
switch(a) {
case '(':
s1.push(a);
break;
case '[':
s1.push(a);
break;
case '{':
s1.push(a);
break;
case ')':
if(!s1.isempty()&&s1.get().equals('(')) {
s1.pop();
break;
}else {
System.out.println("匹配不成功");
System.out.println("期望(");
return;
}
case ']':
if(!s1.isempty()&&s1.get().equals('[')) {
s1.pop();
break;
}else {
System.out.println("匹配不成功");
System.out.println("期望[");
return;
}
case '}':
if(!s1.isempty()&&s1.get().equals('{')) {
s1.pop();
break;
}else {
System.out.println("匹配不成功");
System.out.println("期望{");
return;
}
}
}
if(s1.isempty()) {
System.out.println("匹配成功");
}
}
public static void main(String[] args) {
ismatch("((({[]})))");
}
}
方法二:
class match{
public static void ismatched(String s) {
Stack1 s1=new Stack1(s.length());
for(int i=0;i<s.length();i++) {
char ch = s.charAt(i);
switch(ch) {
case '(':
s1.push(ch+"");
break;
case '{':
s1.push(ch+"");
break;
case '[':
s1.push(ch+"");
break;
case ')':
if(!s1.isempty()&&s1.get().equals("(")) {
s1.pop();
break;
}else {
System.out.println("期望(");
return;
}
case '}':
if(!s1.isempty()&&s1.get().equals("{")) {
s1.pop();
break;
}else {
System.out.println("期望{");
return;
}
case ']':
if(!s1.isempty()&&s1.get().equals("[")) {
s1.pop();
break;
}
else {
System.out.println("期望[");
return;
}
}
}
if(s1.isempty()) {
System.out.println("匹配成功");
}
}
}
两种方法不同之处在于:
push()进栈的一个是char型数据,一个是字符串型数据。
导致后面equal()里面的一个是" ",一个是’ ’