Visual Basic - PASAR CODIGO JAVA A VISUAL BASIC

Life is soft - evento anual de software empresarial
 
Vista:

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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

PASAR CODIGO JAVA A VISUAL BASIC

Publicado por amigo (1 intervención) el 22/04/2018 18:02:47
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
Imports System
 
' Exception class for parser errors.
Friend Class ParserException
	Inherits Exception
 
  Friend errStr As String ' describes the error
 
  Public Sub New(ByVal str As String)
	errStr = str
  End Sub
 
  Public Overrides Function ToString() As String
	Return errStr
  End Function
End Class
 
Friend Class Parser
  ' These are the token types. 
  Friend ReadOnly NONE As Integer = 0
  Friend ReadOnly DELIMITER As Integer = 1
  Friend ReadOnly VARIABLE As Integer = 2
  Friend ReadOnly NUMBER As Integer = 3
  'CODIGO NUEVO
  Friend ReadOnly FUNCION As Integer = 4
  Friend ReadOnly CONSTANTE As Integer = 5
 
 
  ' These are the types of syntax errors. 
  Friend ReadOnly SYNTAX As Integer = 0
  Friend ReadOnly UNBALPARENS As Integer = 1
  Friend ReadOnly NOEXP As Integer = 2
  Friend ReadOnly DIVBYZERO As Integer = 3
 
 
   ' This token indicates end-of-expression. 
  Friend ReadOnly EOE As String = ChrW(&O0).ToString()
 
  Private exp As String ' refers to expression string
  Private expIdx As Integer ' current index into the expression
  Private token As String ' holds current token
  Private tokType As Integer ' holds token's type
 
  ' Array for variables.  
  Private vars(25) As Double
 
  ' Parser entry point.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: public double evaluate(String expstr) throws ParserException
  Public Overridable Function evaluate(ByVal expstr As String) As Double
	Dim result As Double
	exp = expstr
	expIdx = 0
 
	Token
	If token.Equals(EOE) Then
	  handleErr(NOEXP) ' no expression present
	End If
 
	' Parse and evaluate the expression. 
	result = evalExp1()
 
	If Not token.Equals(EOE) Then ' last token must be EOE
	  handleErr(SYNTAX)
	End If
 
	Return result
  End Function
 
  ' Process an assignment.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp1() throws ParserException
  Private Function evalExp1() As Double
	Dim result As Double
	Dim varIdx As Integer
	Dim ttokType As Integer
	Dim temptoken As String
 
	If tokType = VARIABLE Then
	  ' save old token  
	  temptoken = token
	  ttokType = tokType
 
	  ' Compute the index of the variable.  
	  varIdx = Char.ToUpper(token.Chars(0)) - AscW("A"c)
 
	  Token
	  If Not token.Equals("=") Then
		putBack() ' return current token
		' restore old token -- not an assignment  
		token = temptoken
		tokType = ttokType
	  Else
		Token ' get next part of exp
		result = evalExp2()
		vars(varIdx) = result
		Return result
	  End If
	End If
 
	Return evalExp2()
  End Function
 
  ' Add or subtract two terms.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp2() throws ParserException
  Private Function evalExp2() As Double
	Dim op As Char
	Dim result As Double
	Dim partialResult As Double
 
	result = evalExp3()
 
'JAVA TO VB CONVERTER WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: while((op = token.charAt(0)) == "+"c || op == "-"c)
	op = token.Chars(0)
	Do While op = "+"c OrElse op = "-"c
	  Token
	  partialResult = evalExp3()
	  Select Case op
		Case "-"c
		  result = result - partialResult
		Case "+"c
		  result = result + partialResult
	  End Select
		op = token.Chars(0)
	Loop
	Return result
  End Function
	   ' Add or subtract two terms.  
 
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp3() throws ParserException
  Private Function evalExp3() As Double
	Dim op As Char
	Dim result As Double
	Dim partialResult As Double
 
	result = evalExp4()
 
'JAVA TO VB CONVERTER WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: while((op = token.charAt(0)) == "*"c || op == "/"c || op == "%"c)
	op = token.Chars(0)
	Do While op = "*"c OrElse op = "/"c OrElse op = "%"c
	  Token
	  partialResult = evalExp4()
	  Select Case op
		Case "*"c
		  result = result * partialResult
		Case "/"c
		  If partialResult = 0.0 Then
			handleErr(DIVBYZERO)
		  End If
		  result = result / partialResult
		Case "%"c
		  If partialResult = 0.0 Then
			handleErr(DIVBYZERO)
		  End If
		  result = result Mod partialResult
	  End Select
		op = token.Chars(0)
	Loop
	If result = 0 Then
		result = Math.Abs(result)
	End If
	Return result
  End Function
 
  ' Process an exponent.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp4() throws ParserException
  Private Function evalExp4() As Double
	Dim result As Double
	Dim partialResult As Double
	Dim ex As Double
	Dim t As Integer
 
	result = evalExp5()
 
	If token.Equals("^") Then
	  Token
	  partialResult = evalExp4()
	  ex = result
	  If partialResult = 0.0 Then
		result = 1.0
	  Else
		For t = CInt(Math.Truncate(partialResult))-1 To 1 Step -1
		  result = result * ex
		Next t
	  End If
	End If
 
	Return result
  End Function
 
  ' Evaluate a unary + or -.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp5() throws ParserException
  Private Function evalExp5() As Double
	Dim result As Double
	Dim op As String
 
	op = ""
	If (tokType = DELIMITER) AndAlso token.Equals("+") OrElse token.Equals("-") Then
	  op = token
	  Token
	End If
	result = evalExp8()
 
	If op.Equals("-") Then
		result = -result
	End If
 
	Return result
  End Function
 
 
 
  ' lee el menos 
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double evalExp8() throws ParserException
  Private Function evalExp8() As Double
	Dim result As Double
 
	If token.Equals("(") Then
	  Token
 
	  result = evalExp2()
 
	  If Not token.Equals(")") Then
		handleErr(UNBALPARENS)
	  End If
	  Token
	Else
		result = atom()
	End If
 
	Return result
  End Function
 
  ' Get the value of a number or variable.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double atom() throws ParserException
  Private Function atom() As Double
	Dim result As Double = 0.0
 
	Select Case tokType
	  Case NUMBER
		Try
		  result = Convert.ToDouble(token)
 
		Catch exc As NumberFormatException
		  handleErr(SYNTAX)
		End Try
		Token
 
		Case CONSTANTE
		   If token.Equals("pi") OrElse token.Equals("PI") Then
		   result = Math.PI
		   End If
		   If token.Equals("E") OrElse token.Equals("e") Then
		   result = Math.E
		   End If
		  Token
	   'System.out.println(result);
 
 
 
	  Case VARIABLE
		result = findVar(token)
		Token
 
 
	  Case Else
	   handleErr(SYNTAX)
	End Select
	Return result
  End Function
 
   ' Return the value of a variable.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private double findVar(String vname) throws ParserException
  Private Function findVar(ByVal vname As String) As Double
	If Not Char.IsLetter(vname.Chars(0)) Then
	  handleErr(SYNTAX)
	  Return 0.0
	End If
	Return vars(Char.ToUpper(vname.Chars(0))-AscW("A"c))
  End Function
 
  ' Return a token to the input stream.  
  Private Sub putBack()
	If token = EOE Then
		Return
	End If
	For i As Integer = 0 To token.Length - 1
		expIdx -= 1
	Next i
  End Sub
 
  ' Handle an error.  
'JAVA TO VB CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
'ORIGINAL LINE: private void handleErr(int error) throws ParserException
  Private Sub handleErr(ByVal [error] As Integer)
	Dim err() As String = { "Syntax Error", "Unbalanced Parentheses", "No Expression Present", "Division by Zero"}
 
	Throw New ParserException(err([error]))
  End Sub
 
  ' Obtain the next token.  
  Private Sub getToken()
	tokType = NONE
	token = ""
 
	' Check for end of expression.  
	If expIdx = exp.Length Then
	  token = EOE
	  Return
	End If
 
	' Skip over white space. 
	Do While expIdx < exp.Length AndAlso Char.IsWhiteSpace(exp.Chars(expIdx))
		expIdx += 1
	Loop
 
	' Trailing whitespace ends expression. 
	If expIdx = exp.Length Then
	  token = EOE
	  Return
	End If
 
	If isDelim(exp.Chars(expIdx)) Then ' is operator
	  token &= exp.Chars(expIdx)
	  expIdx += 1
	  tokType = DELIMITER
	ElseIf Char.IsLetter(exp.Chars(expIdx)) Then ' is variable
	  Do While Not isDelim(exp.Chars(expIdx))
		token &= exp.Chars(expIdx)
		expIdx += 1
		If expIdx >= exp.Length Then
			Exit Do
		End If
	  Loop
		  If isfuncion(token) Then
			  tokType=FUNCION
 
			  Console.WriteLine("Estoy en Funcion")
		  ElseIf isconstante(token) Then
			  tokType=CONSTANTE
			  Console.Write("Estoy en Constantes")
		  Else
 
	  tokType = VARIABLE
		  End If
 
	ElseIf Char.IsDigit(exp.Chars(expIdx)) Then ' is number
	  Do While Not isDelim(exp.Chars(expIdx))
		token &= exp.Chars(expIdx)
		expIdx += 1
		If expIdx >= exp.Length Then
			Exit Do
		End If
	  Loop
	  tokType = NUMBER
	End If
 
  End Sub
 
  ' Return true if c is a delimiter.
  Private Function isDelim(ByVal c As Char) As Boolean
	If (" +-/*%^=()".IndexOf(c) <> -1) Then
	  Return True
	End If
	Return False
  End Function

'Lee las Funciones
  Private Function isfuncion(ByVal s As String) As Boolean
	If ("cos tan sin sqrt".IndexOf(s) <> -1) Then
	  Return True
	End If
	Return False
  End Function

'Lee las Constantes
   Private Function isconstante(ByVal a As String) As Boolean
	If ("pi PI E e".IndexOf(a) <> -1) Then
	  Return True
	End If
	Return False
   End Function

End Class
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar