中止陷阱:在C

时间:2017-09-24 23:56:33

标签: c

我正在编写这段代码,它从命令行上的文件中进行计算,然后计算它。我已经达到了能够将文件的每个部分都变成字符串数组的程度,但是当我尝试将该数组运行到一个方法来计算它时,我得到一个中止陷阱:运行时出错6个错误。我正在写c使用emacs和终端中的cc进行编译。

我在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

我感到很失落,如果有人可以提供帮助那就太棒了。

1 个答案:

答案 0 :(得分:0)

<{1>} calculate while(c!='\0')是一个无限循环。 c=items[n][m]; c永远不会为m获得新值,因为i除非检测到点,否则不会增加numbers[i]i已递增,因此每次迭代都会被numbers[30]访问,当numbers[i]=numbers[i]+((c-'0')/(10^decindicate));超出^的边界时,它会失败。
#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; } 是另一个问题,因为<Image Source="{Binding Image}" x:Name="image1" HeightRequest="200" WidthRequest="200" IsEnabled="True" Aspect="Fill" Margin="10"> <Image.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1"/> </Image.GestureRecognizers> </Image> 是XOR。不是你想在这里提高10的力量。

private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var page = new Page1();
    page.BindingContext = image1.Source;
    await Navigation.PushPopupAsync(page);
}