Advertisement
the_alator

Untitled

Sep 22nd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.38 KB | None | 0 0
  1. package calculator;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6. class MainClass2 {
  7.  
  8.      static int nums[];
  9.         static char signs[];
  10.         static int signssync[];
  11.         static boolean numsrel[];
  12.        
  13.  
  14.         static String buffer;
  15.         static int numLocation;
  16.         static int signLocation;
  17.         static int for_b;
  18.         static int callmetod;
  19.         static int firstmultsign;
  20.         static int calcnum;
  21.         static int calc2num;
  22.         static boolean calccheck;
  23.         static boolean lever;
  24.         static boolean calc2check;
  25.         static boolean endOfString;
  26.         static boolean minus;
  27.         //static boolean calc2numcheck;
  28.        
  29.         static boolean repeat_ask = true;
  30.        
  31.         static boolean lever_for_calculation2 = false;
  32.     //static String a = "12+(35+45*2)■";
  33.     //static String a = "-12+11+(10+35+45*2*3)■";
  34.     //static String a = "-12+2*(10+35+45*2*3/2*2+3)";
  35.     //static String a = "12-3*(1+4/2)-(6+1)";
  36.    
  37.     //static String to_change_repeat_ask;
  38.    
  39.  
  40.     static String a;
  41.  
  42.     public static void main(String[] args) throws IOException{
  43.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  44.         while(repeat_ask){
  45.             System.out.println("Введите выражение");
  46.             a = br.readLine();
  47.            
  48.             MainClass2 mc = new MainClass2();
  49.             mc.aftermain();
  50.             mc = null;
  51.             System.out.println("Желаете посчитать еще одно выражение? д/н");
  52.             if(!br.readLine().equals("д"))repeat_ask = false;
  53.         }
  54.        
  55.     }  
  56.     MainClass2(){
  57.         int nums[];
  58.         char signs[];
  59.         int signssync[];
  60.         boolean numsrel[];
  61.        
  62.  
  63.         buffer = "";
  64.         numLocation = 0;
  65.         signLocation = 0;
  66.         for_b = 0;
  67.         callmetod = 0;
  68.         int firstmultsign;
  69.         calcnum = 1;
  70.         calc2num = 0;
  71.         calccheck = true;
  72.         lever = false;
  73.         calc2check = true;
  74.         endOfString = true;
  75.         minus = false;
  76.         //calc2numcheck = false;
  77.         repeat_ask = true;
  78.         String a;
  79.         lever_for_calculation2 = false;
  80.     }
  81.     public static void aftermain() throws IOException{
  82.         int arraylengthsigns = 0;
  83.         int arraylengthnums = 0;
  84.         boolean lever1 = false;
  85.  
  86.         for(int cycle = 0;a.length() > cycle;cycle++){
  87.  
  88.             char arraylengthfind = a.charAt(cycle);
  89.  
  90.             switch(arraylengthfind){
  91.                 case '+':
  92.                 case '-':
  93.                 case '*':
  94.                 case '/':
  95.                 case '(':
  96.                 case ')':
  97.  
  98.                     if(lever1){
  99.                         arraylengthnums++;
  100.                         if(arraylengthsigns == 0){
  101.                             arraylengthsigns++;
  102.                         }
  103.                     }
  104.                     arraylengthsigns++;
  105.  
  106.                     lever1 = false;
  107.                     break;
  108.                 default:
  109.  
  110.                     lever1 = true;
  111.                     if(a.length()-1 == cycle)arraylengthnums++;
  112.             }
  113.         }
  114.  
  115.         nums = new int[arraylengthnums];
  116.  
  117.         signs = new char[arraylengthsigns];
  118.         signssync = new int[arraylengthsigns];
  119.         numsrel = new boolean[arraylengthnums];
  120.  
  121.         for(int cycle = 0;a.length() > cycle;cycle++){
  122.  
  123.             char stringPart = a.charAt(cycle);
  124.             if(stringPart == '-'){
  125.  
  126.                 if(cycle==0){
  127.                     buffer="-";
  128.                     signLocation++;
  129.                     continue;
  130.                 }else{
  131.                     minus = true;
  132.                     stringPart = '+';
  133.                 }
  134.             }
  135.             if(stringPart == '+' || stringPart == '-' || stringPart == '*'|| stringPart == '/'|| stringPart == '('|| stringPart == ')'){
  136.                 if(a.length()-1 == cycle){
  137.                     endOfString = false;
  138.                 }
  139.                 if(lever){
  140.                     nums[numLocation] = Integer.parseInt(buffer);
  141.                     numsrel[numLocation] = true;
  142.                     if(minus) buffer = "-";
  143.                     else buffer = "";
  144.                     numLocation++;
  145.                     lever = false;
  146.                 }
  147.  
  148.                 signs[signLocation] = stringPart;
  149.                 signssync[signLocation] = numLocation;
  150.                 signLocation++;
  151.             }else{
  152.                 lever = true;
  153.                 buffer += stringPart;
  154.                 if(signLocation == 0) signLocation++;
  155.             }
  156.             if(endOfString && a.length()-1 == cycle){
  157.                 nums[numLocation] = Integer.parseInt(buffer);
  158.                 numsrel[numLocation] = true;
  159.             }
  160.         }
  161.  
  162.  
  163.         //ЭТАП 2.цикл закончен,поиск скобок
  164.         currentinfo();
  165.         findbracket(0);
  166.     }
  167.  
  168.     static void findbracket(int cellnum) throws IOException{
  169.         System.out.println("LAUNCHED findbracket, cellnum = " + cellnum);
  170.         for(int for_c = cellnum;for_c < signs.length; for_c++){
  171.             if(signs[for_c] == '(' ){
  172.                 callmetod++;
  173.                 findbracket(for_c+1);
  174.                 callmetod--;
  175.             }else{
  176.                 if(signs[for_c] == ')'){
  177.                     calculation1(cellnum-1,for_c,false);
  178.                     signs[for_c] =  signs[cellnum-1] = ' ';
  179.                     break;
  180.                 }
  181.             }
  182.         }
  183.         if(callmetod == 0)calculation1(0,signs.length,true);
  184.         System.out.println("CLOSET findbracket");
  185.     }
  186. /////////////////////////////////////////////////////////////////////////////////////////////
  187.  
  188.     static void calculation1(int a,int b,boolean preend) throws IOException{
  189.         System.out.println("LAUNCHED calculation1, a = " + a + " b = " + b + " preend = " + preend);
  190.        
  191.         for(int for_d = a;for_d < b;for_d++){
  192.             char test = signs[for_d];
  193.             if(test == '*'){
  194.                 if(calccheck && numsrel[signssync[for_d]-1]){
  195.                     for(int for_h = for_d;for_h >= 0 && for_h >= a;for_h--)
  196.                         if(numsrel[signssync[for_h]]){
  197.                             calcnum = nums[signssync[for_h]];
  198.                             firstmultsign = for_h;
  199.                         }
  200.                     //calcnum = nums[signssync[for_d]-1];
  201.                     calccheck = false;
  202.                 }
  203.                 if(numsrel[signssync[for_d]]){
  204.                     calcnum *= nums[signssync[for_d]];
  205.                     numsrel[signssync[for_d]] = false;
  206.                 }
  207.                 //signs[for_d] = ' ';
  208.             }
  209.            
  210.             if(test== '/'){
  211.                 if(calccheck && numsrel[signssync[for_d]-1]){
  212.                         for(int for_h = for_d;for_h >= 0 && for_h >= a;for_h--)
  213.                             if(numsrel[signssync[for_h]]){
  214.                                 calcnum = nums[signssync[for_h]];
  215.                                 firstmultsign = for_h;
  216.                             }
  217.                     //calcnum = nums[signssync[for_d]-1];
  218.                     calccheck = false;
  219.                 }
  220.                 if(numsrel[signssync[for_d]]){
  221.                     calcnum = calcnum/nums[signssync[for_d]];
  222.                     numsrel[signssync[for_d]] = false;
  223.                 }
  224.                 //signs[for_d] = ' ';
  225.             }
  226.            
  227.             if(signs[for_d] == '+' || for_d == b-1){
  228.                 if(!calccheck){
  229.                     nums[signssync[firstmultsign]] = calcnum;
  230.                     calccheck = true;
  231.                 }
  232.             }
  233.            
  234.         }
  235.        
  236.         System.out.println("CLOSET calculation1, calling calculation2, calcnum = " + calcnum);
  237.         currentinfo();
  238.         calculation2(a,b,preend);
  239.     }
  240.     /////////////////////////////////////////////////////////////////////////////////////////////
  241.  
  242.     static void calculation2(int a,int b,boolean end) throws IOException{
  243.         calccheck = true;//test
  244.         System.out.println("LAUNCHED calculation2, a = " + a + " b = " + b + " end = " + end);
  245.         for(int for_f = a;for_f < b;for_f++){
  246.             if(signs[for_f] == '+'){
  247.                 if(calc2check){
  248.                     for(int for_g = for_f;for_g >= 0 && for_g >= a;for_g--)
  249.                     if(numsrel[signssync[for_g]])calc2num = nums[signssync[for_g]];
  250.                    
  251.                     calc2check = false;
  252.                 }
  253.                
  254.                 if(numsrel[signssync[for_f]]){
  255.                     calc2num += nums[signssync[for_f]];
  256.                     numsrel[signssync[for_f]] = false;
  257.                 }
  258.                 //signs[for_f] = ' ';
  259.             }
  260.             System.out.println("calc2num = " + calc2num);
  261.         }
  262.        
  263.        
  264.         if(!calc2check){
  265.             nums[signssync[a]] = calc2num;
  266.             calc2check = true;
  267.         }
  268.         if(end){
  269.             System.out.println("RESULT = " + nums[0] );
  270.                 }
  271.        
  272.         System.out.println("calc2num after cycle = "+calc2num);
  273.         currentinfo();
  274.         System.out.println("CLOSET calcultion2");
  275.         lever_for_calculation2 = false;
  276.     }
  277.    
  278.  
  279.  
  280.  
  281.     static void currentinfo(){
  282.         System.out.println("массив чисел:");
  283.         for(int x:nums){
  284.  
  285.             System.out.print(x + " ");
  286.  
  287.         }
  288.         System.out.println("");
  289.         System.out.println("массив знаков:");
  290.         for(char x:signs){
  291.  
  292.  
  293.             System.out.print(x + " ");
  294.  
  295.         }
  296.  
  297.         System.out.println("");
  298.         System.out.println("массив синхронизации знаков:");
  299.         for(int x:signssync){
  300.  
  301.  
  302.             System.out.print(x + " ");
  303.  
  304.         }
  305.         System.out.println("");
  306.         System.out.println("массив актуальности чисел:");
  307.         for(boolean x:numsrel){
  308.  
  309.  
  310.             System.out.print(x + " ");
  311.  
  312.         }
  313.         System.out.println("");
  314.     }
  315.  
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement