Question

Im very new to VC++. Yesterday my VC++ instructor gave us this code and asked us to make it as a exe. I have no idea of where to start and end. How to make this single file into exe. How and where to paste this working code in visual studio. If my question sounds too dumb, sorry. But i'm. Please help me out in making an exe from this single file. By the way this is Josephus circle algorithm

Code :
//////////////////Header file

#include<iostream.h>
template <class T>
class ex
{
    private:
        struct node
        {
            T data;
            struct node *next;
        };
        struct node *head,*front,*rear;
    public:
        ex()
        {
            head=new node;
            head->next=NULL;
            front=rear=head;
        }
        void enqueue(T x);
        T dequeue();
        void print();
        void move_next();
};



//////////////////Implementation file
#include "ex.h"
template <class T>
void ex<T>::enqueue(T x)
{
    node *p;
    p=new node;
    p->data=x;
    if(head->next==NULL)
    {
        front=rear=p;
        head->next=p;
        p->next=p;
    }
    else
    {
        rear->next=p;
        p->next=front;
        rear=rear->next;
    }
}

template<class T>
T ex<T>::dequeue()
{
    node *t;
    T x;
    t=front;
    x=t->data;
    front=front->next;
    rear->next=front;
    delete(t);
    return x;
}

template<class T>
void ex<T>::print()
{
    node *p=front;
    do
    {
        cout<<p->data<<endl;
        p=p->next;
    }while(p!=rear->next);
}

template<class T>
void ex<T>::move_next()
{
    front=front->next;
    rear=rear->next;
}



/////////////////Application file
#include "ex.cpp"
void main()
{
    ex<int> e;
    int m,n,i,d;
    cout<<"Enter the number of people";
    cin>>n;
    cout<<"Enter the number of passes";
    cin>>m;
    for(i=1;i<=n;i++)
        e.enqueue(i);
    cout<<"The players are
";
    e.print();
    cout<<"Eliminated in order
";
    while(n>1)
    {
        for(i=1;i<=m;i++)
            e.move_next();
        d=e.dequeue();
        cout<<d<<endl;
        n--;
    }
    d=e.dequeue();
    cout<<"Winning player:  "<<d<<endl;
}
Was it helpful?

Solution

Make the first file named ex.h

#include<iostream.h>
template <class T>
class ex
{
    private:
        struct node
        {
            T data;
            struct node *next;
        };
        struct node *head,*front,*rear;
    public:
        ex()
        {
            head=new node;
            head->next=NULL;
            front=rear=head;
        }
        void enqueue(T x);
        T dequeue();
        void print();
        void move_next();
};

Second file into ex.cpp

#include "ex.h"
template <class T>
void ex<T>::enqueue(T x)
{
    node *p;
    p=new node;
    p->data=x;
    if(head->next==NULL)
    {
        front=rear=p;
        head->next=p;
        p->next=p;
    }
    else
    {
        rear->next=p;
        p->next=front;
        rear=rear->next;
    }
}

template<class T>
T ex<T>::dequeue()
{
    node *t;
    T x;
    t=front;
    x=t->data;
    front=front->next;
    rear->next=front;
    delete(t);
    return x;
}

template<class T>
void ex<T>::print()
{
    node *p=front;
    do
    {
        cout<<p->data<<endl;
        p=p->next;
    }while(p!=rear->next);
}

template<class T>
void ex<T>::move_next()
{
    front=front->next;
    rear=rear->next;
}

And the third file into Main.cpp or something.

#include "ex.cpp"
void main()
{
    ex<int> e;
    int m,n,i,d;
    cout<<"Enter the number of people";
    cin>>n;
    cout<<"Enter the number of passes";
    cin>>m;
    for(i=1;i<=n;i++)
        e.enqueue(i);
    cout<<"The players are
";
    e.print();
    cout<<"Eliminated in order
";
    while(n>1)
    {
        for(i=1;i<=m;i++)
            e.move_next();
        d=e.dequeue();
        cout<<d<<endl;
        n--;
    }
    d=e.dequeue();
    cout<<"Winning player:  "<<d<<endl;
}

Then compile it. Also, it's supposed to be int main() not void main()

OTHER TIPS

To add to Alex F. answer, you will also need to create an ex.h file and an ex.cpp file and paste their respective code inside

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top