河内塔计划失去了数字

时间:2015-02-16 21:54:28

标签: arrays numbers

我正在用c编写一个程序,它将作为一个带有数字和数组的河内塔游戏(每个数组代表一个不同的塔)。 此时我正致力于在数组之间传输数字,将它们放在正确的位置,然后从前一个位置移除它们。该程序似乎在前几个操作中正常工作。然后数字开始消失,我无法弄清楚原因。 这是代码:

 #include <stdio.h>
 #include <stdlib.h>
 #define N 3
 #define Z '\0'

 int najm(int a[]); //counts the smallest number in array
 int najmloc(int a[]);//displays the position of the smallest number
 void wypisz (int a[]);

 int main(void)
 {
 int x[N] = {3,2,1}; 
 int y[N] = {Z, Z, Z};
 int z[N] = {Z, Z, Z};
 int wx, wy, wz;  // The smallest number
 int lwx, lwy, lwz;
 int win = 0;   // Have you won
 while (win == 0)
 {
 wypisz (x);  //wypisz = write
 wypisz (y);
 wypisz (z);

 wx = najm (x);   //counts smallest
 wy = najm (y);
 wz = najm (z);

 lwx = najmloc (x);   //counts locations
 lwy = najmloc (y);
 lwz = najmloc (z);

 printf ("x %d %d\n", wx, lwx); //this part is optional, it was just for testing.
 printf ("y %d %d\n", wy, lwy);
 printf ("z %d %d\n", wz, lwz);

 char a, b; //controler 
 printf ("\n\n write down first where, and then where to after spacebar (XYZ): ");
 scanf ("%c %c", &a, &b);

 int tmp;

 switch (a) {
 case 'X':
     {
         tmp = x[lwx];
         x[lwx] = Z;
         break;
     }
 case 'Y':
     {
         tmp = y[lwy];
         y[lwy] = Z;
         break;
     }
 case 'Z':
     {
         tmp = y[lwy];
         z[lwz] = Z;
         break;
     }}


 switch (b){
 case 'X':
     {
         x[lwx+1]= tmp;
         break;
     }
 case 'Y':
     {
        y[lwy+1]= tmp;
         break;
     }
 case 'Z':
     {
         z[lwz+1]= tmp;
         break;
     }

 }

  while ( getchar() != '\n' );
    }
    return 0;
    }


    void wypisz (int a[])
    {
int i;
for (i = 0; i < N; i++) printf ("%d ", a[i]);
printf ("\n");
}
 int najm(int a[])
{

int i, k = 1;
int loc;
int wa = N;
for (i=0; i<N; i++){
    if (a[i] > 0)
    {
        if (a[i] < wa)
            {
            wa = a[i];
            loc = i;
            k = 0;
            }}
    if (a[i] == 0 && k == 1) {wa = 0;}
    }
    return wa;
    }

    int najmloc(int a[])
    {

int i, k = 1;
int loc;
int wa = N;
for (i=0; i<N; i++){
    if (a[i] > 0)
    {
        if (a[i] < wa)
            {
            wa = a[i];
            loc = i;
            k = 0;
            }}
    if (a[i] == 0 && k == 1) {loc = -1;}
    }
    return loc;
    }

这里的任何帮助对我来说都是非常重要的事情。

0 个答案:

没有答案