ทำยังไงให้เข้าใจโครงสร้างข้อมูลอัลกอริทึ่มมากขึ้นคะ (java) เราพยายามแล้วค่ะซื้อหนังสือมาอ่าน อ่านตามห้องสมุด หาตามเนต ใช้stackoverflow
แล้วยังไม่รู้เรื่องค่ะ ไม่รู้ว่าเป็นเพราะสมองเราlogicต่ำหรือเปล่าไม่รู้ ทฤษฏีอาจจะถูไปได้(แต่ก็สอบตกเหลืออีก3คะแนนจะผ่าน) พอมาแปลงเป็นโค้ดจาวาเรางงมากเลยค่ะ ใครมีเทคนิคดีๆได้โปรดช่วยชี้แนะทีค่ะ พลีสสสสส ขอบคุณค่ะ
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้เรารู้สึกแย่มากคือเท่าไหร่ก็ไม่เข้าใจ ไปต่อไม่ได้ โดนทิ้งไว้กลางทาง
นี่โค้ดเราค่ะ เราพยายามแล้ว แต่กดช้อยต์สามมันไม่ออก....อะไรเลยค่ะ
import java.util.Scanner;
class ArithmeticExpression
{
public static void main(String args[])
{
Scanner kb = new Scanner(System.in);
IntArrayStack stack = new IntArrayStack();
int choice=0,index;
String output, expression = "";
char ch,ch1,ch2;
int o=0,v=0,answer=0;
int result;
do{
System.out.println("=============Menu==========");
System.out.println("= 1. Input Infix =");
System.out.println("= 2. Convert to Postfix =");
System.out.println("= 3. Calculate Expression =");
System.out.println("= 0. Exit =");
System.out.println("=============Menu==========");
System.out.print("===>Select choice ::");
choice=kb.nextInt();
switch(choice)
{
case 1: System.out.print("Input Infixed Expression : ");
expression = kb.next();
break;
case 2: output = "";
for(int i = 0;i <expression.length();i++)
{
ch = expression.charAt(i);
if ((ch >= 'A' && ch <= 'Z')||(ch >= 'a' && ch <= 'z'))
{
output = output + ch;
}
else if (stack.isEmpty())
{
stack.push(ch);
}
else if (ch =='(')
{
stack.push(ch);
}
else if (ch == ')')
{
while(stack .top() != ')')
output = output + (char)stack.pop();
stack.pop();
}
else
{
if (priority(ch) > priority((char)stack.top()))
{
stack.push(ch);
}
else
{
while(priority(ch) <= priority((char)stack.top()))
output = output + (char)stack.pop();
stack.push(ch);
}
}
}
while(!stack.isEmpty())
{
output = output + (char)stack.pop();
}
System.out.println("Porfixed Expression is "+output);
break;
case 3: //a to i 1 --> 9
output = "";
int a,A = 1;
int b,B = 2;
int c,C = 3;
int d,D = 4;
int e,E = 5;
int f,F = 6;
int g,G = 7;
int h,H = 8;
int i,I = 9;
for(int w = 0;w <expression.length();w++)
{
ch = expression.charAt(w);
if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/'))
{
ch1=(char)stack.pop();
ch2=(char)stack.pop();
if(ch1 == 'A') o = A;
else if(ch1 == 'B') o = B;
else if(ch1 == 'C') o = C;
else if(ch1 == 'D') o = D;
else if(ch1 == 'E') o = E;
else if(ch1 == 'F') o = F;
else if(ch1 == 'G') o = G;
else if(ch1 == 'H') o = H;
else if(ch1 == 'I') o = I;
if(ch2 == 'A') v = A;
else if(ch2 == 'B') v = B;
else if(ch2 == 'C') v = C;
else if(ch2 == 'D') v = D;
else if(ch2 == 'E') v = E;
else if(ch2 == 'F') v = F;
else if(ch2 == 'G') v = G;
else if(ch2 == 'H') v = H;
else if(ch2 == 'I') v = I;
if(ch== '+') answer = o+v;
else if(ch== '-') answer = o-v;
else if(ch== '*') answer = o*v;
else if(ch== '/') answer = o/v;
stack.push(answer);
}
else if (stack.isEmpty())
{
stack.push(ch);
}
else if (ch =='(')
{
stack.push(ch);
}
else if (ch == ')')
{
while(stack .top() != ')')
output = output + (char)stack.pop();
stack.pop();
}
else
{
if (priority(ch) > priority((char)stack.top()))
{
stack.push(ch);
}
else
{
while(priority(ch) <= priority((char)stack.top()))
output = output + (char)stack.pop();
stack.push(ch);
}
}
}
while(!stack.isEmpty())
{
output = output + (char)stack.pop();
}
System.out.println("Postfix Calculate is "+answer);
break;
case 0: System.exit(0);
default: System.out.println("Invalid choice");
}
}while(choice!=0);
}
public static int priority(char ch)
{
if(ch=='^')
return 3;
if(ch=='+' || ch=='-')
return 1;
if(ch=='*' || ch=='/')
return 2;
return 0;
}
}
ทำยังไงให้เข้าใจโครงสร้างข้อมูลอัลกอริทึ่มมากขึ้นคะ (java)
แล้วยังไม่รู้เรื่องค่ะ ไม่รู้ว่าเป็นเพราะสมองเราlogicต่ำหรือเปล่าไม่รู้ ทฤษฏีอาจจะถูไปได้(แต่ก็สอบตกเหลืออีก3คะแนนจะผ่าน) พอมาแปลงเป็นโค้ดจาวาเรางงมากเลยค่ะ ใครมีเทคนิคดีๆได้โปรดช่วยชี้แนะทีค่ะ พลีสสสสส ขอบคุณค่ะ
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้