Thread: Can anyone change this into C format?

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

    Can anyone change this into C format?

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



    if not do you think you can at least give me some clues as to how the ? and : work and how the curr = incr_decr(curr); works.
    I have not been in a C++ class before and i could use some help making this into a C file.
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Then I suggest you learn how classes works. You should know the ?: operator if you know C++.
    // Gliptic

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    I said I have NOT been in a C++ class before and have not worked with C++ before.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I didn't actually get that.

    Anyway, this is homework.
    // Gliptic

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    If you didn't know how to convert thats all you had to say. Heck you didnt need to put your two cents in at all.

    Also give me some examples of some highschools that give this question for a homework question. If anything they would assign the recursive way to solve it.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    uh

    No... I think glip meant class... as in object-orientation >_<;

  7. #7
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I haven't tested this much, so it might need a bit of work. But it should give to the general idea -

    Code:
    #include <stdlib.h> 
    #include <stdio.h>
    
    struct cell
    {
    	int disc; 
    	struct cell *next; 
    };
    
    void cellInit1(struct cell* p,int d)
    {
    	p->disc = d; 
    	p->next = p; 
    }
    
    void cellInit2(struct cell* p,int d, struct cell **n)
    {
    	p->disc=d;
    	p->next=*n;
    }
    
    struct list{
    	struct cell *rear; 
    };
    
    
    int empty(struct list* p) { 
    	return p->rear == p->rear->next;
     } 
    
    
    void pop(struct list* p,int *i) 
    { 
    	
    	struct cell *front;
    	*i = 0; 
    	if( empty(p) ) return; 
    	front = p->rear->next; 
    	p->rear->next = front->next; 
    	*i =front->disc; 
    	free(front);
    	
    } 
    
    void push(struct list* p,int d) { 
    
    	struct cell* temp = p->rear->next;
    	p->rear->next = malloc(sizeof(struct cell));
    	cellInit2(p->rear->next,d,&temp); 
    } 
    
    
    
    void listInit(struct list* p) {
    	p->rear = malloc(sizeof(struct cell));
    	cellInit1(p->rear,0); 
    } 
    
    void listKill(struct list* p){ 
    	int i;
    	while(!empty(p)) pop(p,&i); 
    } 
     
    
    
    struct 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; 
    	int i;
    	oper incr_decr; 
    	int curr=0; 
    	int p1 = 1, p2 = 2; 
    	int d0,d1,d2; 
    	listInit(&pegs[0]);
    	listInit(&pegs[1]);
    	listInit(&pegs[2]);
    	printf("Enter Number Of Discs:");
    	scanf("%d",&n);  
     
    	for( i = n; i > 0; i--) 
    	push(&pegs[0],i); 
    	incr_decr = n%2 ? decr : incr; //conditional statement 
    	while(1) 
    	{ 
    		pop(&pegs[curr],&d0); 
    		printf("Disc %d From %d",d0,curr); 
    		curr = incr_decr(curr); 
    		push(&(*(&pegs[curr])),d0); 
    		printf(" To %d\n",curr); 
    		p1 = incr_decr(p1); 
    		p2 = incr_decr(p2); 
    	if(!empty(&pegs[p1]) && !empty(&pegs[p2])) 
    	{ 
    		pop(&pegs[p1],&d1); 
    		pop(&pegs[p2],&d2); 
    
    		if(d1 < d2) 
    		{ 
    			push(&pegs[p2],d2); 
    			push(&pegs[p2],d1); 
    			printf("Disc %d From %d To %d\n",d1,p1,p2); 
    		} 
    		else 
    		{ 
    			push(&pegs[p1],d1); 
    			push(&pegs[p1],d2); 
    			printf("Disc %d From %d To %d\n",d2,p2,p1); 
    		} 
    	} 
    	else 
    	{ 
    		if(empty(&pegs[p1]) && empty(&pegs[p2])) 
    			break; 
    		if(empty(&pegs[p1])) 
    		{ 
    			pop(&pegs[p2],&d2); 
    			push(&pegs[p1],d2); 
    			printf("Disc %d From %d To %d\n",d2,p2,p1); 
    		} 
    		else 
    		{ 
    			pop(&pegs[p1],&d1); 
    			push(&pegs[p2],d1); 
    			printf("Disc %d From %d To %d\n",d1,p1,p2); 
    		} 
    	} 
    } 
    	listKill(&pegs[0]);
    	listKill(&pegs[1]);
    	listKill(&pegs[2]);
    	return 0; 
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  2. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 09:57 AM
  3. How to change data format between unsigned char and CString?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 02:49 PM
  4. Change data format from CString to int
    By ooosawaddee3 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 10:43 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM