c - 为什么 c 中的 scanf() 函数中的多个参数会使应用程序崩溃?

标签 c arrays crash scanf

如果是下面的scanf()的语法我真的看不懂

这个程序的目的是读取数学输入,例如: 6x+7y+8z=2(x3 次)
然后输出一个包含输入数值的数组。

成功输入后应用程序崩溃
尽管编译器没有显示任何错误或警告

#include <stdio.h>
int main()  
{
    int number[3][4],i,j;
    char vch[3][4];

for(i=0;i<3;i++){
scanf("%d%c%d%c%d%c%c%d",&number[i][0],&vch[i][0],&number[i][1],&vch[i][1],&number[i][2],&vch[i][2],&vch[i][3],&number[i][3]);
printf("\n");
}
for(i=0;i<3;i++)
    for(j=0;j<4;j++)
        printf("%d",number[i][j]);
return 0;
}

最佳答案

要阅读您显示的等式,您确实需要阅读第一组数字之间的两个字符(而不是一个)。所以你会想做

#include <stdio.h>
int main()
{
    int number[3][4],i,j;
    char vch[3][6];

for(i=0;i<3;i++){
printf("enter equation %d:\n", i);
scanf("%d %c %c %d %c %c %d %c %c %d",&number[i][0], &vch[i][0], &vch[i][1], \
                                      &number[i][1], &vch[i][2], &vch[i][3], \
                                      &number[i][2], &vch[i][4], &vch[i][5], \
                                      &number[i][3]);
}
for(i=0;i<3;i++) {
    for(j=0;j<4;j++)
        printf("%4d ",number[i][j]);
    printf("\n");
}
return 0;
}

输出:

enter equation 0:
2x+3y+4z=0
enter equation 1:
2x+5y+10z=-15
enter equation 2:
5x+7y+9z=3
   2    3    4    0 
   2    5   10  -15 
   5    7    9    3 

编辑 一个不使用 scanf 并处理各种其他输入的改进程序可能如下所示:

#include <stdio.h>
#include <string.h>

int interpret(char* s) {
  // interpret the string as a number
  // if there is only a sign, return +1 or -1 as appropriate

  int it;
  if(sscanf(s, "%d", &it) == 1) return it;
  // look for just a sign with no number
  if( strstr(s, "-") > 0) return -1;
  return 1;
}

void squeezeWhite(char* s) {
  // remove all spaces
  char *t = s;
  int ii = 0;
  while(*t !='\0') {
    if (*t != ' ') s[ii++] = *t;
    t++;
  }
  s[ii] = '\0';
}

void tokenize(char *buf, int *arr) {
  char *temp;
  int it;
  squeezeWhite(buf);
  temp = strtok(buf, "xyz");
  // handle the case of nothing in front of x:
  if(temp == buf + 1) {
    arr[0] = 1;
    arr[1] = interpret(temp);
  }
  else {
    arr[0] = interpret(temp);
    temp = strtok(NULL, "xyz");
    arr[1] = interpret(temp);
  }
  temp = strtok(NULL, "xyz");
  arr[2] = interpret(temp);
  temp = strtok(NULL, "=");
  arr[3] = interpret(temp);
}

int main()
{
    int number[3][4],i,j;
    char vch[3][6];
    char buffer[100];

  for(i=0;i<3;i++){
    printf("enter equation %d:\n", i);
    fgets(buffer, 100, stdin);
    tokenize(buffer, number[i]);
  }
  for(i=0;i<3;i++) {
    for(j=0;j<4;j++)
      printf("%4d ",number[i][j]);
    printf("\n");
  }
  return 0;
}

输入/输出示例:

enter equation 0:
-x+y+z=1
enter equation 1:
2x + 3 y + 4 z = 7
enter equation 2:
+2x+2y+2z=+2
  -1    1    1    1 
   2    3    4    7 
   2    2    2    2 

如您所见,它可以优雅地处理系数,即使没有数字(只有 + 或 - 符号)也是如此。当您的用户可能不符合您的输入规范时,我在这里展示的基本想法 - 分步处理输入 - 是一个好主意。它比阅读 scanf 强得多,后者将落在第一关。它确实涉及编写一些“辅助函数”。这通常是它的工作方式......

关于c - 为什么 c 中的 scanf() 函数中的多个参数会使应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829115/

相关文章:

c - 在 C 中使用递归缩进行

c - 解析json数组对象

arrays - 从Scala中的数组中过滤出空数组

android - 动画期间在设备上 react native android崩溃

java - 将数据添加到ListView后崩溃

c++ - 无法理解线路的工作原理

sql - 您可以使用 SQL 从 JSON 数组中选择值吗?

C. 函数修改动态分配的二维数组时出现段错误

ios - NSURLConnection sendSynchronousRequest 在慢速网络上崩溃

C 函数无论如何都返回零