|
30鱼币
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
int main()
{
printf("============================\n");
printf("* 欢迎使用此程序,指令如下 *\n");
printf("* 1.生成一个 M*N 的矩阵 *\n");
printf("* 2.初始化矩阵 *\n");
printf("* 3.给矩阵中某个元素赋值 *\n");
printf("* 4.读取矩阵中的某个元素 *\n");
printf("* 5.打印整个矩阵 *\n");
printf("* 6.结束程序 *\n");
printf("============================\n");
int order = 0, indicator = 0, data;
int M, N, m, n, i, j;
//M,N用来表示矩阵规模
//m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
//data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
//i,j用来作为输出二维数组的标志
char judge;
while(order != 6)
{
printf("请输入指令:");
scanf("%d", &order);
switch(order)
{
case 1:
if(indicator)
{
printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
scanf("%c", &judge);
if(judge == 'N')
{
continue;
}
}
printf("请输入新矩阵的规模(M*N)-> ");
scanf("%d*%d", &M, &N);
int (*pstr)[N];
pstr = (int (*)[N])calloc(M * N, sizeof(int));
if(pstr == NULL)
{
printf("内存空间不足!\n");
exit(1);
}
printf("%d*%d的新矩阵创建成功!\n", M, N);
indicator = 1;
break;
case 2:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入一个数字:");
scanf("%d", &data);
memset(pstr, data, M * N * sizeof(int));
break;
case 3:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要修改的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("请输入一个数字:");
scanf("%d", &data);
pstr[m - 1][n - 1] = data;
break;
case 4:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
printf("请输入要读取的位置(行/列)-> ");
scanf("%d,%d", &m, &n);
printf("第%d行,第%d列的数字是:%d\n", m, n, pstr[m - 1][n - 1]);
break;
case 5:
if(!indicator)
{
printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
continue;
}
for(i = 0; i < M; i++)
{
for(j = 0; j < N; j++)
{
printf("%4d", pstr[i][j]);
}
printf("\n");
}
break;
case 6:
printf("感谢使用本程序^_^\n");
break;
default:
printf("当前输入的指令无效,请重新输入!\n");
continue;
}
}
free(pstr);
return 0;
}
为什么编译的时候会如下报错?
||=== 构建: Debug 在 test2 中 (编译器: GNU GCC Compiler) ===|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c||In function 'main':|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|56|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|56|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|68|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|68|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|83|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|83|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|94|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|94|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|110|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|110|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|113|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: 'pstr' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|113|error: switch jumps into scope of identifier with variably modified type|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|31|note: switch starts here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|46|note: '({anonymous})' declared here|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|119|error: 'pstr' undeclared (first use in this function)|
F:\study\13.the study of c language\the homework of c with fishc\test2\main.c|119|note: each undeclared identifier is reported only once for each function it appears in|
||=== 构建 失败: 13 error(s), 0 warning(s) (0 分, 0 秒) ===|
自己看吧。
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <string.h>
- int main()
- {
- printf("============================\n");
- printf("* 欢迎使用此程序,指令如下 *\n");
- printf("* 1.生成一个 M*N 的矩阵 *\n");
- printf("* 2.初始化矩阵 *\n");
- printf("* 3.给矩阵中某个元素赋值 *\n");
- printf("* 4.读取矩阵中的某个元素 *\n");
- printf("* 5.打印整个矩阵 *\n");
- printf("* 6.结束程序 *\n");
- printf("============================\n");
- int order = 0, indicator = 0, data;
- int M, N, m, n, i, j;
- //M,N用来表示矩阵规模
- //m,n表示矩阵具体位置,由于读取和修改互不影响,所以共用同一组变量
- //data表示指令中更改矩阵元素的数值,由于不会影响其他指令,所以共用同一个变量data
- //i,j用来作为输出二维数组的标志
- char judge;
- int (*pstr)[N];
- pstr = (int (*)[N])calloc(M * N, sizeof(int));
- while(order != 6)
- {
- printf("请输入指令:");
- scanf("%d", &order);
- switch(order)
- {
- case 1:
- {
- if(indicator)
- {
- printf("矩阵已存在,是否需要重新创建?(Y/N)-> ");
- scanf("%c", &judge);
- if(judge == 'N')
- {
- continue;
- }
- }
- printf("请输入新矩阵的规模(M*N)-> ");
- scanf("%d*%d", &M, &N);
- if(pstr == NULL)
- {
- printf("内存空间不足!\n");
- exit(1);
- }
- printf("%d*%d的新矩阵创建成功!\n", M, N);
- indicator = 1;
- break;
- }
- case 2:
- {
- if(!indicator)
- {
- printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
- continue;
- }
- printf("请输入一个数字:");
- scanf("%d", &data);
- memset(pstr, data, M * N * sizeof(int));
- break;
- }
- case 3:
- {
- if(!indicator)
- {
- printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
- continue;
- }
- printf("请输入要修改的位置(行/列)-> ");
- scanf("%d,%d", &m, &n);
- printf("请输入一个数字:");
- scanf("%d", &data);
- pstr[m - 1][n - 1] = data;
- break;
- }
- case 4:
- {
- if(!indicator)
- {
- printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
- continue;
- }
- printf("请输入要读取的位置(行/列)-> ");
- scanf("%d,%d", &m, &n);
- printf("第%d行,第%d列的数字是:%d\n", m, n, pstr[m - 1][n - 1]);
- break;
- }
- case 5:
- {
- if(!indicator)
- {
- printf("您尚未生成一个M*N的矩阵,请重新输入指令!\n");
- continue;
- }
- for(i = 0; i < M; i++)
- {
- for(j = 0; j < N; j++)
- {
- printf("%4d", pstr[i][j]);
- }
- printf("\n");
- }
- break;
- }
- case 6:
- {
- printf("感谢使用本程序^_^\n");
- break;
- }
- default:
- {
- printf("当前输入的指令无效,请重新输入!\n");
- continue;
- }
- }
- }
- free(pstr);
- return 0;
- }
复制代码
|
|