C中的二维数组 [英] 2 dimensional arrays in C

查看:64
本文介绍了C中的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,朋友们!我在用C处理二维数组时遇到麻烦.我尝试过的代码....
1 4 15 7
8 10 2 11
14 3 6 13
12 9 5 _
最后一个元素是光标应放置和放置的空格.每当移动箭头键时,值都必须与空格交换.就像现在按下左箭头键一样,5将移到右侧&.空格将替换为5.其余的箭头键也是如此.下面的getkey()方法提供了每个箭头键的扫描代码.

Hello frnds! I am having a trouble in dealing with 2 dimensional arrays in C. Following is the design & code I tried for....
1 4 15 7
8 10 2 11
14 3 6 13
12 9 5 _
The last element is space whr the cursor should be placed & whenever the arrow keys are moved the value must be swapped with the space. Like if now left arrow key is pressed, 5 will move to the right & space will replace 5. Likewise for the remaining arrow keys. The getkey() method below gives the scan code for each arrow key.

// PUZZLE
#include<stdio.h>
#include<conio.h>
#include<dos.h>
int main()
{
	int a[4][4]={{1,4,15,7},{8,10,2,11},{14,3,6,13},{12,9,5,}};
	int i,j,cnt=0,key,ukey,dkey,lkey,rkey,x,y;
	int pos,val;
	 int temp;
	clrscr();
	printf("\nFollowing is the puzzle:\n");
	printf("\nUse the arrow keys for solving the puzzle\n---------------------------------------");
	for(i=0;i<4;i++)
	{
	   printf("\n");
	   for(j=0;j<4;j++)
	   {
	      if(i==3 && j==3)
		{
		     a[3][3]='' '';
		     printf("%c",a[3][3]);
		     break;
		}
		printf("%d\t",a[i][j]);
	   }
	}
	//pos=a[4][4];
	if(cnt==0)
	{
	   key=getkey();
	   if(key==77 || key==80) //check for right & down key and do nothing
	   {   }
	   else
	   {
	       i=3;j=3;
	       if(key==72)        //up arrow
	       {
		   // printf("\nYou pressed the %d arrow key",key);
		   temp=a[i][j];
		   a[i][j]=a[i-1][j];
		   a[i-1][j]=temp;		 
		   for(i=0,j=0;i<4,j<4;i++,j++)
		       printf("%d",a[i][j]);
	       }
	       else if(key==75)        //left arrow
	       {
		   //pos=a[i][j];
		   temp=a[i][j];
		   a[i][j]=a[i][j-1];
		   a[i][j-1]=temp;
	       }
	   }//else
	   cnt++;
	}//outer if
	getch();
	return 0;
}
int getkey()
{
	union REGS i,o;
	while(!kbhit())
		;
	i.h.ah=0;
	int86(22,&i,&o);
	return(o.h.ah);
}


任何建议将不胜感激.谢谢.


Any suggestion will be appreciated. Thanks.

推荐答案

首先,使用更合理的名称:如果通过反映其作用的名称来调用变量,则很容易理解您的操作.例如,有两个名为userRow和userCol的变量来告诉您用户实际在哪里.
其次,使用例程来分解您的代码并使其更易于阅读:
Firstly, use more sensible names: if you call your variables by names that reflect what they do, it is a lot easier to understand what you are doing. For example, have two variables called userRow and userCol to tell you where the user actually is.
Secondly, use routines to break up your code and make it easier to read:
#include <stdio.h>
#define ROWS_COUNT 4
#define COLS_COUNT 4
#define TRUE (1==1)
int playSurface[ROWS_COUNT][COLS_COUNT]={{1,4,15,7},{8,10,2,11},{14,3,6,13},{12,9,5,-1}};
int userRow = ROWS_COUNT - 1;
int userCol = COLS_COUNT - 1;
int main()
   {
   int key;
   do
      {
      ShowPlaySurface();
      key = GetKey();
      if (key == ''Q'' || key == ''q'')
         {
         break;
         }
      DoMove(key);
      } while (TRUE);
   exit(0);
   }
int ShowPlaySurface()
   {
   int i, j;
   for (i = 0; i < ROWS_COUNT; i++)
      {
      for (j = 0; j < COLS_COUNT; j++)
         {
         if (i == userRow && j == userCol)
            {
            printf ("\t");
            }
         else
            {
            printf ("%d\t", playSurface[i][j]);
            }
         }
      printf("\n");
      }
   }
   
int GetKey()
   {
   return (int) getch();
   }
int DoMove(int key)
   {
   }

现在,您所要做的就是专注于移动"功能-这只是检查按键并朝着适当方向移动的一种情况.简单!

Now, all you have to do is concentrate on the Do Move function - which is just a case of checking keys, and moving in the appropriate direction, if you can. Easy!


这篇关于C中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆