主要问题:两位数或者多位数怎么办?
答案:找到一个数字时,继续向后扫描,直到不是数字;将扫描到的拼成一个数字,在后缀字符串post中以#号分割
中缀转化为后缀:
package text_6;
public class Queue {
private Object[] original;
private int startLength=20;
private int front;
private int rear;
public Queue() {
original=new Object[startLength];
this.front = 0;
this.rear = 0;
}
public Object[] getOriginal() {
return original;
}
public void setOriginal(Object[] original) {
this.original = original;
}
public int getFront() {
return front;
}
public void setFront(int front) {
this.front = front;
}
public int getRear() {
return rear;
}
public void setRear(int rear) {
this.rear = rear;
}
}
package text_6;
import java.io.IOException;
//import javax.print.attribute.standard.OrientationRequested;
public class Operation {
private Queue queue;
private Object[] Original;
private int startLength=20;
private int top;
private int[] Nunber;
private char[] Char;
static int flag=0;
public int Gettop(){
return top;
}
/*
* 初始化所建立的对象的数组长度和top
*/
public Operation(){
Original=new Object[startLength];
this.top=0;
this.queue=new Queue();
}
/*
* 入队
*/
public void EnQueue(Object object){
Object[] original=queue.getOriginal();
if(queue.getFront()==queue.getRear()){
original[0]=object;
queue.setRear(queue.getRear()+1);
}else{
original[queue.getRear()]=object;
queue.setRear(queue.getRear()+1);
}
}
/*
* 出队
*/
public Object DeQueue(){
Object[] original=queue.getOriginal();
int front=queue.getFront();
if(queue.getRear()==0){
System.out.println("空队");
return null;
}else{
queue.setFront(queue.getFront()+1);
return original[front];
// return object;
}
}
public Object GetRear(){
Object[] original=queue.getOriginal();
if(queue.getRear()==0){
System.out.println("空队");
return null;
}else{
return original[queue.getRear()-1];
}
}
/*
* 进栈,top上移
*/
public void Push(Object object){
if(this.top==0){
this.Original[0]=object;
this.top++;
}else{
Original[this.top]=object;
this.top++;
}
}
/*
* 出栈,并且top的值下移
*/
public Object Pop(){
if(this.top==0){
System.out.println("次栈是空栈");
return null;
}else{
Object object=Original[this.top-1];
this.top--;
return object;
}
}
public Object getTop(){
if(this.Gettop()==0){
System.out.println("空栈");
return null;
}else{
Object object=this.Original[this.top-1];
return object;
}
}
/*
* 判断并返回两个运算符之间的优先级
*/
public int priority(char n,char m){
int flag=1;
char [][] compare=new char[7][7];
return flag;
}
static public int Operator(int m,int n,char Char){
if(Char=='+'||Char=='-'||Char=='*'||Char=='/'){
switch(Char){
case '*':
return m*n;
case '/':
if(n==0){
flag=1;
System.out.println("除0错误操作");
return 000;
}
return m/n;
case '+':
return m+n;
case '-':
return m-n;
default:
return 0;
}
}
else{
System.out.println("非法标志符");
return 000;
}
// return Char;
}
/*
* 若s1优先则返回>,若s2优先则返回<,若s1,s2相同则返回=
*/
static public char Precede(char s1,char s2)
{
char f = 0;
switch(s2)
{
case '+':
case '-':
if(s1=='('|| s1=='#')
f='<';
else
f='>';
break;
case '*':
case '/':
if(s1=='*'||s1=='/'||s1==')')
f='>';
else
f='<';
break;
case '(':
f='<';
break;
case ')':
f='>';
break;
case '#':
if(s1=='#')
f='=';
else
f='>';
break;
default :
break;
}
return f;
}
public char[] InputChar() throws IOException{
char[] in = new char [20];
int i=0;//输入的字符数组的长度。
System.out.println("请输入字符:");
char ch=(char)System.in.read();
while(ch!='#'){
in[i]=ch;
i++;
ch=(char)System.in.read();
}
return in;
}
/*
*中缀计算
*/
public int during(char[] in){
Operation operation=new Operation();
Operation number=new Operation();
operation.Push('#');
number.Push('#');
int x=0,y=0;
for(int j=0;j='0' && c<='9'){
number.Push((int)c-48);System.out.println(" "+number.getTop());
}else{
char s1=(char)operation.getTop();
switch(Precede(s1,c)){
case '>':
if(c==')'){
for(int i=operation.Gettop()-1;i>=0;i--){
char c1=(char)operation.getTop();
if(c1=='('){
operation.Pop();
break;
}
operation.Pop();
x=(int)(number.Pop());
y=(int)(number.Pop());
number.Push(Operator(y,x,c1));System.out.println(" "+number.getTop());
}
}else{
x=(int)(number.Pop());
y=(int)(number.Pop());
number.Push(Operator(y, x, s1));System.out.println(" "+number.getTop());
operation.Pop();
operation.Push(c);
}
break;
case '<':
operation.Push(c);
break;
case '=':
//operation.Pop();
break;
default:
break;
}
}
}
for(int i=operation.Gettop();i>=0;i--){
char c1=(char)operation.getTop();
if(c1=='#'){
break;
}
x=(int)(number.Pop());
y=(int)(number.Pop());
number.Push(Operator(y,x,c1));System.out.println(" "+number.getTop());
operation.Pop();
}
return (int)number.Pop();
}
/*
* 中缀表达式转化成后缀表达式
*/
public Operation Input(char[] in) throws IOException{
Operation operation=new Operation();
Operation number=new Operation();
Operation End=new Operation();
operation.Push('#');
number.Push('#');
for(int j=0;j=48 && c<=57){
System.out.println(c);
End.EnQueue(c);
// End.Push(c);System.out.print("End "+End.getTop());
}else{
if(operation.Gettop()==0){
operation.Push(c);
}else{
char s1=(char)operation.getTop();
switch(Precede(s1,c)){
case '>':
if(c==')'){
for(int k=operation.top-1;k>=0;k--){
char c1=(char)operation.getTop();
if(c1=='('){
operation.Pop();
break;
}
End.EnQueue(operation.getTop());
// End.Push(operation.getTop());System.out.print("End "+End.getTop());
System.out.println(operation.Pop());
}
}else{
// End.Push(operation.getTop());System.out.print("End "+End.getTop());
End.EnQueue(operation.getTop());
System.out.println(operation.Pop());
char c2=(char)operation.getTop();
if(Precede(c2, c)=='>'){
// End.Push(operation.getTop());System.out.print("End "+End.getTop());
End.EnQueue(operation.getTop());
System.out.println(operation.Pop());
}
operation.Push(c);
}
break;
case '<':
operation.Push(c);
break;
}
}
}
// System.out.println(" "+j);
}
System.out.println("栈顶"+operation.Gettop());
for(int j=operation.top-1;j>0;j--){
System.out.println(operation.Original[j]);
End.EnQueue(operation.Original[j]);
}
return End;
}
/*
*
*/
public void front_count(){
}
/*
* 后缀表达式计算;
*/
public void back_count(){
Operation back_CPush=new Operation();
Object object=null;int x=0,y=0;
for(int i=0;i='0' && c<='9'){
back_CPush.Push((int)c-48);
}else{
// System.out.println("shazi"+y+c+x);
x=(int)back_CPush.Pop();
y=(int)back_CPush.Pop();
back_CPush.Push(Operator(y, x, c));
}
}
}
System.out.println(" "+back_CPush.Pop());
}
public void system_Queue(){
System.out.println("输出队中的元素");
for(int i=0;i