1+ /**
2+ * Description : This program is inspired from python running on my Systems Comand Prompt terminal.
3+ * I started this program with the idea of making a calculator
4+ * Later i open my window and thought about what if i make it feel like python running
5+ * But its actually JAVA my old buddy :-)
6+ * ------------------------------------------------------------------------------------------------
7+ * @Rohit_Roy
8+ * @version 2021.0 (created on 01.12.2021)
9+ * @Student, Bsc CS Hons. (1st Year)
10+ * @SXC, Kolkata
11+ * ------------------------------------------------------------------------------------------------
12+ */
13+ /**#
14+ * PythonTerminal => String input errors updated w.r.t quotes and blank spaces
15+ */
16+ import java .util .*;
17+ // import java.lang.String;
18+ public class PythonTerminal
19+ {
20+ static String inputs ; // gets the string from user
21+ static double num []; // to store the numbers or the operands
22+ static char opera []; // to store the operators
23+ static int size ; // stores the number of operators
24+ static int count ; // no. of spaces present
25+ public static void accept ()
26+ {
27+ Scanner sc =new Scanner (System .in );
28+ //System.out.println("Your Calculator is here...");
29+ System .out .print ("\n >>> " );
30+ inputs = sc .nextLine ();
31+ int len =inputs .length ();
32+ size =0 ;
33+ for (int i =0 ;i <len ;i ++)
34+ {
35+ char ch =inputs .charAt (i );
36+ if (ch =='+' || ch =='-' || ch =='*' || ch =='/' || ch =='^' )
37+ {
38+ size ++;
39+ }
40+ }
41+
42+ } // end of accept()
43+
44+ // Recursive function to store operators and operands seperately in arrays:
45+ public static void fillArray (String x , String temp , int i , int j )
46+ {
47+ //System.out.println(i + ", " +j);
48+ if (j ==size +1 || x .equals ("" ))
49+ System .out .print ("" );
50+ else
51+ {
52+ char ch =x .charAt (0 );
53+ if (ch =='+' || ch =='-' || ch =='*' || ch =='/' || ch =='^' )
54+ {
55+ if (!temp .equals ("" ))
56+ {
57+ num [j ]=Double .parseDouble (temp );
58+ j +=1 ;
59+ }
60+ opera [i ]=ch ;
61+ fillArray (x .substring (1 ),"" ,i +1 ,j );
62+ }
63+ else if (Character .isDigit (ch ) || ( ch =='.' && Character .isDigit ( x .charAt (1 ) ) ) )
64+ {
65+ temp +=ch ;
66+ fillArray (x .substring (1 ),temp ,i ,j );
67+ }
68+ else
69+ {
70+ if (!temp .equals ("" ))
71+ {
72+ num [j ]=Double .parseDouble (temp );
73+ j +=1 ;
74+ }
75+ fillArray (x .substring (1 ),"" ,i ,j );
76+ }
77+ }
78+ } // end of fillArray()
79+
80+ //inputsulations of the instructuions given by user:
81+ public static double operator (char c , double a , double b )
82+ {
83+ double cal =0.0d ;
84+ switch (c )
85+ {
86+ case '+' :
87+ cal =a +b ;
88+ break ;
89+
90+ case '-' :
91+ cal =a -b ;
92+ break ;
93+
94+ case '*' :
95+ cal =a *b ;
96+ break ;
97+
98+ case '/' :
99+ cal =a / b ;
100+ break ;
101+
102+ case '^' :
103+ cal =Math .pow (a ,b );
104+ break ;
105+
106+ }
107+ return cal ;
108+ } // end of operator()
109+
110+ public static String addChar (String x , char ch )
111+ {
112+ int len =x .length ();
113+ String sp ="" ;
114+ for (int i =1 ; i <=len ;i ++)
115+ {
116+ sp +=ch ;
117+ }
118+ return sp ;
119+ }
120+
121+ public static void countSpace (String x )
122+ {
123+ count =0 ;
124+ for (int i =0 ;i <x .length ();i ++)
125+ {
126+ char ch =x .charAt (i );
127+ if (ch ==' ' )
128+ count ++;
129+ }
130+ }
131+
132+ public static void recognize (String x )
133+ {
134+ int len =x .length ();
135+ char begin =x .charAt (0 );
136+ char end =x .charAt (len -1 );
137+ String temp = x .substring (1 ,len -1 );
138+ countSpace (x );
139+ if ( (begin == '\"' && end == '\"' ) || (begin == '\'' && end == '\'' ) )
140+ {
141+ System .out .println ("\' " + temp + "\' " );
142+ }
143+ else
144+ {
145+ if ( begin == '\"' || begin == '\'' )
146+ {
147+ System .out .println (" File \" <stdin>\" , line 1" );
148+ System .out .println (" " + x );
149+ System .out .println (" ^" );
150+ System .out .println ("SyntaxError: unterminated string literal (detected at line1)" );
151+ }
152+ else if ( end == '\"' || end == '\'' )
153+ {
154+ System .out .println (" File \" <stdin>\" , line 1" );
155+ System .out .println (" " + x );
156+ System .out .println (" " + addChar (x ,' ' )+"^" );
157+ System .out .println ("SyntaxError: unterminated string literal (detected at line1)" );
158+ }
159+ else if (count >=1 )
160+ {
161+ System .out .println (" File \" <stdin>\" , line 1" );
162+ System .out .println (" " + x );
163+ System .out .println (" " +addChar (x ,'^' ));
164+ System .out .println ("SyntaxError: invalid syntax. Perhaps you forget a comma?" );
165+ }
166+ else
167+ {
168+ System .out .println ("Traceback (most recent call last):" );
169+ System .out .println (" File \" <stdin>\" , line 1, in <module>" );
170+ System .out .println ("NameError: name '" +x + "' is not defined" );
171+ }
172+ }
173+ }
174+
175+ public static void main (String [] args )
176+ {
177+ accept ();
178+ if (inputs .equals ("exit()" ) || inputs .equals ("exit(0)" ))
179+ {
180+ System .exit (0 );
181+ }
182+ else
183+ {
184+ num =new double [size +1 ];
185+ opera =new char [size ];
186+ fillArray (inputs +" " ,"" ,0 ,0 );
187+ /*
188+ for(int i=0;i<size;i++)
189+ System.out.print(opera[i]+", ");
190+
191+ System.out.println("\nNumbers are: ");
192+ for(int i=0;i<size+1;i++)
193+ System.out.print(num[i]+", ");
194+ */
195+ double result =0.0d ;
196+ int j =0 ;
197+ for (int i =0 ;i <size +1 ;i ++)
198+ {
199+ if (j <size )
200+ {
201+ result =operator (opera [j ],num [i ],num [i +1 ]);
202+ num [i +1 ]=result ;
203+ j +=1 ;
204+ }
205+ }
206+
207+ if (size >0 )
208+ System .out .println (result );
209+ else
210+ recognize (inputs );
211+ }
212+ main (args ); // calls the main function
213+ }// end of main()
214+ } // end of class
0 commit comments