|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 yexing 于 2020-4-26 15:53 编辑
原题:
我的代码:(还未编写完成)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void ass_element(int *pta, int row, int col, int num);
- int get_element(int * pta, int row, int col);
- void print_array(int * pta, int row, int col);
- void ass_element(int * pta, int row, int col, int num)
- {
- //待开发
- }
- int get_element(int * pta, int row, int col)
- {
- //待开发
- }
- void print_array(int * pta, int row, int col)
- {
- int i, j;
- for (i = 0; i < row; i++)
- {
- for (j = 0; j < col; j++)
- {
- printf("%d ", pta[i*j + j]);
- }
- putchar('\n');
- }
- putchar('\n');
- }
- int main(void)
- {
- int command, row, col, temp_num;
- int row_array, col_array;
- int * pta = NULL;
- char confirm, c;
-
- printf("\n\
- ============================\n\
- * 欢迎使用该程序,指令如下 *\n\
- * 1.生成一个 M*N 的矩阵 *\n\
- * 2.初始化矩阵 *\n\
- * 3.给矩阵中某个元素赋值 *\n\
- * 4.读取矩阵中某个元素 *\n\
- * 5.打印整个矩阵 *\n\
- * 6.结束程序 *\n\
- ============================\n");
- while (1)
- {
- printf("\n请输入指令:");
- scanf("%d", &command);
- while (command < 1 || command > 6)
- {
- printf("指令输入错误,请重新输入:");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d", &command);
- }
-
- if (command == 6)
- break;
-
- if (command == 1)
- {
- if (pta == NULL)
- {
- printf("请输入新矩阵的规模:");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d*%d", &row, &col);
- pta = (int *)malloc(row * col*sizeof(int));
- memset(pta, 0, row*col*sizeof(int));
- row_array = row;
- col_array = col;
- printf("%d*%d 的矩形创建成功!\n", row, col);
- }
- else
- {
- printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%c", &confirm);
- if (confirm == 'Y')
- {
- printf("请输入新矩阵的规模:");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d*%d", &row, &col);
- free(pta);
- pta = (int *)malloc(row * col * sizeof(int));
- row_array = row;
- col_array = col;
- memset(pta, 0, row_array * col_array * sizeof(int));
- printf("%d*%d 的矩形创建成功!\n", row_array, col_array);
- }
- else
- {
- printf("创建失败。\n");
- }
- }
- }
-
- else if (command == 2)
- {
- if (pta == NULL) {
- printf("矩形未创建!初始化失败。\n");
- }else{
- printf("请输入一个数字:");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d", &temp_num);
- memset(pta, temp_num, row_array * col_array * sizeof(int));
- putchar('\n');
- }
- }
-
- else if (command == 3)
- {
- if (pta == NULL) {
- printf("矩形未创建!无法赋值。\n");
- }else{
- printf("请输入要修改的位置(行,列)-> ");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d,%d", &row, &col);
- printf("请输入一个数字:");
- scanf("%d", &temp_num);
- ass_element(pta, row, col, temp_num);
- }
- }
-
- else if (command == 4)
- {
- if (pta == NULL) {
- printf("矩阵未创建!无法读取元素。\n");
- }else{
- printf("请输入要读取的位置(行,列)-> ");
- while ((c = getchar()) != EOF && c != '\n')
- ;
- scanf("%d,%d", &row, &col);
- printf("第%d行,第%d列的数字是:%d\n", row, col, get_element(pta, row, col));
- }
- }
-
- else if (command == 5)
- {
- if (pta == NULL) {
- printf("矩形未创建!无法打印。\n");
- }else{
- print_array(pta, row_array, col_array);
- }
- }
- }
-
- printf("\n感谢使用本程序^_^\n");
- free(pta);
-
- return 0;
- }
复制代码
运行结果:
我的问题:
1. 为什么使用指令1后再次使用,会直接创建失败?求指明问题并修改(已解决)
2. 为什么使用指令2会初始化一堆随机数值?求指明问题并修改
你在第84行之前需要清空缓冲区,否则,scanf 会将缓冲区的'\n'当做%c赋值给confirm,这样就出错了。
有的编译器可以用以下函数,但是有的编译器不认识这个函数
fflush(stdin)
为了适应更多的编译器,建议使用以下代码清空缓冲区
- while ((char c = getchar()) != EOF && c != '\n');
复制代码
|
|