MrSwordsman MrSwordsman
Time
关注数: 21 粉丝数: 51 发帖数: 5,788 关注贴吧数: 11
【海龟图】不知道哪儿错了。。编出来怪怪的。。。。求指导。。。 不多说了,直接放程序: #include<stdio.h> void getCommands(int commands[][2]) { int i,n; printf( "Enter command ( 9 to end input ): " ); scanf("%d",&n); for(i=0;n!=9&&i<100;i++) { commands[i][0]=n; if(n==5) { scanf(",%d",&commands[i][1]); } printf("\nEnter command ( 9 to end input ): " ); scanf("%d",&n); i++; } commands[i][0]=9; } int turnRight( int d ) { d ++; if (d = 4) d = 0; return d; } int turnLeft( int d ) { d --; if (d = -1) d = 3; return d; } void movePen( int down, int a[][ 50 ], int dir, int dist) { int i; static int x, y = 0; for (i = 0; i < dist;i ++) { switch(dir) { case 0: y = y + dist - 1; if (down = 1) a[x][y - i] = 1; break; case 1: x = x + dist - 1; if (down = 1) a[x - i][y] = 1; break; case 2: y = y - dist + 1; if (down = 1) a[x][y + i] = 1; break; case 3: x = x - dist + 1; if (down = 1) a[x + i][y] = 1; break; } } } void printArray( int a[][ 50 ] ) { int i,j; for (i = 0;i < 50;i ++) { for (j = 0;j < 50;j ++) { if (a[i][j] = 1) printf("*"); else printf(" "); if (j = 49) printf("\n"); } } } int main() { int command,dist; int commands[100][2]; int floor[50][50]={0}; int k=0; int pendown; int direction = 0; printf("****************************************\n"); printf("* 1:penUp 2:penDown *\n"); printf("* 3:turnRight 4:turnLeft *\n"); printf("* 5:move 6:print *\n"); printf("* 9:end *\n"); printf("****************************************\n"); getCommands(commands); command=commands[0][0]; while(command!=9) { switch(command) { case 1: pendown=0; break; case 2: pendown=1; break; case 3: direction=turnRight(direction); break; case 4: direction=turnLeft(direction); break; case 5: dist = commands[k][1]; movePen(pendown, floor,direction,dist ); break; case 6: printArray( floor); break; } k++; command=commands[k][0]; } return 0; }
1 下一页