Thread: Can anyone translate this into C? from C++?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    34

    Can anyone translate this into C? from C++?

    #include<iostream.h>


    class cell {
    int disc;
    cell *next;
    cell (int d)
    {
    disc = d;
    next = this;
    }
    cell (int d, cell *n)
    {
    disc = d;
    next = n;
    }
    friend class list;
    };

    class list{
    cell *rear;
    public:
    int empty() { return rear == rear->next; }


    void pop(int &i)
    {
    i = 0;
    if( empty() ) return;
    cell *front = rear->next;
    rear->next = front->next;
    i = front->disc;
    delete front;
    return;
    }

    void push(int d)
    {
    rear->next = new cell(d,rear->next);
    }



    list() { rear = new cell(0); }

    ~list()
    {
    int i,j,l;
    long k;
    while(!empty()) pop(i);
    }
    };


    list pegs[3];

    typedef int (*oper)(int);

    int decr(int a)
    {
    a--;
    if(a<0)
    a=2;
    return a;
    }

    int incr(int a)
    {
    a = (a+1)%3;
    return a;
    }



    int main(void)
    {
    int n;
    oper incr_decr;
    int curr=0;
    int p1 = 1, p2 = 2;
    int d0,d1,d2;
    cout << "Enter Number Of Discs:";
    cin >> n;
    for(int i = n; i > 0; i--)
    pegs[0].push(i);
    incr_decr = n%2 ? decr : incr; //conditional statement
    while(1)
    {
    pegs[curr].pop(d0);
    cout<<"Disc "<<d0<<" From "<<curr;
    curr = incr_decr(curr);
    pegs[curr].push(d0);
    cout<<" To "<<curr<<endl;
    p1 = incr_decr(p1);
    p2 = incr_decr(p2);
    if(!pegs[p1].empty() && !pegs[p2].empty())
    {
    pegs[p1].pop(d1);
    pegs[p2].pop(d2);

    if(d1 < d2)
    {
    pegs[p2].push(d2);
    pegs[p2].push(d1);
    cout<<"Disc "<<d1<<" From "<<p1<<" To "<<p2<<endl;
    }
    else
    {
    pegs[p1].push(d1);
    pegs[p1].push(d2);
    cout<<"Disc "<<d2<<" From "<<p2<<" To "<<p1<<endl;
    }
    }
    else
    {
    if(pegs[p1].empty() && pegs[p2].empty())
    break;
    if(pegs[p1].empty())
    {
    pegs[p2].pop(d2);
    pegs[p1].push(d2);
    cout<<"Disc "<<d2<<" From "<<p2<<" To "<<p1<<endl;
    }
    else
    {
    pegs[p1].pop(d1);
    pegs[p2].push(d1);
    cout<<"Disc "<<d1<<" From "<<p1<<" To "<<p2<<endl;
    }
    }
    }
    return 1;
    }


    I have already compiled it and it works fine, but i need the source code to be in C.
    Thanks for the help

  2. #2
    Unregistered
    Guest
    If you are smart enough to write this in C++ then you should be smart enough to translate this to C. But I am not going to sit here and change your OOP program to a procedural program, it is a waste of my time. I will give you a hint, write a stack and stack handling routine. Use that...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, they didn't write the original code.
    Second, I already did the conversion for them, but I didn't compile it. As such, there were some "very hard to fix" errors... you know the kind, hard things like:

    'rear' undeclared, because I referenced 'rear' instead of 'l->rear'.

    "hard" things like that...

    If they can't even solve such simple errors in source code, when the compiler gives you the line they're on, then no, there's no way in hell they wrote the originial code.

    [edit] Actually, I compiled the code, and it appears as though there's a bit more to it, however, in the other thread they mentioned the above problems. Anyway, I the rewrite is done, tidy it up if you want, fixing push() and pop() should be trivial...then again, fixing 'undeclared ''rear''' was trivial also...

    Quzah.
    Last edited by quzah; 12-19-2001 at 03:00 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please translate
    By Ron in forum C Programming
    Replies: 24
    Last Post: 07-31-2008, 06:37 AM
  2. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  3. translate
    By limitmaster in forum C++ Programming
    Replies: 9
    Last Post: 08-13-2006, 01:55 AM
  4. Can Someone Translate this text
    By lanh215 in forum C++ Programming
    Replies: 24
    Last Post: 01-17-2006, 10:02 AM
  5. Please help me... Translate from c++ to visual basic
    By westcard in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2004, 02:29 PM