CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Aug 2012
    Posts
    28

    [RESOLVED] C++ to C#

    I code a algorithm by C++(Breadth first search), i'm fluent C++, but i'm learning C#, so i don't know function or keyword between it, i need convert to learning and searching, can you help convert it, short:
    Code:
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    const int maxx = 20;
    
    void Read_input_from_user(bool grid[][maxx], int vertices)
    {
        int u, v;
        for(int x = 0; x < vertices; ++x)
        {
            cout << "Enter u : \t"; 
            cin >> u;
            u--;
            cout << "Enter v : \t";
            cin >> v;
            v--;
            grid[u][v] = true;
                    grid[v][u] = true;
            cout << "---------------------\n";
        }
    }
    
    void Breadth_first_search(queue<int> &Q, vector<int> &trace,
                              bool grid[][maxx], int start, int nodes)
    {
        int u;
        vector<int> visited(maxx,0);
        Q.push(start);
        trace[start] = -1;
        visited[start] = 1;
        do{
            u = Q.front();
            Q.pop();
            for(int v = 0; v < nodes; ++v)
            {
                if((grid[u][v] == true) && visited[v] == 0)
                {
                    Q.push(v);
                    trace[v] = u;
                    visited[v] = 1;
                }
            }
        }while(!Q.empty());
    }
    
    void Trace_result(vector<int> &trace, int start, int end, int nodes)
    {
        cout << "From _nodes" << start + 1 << " you can visit :\n";
        for(int v = 0; v < nodes; ++v)
        {
            if(trace[v] != 0)
            {
                cout << " _nodes : " << v + 1 << " , ";
            }
        }
    
        cout << "\n--------------------------------------------\n";
        cout << "The path from " << start + 1 << " to " << end + 1 << '\n';
        
        if(trace[end] == 0){
            cout << "Unavailable.! to go to from " << end + 1 
                 << " to -> " << start + 1 << '\n';
        }
        else{
            while(end != start)
            {
                cout << end + 1 << "<-";
                end = trace[end];
            }
            cout << start + 1 << endl;
        }
        
    }
    
    
    int main()
    {
        //Initialization
        vector<int> trace(maxx, 0);
        queue<int> Q;
        bool grid[maxx][maxx] = {false};
        
        int nodes, vertices;
        cout << "Please input the number of Node : \n";
        cin >> nodes;
        cout << "Please input the number of Vertices : \n";
        cin >> vertices;
    
        //Set value for all vertices.
        Read_input_from_user(grid, vertices); 
    
        //Read the necessary path
        int starting_position, finishing_position;
        cout << "Please Input the Starting Node : \n";
        cin >> starting_position;
        cout << "Please Input the Finishing Node : \n";
        cin >> finishing_position;
        //Decrease to fit with index of C++ start from 0->size-1
        starting_position--;
        finishing_position--;
        //Algorithm starts 
        Breadth_first_search(Q, trace, grid, starting_position, nodes);
        Trace_result(trace, starting_position, finishing_position, nodes);
        system("pause");
        return 0;
    }
    a
    Thanks very much, hope you.
    Last edited by BioPhysEngr; August 25th, 2012 at 11:19 PM. Reason: add code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured