Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package TEST;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Created by Dheshan M (@D-codex) on 11/22/2015.
  8. */
  9. public class ExperimentalCalc {
  10. public static String removePoint(String in){
  11. int l=in.length();
  12. char ch,ch1;
  13. for(int i=l-1;i>0;i--){
  14. ch=in.charAt(i);
  15. ch1=in.charAt(i-1);
  16. if((ch=='0')&&(ch1=='.')){
  17. in=in.substring(0,i-1);
  18. break;
  19. }
  20. }
  21. return(in);
  22. }
  23. public static double CalculatorMain (String input){
  24. String buffer;
  25. double numbuffer;
  26. int u=0;
  27. char buf;
  28. input=input.trim();
  29. int l=input.length();
  30. String buffRecursive;
  31. double num[]=new double[25];
  32. char func[]=new char[25];
  33. for(int i=0;i<l;i++){
  34. char chk=input.charAt(i);
  35. if(!Character.isDigit(chk)){
  36. if(chk=='('){
  37. for(int j=i+1;i<l;j++){
  38. char chk0=input.charAt(j);
  39. if (chk0==')'){
  40. buffRecursive=input.substring(i+1,j);
  41. num[u] =CalculatorMain(buffRecursive);
  42. func[u]=input.charAt(j+1);
  43. input=input.substring(j+2);
  44. u = u + 1;
  45. l = input.length();
  46. i = 0;
  47. break;
  48. }
  49. }
  50. }
  51. else if(chk!='.') {
  52. buffer = input.substring(0, i);
  53. numbuffer = Double.parseDouble(buffer);
  54. num[u] = numbuffer;
  55. buf = input.charAt(i);
  56. func[u] = buf;
  57. input = input.substring(i + 1);
  58. u = u + 1;
  59. l = input.length();
  60. i = 0;
  61. }
  62. }
  63. }
  64. numbuffer=Double.parseDouble(input);
  65. num[u]=numbuffer;
  66. for(int i=0;i<=u;i++){
  67. switch(func[i]){
  68. case '+':
  69. num[i+1]=num[i]+num[i+1];
  70. break;
  71.  
  72. case '*':
  73. num[i+1]=num[i]*num[i+1];
  74. break;
  75.  
  76. case '/':
  77. num[i+1]=num[i]/num[i+1];
  78. break;
  79.  
  80. case '-':
  81. num[i+1]=num[i]-num[i+1];
  82. break;
  83. }
  84. }
  85. double ans=num[u];
  86. String output=String.valueOf(ans);
  87. output=removePoint(output);
  88. return(ans);
  89. }
  90. public static void main(String Args[])throws IOException {
  91. Scanner in=new Scanner(System.in);
  92. String input,output;
  93. double out;
  94. System.out.println("Enter you Mathematical Statement \n" +
  95. "[Don't Forget to type the proper Mathematical Symbols]\n" +
  96. "Enter the Equation :");
  97. input=in.nextLine();
  98. out=CalculatorMain(input);
  99. output=removePoint(String.valueOf(out));
  100. System.out.println("RESULT :"+output);
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement