Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Pasar codigo de C a C++

Estas en el tema de Pasar codigo de C a C++ en el foro de C/C++ en Foros del Web. Quiero que me ayuden en esto, resulta que este codigo esta en C, yo estoy aprendiendo a programar en C++, ese es mi inconveniente, quiero ...
  #1 (permalink)  
Antiguo 24/09/2011, 20:04
 
Fecha de Ingreso: septiembre-2011
Ubicación: lima
Mensajes: 2
Antigüedad: 12 años, 6 meses
Puntos: 0
Pregunta Pasar codigo de C a C++

Quiero que me ayuden en esto, resulta que este codigo esta en C, yo estoy aprendiendo a programar en C++, ese es mi inconveniente, quiero que me digan que cambiar para pasarlo a C++, que compile y que se ejecute.

No se que problema hay porque cuando lo ejecuto el codigo en C en DEV C++ sale un pantalla solo un 0 y no me deja meter los valores.

Para pasarlo a C++ creo que falta el #include<iostream> y el using namespace std para las entradas y las salidas cout y cin, aparte de eso tambien falta un getchar() en el main() final para que no se cierre tan rapido.

Lo que tambien me confunde es la funcion "void read_input_file()" en fopen y fscanf no domino bien eso.

En la parte final de esta pregunta hay un ejemplo de que es lo que introduce y que es lo que bota el programa. Es el algoritmo de ford fulkerson. Gracias de antemano.

#include <stdio.h>
//Basic Definitions

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_NODES 1000
#define oo 1000000000
//Declarations

int n; // number of nodes
int e; // number of edges
int capacity[MAX_NODES][MAX_NODES]; // capacity matrix
int flow[MAX_NODES][MAX_NODES]; // flow matrix
int color[MAX_NODES]; // needed for breadth-first search
int pred[MAX_NODES]; // array to store augmenting path

int min (int x, int y) {
return x<y ? x : y; // returns minimum of x and y
}
//A Queue for Breadth-First Search

int head,tail;
int q[MAX_NODES+2];

void enqueue (int x) {
q[tail] = x;
tail++;
color[x] = GRAY;
}

int dequeue () {
int x = q[head];
head++;
color[x] = BLACK;
return x;
}
//Breadth-First Search for an augmenting path

int bfs (int start, int target) {
int u,v;
for (u=0; u<n; u++) {
color[u] = WHITE;
}
head = tail = 0;
enqueue(start);
pred[start] = -1;
while (head!=tail) {
u = dequeue();
// Search all adjacent white nodes v. If the capacity
// from u to v in the residual network is positive,
// enqueue v.
for (v=0; v<n; v++) {
if (color[v]==WHITE && capacity[u][v]-flow[u][v]>0) {
enqueue(v);
pred[v] = u;
}
}
}
// If the color of the target node is black now,
// it means that we reached it.
return color[target]==BLACK;
}
//Ford-Fulkerson Algorithm

int max_flow (int source, int sink) {
int i,j,u;
// Initialize empty flow.
int max_flow = 0;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
flow[i][j] = 0;
}
}
// While there exists an augmenting path,
// increment the flow along this path.
while (bfs(source,sink)) {
// Determine the amount by which we can increment the flow.
int increment = oo;
for (u=n-1; pred[u]>=0; u=pred[u]) {
increment = min(increment,capacity[pred[u]][u]-flow[pred[u]][u]);
}
// Now increment the flow.
for (u=n-1; pred[u]>=0; u=pred[u]) {
flow[pred[u]][u] += increment;
flow[u][pred[u]] -= increment;
}
max_flow += increment;
}
// No augmenting path anymore. We are done.
return max_flow;
}
//Reading the input file and the main program

void read_input_file() {
int a,b,c,i,j;
FILE* input = fopen("mf.in","r");
// read number of nodes and edges
fscanf(input,"%d %d",&n,&e);
// initialize empty capacity matrix
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
capacity[i][j] = 0;
}
}
// read edge capacities
for (i=0; i<e; i++) {
fscanf(input,"%d %d %d",&a,&b,&c);
capacity[a][b] = c;
}
fclose(input);
}

int main () {
read_input_file();
printf("%d\n",max_flow(0,n-1));
return 0;
}





The Input File

6 10 // 6 nodes, 10 edges
0 1 16 // capacity from 0 to 1 is 16
0 2 13 // capacity from 0 to 2 is 13
1 2 10 // capacity from 1 to 2 is 10
2 1 4 // capacity from 2 to 1 is 4
3 2 9 // capacity from 3 to 2 is 9
1 3 12 // capacity from 1 to 3 is 12
2 4 14 // capacity from 2 to 4 is 14
4 3 7 // capacity from 4 to 3 is 7
3 5 20 // capacity from 3 to 5 is 20
4 5 4 // capacity from 4 to 5 is 4
Output of the Program

The program computes the maximum flow from 0 to 5.
23
  #2 (permalink)  
Antiguo 26/09/2011, 02:08
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 19 años, 11 meses
Puntos: 74
Respuesta: Pasar codigo de C a C++

No parece que haga ninguna falta pasar el codigo ... ¿Creaste un archivo llamado mf.in con esos valores y en el directorio del ejecutable?
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #3 (permalink)  
Antiguo 26/09/2011, 02:45
 
Fecha de Ingreso: septiembre-2011
Ubicación: lima
Mensajes: 2
Antigüedad: 12 años, 6 meses
Puntos: 0
Respuesta: Pasar codigo de C a C++

Gracias Eternal Idol por tu ayuda. Estoy muy agradecido contigo. Hablamos
  #4 (permalink)  
Antiguo 26/09/2011, 04:21
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 19 años, 11 meses
Puntos: 74
Respuesta: Pasar codigo de C a C++

De nadas
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #5 (permalink)  
Antiguo 15/10/2011, 22:00
Avatar de sbcl  
Fecha de Ingreso: julio-2011
Mensajes: 29
Antigüedad: 12 años, 9 meses
Puntos: 5
Respuesta: Pasar codigo de C a C++

Se que la duda esta resuelta, pero solo quería dejar un consejito:
Cuando tenes que hacer una serie de constantes Númericas ordenadas(0,1 y 2 en este caso) Podes usar una Enum.
Como se usa una Enum?

enum {WHITE , GRAY , BLACK} ;

Esto es equivalente a hacer:

#define WHITE 0
#define GRAY 1
#define BLACK 2

Con esto mismo, se podia hacer:
enum {WHITE, GRAY, BLACK, YELLOW, RED};
Y seria equivalente a:

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define YELLOW 3
#define RED 4

Es una comodidad mas que nada, para ahorrarte los Defines ^^

Etiquetas: c++, dev, traslado
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 04:52.