Advertisement
the_alator

Untitled

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