Guest User

Untitled

a guest
Jan 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. package com.example.a24107.computeapplication2;
  2. import java.text.ParseException;
  3. /**
  4. * Created by 24107 on 2017/12/8.
  5. */
  6.  
  7. public class StrToNum {
  8. /**
  9. * 从左往右遍历,先把括号找出来,再计算括号中的值,去掉括号,再计算*和/,再计算+和-,
  10. */
  11.  
  12. //public static int kuoCount = 0;
  13. public static int[] kuoweizhi;
  14. public static String str = "5×((2+2)+2)+((2+2)÷2)";
  15. //5×((2+2)+2)+2+2÷2
  16.  
  17. public static void main(String[] args){
  18. String answer = LeftToRightCompute(str);
  19. System.out.println(answer);
  20. }
  21.  
  22. public static void Swap(int a,int b){
  23. int temp=a;
  24. a=b;
  25. b=temp;
  26. }
  27.  
  28. public static String LeftToRightCompute(String str) {
  29. int numcount = -1;
  30. float firstnum = 1;
  31. float answer = 0;
  32. for (int i = 0; i < str.length(); i++) {
  33. if (str.charAt(i) >= '0' && str.charAt(i) <= '9'||str.charAt(i)=='.') {
  34. if (numcount==-1) {
  35. numcount = i;
  36. }
  37. firstnum = Float.parseFloat(str.substring(numcount, i+1));
  38. answer = firstnum;
  39. //return answer;
  40. }
  41. else {
  42. switch (str.charAt(i)) {
  43. case '+'://当我加的时候,其实是加后面的整个结果
  44. numcount = -1;
  45. answer=firstnum + Float.parseFloat(LeftToRightCompute(str.substring(i + 1)));
  46. return answer+"";
  47. //break;
  48. case '-'://当我减的时候,其实是减后面的整个结果
  49. numcount = -1;
  50. answer=firstnum - Float.parseFloat(LeftToRightCompute(str.substring(i + 1)));
  51. return answer+"";
  52. //break;
  53. case '×'://当我乘的时候,其实是乘第一个数
  54. numcount = -1;
  55. float secondnummulti = Float.parseFloat(FirstnumLeftToRight(str.substring(i+1)));
  56. float answer1 = firstnum * secondnummulti;
  57. String newstringmuti="";
  58. if(str.charAt(i+1)=='('){
  59. int kuocount=1;
  60. i=i+2;
  61. //int temp=i;
  62. for (;i<str.length();i++){
  63. if(str.charAt(i)=='('){
  64. kuocount++;
  65. }
  66. else if(str.charAt(i)==')'){
  67. kuocount--;
  68. }
  69. if(kuocount==0){
  70. newstringmuti = str.substring(i+1);
  71. break;
  72. }
  73. }
  74. }
  75. else if (secondnummulti==(int)secondnummulti) {
  76. newstringmuti = str.substring(i + 1 + Float.toString(secondnummulti).length()-2);
  77. }
  78. else{
  79. newstringmuti = str.substring(i + 1 + Float.toString(secondnummulti).length());
  80. }
  81. newstringmuti = answer1+ newstringmuti;
  82. answer = Float.parseFloat(LeftToRightCompute(newstringmuti));
  83. return answer+"";
  84. //break;
  85. case '÷'://当我除的时候,其实是除第一个数
  86. numcount = -1;
  87. float secondnumchu = Float.parseFloat(FirstnumLeftToRight(str.substring(i+1)));
  88. float answer2 = firstnum / secondnumchu;
  89. String newstringchu="";
  90. if(str.charAt(i+1)=='('){
  91. int kuocount=1;
  92. i=i+2;
  93. //int temp=i;
  94. for (;i<str.length();i++){
  95. if(str.charAt(i)=='('){
  96. kuocount++;
  97. }
  98. else if(str.charAt(i)==')'){
  99. kuocount--;
  100. }
  101. if(kuocount==0){
  102. newstringchu = str.substring(i);
  103. break;
  104. }
  105. }
  106. }
  107. else if (secondnumchu==(int)secondnumchu) {
  108. newstringchu = str.substring(i + 1 + Float.toString(secondnumchu).length()-2);
  109. }
  110. else{
  111. newstringchu = str.substring(i + 1 + Float.toString(secondnumchu).length());
  112. }
  113. newstringchu = answer2+ newstringchu;
  114. answer = Float.parseFloat(LeftToRightCompute(newstringchu));
  115. return answer+"";
  116. case '(':
  117. int temp=i;
  118. int kuocount=1;//检测这个字符串是否到头或者有区域外的括号如(a+b)+(c+d)
  119. i=i+1;
  120. for (;i<str.length();i++){
  121. if(str.charAt(i)=='('){
  122. kuocount++;
  123. }
  124. else if(str.charAt(i)==')'){
  125. kuocount--;
  126. }
  127. if(kuocount==0){
  128. float answer3= Float.parseFloat(LeftToRightCompute(str.substring(temp+1,i)));//但str本身并未被改变
  129. return LeftToRightCompute(answer3+str.substring(i));
  130. }
  131. }
  132. //break;
  133. case ')':
  134. //continue;
  135. }
  136. }
  137. }
  138. return answer+"";
  139. }
  140.  
  141. public static String FirstnumLeftToRight(String str) {
  142. System.out.println(str);
  143. int numcount = -1;
  144. float firstnum = 0;
  145. if (str.charAt(0)=='('){
  146. int i=0;
  147. float answer=0;
  148. int temp=i;
  149. int kuocount=1;//检测这个字符串是否到头或者有区域外的括号如(a+b)+(c+d)
  150. i=i+1;
  151. for (;i<str.length();i++){
  152. if(str.charAt(i)=='('){
  153. kuocount++;
  154. }
  155. else if(str.charAt(i)==')'){
  156. kuocount--;
  157. }
  158. if(kuocount==0){
  159. float answer3= Float.parseFloat(LeftToRightCompute(str.substring(temp+1,i)));//但str本身并未被改变
  160. return answer3+"";
  161. }
  162. }
  163. /*
  164. int i=0;
  165. int temp=i;
  166. int kuocount=1;//检测这个字符串是否到头或者有区域外的括号如(a+b)+(c+d)
  167. i=i+1;
  168. for (;i<str.length();i++){
  169. if(str.charAt(i)=='('){
  170. kuocount++;
  171. }
  172. else if(str.charAt(i)==')'){
  173. kuocount--;
  174. }
  175. if(kuocount==0){
  176. StringBuffer sb=new StringBuffer();
  177. sb.append(str).replace(temp,i+1,"");
  178. str=sb.toString();
  179. System.out.println(sb.toString());
  180. System.out.println(str);
  181. }
  182. }
  183. */
  184. return answer+"";
  185. }
  186. else {
  187. for (int i = 0; i < str.length(); i++) {
  188. if (str.charAt(i) >= '0' && str.charAt(i) <= '9'||str.charAt(i)=='.') {
  189. if (numcount==-1) {
  190. numcount = i;
  191. }
  192. firstnum = Float.parseFloat(str.substring(numcount, i + 1));
  193. }
  194. else {
  195. return firstnum+"";
  196. }
  197. }
  198. }
  199. return firstnum+"";
  200. }
  201. }
Add Comment
Please, Sign In to add comment