📄 main1.bak
字号:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//Used to define the direction
#define UPRIGHT 0
#define DOWNRIGHT 1
#define UPLEFT 2
#define DOWNLEFT 3
//define the size of the matrix
#define MSIZE 3
int direction; //used to store the direction of filling
struct node{
int value; //store the digital value
int used; //is this field captured?
};
int startvalue; //value used to start filling
int endvalue; //value when the filling termed
int oldhvalue;
int oldvvalue;
int newhvalue;
int newvvalue;
struct node array[MSIZE][MSIZE];
void clearmatrix()
{
int v,h;
for( v=0; v<MSIZE; v++ )
for( h=0; h<MSIZE; h++ ){
array[v][h].value = 0;
array[v][h].used = 0;
}
}
void showmatrix()
{
int v,h;
for( v=0; v<MSIZE; v++ ){
for( h=0; h<MSIZE; h++ ){
printf( "%d ", array[v][h].value );
}
printf( "\n" );
}
}
int main()
{
int tempvalue;
startup:
clrscr();
clearmatrix();
// showmatrix();
printf( "Please Input the direction(0-UR,1-DR,2-UL,3-DL):" );
scanf( "%d", &direction );
if ( direction > 3 ) return 0;
printf( "Input start horizen position:" );
scanf( "%d", &oldhvalue );
printf( "Input start vertical position:" );
scanf( "%d", &oldvvalue );
//define the start value and end value
startvalue = 1;
endvalue = MSIZE*MSIZE;
//define the position of the startvalue first-of-all!
// oldhvalue = 0;
// oldvvalue = 0;
array[oldvvalue][oldhvalue].value = startvalue;
array[oldvvalue][oldhvalue].used = 1;
//filling starts
for( tempvalue = startvalue+1; tempvalue<=endvalue; tempvalue++){
// printf( "temp value is:%d.\n", tempvalue );
switch( direction ){
case UPRIGHT: newhvalue = oldhvalue+1;
newvvalue = oldvvalue-1;
if(newhvalue>=MSIZE) newhvalue = 0;
if(newvvalue<0) newvvalue = MSIZE-1;
break;
case DOWNRIGHT: newhvalue = oldhvalue+1;
newvvalue = oldvvalue+1;
if(newhvalue>=MSIZE) newhvalue = 0;
if(newvvalue>=MSIZE) newvvalue = 0;
break;
case UPLEFT: newhvalue = oldhvalue-1;
newvvalue = oldvvalue-1;
if(newhvalue<0) newhvalue = MSIZE-1;
if(newvvalue<0) newvvalue = MSIZE-1;
case DOWNLEFT: newhvalue = oldhvalue-1;
newvvalue = oldvvalue+1;
if(newhvalue<0) newhvalue = MSIZE-1;
if(newvvalue>=MSIZE) newvvalue = 0;
}
// printf( "newhvalue:%d, newvvalue:%d.\n", newhvalue, newvvalue );
if ( array[newvvalue][newhvalue].used == 1 ){
// printf( "used field detected!\n" );
newhvalue = oldhvalue;
if ( (newvvalue = oldvvalue+1) >=MSIZE )
newvvalue = 0;
if ( array[newvvalue][newhvalue].used ){
printf( "Error Detected!\n" );
return -1;
} else {
array[newvvalue][newhvalue].value = tempvalue;
array[newvvalue][newhvalue].used = 1;
}
} else {
array[newvvalue][newhvalue].value = tempvalue;
array[newvvalue][newhvalue].used = 1;
} // if
oldvvalue = newvvalue;
oldhvalue = newhvalue;
}//for
showmatrix();
getch();
goto startup;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -