Java to C#

toninho_77

Power Member
Caros amigos, tenho um código de uma aplicação em Java, e gostaria de a passar a c# alguém sabe se é possível fazer uma coisa dessas?
Cumps.
 
As linguagens são parecidas. É começares a fazer copy-paste do código e ir adaptando....

O meu problema é que existem coisas que desconheço por completo em java, visto nunca ter tido contacto com a linguagem, não as sei traduzir em c#.
Cumps.
 
Última edição:
Este é o código em java que queria passar para c#, alguém me dá umas dicas? A minha dificuldade é que não sei diferenciar o codigo que fica no "botão" e o que fica na classe.
Por isso peço um pouco da vossa ajuda.
Cumps.

class Kruskal
{
public static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

static int [][] G;
static int [][] t;
static boolean [][] in;
static boolean [][] temp;

static int n;
static int mincost = 0;
static int k, l, num_ed=0;

public static void main (String[] args) throws IOException
{
System.out.println("\t\t\t\tPrim's Algorithm");
System.out.print("\nEnter the number of the vertices: ");
n = Integer.parseInt(br.readLine());

G = new int[n+1][n+1];
in = new boolean[n+1][n+1];
t = new int[n+1][3];

System.out.print("\nIf edge between the following vertices enter its cost (not more than 7000) else 0:\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
in[j] = in[j] = false;
if((i!=j)&&(i<j))
{
System.out.print(i+" and "+j+": ");
G[j] = G[j] = Integer.parseInt(br.readLine());
if(G[j] == 0 )
G[j] = G[j] = 7001;
}
if(i==j)
G[j]=7001;
}

System.out.println("\n\nSolution : \n\n");
Kruskals();
System.out.println("\n\n\n\ncost incured is: "+ mincost);
}

static void Kruskals()
{
for (int i = 1; i<=n; i++)
{
getMinKL();
if(k==l)
break;
System.out.print(l + "-" +k);
if(formscycle(i-1))
{
System.out.println(" --> Forms cycle, hence rejected!");
i--;
continue;
}
else
System.out.println();

mincost = mincost + G[k][l];
num_ed = (isPresent(i, k))?num_ed:num_ed+1;
num_ed = (isPresent(i, l))?num_ed:num_ed+1;

t[1] = l;
t[2] = k;
if(num_ed >= n)
{
if(allconnect(i)) //not a forest
return;
}

}
System.out.println("\nNo Solution Available!");
}

static boolean allconnect(int i)
{
for(int c=2;c<=n;c++)
{
temp = new boolean[n+1][n+1];
for(int a=1;a<=n;a++)
for(int b=1;b<=n;b++)
temp[a] = temp[a] = false;

if(can_reach(1, c, i) == false)
return false;
}
return true;
}

static boolean formscycle(int i)
{
if(isPresent(i, k) && isPresent(i, l))
{
temp = new boolean[n+1][n+1];
for(int a=1;a<=n;a++)
for(int b=1;b<=n;b++)
temp[a] = temp[a] = false;

if(can_reach(k, l, i) )
return true;
}
return false;
}

static boolean can_reach(int k, int l, int i)
{
temp[k][l] = temp[l][k] = true;
for(int o=1; o<=i; o++)
{
if(((k == t[o][1]) && (l == t[o][2])) || ((l == t[o][1]) && (k == t[o][2])))
return true;
if((k == t[o][1]) && !(temp[t[o][2]][l]) )
{
if(can_reach(t[o][2], l, i) == true)
return true;
}
else if((k == t[o][2]) && !(temp[t[o][1]][l]))
{
if(can_reach(t[o][1], l, i) == true)
return true;
}
}
return false;
}

static boolean isPresent(int i, int val)
{
for(int o=1; o<=i; o++)
if((val == t[o][1]) || ((val == t[o][2]) ))
return true;
return false;
}

static void getMinKL()
{
int k1 = 1, l1 = 1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if((i!=j)&&(i<j))
{
if((G[j] < G[k1][l1]) && G[j] !=0 && in[j]==false)
{
k1 = i;
l1 = j;
}
}
}
if(G[k1][l1] !=0 )
{
k =k1; l=l1;
in[k][l] = in[l][k] = true;
}
}
}
 
Isso não muda quase nada, só onde tens System.out.println fica System.Console.WriteLine, o boolean passa a bool, e a leitura dos dados a partir do stdin também muda, penso que é Console.ReadLine ou algo do género. Basta usar o google.
 
Pessoal fiz a alteração para C#, sobraram algumas duvidas, a aplicação ainda não corre mas fiz quase todas as alterações lógicas, restam 5 erros, podem prestar algum auxilio nesta recta final?
Cumprimentos.

Um dos erros é " public static BufferedReader br =new BufferedReader(new InputStreamReader(Console.In));"
o outro é "throws IOException" estas são as partes que aparecem sublinhadas de entre outras, e não consigo convertê-las. Quem puder tentar correr este codigo, agradeço desde já mesmo porque estou á nora e preciso mesmo disto.


namespace Kruskal
{
class Program
{

public static BufferedReader br =new BufferedReader(new InputStreamReader(Console.In));

static int [][] G;
static int [][] t;
static bool [][] in;
static bool [][] temp;

static int n;
static int mincost = 0;
static int k, l, num_ed=0;


public static void main (String[] args) throws IOException

{
System.Console.WriteLine("\t\t\t\tPrim's Algorithm");
System.Console.WriteLine("\nEnter the number of the vertices: ");
n = Integer.parseInt(Console.ReadLine());

G = new int[n+1][n+1];
in = new bool[n+1][n+1];
t = new int[n+1][3];

System.Console.WriteLine("\nIf edge between the following vertices enter its cost (not more than 7000) else 0:\n");

for(int i=1;i<=n;i++)

for(int j=1;j<=n;j++)

{
in[j] = in[j] = false;
if((i!=j)&&(i<j))

{

System.Console.WriteLine(i+" and "+j+": ");
G[j] = G[j] = Integer.parseInt(br.readLine());
if(G[j] == 0 )

G[j] = G[j] = 7001;

}

if(i==j)

G[j]=7001;

}



System.Console.WriteLine("\n\nSolution : \n\n");

Kruskals();

System.out.println("\n\n\n\ncost incured is: "+ mincost);

}



static void Kruskals()

{

for (int i = 1; i<=n; i++)

{

getMinKL();

if(k==l)

break;

System.Console.WriteLine(l + "-" +k);

if(formscycle(i-1))

{

System.Console.WriteLine(" --> Forms cycle, hence rejected!");

i--;

continue;

}

else

System.Console.WriteLine();



mincost = mincost + G[k][l];

num_ed = (isPresent(i, k))?num_ed:num_ed+1;

num_ed = (isPresent(i, l))?num_ed:num_ed+1;



t[1] = l;

t[2] = k;

if(num_ed >= n)

{

if(allconnect(i)) //not a forest

return;

}



}

System.Console.WriteLine("\nNo Solution Available!");

}



static bool allconnect(int i)

{

for(int c=2;c<=n;c++)

{

temp = new bool[n+1][n+1];

for(int a=1;a<=n;a++)

for(int b=1;b<=n;b++)

temp[a] = temp[a] = false;



if(can_reach(1, c, i) == false)

return false;

}

return true;

}



static bool formscycle(int i)

{

if(isPresent(i, k) && isPresent(i, l))

{

temp = new boolean[n+1][n+1];

for(int a=1;a<=n;a++)

for(int b=1;b<=n;b++)

temp[a] = temp[a] = false;



if(can_reach(k, l, i) )

return true;

}


}
}
}
 
Alguém pode me ajudar a traduzir isso para c

Ficaria muito grato, não sei java e precisava passar esse código para C:


// print all subsets of the characters in s
public static void comb1(String s) { comb1("", s); }

// print all subsets of the remaining elements, with given prefix
private static void comb1(String prefix, String s) {
if (s.length() > 0) {
System.out.println(prefix + s.charAt(0));
comb1
(prefix + s.charAt(0), s.substring(1));
comb1
(prefix, s.substring(1));
}
}

// read in N from command line, and print all subsets among N elements
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String elements = alphabet.substring(0, N);

// using first implementation
comb1
(elements);
System.out.println();
}
 
Back
Topo