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