Conversor de C a Visual Basic Curso de Programación Visual Basic

Lección:

Administración de Archivos


Ejercicio:

Conversor de C a Visual Basic 29


Objetivo:

Cree un programa para convertir programas de C simples, como el siguiente, a Visual Basic:

Nota: el programa resultante debe compilar correctamente. Pruébelo con otros programas C similares.


Código:

Imports System
Imports System.IO
Class ConvertOfCToCSharp
    Private Shared Sub Main()
        Console.Write("Name of file: ")
        Dim name As String = Console.ReadLine()

        If Not File.Exists(name) Then
            Console.WriteLine("Not found!")
        Else

            Try
                Dim fileC As StreamReader = New StreamReader(name)
                Dim fileCSharp As StreamWriter = New StreamWriter(name & ".cp")
                Dim line As String

                Do
                    line = fileC.ReadLine()

                    If line IsNot Nothing Then
                        line = line.Replace("#include ", "using System;/npublic class Example{")
                        line = line.Replace("int main()", "public static void Main()")

                        If line.Contains("scanf(") Then
                            line = line.Replace("scanf(""%d"", &", "")
                            line = line.Replace(");", " = Convert.ToInt32(Console.ReadLine());")
                        End If

                        line = line.Replace("printf", "Console.Write")
                        line = line.Replace("%d", "{0}")
                    End If
                Loop While line IsNot Nothing

            Catch e As Exception
                Console.WriteLine("Error, " & e.Message)
            End Try
        End If
    End Sub
End Class