943,718 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1653
  • Java RSS
Aug 4th, 2008
0

Unary (-, +) , Parenthesis, Exponent. Need Help

Expand Post »
Java Syntax (Toggle Plain Text)
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class ExpressionCalculator implements KeyListener
  7. {
  8. private JFrame frame;
  9. private JLabel label;
  10. private JPanel boxLayout,panel0,panel1,panel2,panel3;
  11. private JTextField TxtInfix,TxtPostfix,TxtPrefix,TxtResult;
  12.  
  13. private Font font;
  14.  
  15. public ExpressionCalculator()
  16. {
  17. boxLayout = new JPanel();
  18.  
  19. font = new Font("Serif",Font.BOLD,12);
  20.  
  21. //For infix notation
  22. panel0 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  23. panel0.add(label = new JLabel("Infix :"));
  24. panel0.add(TxtInfix = new JTextField("",15));
  25.  
  26. label.setFont(font);
  27.  
  28. label.setPreferredSize(new Dimension(49,16));
  29. label.setHorizontalAlignment(JLabel.RIGHT);
  30.  
  31. //KeyListener
  32. TxtInfix.addKeyListener(this);
  33.  
  34. //For postfix notation
  35. panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  36. panel1.add(label = new JLabel("Postfix :"));
  37. panel1.add(TxtPostfix = new JTextField("",15));
  38.  
  39. label.setFont(font);
  40. label.setPreferredSize(new Dimension(49,16));
  41. label.setHorizontalAlignment(JLabel.RIGHT);
  42.  
  43. TxtPostfix.addKeyListener(this);
  44.  
  45. //For the result of postfix evaluatro
  46. panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  47. panel2.add(label = new JLabel("Prefix :"));
  48. panel2.add(TxtPrefix = new JTextField("",15));
  49.  
  50. label.setFont(font);
  51. label.setPreferredSize(new Dimension(49,16));
  52. label.setHorizontalAlignment(JLabel.RIGHT);
  53.  
  54. panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  55. panel3.add(label = new JLabel("Result :"));
  56. panel3.add(TxtResult = new JTextField("",15));
  57.  
  58. label.setFont(font);
  59. label.setForeground(Color.RED);
  60. label.setPreferredSize(new Dimension(49,16));
  61. label.setHorizontalAlignment(JLabel.RIGHT);
  62.  
  63. TxtResult.setFocusable(false);
  64. }
  65.  
  66. public Container createContent()
  67. {
  68. boxLayout.setLayout(new BoxLayout(boxLayout,
  69. BoxLayout.PAGE_AXIS));
  70.  
  71. boxLayout.setOpaque(true);
  72.  
  73. boxLayout.add(panel0);
  74. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  75. boxLayout.add(panel1);
  76. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  77. boxLayout.add(panel2);
  78. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  79. boxLayout.add(panel3);
  80. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  81.  
  82. boxLayout.setBorder(BorderFactory.createTitledBorder("Notations"));
  83.  
  84. JPanel layoutPanel = new JPanel();
  85.  
  86. layoutPanel.add(boxLayout);
  87.  
  88. return layoutPanel;
  89. }
  90.  
  91. public void keyPressed(KeyEvent e)
  92. {
  93. Object source = e.getSource();
  94. String keyText = e.getKeyText(e.getKeyCode());
  95.  
  96. if(source.equals(TxtInfix))
  97. {
  98. if(keyText.equals("Enter"))
  99. {
  100. String notation;
  101.  
  102. notation = new String(TxtInfix.getText());
  103.  
  104. PostfixNotation i;
  105. i = new PostfixNotation(notation);
  106.  
  107. TxtPostfix.setText(i.postfixNotation());
  108.  
  109. PrefixNotation n;
  110. n = new PrefixNotation(notation);
  111.  
  112. TxtPrefix.setText(n.prefixNotation());
  113.  
  114. PostfixEvaluator p;
  115. p = new PostfixEvaluator(TxtPostfix.getText());
  116.  
  117. TxtResult.setText(p.getResult());
  118. }
  119. }
  120. else if(source.equals(TxtPostfix))
  121. {
  122. if(keyText.equals("Enter"))
  123. {
  124. PostfixEvaluator p;
  125. p = new PostfixEvaluator(TxtPostfix.getText());
  126.  
  127. TxtResult.setText(p.getResult());
  128. }
  129. }
  130. }
  131.  
  132. public void keyReleased(KeyEvent e){}
  133. public void keyTyped(KeyEvent e){}
  134.  
  135. public void showGUI()
  136. {
  137.  
  138. frame = new JFrame("Expression Calculator");
  139. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140. frame.setContentPane(createContent());
  141.  
  142. frame.pack();
  143. frame.setVisible(true);
  144. frame.setResizable(false);
  145. }
  146.  
  147. public static void main(String[] args)
  148. {
  149. Runnable doRun = new Runnable(){
  150. public void run()
  151. {
  152. new ExpressionCalculator().showGUI();
  153. }
  154. };
  155. javax.swing.SwingUtilities.invokeLater(doRun);
  156. }
  157. }

Java Syntax (Toggle Plain Text)
  1.  
  2. public class MyException extends Exception
  3. {
  4. public MyException(String msg)
  5. {
  6. System.out.println(msg);
  7. }
  8. }

Java Syntax (Toggle Plain Text)
  1.  
  2. public class Numeral
  3. {
  4. public static boolean isOperand(String o)
  5. {
  6. boolean operand = true;
  7. for(int i = 0; i < o.length(); i++)
  8. {
  9. char c = o.charAt(i);
  10. if(!Character.isDigit(c))
  11. {
  12. operand = false;
  13. break;
  14. }
  15. }
  16. return operand;
  17. }
  18.  
  19. public static boolean isOperator(String o)
  20. {
  21. return(o.equals("+") || o.equals("*") ||
  22. o.equals("-") || o.equals("/"));
  23. }
  24. }

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3.  
  4. public class PostfixEvaluator
  5. {
  6. StackArray stack;
  7. StringTokenizer st;
  8.  
  9. private int val1,val2;
  10. private int result;
  11.  
  12. public PostfixEvaluator(String postfix)
  13. {
  14. stack = new StackArray();
  15.  
  16. st = new StringTokenizer(postfix);
  17.  
  18. while(st.hasMoreElements())
  19. {
  20. String token = st.nextToken();
  21. if(Numeral.isOperand(token))
  22. {
  23. stack.push(token);
  24. }
  25. else if(Numeral.isOperator(token))
  26. {
  27. if(token.equals("+"))
  28. {
  29. val1 = Integer.parseInt(stack.pop().toString());
  30. val2 = Integer.parseInt(stack.pop().toString());
  31.  
  32. result = val1 + val2;
  33.  
  34. stack.push(new Integer(result));
  35. }
  36. else if(token.equals("-"))
  37. {
  38. val1 = Integer.parseInt(stack.pop().toString());
  39. val2 = Integer.parseInt(stack.pop().toString());
  40.  
  41. result = val2 - val1;
  42.  
  43. stack.push(new Integer(result));
  44. }
  45. else if(token.equals("*"))
  46. {
  47. val1 = Integer.parseInt(stack.pop().toString());
  48. val2 = Integer.parseInt(stack.pop().toString());
  49.  
  50. result = val1 * val2;
  51.  
  52. stack.push(new Integer(result));
  53. }
  54. else if(token.equals("/"))
  55. {
  56. val1 = Integer.parseInt(stack.pop().toString());
  57. val2 = Integer.parseInt(stack.pop().toString());
  58.  
  59. result = val2 / val1;
  60.  
  61. stack.push(new Integer(result));
  62. }
  63. }
  64. }
  65. }
  66.  
  67. public String getResult()
  68. {
  69. return String.valueOf(stack.top());
  70. }
  71. }


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class PostfixNotation
  6. {
  7. private StackArray stack;
  8. private StringTokenizer st;
  9. private StringBuffer postfix = new StringBuffer();
  10.  
  11. public PostfixNotation(String infix)
  12. {
  13. convert(infix);
  14. }
  15.  
  16. public void convert(String infix)
  17. {
  18. stack = new StackArray();
  19.  
  20. st = new StringTokenizer(infix);
  21.  
  22. while(st.hasMoreElements())
  23. {
  24. String token = st.nextToken();
  25.  
  26. if(Numeral.isOperand(token))
  27. {
  28. toPostfix(token);
  29. }
  30.  
  31. else if(Numeral.isOperator(token))
  32. {
  33. if(!stack.isEmpty())
  34. {
  35. if(Priority.icp(token) < Priority.isp(String.valueOf(stack.top())))
  36. {
  37. while(!stack.isEmpty())
  38. {
  39. toPostfix(String.valueOf(stack.pop()));
  40. }
  41.  
  42. stack.push(token);
  43. }
  44. else if(Priority.icp(token) > Priority.isp(String.valueOf(stack.top())))
  45. {
  46. stack.push(token);
  47. }
  48. }
  49. else
  50. {
  51. stack.push(token);
  52. }
  53. }
  54. else
  55. {
  56. JOptionPane.showMessageDialog(null,"Invalid");
  57. break;
  58. }
  59.  
  60. if(!st.hasMoreTokens())
  61. {
  62. while(!stack.isEmpty())
  63. {
  64. toPostfix(String.valueOf(stack.pop()));
  65. }
  66. }
  67. }
  68. }
  69.  
  70. public void toPostfix(String token)
  71. {
  72. postfix.append(token);
  73. postfix.insert(postfix.length()," ");
  74. }
  75.  
  76. public String postfixNotation()
  77. {
  78. return postfix.toString();
  79. }
  80. }

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3.  
  4. public class PrefixNotation
  5. {
  6. private StackArray stack;
  7.  
  8. private StringTokenizer st;
  9.  
  10. private StringBuffer prefix = new StringBuffer();
  11. private String[] tokenOfArray = new String[50];
  12.  
  13. private int topPtr;
  14.  
  15. public PrefixNotation(String infix)
  16. {
  17. convert(infix);
  18. }
  19.  
  20. public void convert(String infix)
  21. {
  22. stack = new StackArray();
  23.  
  24. st = new StringTokenizer(infix);
  25.  
  26. while(st.hasMoreElements())
  27. {
  28. String token = st.nextToken();
  29. tokenOfArray[topPtr++] = token;
  30. }
  31.  
  32. while(topPtr > 0)
  33. {
  34. --topPtr;
  35.  
  36. if(Numeral.isOperand(tokenOfArray[topPtr]))
  37. {
  38. toPostfix(tokenOfArray[topPtr]);
  39. }
  40.  
  41. else if(Numeral.isOperator(tokenOfArray[topPtr]))
  42. {
  43. if(!stack.isEmpty())
  44. {
  45. if(Priority.icp(tokenOfArray[topPtr]) < Priority.isp(String.valueOf(stack.top())))
  46. {
  47. toPostfix(String.valueOf(stack.pop()));
  48.  
  49. stack.push(tokenOfArray[topPtr]);
  50. }
  51. else if(Priority.icp(tokenOfArray[topPtr]) > Priority.isp(String.valueOf(stack.top())))
  52. {
  53. stack.push(tokenOfArray[topPtr]);
  54. }
  55. }
  56. else
  57. {
  58. stack.push(tokenOfArray[topPtr]);
  59. }
  60. }
  61. }
  62.  
  63. if(topPtr == 0)
  64. {
  65. while(!stack.isEmpty())
  66. {
  67. toPostfix(String.valueOf(stack.pop()));
  68. }
  69. }
  70. }
  71.  
  72. //display
  73. public void toPostfix(String token)
  74. {
  75. int offset = 0;
  76. prefix.insert(offset--,token.concat(" "));
  77. }
  78.  
  79. public String prefixNotation()
  80. {
  81. return prefix.toString();
  82. }
  83. }

Java Syntax (Toggle Plain Text)
  1.  
  2. public class Priority
  3. {
  4. public static int icp(String x)
  5. {
  6. int priority = 0;
  7.  
  8. if(x.equals("+") || x.equals("-"))
  9. {
  10. priority = 1;
  11. }
  12. else if(x.equals("/") || x.equals("*"))
  13. {
  14. priority = 3;
  15. }
  16.  
  17. return priority;
  18. }
  19.  
  20. public static int isp(String x)
  21. {
  22. int priority = 0;
  23.  
  24. if(x.equals("+") || x.equals("-"))
  25. {
  26. priority = 2;
  27. }
  28. else if(x.equals("/") || x.equals("*"))
  29. {
  30. priority = 4;
  31. }
  32.  
  33. return priority;
  34. }
  35. }

Java Syntax (Toggle Plain Text)
  1. public class Reverse
  2. {
  3. public StringBuffer rev;
  4. public String[] arr;
  5. public int len;
  6. public int ptr;
  7.  
  8. public String wrd;
  9.  
  10. public Reverse(String word)
  11. {
  12. arr = new String[100];
  13. wrd = word;
  14.  
  15. split(wrd);
  16. }
  17.  
  18. public void split(String word)
  19. {
  20. int offset = 0;
  21. char space = ' ';
  22.  
  23. for(int i = 0; i < word.length() - 1; i++)
  24. {
  25. if(word.charAt(i) == space)
  26. {
  27. arr[ptr++] = word.substring(offset,i);
  28. offset = i;
  29. }
  30. }
  31.  
  32. //last word to store into array
  33. arr[ptr++] = word.substring(offset,(word.length()));
  34. }
  35.  
  36. public String reverse()
  37. {
  38. rev = new StringBuffer();
  39.  
  40. for(int i = 0; i < ptr ; i++)
  41. {
  42. String token = arr[i].trim();
  43. len = token.length();
  44.  
  45. while(len > 0)
  46. {
  47. rev.append(token.charAt(--len));
  48. }
  49.  
  50. rev.insert(rev.length()," ");
  51. }
  52.  
  53. return rev.toString();
  54. }
  55.  
  56. public static void main(String[] args)
  57. {
  58. Reverse p = new Reverse("pare");
  59.  
  60. System.out.println(p.reverse());
  61. System.out.println("\n");
  62.  
  63.  
  64. }
  65. }

Java Syntax (Toggle Plain Text)
  1.  
  2. public interface Stack
  3. {
  4. public void push(Object item);
  5. public Object pop();
  6. public Object top();
  7. public boolean isEmpty();
  8. public boolean isFull();
  9. public void clear();
  10. }

Java Syntax (Toggle Plain Text)
  1.  
  2. public class StackArray implements Stack
  3. {
  4. private Object stack[];
  5. private int sizeIndex = 100;
  6. private int topPtr;
  7.  
  8. public StackArray()
  9. {
  10. stack = new Object[sizeIndex];
  11. }
  12.  
  13. public void push(Object stackItem)
  14. {
  15. if(!isFull())
  16. {
  17. stack[topPtr] = stackItem;
  18. topPtr++;
  19. }
  20. }
  21.  
  22. public Object pop()
  23. {
  24. Object x = null;
  25. if(!isEmpty())
  26. {
  27. x = stack[--topPtr];
  28. stack[topPtr] = null;
  29. }
  30. return x;
  31. }
  32.  
  33. public Object top()
  34. {
  35. Object ts = null;
  36. if(!isEmpty())
  37. {
  38. ts = stack[topPtr - 1];
  39. }
  40. return ts;
  41. }
  42.  
  43. public boolean isEmpty()
  44. {
  45. return topPtr == 0;
  46. }
  47.  
  48. public boolean isFull()
  49. {
  50. return topPtr == sizeIndex;
  51. }
  52.  
  53. public void clear()
  54. {
  55. for(int i =0; i < sizeIndex; i++)
  56. {
  57. stack[i] = null;
  58. topPtr--;
  59. }
  60. }
  61.  
  62. public boolean contains(Object stackItem)
  63. {
  64. boolean contains = false;
  65. if(!isEmpty())
  66. {
  67. for(int i = 0; i < sizeIndex; i++)
  68. {
  69. if(stack[i].equals(stackItem))
  70. {
  71. contains = true;
  72. break;
  73. }
  74. }
  75. }
  76. return contains;
  77. }
  78.  
  79. public String toString()
  80. {
  81. StringBuffer display = new StringBuffer();
  82.  
  83. display.append("{");
  84. for(int i = 0; i< sizeIndex; i++)
  85. {
  86. if(stack[i] != null)
  87. {
  88. display.append(stack[i]);
  89. if( i < topPtr - 1)
  90. {
  91. display.append(",");
  92. }
  93. }
  94. }
  95.  
  96. display.append("}");
  97. return display.toString();
  98. }
  99. }


Hi! Guys I'm back again and need some help for us.

I'm One that Requesting a Calculator Expression before. and now I'm Here That need your help. I have code above that you can see, that code is something downloaded from other site that can i combined of my Idea, They Other code is from a pscode.com, but that program is proven and tested, When I try in my JCreator Language..

This Calculator Expression for MDAS rules, Can you help me or share your about the PEMDAS and unary function. My problem is How to organize the parenthesis , Exponent and Unary code in my downloaded codes and copy paste. I'm beginners but I'm tying my best to do that in that insertion of the codes. If you want to help me. <SNIP> post your suggestion and comments here.

<SNIP>

Thanks.
Last edited by happygeek; Aug 5th, 2008 at 6:51 am. Reason: keep it onsite - email details removed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darlineth is offline Offline
14 posts
since Jun 2008
Aug 5th, 2008
1

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

hi again... here is something that may help:
Java Syntax (Toggle Plain Text)
  1. expression = term && (plusterm || minusterm)*
  2. plusterm = '+' && term
  3. minusterm = '-' && term
  4. term = factor && (timesfactor || dividefactor)*
  5. timesfactor = '*' && factor
  6. dividefactor = '/' && factor
  7. factor = phrase && (expfactor || phrase)
  8. expfactor= '^' && factor
  9. phrase = '(' && expression && ')' || number

here * at the end means 0 or more repeats && means an ordered series

it's just the grammar for arithmetic
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 5th, 2008
0

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

In the future, please post your question before the wall of code, so people don't have to scroll past it all before they even know what you are asking about.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 7th, 2008
0

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
In the future, please post your question before the wall of code, so people don't have to scroll past it all before they even know what you are asking about.

Ezzaral, thanks for the Suggestion.

But My question is understand in above in my post it's clearly and soft, Unary, parenthesis and Exponent. The code that can i post in above is proven and tested of MDAS rules, but i need to used in a PEMDAS rules..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darlineth is offline Offline
14 posts
since Jun 2008
Aug 7th, 2008
0

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

i suggest you read this thread
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 8th, 2008
0

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
i suggest you read this thread
that thread was can't be my code now, i know the code before that can I post is Expression Calculator, bu I can't solve it.. I think that thread was deleted by the admin.


Sorry.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
darlineth is offline Offline
14 posts
since Jun 2008
Aug 8th, 2008
0

Re: Unary (-, +) , Parenthesis, Exponent. Need Help

so what your asking for is for people to explain code that you found on the internet?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: getResourceAsStream vs getResource
Next Thread in Java Forum Timeline: error in code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC