السلام عليكم
لو سمحتوا أبغى كود جافا يحول من
infix to prefix
مع مراعاة أولويات العمليات
لأنه أحتاجه في خطوة من المشروع و ما عندي وقت ابرمجه لانه التسليم قريب
بليز ترى مرة محتاجته
السلام عليكم
لو سمحتوا أبغى كود جافا يحول من
infix to prefix
مع مراعاة أولويات العمليات
لأنه أحتاجه في خطوة من المشروع و ما عندي وقت ابرمجه لانه التسليم قريب
بليز ترى مرة محتاجته
الأخ الكريم/الأخت الكريمة
السلام عليكم ورحمة الله وبركاته
مرحباً بكم في منتدى الفريق العربي للبرمجة
نود تنبيهك أن العنوان غير مناسب.
الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.
وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!
قواعد المشاركة
شاكرين لكم حُسن تعاونكم
الاخ هويدى مشكور بس تحويل
infix to postfix
الي انا ابغاه من
infix to prefix
بس مشكور على ردك
تم تعديل هذه المشاركة بواسطة miss RoOoby في 26 أبريل 2010 في 01:15
معقووولة ما في ولا رد ؟؟؟
بليز الي يعرف جافا لا يبخل عليا مرة محتاجة مساعدة والله و من جد ما رح انسى الي يساعدني من الدعاء
ممكن تعطي لنا الخوارزميه لكي نساعد في برمجتها ؟
لا أعلم ما هي الخوارزمية التي سنتبعها للبرمجة ولكني اعلم كيفية التحويل من infix to prefix كمسألة على الورق و ها هي
القاعدة هي كالتالي :
لنفرض infix = A * B + C / D
نكون ال Tree
سوف يكون شكل الشجرة كالتالي :
+
/ *
A B C D
سنمر على الشجر ( Traversin ) من اليسار الى اليمين بداية من ال root و في كل مرة نمر على يسار احدة ال nodes نقوم بوضعها في ال prefix
بالتالي نحصل على :
AB/CD*+
** ملاحظة عند بناء الشجرة يجب مراعاة الاولوية
هذا رابط يشرح الموضوع اذا لم يكن شرحي واضحا
http://www.cs.man.ac.uk/~pjj/cs2121/fix.html
انا حاولت و كتبت code و حين عمل run للبرنامج يقوم بعمل صحيح فقط على جملة واحد و هي A*(B+C)/D
و لكن حين وضع اي جملة اخرى لا يعمل البرنامج بالشكل الصحيح
سأرفق البرنامج الذي كتبته
import java.util.*;
public class Tryinf
{
public static void main(String [] args)
{
String infix="A*(B+C)/D",prefix="", token="";
String [] Lett = new String[90];
StringTokenizer st1, st2 ;
int br=1,prio,Min=0,opcoun=0,L=0,m=0; //br for the brackets , prio for priority , Min for putting the min priority , opcoun for putting the op to the stack of the op , L counter for putting the letters to the stack , m temp
int tempPrio;
int popLett=0; //counter for poping the letters and putting it in the prefix string
boolean brackClosed = true;
PreTables [] opTemp = new PreTables[1000];
String infop = ("* ( + / ) ");
String inflett= ("A B C D");
st1 = new StringTokenizer(infop);
while (st1.hasMoreTokens())
{
token=st1.nextToken();
System.out.println(token);
if(token.equals("("))
{br++;
// brackClosed = false;
}// if (
else if(token.equals(")"))
br=1;
else if(token.equals("+")||token.equals("-"))
{ prio=br*2;
opTemp[opcoun] = new PreTables(token,prio);
opcoun++; }// + -
else if(token.equals("*")||token.equals("/" ))
{ prio=br*3;
opTemp[opcoun] = new PreTables(token,prio);
opcoun++; }// * /
}//while st1
st2 = new StringTokenizer(inflett);
while (st2.hasMoreTokens())
{
token =st2.nextToken();
Lett[L] = token;
System.out.println(Lett[L]);
L++;
} //while st2
for(int i=0;i<L;i++)
System.out.println(Lett);
for(int z =0;z<opcoun;z++)
System.out.println(opTemp[z]);
int counMin=0;
/// coverting to prefix
for(int i=0; i<opcoun;i++)
{ Min=opTemp.getPrio() ;
for(int j=i+1;j<opcoun;j++)
{ tempPrio=opTemp[j].getPrio();
if(tempPrio<Min)
{
Min=tempPrio;
m=j;
counMin++;
}//if
if(tempPrio==Min)
{
if(i<j)
Min= Min=tempPrio;
m=j;
}
}//inner for
}//for
prefix = prefix +opTemp[m].getToken();
if(counMin>0)
{
for(int z=m-1; z>=0; z--)
{ tempPrio =opTemp[z].getPrio();
token= opTemp[z].getToken();
if(tempPrio==Min&&(!token.equals("null")))
{prefix= prefix+opTemp[z].getToken();
// opTemp[z].setToken("null");
}
}//inner for
}//if there is more than one op has the same priority
prefix = prefix +Lett[popLett];
Lett[popLett]="null";
popLett++;
int c=0;
for(int y=0 ; y<m; y++)
{
tempPrio =opTemp[y].getPrio();
Min =opTemp[m].getPrio();
if(tempPrio>Min&&(!token.equals("null")))
{
prefix = prefix +opTemp[y].getToken();
c++;
opTemp[y].setToken("null");
}
}//for
System.out.println(c);
int r=0;
if(c>0)
{
for(int i=popLett;r<c;i++)
{
prefix = prefix + Lett[popLett];
Lett[popLett]="null";
popLett++;
r++;
}//for
}//if
boolean flag = false;
for(int i=0;i<opcoun;i++)
{ token = opTemp.getToken();
if(token.equals("null"))
flag = true;
}
if(flag==true)
for(int i=popLett;i<L;i++)
{
prefix = prefix + Lett;
Lett="null";
popLett++;
}
System.out.println("\n\nPrefix = "+prefix);
}
}و شكرا
السلام عليكم
نسيت ارفاق ال class التالي
import java.text.NumberFormat;
public class PreTables
{
String token;
int prio;
public PreTables(String tok, int p)
{
token=tok;
prio=p;
}
public String getToken()
{
return token;
}
public void setToken(String tok)
{
token = tok;
}
public int getPrio()
{
return prio;
} //get prio
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = " ";
report +=token+" "+prio+"\n";
return report;
}
}المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية
جارٍ التحقق من المتواجدين…