PASAR CODIGO JAVA A VISUAL BASIC
Publicado por evelyn chiquete (1 intervención) el 09/11/2016 17:51:51
BUENAS TARDES TENGO ESTE CODIGO ECHO EN JAVA OKUPO EL MISMO PROGRAMA SOLO QUE EN VISUAL BASIC SOY NUEVA PROGRAMANDO SI ALGUIEN ME PUDIERA AYUDAR
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Exception class for parser errors.
class ParserException extends Exception {
String errStr; // describes the error
public ParserException(String str) {
errStr = str;
}
public String toString() {
return errStr;
}
}
class Parser {
// These are the token types.
final int NONE = 0;
final int DELIMITER = 1;
final int VARIABLE = 2;
final int NUMBER = 3;
//CODIGO NUEVO
final int FUNCION = 4;
final int CONSTANTE = 5;
// These are the types of syntax errors.
final int SYNTAX = 0;
final int UNBALPARENS = 1;
final int NOEXP = 2;
final int DIVBYZERO = 3;
// This token indicates end-of-expression.
final String EOE = "\0";
private String exp; // refers to expression string
private int expIdx; // current index into the expression
private String token; // holds current token
private int tokType; // holds token's type
// Array for variables.
private double vars[] = new double[26];
// Parser entry point.
public double evaluate(String expstr) throws ParserException
{
double result;
exp = expstr;
expIdx = 0;
getToken();
if(token.equals(EOE))
handleErr(NOEXP); // no expression present
// Parse and evaluate the expression.
result = evalExp1();
if(!token.equals(EOE)) // last token must be EOE
handleErr(SYNTAX);
return result;
}
// Process an assignment.
private double evalExp1() throws ParserException
{
double result;
int varIdx;
int ttokType;
String temptoken;
if(tokType == VARIABLE) {
// save old token
temptoken = new String(token);
ttokType = tokType;
// Compute the index of the variable.
varIdx = Character.toUpperCase(token.charAt(0)) - 'A';
getToken();
if(!token.equals("=")) {
putBack(); // return current token
// restore old token -- not an assignment
token = new String(temptoken);
tokType = ttokType;
}
else {
getToken(); // get next part of exp
result = evalExp2();
vars[varIdx] = result;
return result;
}
}
return evalExp2();
}
// Add or subtract two terms.
private double evalExp2() throws ParserException
{
char op;
double result;
double partialResult;
result = evalExp3();
while((op = token.charAt(0)) == '+' || op == '-') {
getToken();
partialResult = evalExp3();
switch(op) {
case '-':
result = result - partialResult;
break;
case '+':
result = result + partialResult;
break;
}
}
return result;
}
// Add or subtract two terms.
private double evalExp3() throws ParserException
{
char op;
double result;
double partialResult;
result = evalExp4();
while((op = token.charAt(0)) == '*' ||
op == '/' || op == '%') {
getToken();
partialResult = evalExp4();
switch(op) {
case '*':
result = result * partialResult;
break;
case '/':
if(partialResult == 0.0)
handleErr(DIVBYZERO);
result = result / partialResult;
break;
case '%':
if(partialResult == 0.0)
handleErr(DIVBYZERO);
result = result % partialResult;
break;
}
}
if(result == 0){
result = Math.abs(result);
}
return result;
}
// Process an exponent.
private double evalExp4() throws ParserException
{
double result;
double partialResult;
double ex;
int t;
result = evalExp5();
if(token.equals("^")) {
getToken();
partialResult = evalExp4();
ex = result;
if(partialResult == 0.0) {
result = 1.0;
} else
for(t=(int)partialResult-1; t > 0; t--)
result = result * ex;
}
return result;
}
// Evaluate a unary + or -.
private double evalExp5() throws ParserException
{
double result;
String op;
op = "";
if((tokType == DELIMITER) &&
token.equals("+") || token.equals("-")) {
op = token;
getToken();
}
result = evalExp8();
if(op.equals("-")) result = -result;
return result;
}
// lee el menos
private double evalExp8() throws ParserException
{
double result;
if(token.equals("(")) {
getToken();
result = evalExp2();
if(!token.equals(")"))
handleErr(UNBALPARENS);
getToken();
}
else result = atom();
return result;
}
// Get the value of a number or variable.
private double atom() throws ParserException
{
double result = 0.0;
switch(tokType) {
case NUMBER:
try {
result = Double.parseDouble(token);
} catch (NumberFormatException exc) {
handleErr(SYNTAX);
}
getToken();
break;
case CONSTANTE:
if( token.equals("pi")|| token.equals("PI") ){
result = Math.PI;
}
if(token.equals("E") || token.equals("e")){
result = Math.E;
}
getToken();
//System.out.println(result);
break;
case VARIABLE:
result = findVar(token);
getToken();
break;
default:
handleErr(SYNTAX);
break;
}
return result;
}
// Return the value of a variable.
private double findVar(String vname) throws ParserException
{
if(!Character.isLetter(vname.charAt(0))){
handleErr(SYNTAX);
return 0.0;
}
return vars[Character.toUpperCase(vname.charAt(0))-'A'];
}
// Return a token to the input stream.
private void putBack()
{
if(token == EOE) return;
for(int i=0; i < token.length(); i++) expIdx--;
}
// Handle an error.
private void handleErr(int error) throws ParserException
{
String[] err = {
"Syntax Error",
"Unbalanced Parentheses",
"No Expression Present",
"Division by Zero",
};
throw new ParserException(err[error]);
}
// Obtain the next token.
private void getToken()
{
tokType = NONE;
token = "";
// Check for end of expression.
if(expIdx == exp.length()) {
token = EOE;
return;
}
// Skip over white space.
while(expIdx < exp.length() &&
Character.isWhitespace(exp.charAt(expIdx))) ++expIdx;
// Trailing whitespace ends expression.
if(expIdx == exp.length()) {
token = EOE;
return;
}
if(isDelim(exp.charAt(expIdx))) { // is operator
token += exp.charAt(expIdx);
expIdx++;
tokType = DELIMITER;
}
else if(Character.isLetter(exp.charAt(expIdx))) { // is variable
while(!isDelim(exp.charAt(expIdx))) {
token += exp.charAt(expIdx);
expIdx++;
if(expIdx >= exp.length()) break;
}
if(isfuncion(token)){
tokType=FUNCION;
System.out.println("Estoy en Funcion");
}else if(isconstante(token)){
tokType=CONSTANTE;
System.out.print("Estoy en Constantes");
}else
tokType = VARIABLE;
}
else if(Character.isDigit(exp.charAt(expIdx))) { // is number
while(!isDelim(exp.charAt(expIdx))) {
token += exp.charAt(expIdx);
expIdx++;
if(expIdx >= exp.length()) break;
}
tokType = NUMBER;
}
}
// Return true if c is a delimiter.
private boolean isDelim(char c)
{
if((" +-/*%^=()".indexOf(c) != -1))
return true;
return false;
}
//Lee las Funciones
private boolean isfuncion(String s)
{
if(( "cos tan sin sqrt".indexOf(s) != -1))
return true;
return false;
}
//Lee las Constantes
private boolean isconstante(String a)
{
if(("pi PI E e".indexOf(a) != -1))
return true;
return false;
}
}
Valora esta pregunta


0