Advertisement
the_alator

Untitled

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