Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calculator;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- class MainClass2 {
- static int nums[];
- static char signs[];
- static int signssync[];
- static boolean numsrel[];
- static String buffer;
- static int numLocation;
- static int signLocation;
- static int for_b;
- static int callmetod;
- static int firstmultsign;
- static int calcnum;
- static int calc2num;
- static boolean calccheck;
- static boolean lever;
- static boolean calc2check;
- static boolean endOfString;
- static boolean minus;
- static boolean calc2numcheck;
- static boolean repeat_ask = true;
- //static String a = "12+(35+45*2)■";
- //static String a = "12+11+(10+35+45*2*3)■";
- //static String a = "-12+2*(10+35+45*2*3/2*2+3)";
- //static String a = "12-3*(1+4/2)-(6+1)";
- //static String to_change_repeat_ask;
- static String a;
- public static void main(String[] args) throws IOException{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- while(repeat_ask){
- System.out.println("Введите выражение");
- a = br.readLine();
- MainClass2 mc = new MainClass2();
- mc.aftermain();
- mc = null;
- System.out.println("Желаете посчитать еще одно выражение? д/н");
- if(!br.readLine().equals("д"))repeat_ask = false;
- }
- }
- MainClass2(){
- int nums[];
- char signs[];
- int signssync[];
- boolean numsrel[];
- buffer = "";
- numLocation = 0;
- signLocation = 0;
- for_b = 0;
- callmetod = 0;
- int firstmultsign;
- calcnum = 1;
- calc2num = 0;
- calccheck = true;
- lever = false;
- calc2check = true;
- endOfString = true;
- minus = false;
- calc2numcheck = false;
- repeat_ask = true;
- String a;
- }
- public static void aftermain() throws IOException{
- int arraylengthsigns = 0;
- int arraylengthnums = 0;
- boolean lever1 = false;
- for(int cycle = 0;a.length() > cycle;cycle++){
- char arraylengthfind = a.charAt(cycle);
- switch(arraylengthfind){
- case '+':
- case '-':
- case '*':
- case '/':
- case '(':
- case ')':
- if(lever1){
- arraylengthnums++;
- if(arraylengthsigns == 0){
- arraylengthsigns++;
- }
- }
- arraylengthsigns++;
- lever1 = false;
- break;
- default:
- lever1 = true;
- if(a.length()-1 == cycle)arraylengthnums++;
- }
- }
- nums = new int[arraylengthnums];
- signs = new char[arraylengthsigns];
- signssync = new int[arraylengthsigns];
- numsrel = new boolean[arraylengthnums];
- for(int cycle = 0;a.length() > cycle;cycle++){
- char stringPart = a.charAt(cycle);
- if(stringPart == '-'){
- if(cycle==0){
- buffer="-";
- signLocation++;
- continue;
- }else{
- minus = true;
- stringPart = '+';
- }
- }
- if(stringPart == '+' || stringPart == '-' || stringPart == '*'|| stringPart == '/'|| stringPart == '('|| stringPart == ')'){
- if(a.length()-1 == cycle){
- endOfString = false;
- }
- if(lever){
- nums[numLocation] = Integer.parseInt(buffer);
- numsrel[numLocation] = true;
- if(minus) buffer = "-";
- else buffer = "";
- numLocation++;
- lever = false;
- }
- signs[signLocation] = stringPart;
- signssync[signLocation] = numLocation;
- signLocation++;
- }else{
- lever = true;
- buffer += stringPart;
- if(signLocation == 0) signLocation++;
- }
- if(endOfString && a.length()-1 == cycle){
- nums[numLocation] = Integer.parseInt(buffer);
- numsrel[numLocation] = true;
- }
- }
- //ЭТАП 2.цикл закончен,поиск скобок
- currentinfo();
- findbracket(0);
- }
- static void findbracket(int cellnum) throws IOException{
- System.out.println("LAUNCHED findbracket, cellnum = " + cellnum);
- for(int for_c = cellnum;for_c < signs.length; for_c++){
- if(signs[for_c] == '(' ){
- callmetod++;
- findbracket(for_c+1);
- callmetod--;
- }else{
- if(signs[for_c] == ')'){
- calculation1(cellnum-1,for_c,false);
- signs[for_c] = ' ';
- break;
- }
- }
- }
- if(callmetod == 0)calculation1(0,signs.length,true);
- System.out.println("CLOSET findbracket");
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- static void calculation1(int a,int b,boolean preend) throws IOException{
- System.out.println("LAUNCHED calculation1, a = " + a + " b = " + b + " preend = " + preend);
- for(int for_d = a;for_d < b;for_d++){
- char test = signs[for_d];
- if(test == '*'){
- if(calccheck){
- firstmultsign = for_d; //как насчет проверки актуальности здесь?
- if(numsrel[signssync[for_d]-1])calcnum = nums[signssync[for_d]-1];
- calccheck = false;
- }
- if(numsrel[signssync[for_d]]) calcnum *= nums[signssync[for_d]];
- numsrel[signssync[for_d]] = false;
- }
- if(test== '/'){
- if(calccheck){
- firstmultsign = for_d; //как насчет проверки актуальности здесь?
- if(numsrel[signssync[for_d]-1])calcnum = nums[signssync[for_d]-1];
- calccheck = false;
- numsrel[signssync[for_d]] = false;
- }
- if(numsrel[signssync[for_d]])calcnum = calcnum/nums[signssync[for_d]];
- numsrel[signssync[for_d]] = false;
- }
- if(signs[for_d] == '+' || for_d == b-1){
- /*if(!calccheck){
- nums[signssync[firstmultsign]-1] = calcnum;
- calccheck = true;
- }*/
- if(!calccheck){
- if(a==0){
- nums[signssync[firstmultsign]-1] = calcnum;
- }else{
- nums[signssync[firstmultsign]-1] = calcnum;
- }
- calccheck = false;
- }
- }
- }
- System.out.println("CLOSET calculation1, calling calculation2, calcnum = " + calcnum);
- currentinfo();
- calculation2(a,b,preend);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- static void calculation2(int a,int b,boolean end) throws IOException{
- //System.out.println("LAUNCHED calculation2, a = " + a + " b = " + b + " end = " + end);
- for(int for_f = a;for_f < b;for_f++){
- if(signs[for_f] == '+'){
- if(calc2check){
- for(int for_g = for_f;for_g > 0;for_g--)
- if(numsrel[signssync[for_f]-1])calc2num = nums[signssync[for_f]-1];
- calc2check = false;
- }
- if(numsrel[signssync[for_f]])calc2num += nums[signssync[for_f]];
- calc2numcheck = true;
- numsrel[signssync[for_f]] = false;
- }
- System.out.println("calc2num = " + calc2num);
- }
- calc2check = true;
- if(calc2numcheck){
- if(a==0){
- nums[signssync[a]] = calc2num;
- }else{
- nums[signssync[a]-1] = calc2num;
- }
- calc2numcheck = false;
- }
- if(end){
- System.out.println("RESULT = " + nums[0] );
- }
- System.out.println("calc2num after cycle = "+calc2num);
- currentinfo();
- System.out.println("CLOSET calcultion2");
- }
- public static void end() throws IOException{
- System.out.println("RESULT = " + calc2num );
- //aftermain();
- }
- static void currentinfo(){
- System.out.println("массив чисел:");
- for(int x:nums){
- System.out.print(x + " ");
- }
- System.out.println("");
- System.out.println("массив знаков:");
- for(char x:signs){
- System.out.print(x + " ");
- }
- System.out.println("");
- System.out.println("массив синхронизации знаков:");
- for(int x:signssync){
- System.out.print(x + " ");
- }
- System.out.println("");
- System.out.println("массив актуальности чисел:");
- for(boolean x:numsrel){
- System.out.print(x + " ");
- }
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement