数组的输出错误

时间:2014-10-18 14:00:50

标签: c arrays output

我已经运行了几个小时,并继续得到错误的输出,似乎无法找出原因。似乎一切都应该有效,但是我第一次实现这一点时,我总是得到一个奇怪的角色,并且令牌不会以它们应该的方式移动。这只是练习代码,用汇编语言实现。

    char get_line(void){
  //Char array, buf space 80, int array, hold the numerical value,
  char arr[80];
  int int_arr[80];
  char arr_print[80];
  //Two points to compare whether the value in the given array changed.
  int compare;
  int compare_2;
  //Array points, indexes and size counter.
  int count = -1;
  int i = 0;
  int j = 0;
  int k;

  gets(arr);//Unsafe version of code, but for this implementation negligible.

  while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
    //Runs through and sets the value based on specs, #'s =1, alpha =2, ...
    //For the comparison with the below code.
    if(isalpha(arr[i])){
      int_arr[i] = 2;// printf("%c: 2", arr[i]);
      compare = 2;

    }else if(isdigit(arr[i])){
      int_arr[i] = 1;// printf("%d: 1", arr[i]);
       compare = 1;

    }else if(arr[i] == '$'){
      int_arr[i] = 5;// printf("%c: 5", arr[i]);
      compare = 5;

    }else if(arr[i] == '#'){
      int_arr[i] = 6;// printf("%c: 6", arr[i]);
      compare = 6;

    }else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || 
        arr[i] == '.' || arr[i] == ':'){
      int_arr[i] = 4;// printf("%c: 4", arr[i]);
      compare = 4;

    }else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' ||
        arr[i] == '/'){
      int_arr[i] = 3;//printf("%c: 3", arr[i]);
      compare = 3;

    }else if(isspace(arr[i]))
      int_arr[i] = 5;//Ignore the spaces in this implementation.
    /*
      Runs the comparison point to assure that the 
      tokens are matched up and grouped as needed.
    */     
    if(compare_2 == 0 || (compare != compare_2)){   
      if(compare_2 != 0){
    for(k=0; k<=j ;k++)
      printf("%c", arr_print[k]); 

    j=0;
      }    
      printf("\t\t%d \n", compare_2);
      compare_2 = compare; 

    }else if( isspace(arr[i]) == 0 ){
      arr_print[j] = arr[i];
      //      printf("\t\t\t\t\t%c  | %d\n", arr_print[j],j);
      j++;
    }

    i++;
    count++;
  }
  printf("\n\n");
  //Code for previous implementation in C  
  for(i=0; i<80 && arr[i] != NULL; i++)
    printf("%c", arr[i]);
  printf("\n");

  for(i=0; i< count+1; i++)
    printf("%d", int_arr[i]);
  printf("\n");

  if(i == 0 || count == -1) return '#';

  return arr[count];
}

1 个答案:

答案 0 :(得分:1)

我认为这就是你所需要的

试试此代码

char get_line(void) 
{
  //Char array, buf space 80, int array, hold the numerical value,
  char arr[80];
  int int_arr[80];
  char arr_print[80];
  //Two points to compare whether the value in the given array changed.
  int compare=0;    
  int compare_2=0;
  //Array points, indexes and size counter.
  int count = -1;
  int i = 0;
  int j = 0;
  int k = 0;
  int l = 0;  // I add an int l for 

  gets(arr);//Unsafe version of code, but for this implementation negligible.

  //while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
  while( i < 80 && arr[i] != '\0') 
  {
    //Runs through and sets the value based on specs, #'s =1, alpha =2, ...
    //For the comparison with the below code.
    if(isalpha(arr[i]))
    {
        int_arr[i] = 2;// printf("%c: 2", arr[i]);
        compare = 2;
    }
    else if(isdigit(arr[i]))
    {
        int_arr[i] = 1;// printf("%d: 1", arr[i]);
        compare = 1;
    }
    else if(arr[i] == '$')
    {
        int_arr[i] = 5;// printf("%c: 5", arr[i]);
        compare = 5;
    }
    else if(arr[i] == '#')
    {
        int_arr[i] = 6;// printf("%c: 6", arr[i]);
        compare = 6;
    }
    else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || arr[i] == '.' || arr[i] == ':')
    {
        int_arr[i] = 4;// printf("%c: 4", arr[i]);
        compare = 4;
    }
    else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' || arr[i] == '/')
    {
        int_arr[i] = 3;//printf("%c: 3", arr[i]);
        compare = 3;
    }
    else if(isspace(arr[i]))
        int_arr[i] = 5;//Ignore the spaces in this implementation.
    /*
        Runs the comparison point to assure that the 
        tokens are matched up and grouped as needed.
    */     
    if(compare_2 == 0 || (compare != compare_2))
    {
        if(compare_2 != 0)
        {
            for(k=0; k<=j ;k++) 
                printf("%c", arr[l+k]);   // arr_print replaced by arr
            j=0;
            l+=k;  // int l to go through array arr
        }
        printf("\t\t%d \n", compare_2);
        compare_2 = compare; 
    }
    else if( isspace(arr[i]) == 0 )
    {
        arr_print[j] = arr[i];
        //printf("\t\t\t\t\t%c  | %d\n", arr_print[j],j);
        j++;
    }
    i++;
    count++;
  }

  printf("%c", arr[count]);        // Repeated code to print the last element
  printf("\t\t%d \n", compare_2);
  compare_2 = compare;

  printf("\n\n");
  //Code for previous implementation in C  
  for(i=0; i<80 && arr[i] != NULL; i++)
    printf("%c", arr[i]);
  printf("\n");

  for(i=0; i< count+1; i++)
    printf("%d", int_arr[i]);
  printf("\n");

  if(i == 0 || count == -1) return '#';

  return arr[count];
}