Advertisement
the_alator

Untitled

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