السلام عليكم
انا عندي هادا البرنامج شغال
وبيحول من infix لل postfix
لكن المشكلة اني لو دخلت رقم من كاركتار واحد بيشتغل صح وبيعمل
evaluation صح
لكن لو دخلت رقم من اكتر من كاركتر يعني مثلا 88-8
بيحوّل صح للpostfix
لكن بيعمل evaluationغلط
كود
public class Node {
Object data;
Node next;
public Node(){
}
public Node(Object info,Node n){
this.data=info;
this.next=n;
}
public Object getData(){
return this.data;
}
public void setData(Object data){
this.data=data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
Object data;
Node next;
public Node(){
}
public Node(Object info,Node n){
this.data=info;
this.next=n;
}
public Object getData(){
return this.data;
}
public void setData(Object data){
this.data=data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
كود
public class Stack{
private static Node topNode;
private static int size;
public static boolean isEmpty(){
return (topNode==null);
}
public static int size(){
return size;
}
public static void push(Object ch){
Node newNode = new Node(ch,null);
if(isEmpty())
topNode = newNode;
else{
newNode.setNext(topNode);
topNode = newNode;
}
}
public Object pop(){
Object temp=' ';
if(!isEmpty()){
temp = topNode.getData();
topNode = topNode.getNext();
}
return temp;
}
public Object top(){
if(isEmpty())
System.out.println("Empty stack");
return topNode.getData();
}
public int prec(Object n)
{
if(n.equals('+')||n.equals('-'))
return 1;
else if(n.equals('*')||n.equals('/'))
return 2;
else if(n.equals('^'))
return 3;
else
return 0;
}
public String postfix(String s)
{
String output="";
Stack S=new Stack();
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c==('+')||c==('-')||c==('*')||c==('/')||c==('^'))
{
while(!S.isEmpty() &&prec(S.top())>= prec(c))
output+=S.pop();
S.push(c);
}
else if(c=='(')
{
S.push(c);
}
else if(c==')')
{
while(!S.top().equals('('))
output+=S.pop();
S.pop();
}
else
output+=c;
}
while(!S.isEmpty())
output+=S.pop();
return output;
}
public String Evaluation(String x)
{
Stack S=new Stack();
for(int i=0;i<x.length();i++)
{
char c=x.charAt(i);
String s="0"+c;
if(c=='+')
{
int z=Integer.parseInt((String)S.pop())+Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='*')
{
int z=Integer.parseInt((String)S.pop())*Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='/')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())/u;
S.push(Integer.toString(z));
}
else if(c=='-')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())-u;
S.push(Integer.toString(z));
}
else if(c=='^')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop());
int m=((int)Math.pow(z,u));
S.push(Integer.toString(m));
}
else
S.push(s);
}
return (String)S.pop();
}
}
private static Node topNode;
private static int size;
public static boolean isEmpty(){
return (topNode==null);
}
public static int size(){
return size;
}
public static void push(Object ch){
Node newNode = new Node(ch,null);
if(isEmpty())
topNode = newNode;
else{
newNode.setNext(topNode);
topNode = newNode;
}
}
public Object pop(){
Object temp=' ';
if(!isEmpty()){
temp = topNode.getData();
topNode = topNode.getNext();
}
return temp;
}
public Object top(){
if(isEmpty())
System.out.println("Empty stack");
return topNode.getData();
}
public int prec(Object n)
{
if(n.equals('+')||n.equals('-'))
return 1;
else if(n.equals('*')||n.equals('/'))
return 2;
else if(n.equals('^'))
return 3;
else
return 0;
}
public String postfix(String s)
{
String output="";
Stack S=new Stack();
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c==('+')||c==('-')||c==('*')||c==('/')||c==('^'))
{
while(!S.isEmpty() &&prec(S.top())>= prec(c))
output+=S.pop();
S.push(c);
}
else if(c=='(')
{
S.push(c);
}
else if(c==')')
{
while(!S.top().equals('('))
output+=S.pop();
S.pop();
}
else
output+=c;
}
while(!S.isEmpty())
output+=S.pop();
return output;
}
public String Evaluation(String x)
{
Stack S=new Stack();
for(int i=0;i<x.length();i++)
{
char c=x.charAt(i);
String s="0"+c;
if(c=='+')
{
int z=Integer.parseInt((String)S.pop())+Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='*')
{
int z=Integer.parseInt((String)S.pop())*Integer.parseInt((String)S.pop());
S.push(Integer.toString(z));
}
else if(c=='/')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())/u;
S.push(Integer.toString(z));
}
else if(c=='-')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop())-u;
S.push(Integer.toString(z));
}
else if(c=='^')
{ int u=Integer.parseInt((String)S.pop());
int z=Integer.parseInt((String)S.pop());
int m=((int)Math.pow(z,u));
S.push(Integer.toString(m));
}
else
S.push(s);
}
return (String)S.pop();
}
}
لو سمحتوا تساعدوني فيه اذا ممكن
شكرا الكم