c - 中止陷阱 : 6 error while running this program in C

标签 c

我正在编写这段代码,它从命令行上的文件中进行计算,然后进行计算。我已经能够将文件的每个部分放入字符串数组中,但是当我尝试将该数组运行到计算它的方法中时,我在运行时收到 abort trap: 6 错误。我正在使用 emacs 和终端中的 cc 编写 c 语言进行编译。

当它给出错误时,我在 main 中运行它,并初始化了这些变量:

  char items[10][10];
  int sig;
  int numOfItems = n-1;
  double result[3];
  sig=calculate(items, numOfItems, result);

方法如下,我知道它还没有完全工作,但似乎甚至没有进入该方法,因为它没有打印出我放置的测试打印语句:

int calculate(char items[10][10], int numOfItems, double *res){
  printf("test2");
  int flag, i, c, j, numdigits, decindicate, operatorindicate,n,m;
  double ans;
  double numbers[30];
  char operators[30];

  for(n=0; n<30; n++){
    numbers[n]=0;
  }

  flag=1;
  i=0;
  numdigits=0;
  decindicate=0;
  operatorindicate=0;
  n=0;
  m=0;
  j=0;

  while(n<numOfItems){
    c=items[n][0];
    if(('0'<=c) && (c<='9')){
      m=0;
      while(c!='\0'){
    c=items[n][m];
    if(('0'<=c) && (c<='9')){
      numbers[i] = numbers[i]*10+(c-'0');
    }else if(c=='.'){
      decindicate=1;
      m++;
      break;
    }else{
      flag=0;
      break;
    }while(decindicate>0){
      c=items[n][m];
      if(('0'<=c) && (c<='9')){
        numbers[i]=numbers[i]+((c-'0')/(10^decindicate));
        decindicate++;
      }else{
        flag=0;
        break;
      }m++;
    }
    i++;
      }
    }else if(c=='+' || c=='-' || c=='*' || c=='/'){
      operators[j]=c;
      j++;
    }else{
      flag=0;
      break;
    }n++;
  }

  for(i=0; i<numdigits; i++){
    printf("%lf \n",numbers[i]);
  }for(i=0; i<m; i++){
    printf("char %c \n", operators[i]);
  }

  ans=numbers[0];

  for(i=0; i<n-1; i++){
    if(operators[i]=='+'){
      ans=ans+numbers[i+1];
    }else if(operators[i]=='-'){
      ans=ans-numbers[i+1];
    }else if(operators[i]=='*'){
      ans=ans*numbers[i+1];
    }else if(operators[i]=='/'){
      ans=ans/numbers[i+1];
    }
  }
 *res=ans;
  return flag;
}

这里是完整的代码,如果它超出了我什至没有意识到的范围:

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

int calculate(char items[10][10], int numOfItems, double *res){
  int flag, i, c, j, numdigits, decindicate, operatorindicate,n,m;
  double ans;
  double numbers[30];
  char operators[30];

  for(n=0; n<30; n++){
    numbers[n]=0;
  }

  flag=1;
  i=0;
  numdigits=0;
  decindicate=0;
  operatorindicate=0;
  n=0;
  m=0;
  j=0;



  while(n<numOfItems){
    c=items[n][0];
    if(('0'<=c) && (c<='9')){
      m=0;
      while(c!='\0'){
    c=items[n][m];
    if(('0'<=c) && (c<='9')){
      numbers[i] = numbers[i]*10+(c-'0');
    }else if(c=='.'){
      decindicate=1;
      m++;
      break;
    }else{
      flag=0;
      break;
    }while(decindicate>0){
      c=items[n][m];
      if(('0'<=c) && (c<='9')){
        numbers[i]=numbers[i]+((c-'0')/(10^decindicate));
        decindicate++;
      }else{
        flag=0;
        break;
      }m++;
    }
    i++;
      }
    }else if(c=='+' || c=='-' || c=='*' || c=='/'){
      operators[j]=c;
      j++;
    }else{
      flag=0;
      break;
    }n++;
  }

  for(i=0; i<numdigits; i++){
    printf("%lf \n",numbers[i]);
  }for(i=0; i<m; i++){
    printf("char %c \n", operators[i]);
  }

  ans=numbers[0];

  for(i=0; i<n-1; i++){
    if(operators[i]=='+'){
      ans=ans+numbers[i+1];
    }else if(operators[i]=='-'){
      ans=ans-numbers[i+1];
    }else if(operators[i]=='*'){
      ans=ans*numbers[i+1];
    }else if(operators[i]=='/'){
      ans=ans/numbers[i+1];
    }
  }


  *res=ans;
  return flag;
}



int main(int argc, char **argv){
  int n, m, i;
  char digits[50]; //used to store digits before adding necessary spaces
  char items[10][10]; //will store string array of items in equation
  FILE *fp, *fp2;
  int sig; //will indicate if there is an invalid character
  double result[3] = {0}; //will return result of equation



  fp = fopen(argv[1], "r");
  if(fp==NULL){
    printf("Please provide file");
  }

  fp2 = fopen("temp", "w+"); //will read to file temp

  n=0;
  while(0==0){
    digits[n]=fgetc(fp);
    if(digits[n]==EOF){
      digits[n]='\0';
      break;
    }
    n++;
  }

  n=0;
  char temp1;
  char temp2;
  while(digits[n]!='\0'){
    if((('0'<=digits[n]) && (digits[n]<='9') && (digits[n+1]=='+' || digits[n+1]=='-' || digits[n+1]=='*' || digits[n+1]=='/')) || ((digits[n]=='+' || digits[n]=='-' || digits[n]=='*' || digits[n]=='/') && (('0'<=digits[n+1]) && (digits[n+1]<='9')))){
      temp1=digits[n+1];
      digits[n+1]=' ';
      m=n+2;
      while(digits[m-1]!='\0'){
    temp2=temp1;
    temp1=digits[m];
    digits[m]=temp2;
    m++;
      }
    }
    fputc(digits[n], fp2);
    n++;
  }

  //test if digit array fills correctly
  n=0;
  while(digits[n]!='\0'){
    printf("testings digits array: %c \n", digits[n]);
    n++;
  }

  //scans the temp file to form string array
  rewind(fp2);
  i=1;
  n=0;
  while(i==1){
    i=fscanf(fp2, "%s", items[n]);
    n++;
  }

  int numOfItems = n-1;

  //test if char array items fills correctly
  n=0;
  while(n<numOfItems){
    printf("testing items array: %s \n", items[n]);
    n++;
  }


  sig=calculate(items, numOfItems, result);


  if (sig==0){
    printf("This is not a valid operation. \n");
  }else {
    printf("The calculation equals %lf \n", result[0]);
  }

   remove("temp");



}

我用来测试的文件是 sc1,其中包含以下内容: 34 + 96 - 10/2

这是整个程序在使用 sc1 文件时打印出来的内容:

testings digits array: 3 
testings digits array: 4 
testings digits array:   
testings digits array: + 
testings digits array:   
testings digits array: 9 
testings digits array: 6 
testings digits array:   
testings digits array: - 
testings digits array:   
testings digits array: 1 
testings digits array: 0 
testings digits array:   
testings digits array: / 
testings digits array:   
testings digits array: 2 
testing items array: 34 
testing items array: + 
testing items array: 96 
testing items array: - 
testing items array: 10 
testing items array: / 
testing items array: 2 
Abort trap: 6

我感到很失落,如果有人能帮助那就太好了。

最佳答案

计算中,while(c!='\0')是一个无限循环。 c=items[n][m]; 从未获得 c 的新值,因为除非检测到点,否则 m 不会递增。 i 递增,因此每次迭代都会访问 numbers[i],并且当 i 超出 numbers[30] 的边界时它失败了。
numbers[i]=numbers[i]+((c-'0')/(10^decindicate)); 是另一个问题,因为 ^ 是位异或。这不是您想要的 10 次方。

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

int calculate(char items[10][10], int numOfItems, double *res){
    int flag = 1, i = 0, c, ops = 0, decindicate = 0, nums = 0,item = 0,each = 0;
    double ans;
    double numbers[30] = { 0.0};
    char operators[30];

    printf ( "calculating\n");
    while ( item < numOfItems) {
        c = items[item][0];
        if ( ( '0' <= c && c <= '9') || c == '.') {
            each = 0;
            decindicate = 0;
            while ( '\0' != ( c = items[item][each])) {
                if ( ( '0' <= c) && ( c <= '9')) {
                    numbers[nums] = numbers[nums] * 10 + ( c - '0');
                    if ( decindicate) {
                        decindicate++;
                    }
                }
                else if ( c == '.') {//found a dot
                    decindicate = 1;//set to 1
                    each++;
                    continue;
                } else {
                    flag = 0;
                    break;
                }
                each++;//advance each character
            }
            while ( decindicate > 1) {
                decindicate--;
                numbers[nums] /= 10;//divide by power of 10 digits after dot
            }
            nums++;//advance nums
            if ( nums >= 30) {
                printf ( "too many numbers\n");
                return 0;
            }
        } else if ( c == '+' || c == '-' || c == '*' || c == '/') {
            operators[ops] = c;
            ops++;//sdvance ops
            if ( ops >= 30) {
                printf ( "too many operators\n");
                return 0;
            }
        } else {
            flag = 0;
            break;
        }
        item++;//advance item
    }

    for ( i = 0; i < nums; i++) {
        printf ( "numbers %f \n", numbers[i]);
    }
    for ( i = 0; i < ops; i++) {
        printf ( "operators %c \n", operators[i]);
    }

    ans = numbers[0];

    for ( i = 0; i < item - 1; i++) {
        switch ( operators[i]) {
            case '+':
                ans = ans + numbers[i + 1];
                break;
            case '-':
                ans = ans - numbers[i + 1];
                break;
            case '*':
                ans = ans * numbers[i + 1];
                break;
            case '/':
                ans = ans / numbers[i + 1];
                break;
        }
    }

    *res = ans;
    return flag;
}

int main ( int argc, char **argv) {
    int n = 0, i, digit = 0;
    char digits[50] = ""; //used to store digits before adding necessary spaces
    char items[10][10] = { { ""}}; //will store string array of items in equation
    FILE *fp = NULL, *fp2 = NULL;
    int sig = 0; //will indicate if there is an invalid character
    int space = 1;//skip leading whitespace
    int operator = 1;//must have a number first
    int number = 0;
    int dot = 0;
    double result = 0.0f; //will return result of equation

    if ( NULL == ( fp = fopen ( argv[1], "r"))) {
        printf ( "Please provide file\n");
        return 0;
    }

    if ( NULL == ( fp2 = fopen ( "temp", "w+"))) { //will read to file temp
        printf ( "could not open temp file\n");
        fclose ( fp);
        return 0;
    }

    n = 0;
    while ( EOF != ( digit = fgetc ( fp))) {
        digits[n] = digit;
        n++;
        if ( n >= 49) {
            break;
        }
    }
    digits[n] = '\0';

    fclose ( fp);

    n = 0;

    while ( digits[n] != '\0') {
        switch ( digits[n]) {
            case ' ':
            case '\t':
            case '\n':
                if ( space) {
                    break;
                }
                space = 1;
                //do not reset so as to detect consecutive numbers or operators
                //number = 0;
                //operator = 0;
                dot = 0;
                fputc ( digits[n], fp2);
                break;
            case '.':
                if ( dot) {
                    break;
                }
                if ( space && number) {
                    printf ( "bad format. expected operation\n");
                    return 0;
                }
                if ( !number) {
                    fputc ( ' ', fp2);
                }
                space = 0;
                number = 1;
                operator = 0;
                dot = 1;
                fputc ( digits[n], fp2);
                break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                if ( space && number) {
                    printf ( "bad format. expected operation\n");
                    return 0;
                }
                if ( !number) {
                    fputc ( ' ', fp2);
                }
                space = 0;
                number = 1;
                operator = 0;
                dot = 0;
                fputc ( digits[n], fp2);
                break;
            case '+':
            case '-':
            case '*':
            case '/':
                if ( space && operator) {
                    printf ( "bad format. expected number\n");
                    return 0;
                }
                if ( !operator) {
                    fputc ( ' ', fp2);
                }
                number = 0;
                operator = 1;
                dot = 0;
                fputc ( digits[n], fp2);
                space = 1;
                fputc ( ' ', fp2);
                break;
        }
        n++;
    }

    //scans the temp file to form string array
    rewind(fp2);
    i = 1;
    n = 0;
    while ( i == 1) {
        i = fscanf ( fp2, "%s", items[n]);
        n++;
        if ( n >= 10) {
            break;
        }
    }

    fclose ( fp2);

    int numOfItems = n-1;

    //test if char array items fills correctly
    n = 0;
    while ( n < numOfItems) {
        printf ( "testing items array: %s \n", items[n]);
        n++;
    }

    sig = calculate ( items, numOfItems, &result);

    if ( sig == 0) {
        printf ( "This is not a valid operation. \n");
    } else {
        printf ( "The calculation equals %lf \n", result);
    }

    remove ( "temp");
    return 0;
}

关于c - 中止陷阱 : 6 error while running this program in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46395953/

相关文章:

c - 如何从.txt文件中读取整数并将它们存储在C中的整数类型数组中

c - 在 C 中声明 int * p = & i; 是错误的吗?

c - 没有库函数的 C 中的反向字符串函数

c - 在头文件中包含头文件不会使其包含在实现文件中——或者我只是使用了错误的命令进行编译?

c - 如何缓冲非阻塞 IO?

javascript - 使用node-api将数组缓冲区从C转发到JS

c - 关于c中的前向声明

c - Keccak 中的舍入常量

将正斜杠转换为反斜杠

c - 在带有 while 循环的 C 中使用递归函数的阶乘程序 c