目录
- 4*3
- 3*4
4*3
要实现上下左右OK五个按键来操作一个4x3矩阵,您可以按照类似的思路进行编程。以下是一个示例代码:
#include <stdio.h>#define ROWS 4
#define COLS 3int matrix[ROWS][COLS] = {{1, 2, 3},{4, 5, 6},{7, 8, 9},{10, 11, 12}
};void printMatrix() {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {printf("%-2d ", matrix[i][j]);}printf("\n");}printf("\n");
}void moveUp() {int temp[COLS];for (int j = 0; j < COLS; j++) {temp[j] = matrix[0][j];}for (int i = 0; i < ROWS - 1; i++) {for (int j = 0; j < COLS; j++) {matrix[i][j] = matrix[i + 1][j];}}for (int j = 0; j < COLS; j++) {matrix[ROWS - 1][j] = temp[j];}
}void moveDown() {int temp[COLS];for (int j = 0; j < COLS; j++) {temp[j] = matrix[ROWS - 1][j];}for (int i = ROWS - 1; i > 0; i--) {for (int j = 0; j < COLS; j++) {matrix[i][j] = matrix[i - 1][j];}}for (int j = 0; j < COLS; j++) {matrix[0][j] = temp[j];}
}void moveLeft() {int temp[ROWS];for (int i = 0; i < ROWS; i++) {temp[i] = matrix[i][0];}for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS - 1; j++) {matrix[i][j] = matrix[i][j + 1];}}for (int i = 0; i < ROWS; i++) {matrix[i][COLS - 1] = temp[i];}
}void moveRight() {int temp[ROWS];for (int i = 0; i < ROWS; i++) {temp[i] = matrix[i][COLS - 1];}for (int i = 0; i < ROWS; i++) {for (int j = COLS - 1; j > 0; j--) {matrix[i][j] = matrix[i][j - 1];}}for (int i = 0; i < ROWS; i++) {matrix[i][0] = temp[i];}
}void pressOK() {int sum = 0;for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {sum += matrix[i][j];}}printf("Sum: %d\n", sum);
}int main() {int choice;while (1) {printMatrix();printf("Press 1: Up, 2: Down, 3: Left, 4: Right, 5: OK\n");printf("Enter your choice: ");scanf("%d", &choice);switch (choice) {case 1:moveUp();break;case 2:moveDown();break;case 3:moveLeft();break;case 4:moveRight();break;case 5:pressOK();break;default:printf("Invalid choice!\n");}printf("\n");}return 0;
}
这个示例与之前的示例非常相似,只是调整了矩阵的行和列的顺序,以适应4x3的矩阵。移动操作的实现也有一些微调。
3*4
要实现上下左右OK五个按键来操作一个3x4矩阵,您可以使用一个二维数组来表示该矩阵,并利用按键输入来进行矩阵操作。以下是一个示例代码:
#include <stdio.h>#define ROWS 3
#define COLS 4int matrix[ROWS][COLS] = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}
};void printMatrix() {for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {printf("%-2d ", matrix[i][j]);}printf("\n");}printf("\n");
}void moveUp() {int temp = matrix[0][0];for (int i = 0; i < ROWS - 1; i++) {for (int j = 0; j < COLS; j++) {matrix[i][j] = matrix[i + 1][j];}}for (int j = 0; j < COLS; j++) {matrix[ROWS - 1][j] = temp;}
}void moveDown() {int temp = matrix[ROWS - 1][0];for (int i = ROWS - 1; i > 0; i--) {for (int j = 0; j < COLS; j++) {matrix[i][j] = matrix[i - 1][j];}}for (int j = 0; j < COLS; j++) {matrix[0][j] = temp;}
}void moveLeft() {int temp = matrix[0][0];for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS - 1; j++) {matrix[i][j] = matrix[i][j + 1];}}for (int i = 0; i < ROWS; i++) {matrix[i][COLS - 1] = temp;}
}void moveRight() {int temp = matrix[0][COLS - 1];for (int i = 0; i < ROWS; i++) {for (int j = COLS - 1; j > 0; j--) {matrix[i][j] = matrix[i][j - 1];}}for (int i = 0; i < ROWS; i++) {matrix[i][0] = temp;}
}void pressOK() {int sum = 0;for (int i = 0; i < ROWS; i++) {for (int j = 0; j < COLS; j++) {sum += matrix[i][j];}}printf("Sum: %d\n", sum);
}int main() {int choice;while (1) {printMatrix();printf("Press 1: Up, 2: Down, 3: Left, 4: Right, 5: OK\n");printf("Enter your choice: ");scanf("%d", &choice);switch (choice) {case 1:moveUp();break;case 2:moveDown();break;case 3:moveLeft();break;case 4:moveRight();break;case 5:pressOK();break;default:printf("Invalid choice!\n");}printf("\n");}return 0;
}
在这个示例中,我们定义了一个3x4矩阵matrix
,以及相应的按键操作函数。printMatrix
函数用于打印矩阵。moveUp
、moveDown
、moveLeft
、moveRight
函数分别实现了上、下、左、右按键的移动操作,将矩阵按指定方向进行移动。pressOK
函数用于计算矩阵中所有元素的和。
在main
函数中,我们通过一个循环来接受用户的按键输入,并根据选择调用相应的按键操作函数或打印矩阵的和。