Ejercicio
Conversor de C a java
Objetivo
Cree un programa para convertir programas de C simples, como el siguiente, a java:
Nota: el programa resultante debe compilar correctamente. Pruébelo con otros programas C similares.
Código de Ejemplo
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.print("Name of file: ");
String name = new Scanner(System.in).nextLine();
if (!(new java.io.File(name)).isFile())
{
System.out.println("Not found!");
}
else
{
try
{
java.io.InputStreamReader fileC = new java.io.InputStreamReader(name);
java.io.OutputStreamWriter fileCSharp = new java.io.OutputStreamWriter(name + ".cp");
String line;
do
{
line = fileC.ReadLine();
if (line != null)
{
line = line.replace("#include ", "using System;/npublic class Example{");
line = line.replace("int main()", "public static void Main()");
if (line.contains("scanf("))
{
line = line.replace("scanf(\"%d\", &", "");
line = line.replace(");", " = Convert.ToInt32(Console.ReadLine());");
}
line = line.replace("printf", "Console.Write");
line = line.replace("%d", "{0}");
}
} while (line != null);
}
catch (RuntimeException e)
{
System.out.println("Error, " + e.getMessage());
}
}
}
}