Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args)
  4. {
  5. String[] onp = new String[args.length];
  6. String[] stack = new String[10];
  7. int index = -1;
  8.  
  9. int j = 0;
  10. for (String str: args)
  11. {
  12. onp[j] = "";
  13. if(str.endsWith("="))
  14. {
  15. for(int c = 0; c < str.length(); ++c)
  16. {
  17. if(str.charAt(c) >= '0' && str.charAt(c) <= '9' || str.charAt(c) == '.')
  18. {
  19. onp[j] += str.charAt(c);
  20. if(!(str.charAt(c + 1) >= '0' && str.charAt(c + 1) <= '9') && str.charAt(c + 1) != '.')
  21. {
  22. onp[j] += " ";
  23. }
  24. }
  25. else {
  26. switch(str.charAt(c)){
  27.  
  28. case '-':
  29. case '+':
  30.  
  31. while(index >=0 && !stack[index].equals("(")){
  32. onp[j] += stack[index] + " ";
  33. index--;
  34. }
  35.  
  36. stack[++index] = str.charAt(c) + "";
  37. break;
  38.  
  39. case '*':
  40. case '/':
  41.  
  42. while(index >=0 && !stack[index].equals("(") && !stack[index].equals("+") && !stack[index].equals("-")){
  43. onp[j] += stack[index] + " ";
  44. index--;
  45. }
  46.  
  47.  
  48. stack[++index] = str.charAt(c) + "";
  49. break;
  50.  
  51.  
  52. case '^':
  53. while(index >=0 && !stack[index].equals("(") && !stack[index].equals("+") && !stack[index].equals("-") && !stack[index].equals("*") && !stack[index].equals("/")){
  54. onp[j] += stack[index] + " ";
  55. index--;
  56. }
  57.  
  58.  
  59. stack[++index] = str.charAt(c) + "";
  60. break;
  61.  
  62. case '(':
  63.  
  64. stack[++index] = str.charAt(c) + "";
  65. break;
  66. case ')':
  67.  
  68. while(index >= 0 && !stack[index].equals("(")){
  69. onp[j] += stack[index] + " ";
  70. index--;
  71. }
  72. index--;
  73. break;
  74.  
  75. case '=':
  76. while(index >= 0 ){
  77. onp[j] += stack[index] + " ";
  78. index--;
  79. }
  80. break;
  81.  
  82.  
  83. default:
  84. break;
  85. }
  86. }
  87. }
  88.  
  89. }
  90. else
  91. {
  92. System.out.println("Wprowadzone równanie: '" + str + "' jest blędne.");
  93. }
  94. System.out.println(onp[j]);
  95. j++;
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement