如何编译这个VC ++程序?

时间:2012-08-18 05:58:54

标签: visual-c++ josephus

我对VC ++很新。昨天我的VC ++讲师给了我们这段代码,并要求我们把它作为一个exe。我不知道从哪里开始和结束。如何将这个单个文件转换为exe。如何以及在何处将此工作代码粘贴到visual studio中。如果我的问题听起来太愚蠢,抱歉。但是我。请帮我从这个单独的文件制作一个exe。顺便说一句,这是约瑟夫斯圆算法

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;
}

2 个答案:

答案 0 :(得分:4)

制作名为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();
};

第二个文件到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;
}

将第三个文件放入Main.cpp或其他东西。

#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;
}

然后编译它。此外,它应该是int main()而不是void main()

答案 1 :(得分:0)

要添加到Alex F.回答,您还需要创建一个ex.h文件和一个ex.cpp文件并将其各自的代码粘贴到