1. 定義優(yōu)先級(jí)和優(yōu)先級(jí)表
我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好網(wǎng)站制作、網(wǎng)站建設(shè)服務(wù),我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)公司贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),成都小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。
Java代碼
/**
* 運(yùn)算符優(yōu)先權(quán)
*/
public enum Precede {
/**
* 優(yōu)先權(quán)高
*/
LARGER,
/**
* 優(yōu)先權(quán)低
*/
LESS;
/**
* 優(yōu)先級(jí)表
* + - * /
* +
* -
* *
* /
*/
private static Precede[][] precedes = new Precede[4][4];
static {
// 根據(jù)優(yōu)先級(jí)表初始化precedes數(shù)組
for (int i = 0; i precedes.length; i++) {
for (int j = 0; j precedes[i].length; j++) {
if ((i == 0 || i == 1) j 1) {
precedes[i][j] = LESS;
} else {
precedes[i][j] = LARGER;
}
}
}
}
/**
* 判斷2個(gè)運(yùn)算符的優(yōu)先級(jí)
*/
public static Precede judgePrecede(char operand1, char operand2) {
int left = getIndex(operand1);
int right = getIndex(operand2);
return precedes[left][right];
}
/**
* 獲取運(yùn)算符對(duì)應(yīng)的數(shù)組索引
*/
private static int getIndex(char operand) {
switch (operand) {
case '+':
return 0;
case '-':
return 1;
case '*':
return 2;
case '/':
return 3;
default:
throw new IllegalArgumentException();
}
}
}
2. 表達(dá)式求值
Java代碼
/**
* 整數(shù)表達(dá)式運(yùn)算
*/
public class EvaluateExpression {
/**
* 表達(dá)式
*/
private String expression;
/**
* 最初的表達(dá)式
*/
private String initExpression;
/**
* 運(yùn)算符棧
*/
private MyStackCharacter optr = new MyArrayStackCharacter();
/**
* 操作數(shù)棧
*/
private MyStackInteger opnd = new MyArrayStackInteger();
/**
* 表明下一個(gè)是否應(yīng)該是數(shù)字
*/
private boolean numberNext;
public EvaluateExpression(String expression) {
this.expression = expression;
this.initExpression = expression;
numberNext = true;
}
/**
* 求值
*/
public Integer evaluate() {
delBlank();
handleParentheses();
while (true) {
if ("".equals(expression)) {
break;
}
if (expression.matches("^-?\\d+.*$") numberNext) {
opnd.push(getInteger());
continue;
} else {
Character operand = expression.charAt(0);
numberNext = true;
expression = expression.substring(1);
Character pop = optr.pop();
if (pop == null) {
optr.push(operand);
continue;
} else {
Precede precede = Precede.judgePrecede(pop, operand);
switch (precede) {
// 優(yōu)先級(jí)高時(shí)運(yùn)算前2個(gè)操作數(shù)
case LARGER: {
optr.push(operand);
Integer next = opnd.pop();
Integer last = opnd.pop();
evaluateNow(last, pop, next);
break;
}
// 優(yōu)先級(jí)低時(shí)運(yùn)算前一個(gè)操作數(shù)和后一個(gè)操作數(shù)
case LESS: {
optr.push(pop);
Integer last = opnd.pop();
Integer next = getInteger();
evaluateNow(last, operand, next);
break;
}
}
}
}
}
// 運(yùn)算結(jié)果
Integer result = null;
if (optr.length() == 0 opnd.length() == 1) {
result = opnd.pop();
} else if (optr.length() == 1 opnd.length() == 2) {
Integer next = opnd.pop();
Integer last = opnd.pop();
evaluateNow(last, optr.pop(), next);
result = opnd.pop();
} else {
throw new RuntimeException();
}
return result;
}
/**
* 進(jìn)行實(shí)際的運(yùn)算,并將結(jié)果入棧
*/
private void evaluateNow(Integer last, Character operand, Integer next) {
switch (operand) {
case '+':
opnd.push(last + next);
break;
case '-':
opnd.push(last - next);
break;
case '*':
opnd.push(last * next);
break;
case '/':
opnd.push(last / next);
break;
}
}
/**
* 獲得表達(dá)式開頭部分的整數(shù)
*/
private Integer getInteger() {
StringBuilder sb = new StringBuilder();
int count = 0; // 整數(shù)位
boolean lessZero = false; // 是否是負(fù)數(shù)
if (expression.startsWith("-")) {
sb.append("-");
count++;
lessZero = true;
}
int i = (lessZero ? 1 : 0);
for (; i expression.length(); i++) {
char c = expression.charAt(i);
if (c = '0' c = '9') {
sb.append(c);
count++;
} else {
break;
}
}
expression = expression.substring(count);
numberNext = false;
return Integer.valueOf(sb.toString());
}
/**
* 處理括號(hào). 將括號(hào)內(nèi)的字符串作為子表達(dá)式計(jì)算.
*/
private void handleParentheses() {
while (expression.contains("(")) {
// 左括號(hào)的索引
int left = 0;
// 右括號(hào)的索引
int right = 0;
// 左括號(hào)的數(shù)量
int count = 0;
// 求出左括號(hào)索引
left = expression.indexOf('(');
// 求出對(duì)應(yīng)的右括號(hào)索引
for (int i = left; i expression.length(); i++) {
char c = expression.charAt(i);
if (c == ')') {
count--;
// count為0時(shí)才是對(duì)應(yīng)的右括號(hào)
if (count == 0) {
right = i;
break;
}
} else if (c == '(') {
count++;
} else {
continue;
}
}
// 左右括號(hào)之間是一個(gè)子表達(dá)式, 計(jì)算子表達(dá)式的值,并根據(jù)結(jié)果構(gòu)造出新的表達(dá)式
EvaluateExpression evaluateExpression = new EvaluateExpression(expression.substring(left + 1, right));
expression = expression.substring(0, left) + evaluateExpression.evaluate()
+ expression.substring(right + 1);
}
}
/**
* 刪除表達(dá)式中的空白字符
*/
private void delBlank() {
expression = expression.replaceAll("\\s", "");
}
@Override
public String toString() {
return initExpression;
}
}
3. 進(jìn)行測(cè)試
Java代碼
@Test
public void testEvaluate() {
EvaluateExpression expression = new EvaluateExpression("1 + 2 ");
System.out.println(expression + " = " + expression.evaluate());
expression = new EvaluateExpression("4 + 2 * 3 - 10 / 5");
System.out.println(expression + " = " + expression.evaluate());
expression = new EvaluateExpression("(1+2) * (4 + 5) - (9 / 7)");
System.out.println(expression + " = " + expression.evaluate());
expression = new EvaluateExpression("(1 + (3 * (4 - 9)))");
System.out.println(expression + " = " + expression.evaluate());
expression = new EvaluateExpression("(1 + (3 * (4 - 9))) + (3 * (2 + 3))");
System.out.println(expression + " = " + expression.evaluate());
}
測(cè)試的結(jié)果為:
1 + 2 = 3
4 + 2 * 3 - 10 / 5 = 8
(1+2) * (4 + 5) - (9 / 7) = 26
(1 + (3 * (4 - 9))) = -14
(1 + (3 * (4 - 9))) + (3 * (2 + 3)) = 1
import java.io.IOException;
import java.util.Scanner;
public class LinkList {
private static Scanner san = new Scanner(System.in);
public static void main(String[] args) throws IOException {
List list = new List();
for (int i = 1; i = 10; i++) {
System.out.print("請(qǐng)輸入第" + i + "個(gè)數(shù): ");
list.add(san.nextInt());
list.print();
}
System.out.println("輸入的數(shù)據(jù)如下: ");
list.print();
}
}
class node {
int data;
node next = this; // 指向自己
}
class List {
private node header = new node();
// 循環(huán)鏈表的尾部添加數(shù)據(jù)
public node add(int data) {
node current = new node();
node temp = header;
while (temp.next != header)
temp = temp.next;
current.data = data;
current.next = temp.next;
temp.next = current;
return current;
}
// 查詢某個(gè)數(shù)字的位置 如果不在 返回-1;
public int search(int data) {
node temp = header;
int n = 0;
while (temp.next != header) {
temp = temp.next;
n++;
if (temp.data == data)
break;
}
if (temp.data == data)
return n;
else
return -1;
}
// 打印出整個(gè)鏈表
public void print() {
node temp = header;
while (temp.next != header) {
temp = temp.next;
System.out.print(temp.data + " ");
}
System.out.println();
}
// 插入數(shù)據(jù)
public node Insert(int pos, int data) {
node temp = header;
node current = new node();
for (int i = 0; i pos - 1; i++) {
if (temp.next != header) {
temp = temp.next;
} else
return null;
}
current.data = data;
if (temp.next != header) {
current.next = temp.next;
}
temp.next = current;
return current;
}
// 刪除某個(gè)數(shù)據(jù)
public node del(int data) {
node temp = header;
node oldtemp = null;
node current = null;
while (temp.next != header) {
oldtemp = temp;
temp = temp.next;
if (temp.data == data) {
current = temp;
break;
}
}
if (current == header)
return null;
oldtemp.next = current.next;
return current;
}
}
public class Test {
public static void main(String[] args) {
int length = 5;
int ai = 1;
String data = "data";
String[] array = insertArrar(data, ai, length);
data = delArray(array, ai, length);
System.out.println(data);
}
public static String[] insertArrar(String data,int ai,int length){
String[] array = new String[length];
array[ai] = data;
return array;
}
public static String delArray(String[] array,int ai,int length){
String data = "";
data=array[ai];
array[ai]=null;
for(int i = 0; iarray.length;i++){
System.out.println(array[i]);
}
return data;
}
}
首先Java沒有指針(為了安全和方便編程).
其次數(shù)據(jù)結(jié)構(gòu)和指針無關(guān),和語言也無關(guān).
Java封裝好了各種基本的數(shù)據(jù)結(jié)構(gòu) 比如:
數(shù)組,隊(duì)列,Stack,HashTable,HashSet,HaspMap等等
你說的順序表 Java中你可以用:
ArrayList 這個(gè)類:
例子:
如果你自己想用Java裸寫一個(gè)類似功能的類可以參考這個(gè):
網(wǎng)頁題目:java數(shù)據(jù)結(jié)構(gòu)代碼實(shí)現(xiàn),數(shù)據(jù)結(jié)構(gòu) java版
文章源于:http://aaarwkj.com/article8/dsshsop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站排名、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)