Προς το περιεχόμενο

Επείγουσα επίλυση άσκησης


georginos1989

Προτεινόμενες αναρτήσεις

Δημοσ.

Σε τι γλώσσα; C?

Εσύ έχεις γράψει κώδικα; Που κολλάς στην υλοποίηση του;

Ανέβασε κώδικα που έχεις κάνει αμα είναι για να μην αρχίσει απο την αρχή όποιος και αν ασχοληθεί. (Αν δεν έχεις γράψει κώδικα απο την άλλη...)

Δημοσ.

το κλασσικοτερο προβλημα...

λυνεται και αναδρονικα αλλα και επαναληπτικα

με ενα search στο google το βρισκεις

Δημοσ.

να η λύση στο πρόβλημα σου

 

πύργοι hanoi!!!

 

>
#include <stdio.h>
int main(
){int
z,y,n;
scanf("%d",&n);//νουμερο στύλων
for(y=1;(1<<n)-y
;y<<=z-1,printf(
"disk %i from %i to %i.\n"/**/
,z,(y&y-1)%3,((y|y-1)+1)%3),y
++)for(z=1;!(y&1);z++,y>>=1);
return 0;
}

Δημοσ.

στη συναρτηση θέλεις pointer και περνας pointer σε pointer κτλ.

Δες τα μηνυματα που σου βγάζει ο compiler

 

>
h.cpp
h.cpp(133) : error C2664: 'hanoi' : cannot convert parameter 5 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(135) : error C3861: 'strcmp': identifier not found
h.cpp(135) : error C3861: 'strcmp': identifier not found
h.cpp(139) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(141) : error C3861: 'strcmp': identifier not found
h.cpp(141) : error C3861: 'strcmp': identifier not found
h.cpp(145) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(147) : error C3861: 'strcmp': identifier not found
h.cpp(147) : error C3861: 'strcmp': identifier not found
h.cpp(151) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(153) : error C3861: 'strcmp': identifier not found
h.cpp(153) : error C3861: 'strcmp': identifier not found
h.cpp(157) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(159) : error C3861: 'strcmp': identifier not found
h.cpp(159) : error C3861: 'strcmp': identifier not found
h.cpp(163) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(165) : error C3861: 'strcmp': identifier not found
h.cpp(165) : error C3861: 'strcmp': identifier not found
h.cpp(169) : error C2664: 'scan' : cannot convert parameter 1 from 'stack *' to 'int []'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(171) : error C2664: 'hanoi' : cannot convert parameter 5 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(203) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(205) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(207) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(211) : error C2664: 'Pop' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(212) : error C2664: 'Pop' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(213) : error C2664: 'Pop' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(220) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(221) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(222) : error C2664: 'Push' : cannot convert parameter 1 from 'stack **' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
h.cpp(225) : error C2664: 'hanoi' : cannot convert parameter 5 from 'stack *(*)[3]' to 'stack *'
       Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Δημοσ.

Δεν έχεις διορθώσει τα σφάλματα στο compile τα οποία είναι βέβαιο ότι θα κάνουν το πρόγραμμα να "σκάσει".

 

π.χ. τα μηνύματα που βγαίνουν είναι του στύλ

>
h.cpp(135) : error C2664: 'Top' : cannot convert parameter 1 from 'stack *' to 'stack'
h.cpp(136) : error C2664: 'Pop' : cannot convert parameter 1 from 'stack **' to 'stack *'

τα οποία αναφέρουν ότι έχεις δηλώσει ότι η συνάρτηση Pop θέλει μια παράμετρο τύπου δείκτη σε stack. Εσυ όμως περνάς δείκτη σε δείκτη σε stack. Αυτό σημαίνει ότι στέλνεις τυχαία περιεχόμενα και καταστρέφεις τη μνήμη του προγράμματος σου.

 

Να σου δώσω ένα παράδειγμα πως πρέπει να καλείς τις συναρτησεις του stack σου οι οποίες δουλεύουν σωστά

>
int main(void){
stack root = stackEmpty;
Push(&root,3);
Push(&root,7);
printf("%d\n",Top(root));
Pop(&root);
printf("%d\n",Top(root));
Pop(&root);
return 0;
}

Μόλις βρω χρόνο θα δω τον πίνακα που έχεις κάνει γιατι εκεί έχεις μπερδέψει τους τυπους stack * και stack** και στέλνεις τυχαία πράγματα στις συναρτησεις του stack

Δημοσ.

εχεις φτιαξει πινακα stack *xxx[3].

το stack ειναι δεικτης και βαζεις και το *, οποτε γινεται δεικτης σε δεικτη.

Γι'αυτο εχεις λάθος στον τροπο κλησεις των συναρτησεων.

 

υγ. ξερεις οτι το hanoi σαν επίλυση ειναι πολύ απλό; (5-10 γραμμες κωδικα συνολικα)

 

---------- Το μήνυμα προστέθηκε στις 20:40 ----------

 

στη main κάνεις

>
for(i=0;i<=s;i++)
     Push(&stoiva[0],s-i);
for(i=0;i<s;i++)
     Push(&stoiva[1],0);
for(i=0;i<s;i++)
     Push(&stoiva[2],0);

και μετά

>
for(i=s;i>=0;i--)
{
       left1[i]=Pop(&stoiva[0]);
       middle1[i]=Pop(&stoiva[1]);
       right1[i]=Pop(&stoiva[2]);
}

δεν καταλαβαίνω γιατι μολις εκανες push κανεις pop, αλλά άσχετα με αυτο εδω πέρνεις μπλέ οθονη (την πρωτη), διοτι κάνεις Pop περισσότερα πράγματα απο όσα εκανες push.

Στην stoiva[1] και [2] έχεις βάλει 1 λιγότερο απο την stoiva[0].

Παρ'ολα αυτα οταν κάνεις pop ζητας απο παντου τον ίδιο αριθμο απο pop.

 

Αν αρχίσεις να δουλευεις debugger, θα μπορείς να τρεχεις το προγραμματακι σου μια-μια εντολή και να βλέπεις τι γινεται.

Δημοσ.

με την push βάζω τιμές στην στοίβα και μετά παίρνω τις τιμές αυτές σε πίκανα int ώστε να μπορώ ανάλογα με τον αριθμό που έχει μέσα ο πίνακας να τυπώνω με την συνάρτηση τις αναλογες παύλες

Δημοσ.

Τις υπόλοιπες ερωτησεις/υποδείξεις στο post no#10 τις είδες;

 

Ο λόγος που η pop σκάει αντί να βγάζει μηνυμα οτι η στοιβα είναι αδεια, είναι ότι δεν έχεις δωσει αρχικη τιμή NULL στην αρχη της στοιβας.

 

>
int s,i,left1[size]={0,0,0,0,0},middle1[size]={0,0,0,0,0},right1[size]={0,0,0,0,0};
[color="Red"]stack stoiva[3] = {NULL,NULL,NULL};[/color]

 

Το σημείο και το λόγο που η μνημη σκάει το εχω αναφερει στο #10

Δημοσ.

οκ θα τα κοιταξω....

 

---------- Το μήνυμα προστέθηκε στις 21:27 ----------

 

Σταματάει στην τελευταία επανάληψη όταν πάει να κάνει pop από τιν stoiva[1] στον πίνακα Middle.... Γιατί όμως?

 

---------- Το μήνυμα προστέθηκε στις 21:28 ----------

 

Την άλλαξα τη stack να είναι μονος Pointer...

Δημοσ.

Στο έχω γραψει:

Στην stoiva[1] και [2] έχεις βάλει 1 λιγότερο απο την stoiva[0].

Παρ'ολα αυτα οταν κάνεις pop ζητας απο παντου τον ίδιο αριθμο απο pop.

ζητάς 1 στοιχείο περισσότερο απ'οσο εχει

 

---------- Το μήνυμα προστέθηκε στις 21:47 ----------

 

Μετα ξανακανεις push για να γεμισεις τις στοιβες και βάζεις και 1 στοιχειο παραπάνω απο τα s που ε'ιχες πριν

 

>
for(i=1;i[color="Red"]<=s+1[/color];i++)
{
       Push(&stoiva[0],left1[i-1]);
       Push(&stoiva[1],middle1[i-1]);
       Push(&stoiva[2],right1[i-1]);
}

 

οι μικροαλλαγες που εχω κάνει για να γινεται compile και να μην βγάζει μπλε οθονη:

>// ha.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define size 5

/* Simply linked lists */

typedef struct ListNode_tag {
  int data;
  struct ListNode_tag * next;
} ListNode;


/* Type stack */

typedef ListNode * stack;


/* Empty stack */

const stack stackEmpty = NULL;


/* Push */

void Push (stack * sp, int t)
{
ListNode * n = (ListNode *) malloc(sizeof(ListNode));

/* Check if malloc succeeded */

if (n == NULL) {
	fprintf(stderr, "Out of memory\n");
	getchar();
	exit(1);
}

/* Copy the data */

n->data = t;

/* Add this element to the stack */

n->next = *sp;
*sp = n;
}


/* Pop */

int Pop (stack * sp)
{
ListNode * n;
int result;

/* Error, if the stack was empty */

assert(sp);
if(*sp == NULL) {
	fprintf(stderr, "Nothing to remove from an empty stack\n");
	getchar();
	exit(1);
}

/* Remove and free the first element */

n = *sp;
result = (*sp)->data;
*sp = (*sp)->next;
free(n);

return result;
}


/* Top */

int Top (stack s)
{
/* Error, if the stack was empty */

if (s == NULL) {
	fprintf(stderr, "Nothing to see in an empty stack\n");
	getchar();
	exit(1);
}

return s->data;
}



void disp(stack stoiva2[3])
{
int i,k,m,n,pin1[5],pin2[5],pin3[5];

for(i=1;i<=5;i++)
{
       k=Top(stoiva2[0]);
       Pop(&stoiva2[0]) ;
        if(k==0)
               printf("\n      .     ");
        else if(k==1)
               printf("\n     _._    ");
        else if(k==2)
                printf("\n   __.__   ");
        else if(k==3)
               printf("\n   ___.___  ");
        else if(k==4)
               printf("\n  ____.____ ");
         pin1[i-1]=k;
         k=0;

         m=Top(stoiva2[1]);
         Pop(&stoiva2[1]);
         if(m==0)
               printf("\n      .     ");
        else if(m==1)
               printf("\n     _._    ");
        else if(m==2)
                printf("\n   __.__   ");
        else if(m==3)
               printf("\n   ___.___  ");
        else if(m==4)
               printf("\n  ____.____ ");
         pin2[i-1]=m;
         m=0;

         n=Top(stoiva2[2]);
         Pop(&stoiva2[2]);
         if(n==0)
               printf("\n      .     ");
        else if(n==1)
               printf("\n     _._    ");
        else if(n==2)
                printf("\n   __.__   ");
        else if(n==3)
               printf("\n   ___.___  ");
        else if(n==4)
               printf("\n  ____.____ ");
         pin3[i-1]=n;
         n=0;
}
}
void hanoi (int rings,const char * from,const char * to,const char * auxil, stack stoiva1[3])
{
  int k;
 /* printf("%d", Pop(&stoiva1[1]));
  printf("%d", Pop(&stoiva1[1]));
  printf("%d", Pop(&stoiva1[1]));
  printf("%d", Pop(&stoiva1[1]));
  printf("%d", Pop(&stoiva1[1])); */


  if (rings >= 1) {
     hanoi(rings-1, from, auxil, to,stoiva1);
     //printf("%s -> %s\n", from, to);
     if(strcmp(from,"left")==0 && strcmp(to,"middle")==0)
     {
       k=Top(stoiva1[0]);
       Pop(&stoiva1[0]);
       Push(&stoiva1[1],k);
       disp(stoiva1);
       k=0;
     }
     else if(strcmp(from,"left")==0 && strcmp(to,"right")==0)
     {
       k=Top(stoiva1[0]);
       Pop(&stoiva1[0]);
       Push(&stoiva1[2],k);
       disp(stoiva1);
       k=0;
     }
     else if(strcmp(from,"middle")==0 && strcmp(to,"right")==0)
     {
       k=Top(stoiva1[1]);
       Pop(&stoiva1[1]);
       Push(&stoiva1[2],k);
       disp(stoiva1);
       k=0;
     }
     else if(strcmp(from,"right")==0 && strcmp(to,"left")==0)
     {
       k=Top(stoiva1[2]);
       Pop(&stoiva1[2]);
       Push(&stoiva1[0],k);
       disp(stoiva1);
       k=0;
     }
     else if(strcmp(from,"right")==0 && strcmp(to,"middle")==0)
     {
       k=Top(stoiva1[2]);
       Pop(&stoiva1[2]);
       Push(&stoiva1[1],k);
       disp(stoiva1);
       k=0;
     }
     else if(strcmp(from,"middle")==0 && strcmp(to,"left")==0)
     {
       k=Top(stoiva1[1]);
       Pop(&stoiva1[1]);
       Push(&stoiva1[0],k);
       disp(stoiva1);
       k=0;
     }
     hanoi(rings-1, auxil, to, from,stoiva1);
  }
}

/*solve ( int N, stack *A, stack *B, stack *C)
{
int x;

if(N>0)
solve (N-1,A,B,C);
x=Top(A);
Pop(&A);
Push(&B,x);
scan(&A,&C,&;
solve(N-1,C,B,A);
}  */




void xmain()
{

int s,i,left1[size]={0,0,0,0,0},middle1[size]={0,0,0,0,0},right1[size]={0,0,0,0,0};
stack stoiva[3] = {NULL,NULL,NULL};


//printf("Give size of stack\n");
//scanf("%d", &s);
s = 4;


for(i=0;i<=s;i++)
     Push(&stoiva[0],s-i);
for(i=0;i<=s;i++)
     Push(&stoiva[1],0);
for(i=0;i<=s;i++)
     Push(&stoiva[2],0);


for(i=s;i>=0;i--)
{
       left1[i]=Pop(&stoiva[0]);
       middle1[i]=Pop(&stoiva[1]);
       right1[i]=Pop(&stoiva[2]);
}

//scan(left1,middle1,right1);

for(i=1;i<=s;i++)
{
       Push(&stoiva[0],left1[i-1]);
       Push(&stoiva[1],middle1[i-1]);
       Push(&stoiva[2],right1[i-1]);
}


hanoi(4, "left", "right", "middle",stoiva);
//solve(s,&stoiva[0],&stoiva[2],&stoiva[1]);

getchar();
}


int _tmain(int argc, _TCHAR* argv[])
{
xmain();
return 0;
}

 

Μετα απο αυτο θα πρεπει να ξαναδεις τη λογικη για το πως επιλυετε το hanoi ως αναφορα τον τρόπο μετακινησεων.

 

Οσο για την εκτυπωση παυλών, όταν πχ θελεις να τυπωσεις δισκο 1,2,3,... μην κάνεις if/else για κάθε τιμη χωριστα, κανε μια συναρτηση, θα γλυτωνες γραψιμο αν το ειχες κανει απο την αρχη και θα συμαζευτει και το προγραμμα καπως (σε μεγεθος)

 

πχ.

>print_disk(int n){
   int i;
   for(i=0;i<=size-n;i++)         putchar(' ');
   for(i=0;i<n;i++)                  putchar('_');
   putchar('.');
   for(i=0;i<n;i++)                  putchar('_');
   for(i=0;i<=size-n;i++)         putchar(' ');
}

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...