最近的一个实验作业,要求用Java实现多项式的计算,书上的方法是将中序表达式先转化为后序表达式,然后进行运算。想到上学期实现真值表的时候用到的双栈方法,就尝试用双栈实现了一下,感觉要更好一些。最主要的是自己又把双栈敲了一遍,debug的同时也一点点把Java拾了起来,挺好。
代码实现:
节点类:
//定义节点类
class Lnode<T> implements Comparable<Lnode<T>> {
public T data;
public Lnode<T> next;
public Lnode(T key) {
data = key;
next = null;
}
public Lnode(T key, Lnode<T> next) {
data = key;
this.next = next;
}
public boolean equals(Lnode<T> e) {
return data.equals(e);
}
// 比较器
public int compareTo(Lnode<T> e) {
Comparable<T> x;
if (data instanceof Comparable) {
x = (Comparable<T>) data;
return (int) x.compareTo(e.data);
} else
throw new ClassCastException("类型无法比较!");
}
// 重载toString
public String toString() {
return data.toString();
}
}
栈:
public class LinkList<T> {
Lnode<T> first, last;// 首指针和尾指针
int num;// 元素个数
// 比较其,内部使用
private int compare(Lnode<T> a, Lnode<T> b) {
return a.compareTo(b);
}
// 构造函数
public LinkList() {
first = last = null;
num = 0;
}
// 判断链表是否空
public boolean isEmpty() {
return first == null;
}
// 删除整个链表
public void clear() {
first = last = null;
num = 0;
}
// 求链表长度
public int length() {
return num;
}
public int size() {
return num;
}
// 内部使用,取得第i号结点的指针
protected Lnode<T> getNode(int i) {
if (i < 0 || i > num - 1)
return null;
if (i == 0) {
return first;
}
Lnode<T> p = first;
int j = 0;
while (p != null && j < i) {
p = p.next;
j++;
}
return p;
}
public T get(int i) {
Lnode<T> p = getNode(i);
if (p == null)
return null;
else
return p.data;
}
public boolean set(int i, T x) {
Lnode<T> p = getNode(i);
if (p == null)
return false;
else {
p.data = x;
return true;
}
}
// 核心插入算法
public void insert(int i, T x) {
Lnode<T> p, s;
int j = i - 1;
s = new Lnode<T>(x, null);
if (first == null || num == 0) { // 在空链表中插入结点s
first = s;
last = s;
} else if (j < 0) { // 在首节点之前插入结点s
s.next = first;
first = s;
} else if (j >= num - 1) { // 在尾部插入结点s
last.next = s;
last = s;
} else { // 在链表中间插入结点s
p = getNode(j);
s.next = p.next;
p.next = s;
}
num = num + 1;
}
public void add(int i, T x) {
insert(i, x);
}
// 在尾部插入
public void add(T x) {
insert(num, x);
}
public void addBack(T key) {
insert(num, key);
}
public void append(T key) {
insert(num, key);
}
// 在链表首部插入
public void addFront(T key) {
insert(0, key);
}
// 删除第i个结点,返回删除值
public T remove(int i) {
Lnode<T> p;
p = removeNode(i);
if (p != null)
return p.data;
else
return null;
}
// 删除第i个结点,返回结点指针,内部使用
protected Lnode<T> removeNode(int i) {
Lnode<T> p, q;
if (first == null)
return null;
if (i == 0) {
p = first;
first = first.next;
num = num - 1;
return p;
}
if (i >= 1 && i <= num - 1) {
p = getNode(i - 1);
q = p.next;
p.next = q.next;
if (q == last)
last = p;
num = num - 1;
return q;
}
return null;
}
// 删除首节点
public T removeFront() {
Lnode<T> p = null;
p = removeNode(0);
return p.data;
}
// 删除首节点
public T remove() {
Lnode<T> p = null;
p = removeNode(0);
return p.data;
}
// ////////重载toString
public String toString() {
Lnode<T> p = first;
if (p == null)
return "( )";
String str = "(";
while (p != null) {
if (p == last) {
str = str + p.data.toString() + ")";
break;
} else
str = str + p.data.toString() + "->";
p = p.next;
}
return str;
}
// 转换成数组
public Object[] toArray() {
Object[] a = new Object[num];
Lnode<T> p = first;
for (int i = 0; i < num; i++) {
a[i] = p.data;
p = p.next;
}
return a;
}
public <E> E[] toArray(E[] a) {
if (a.length < num)
a = (E[]) java.lang.reflect.Array.newInstance(a.getClass()
.getComponentType(), num);
int i = 0;
Object[] result = a;
Lnode<T> x = this.first;
for (i = 0; i < num; i++) {
result[i] = x.data;
x = x.next;
}
if (a.length > num)
a[num] = null;
return a;
}
// 链表排序,使用插入排序算法
public void sort() // throws NoComparatorOfType
{
LinkList<T> sl = new LinkList<T>();
Lnode<T> p;
p = this.removeNode(0);
while (p != null) {
sl.insertOrder(p);
p = this.removeNode(0);
}
this.first = sl.first;
this.last = sl.last;
this.num = sl.num;
}
public void addSort(T e) // throws NoComparatorOfType
{
Lnode<T> x = new Lnode<T>(e,null);
insertOrder(x);
}
private void insertOrder(Lnode<T> s) {
if( s == null )
return ;
if( num == 0 ){
first = s;
last = s;
num++;
return ;
}
if( compare(s,first) < 0 ){
s.next = first;
first = s;
}
else if( compare(s,last) >= 0 ){
last.next = s;
last = s;
}
else
{
Lnode<T> p,q;
p = first;
q = p;
while( compare(s,p) >= 0 && p!= null ){
q = p;
p = p.next;
}
q.next = s;
s.next = p;
}
num++;
return ;
}
// 查找key在链表中的编号,
public int indexOf(T key) {
Lnode<T> p=this.first;
int i = 0;
while( p.next != null ){
if( p.data.equals(key) ){
return i;
}
p = p.next;
i++;
}
return -1;
}
// 判断key是否在链表中
public boolean contains(T key) {
Lnode<T> p=this.first;
while( p.next != null ){
if( p.data.equals(key) ){
return true;
}
p = p.next;
}
return false;
}
}
实现类:
public class ExpressionCalculate {
char []opeList={‘#‘,‘+‘,‘-‘,‘*‘,‘/‘,‘(‘,‘)‘};
int []inValue={0,3,3,5,5,1,6};
int []outValue={0,2,2,4,4,6,1};
//得到下标
public int getNum(char x){
switch(x){
case ‘#‘:
return 0;
case ‘+‘:
return 1;
case ‘-‘:
return 2;
case ‘*‘:
return 3;
case ‘/‘:
return 4;
case ‘(‘:
return 5;
case ‘)‘:
return 6;
}
return 0;
}
//判断是否为数字
public boolean if_Num(char x){
if( x >= ‘0‘ && x <= ‘9‘ )
return true;
return false;
}
//比较两个符号大小
public char Compare(char x,char y){
int i1 = getNum(x);
int i2 = getNum(y);
if( inValue[i1] > outValue[i2] ){
return ‘>‘;
}
else if( inValue[i1] < outValue[i2] ){
return ‘<‘;
}
else
return ‘=‘;
}
//操作
private int ope(int x,int y,char z){
switch(z){
case ‘+‘:
return x+y;
case ‘-‘:
return x-y;
case ‘*‘:
return x*y;
case ‘/‘:
return x/y;
}
return 0;
}
public int getValue(char[] expre){
int sym=0;
int value=0;
int flag=0;
int a,b,sum=0;
LinkList<Integer> operator = new LinkList();
LinkList<Integer> num = new LinkList();
operator.append( getNum(‘#‘) );
for( int i = 0; i < expre.length ; )
{
// if (expre[i] == ‘-‘) {
// flag = 1;
// i++;
// continue;
// }
if( if_Num(expre[i]) ){
while( i < expre.length && if_Num(expre[i]) ){
sum+=expre[i]-‘0‘;
sum*=10;
i++;
}
sum/=10;
if( flag == 1 ){
sum=-sum;
flag = 0;
}
num.append(sum);
sum=0;
}
else{
char x = opeList[operator.get(operator.num-1)];
switch( Compare(x,expre[i]) ){
case ‘>‘:
int res=0;
a = num.remove(num.num-1);
b = num.remove(num.num-1);
res = ope(b,a,x);
operator.remove(operator.num-1);
num.append(res);
break;
case ‘<‘:
operator.append( getNum(expre[i]) );
i++;
break;
case ‘=‘:
operator.remove(operator.num-1);
i++;
break;
}
}
}
int index=operator.get(operator.num-1);
while( opeList[index] != ‘#‘ ){
int res=0;
a = num.remove(num.num-1);
b = num.remove(num.num-1);
res = ope(b,a,opeList[index]);
operator.remove(operator.num-1);
num.append(res);
index=operator.get(operator.num-1);
}
return value=num.remove(num.num-1);
}
}
原文地址:http://blog.csdn.net/eashion1994/article/details/44805111